aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-service-testbed_cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/gnunet-service-testbed_cache.c')
-rw-r--r--src/testbed/gnunet-service-testbed_cache.c114
1 files changed, 58 insertions, 56 deletions
diff --git a/src/testbed/gnunet-service-testbed_cache.c b/src/testbed/gnunet-service-testbed_cache.c
index 901065da1..4afa06645 100644
--- a/src/testbed/gnunet-service-testbed_cache.c
+++ b/src/testbed/gnunet-service-testbed_cache.c
@@ -32,13 +32,14 @@
32#undef LOG 32#undef LOG
33#endif 33#endif
34#define LOG(kind, ...) \ 34#define LOG(kind, ...) \
35 GNUNET_log_from(kind, "testbed-cache", __VA_ARGS__) 35 GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__)
36 36
37 37
38/** 38/**
39 * Cache entry 39 * Cache entry
40 */ 40 */
41struct CacheEntry { 41struct CacheEntry
42{
42 /** 43 /**
43 * DLL next ptr for least recently used cache entries 44 * DLL next ptr for least recently used cache entries
44 */ 45 */
@@ -96,16 +97,16 @@ static unsigned int cache_size;
96 * @return the HELLO message; NULL if not found 97 * @return the HELLO message; NULL if not found
97 */ 98 */
98static struct CacheEntry * 99static struct CacheEntry *
99cache_lookup(unsigned int peer_id) 100cache_lookup (unsigned int peer_id)
100{ 101{
101 struct CacheEntry *entry; 102 struct CacheEntry *entry;
102 103
103 GNUNET_assert(NULL != cache); 104 GNUNET_assert (NULL != cache);
104 entry = GNUNET_CONTAINER_multihashmap32_get(cache, peer_id); 105 entry = GNUNET_CONTAINER_multihashmap32_get (cache, peer_id);
105 if (NULL == entry) 106 if (NULL == entry)
106 return NULL; 107 return NULL;
107 GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, entry); 108 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
108 GNUNET_CONTAINER_DLL_insert_tail(cache_head, cache_tail, entry); 109 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
109 return entry; 110 return entry;
110} 111}
111 112
@@ -116,11 +117,11 @@ cache_lookup(unsigned int peer_id)
116 * @param entry the cache entry to free 117 * @param entry the cache entry to free
117 */ 118 */
118static void 119static void
119free_entry(struct CacheEntry *entry) 120free_entry (struct CacheEntry *entry)
120{ 121{
121 GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, entry); 122 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
122 GNUNET_free_non_null(entry->hello); 123 GNUNET_free_non_null (entry->hello);
123 GNUNET_free(entry); 124 GNUNET_free (entry);
124} 125}
125 126
126 127
@@ -131,29 +132,29 @@ free_entry(struct CacheEntry *entry)
131 * @return the newly created entry 132 * @return the newly created entry
132 */ 133 */
133static struct CacheEntry * 134static struct CacheEntry *
134add_entry(unsigned int peer_id) 135add_entry (unsigned int peer_id)
135{ 136{
136 struct CacheEntry *entry; 137 struct CacheEntry *entry;
137 138
138 GNUNET_assert(NULL != cache); 139 GNUNET_assert (NULL != cache);
139 if (cache_size == GNUNET_CONTAINER_multihashmap32_size(cache)) 140 if (cache_size == GNUNET_CONTAINER_multihashmap32_size (cache))
140 { 141 {
141 /* remove the LRU head */ 142 /* remove the LRU head */
142 entry = cache_head; 143 entry = cache_head;
143 GNUNET_assert(GNUNET_OK == 144 GNUNET_assert (GNUNET_OK ==
144 GNUNET_CONTAINER_multihashmap32_remove(cache, (uint32_t) 145 GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t)
145 entry->peer_id, 146 entry->peer_id,
146 entry)); 147 entry));
147 free_entry(entry); 148 free_entry (entry);
148 } 149 }
149 entry = GNUNET_new(struct CacheEntry); 150 entry = GNUNET_new (struct CacheEntry);
150 entry->peer_id = peer_id; 151 entry->peer_id = peer_id;
151 GNUNET_assert(GNUNET_OK == 152 GNUNET_assert (GNUNET_OK ==
152 GNUNET_CONTAINER_multihashmap32_put(cache, 153 GNUNET_CONTAINER_multihashmap32_put (cache,
153 (uint32_t)peer_id, 154 (uint32_t) peer_id,
154 entry, 155 entry,
155 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); 156 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
156 GNUNET_CONTAINER_DLL_insert_tail(cache_head, cache_tail, entry); 157 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
157 return entry; 158 return entry;
158} 159}
159 160
@@ -169,14 +170,14 @@ add_entry(unsigned int peer_id)
169 * GNUNET_NO if not. 170 * GNUNET_NO if not.
170 */ 171 */
171static int 172static int
172cache_clear_iterator(void *cls, uint32_t key, void *value) 173cache_clear_iterator (void *cls, uint32_t key, void *value)
173{ 174{
174 struct CacheEntry *entry = value; 175 struct CacheEntry *entry = value;
175 176
176 GNUNET_assert(NULL != entry); 177 GNUNET_assert (NULL != entry);
177 GNUNET_assert(GNUNET_YES == 178 GNUNET_assert (GNUNET_YES ==
178 GNUNET_CONTAINER_multihashmap32_remove(cache, key, value)); 179 GNUNET_CONTAINER_multihashmap32_remove (cache, key, value));
179 free_entry(entry); 180 free_entry (entry);
180 return GNUNET_YES; 181 return GNUNET_YES;
181} 182}
182 183
@@ -185,15 +186,16 @@ cache_clear_iterator(void *cls, uint32_t key, void *value)
185 * Clear cache 186 * Clear cache
186 */ 187 */
187void 188void
188GST_cache_clear() 189GST_cache_clear ()
189{ 190{
190 if (NULL != cache) 191 if (NULL != cache)
191 { 192 {
192 GNUNET_CONTAINER_multihashmap32_iterate(cache, &cache_clear_iterator, NULL); 193 GNUNET_CONTAINER_multihashmap32_iterate (cache, &cache_clear_iterator,
193 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap32_size(cache)); 194 NULL);
194 GNUNET_CONTAINER_multihashmap32_destroy(cache); 195 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (cache));
195 cache = NULL; 196 GNUNET_CONTAINER_multihashmap32_destroy (cache);
196 } 197 cache = NULL;
198 }
197 cache_size = 0; 199 cache_size = 0;
198 cache_head = NULL; 200 cache_head = NULL;
199 cache_tail = NULL; 201 cache_tail = NULL;
@@ -206,12 +208,12 @@ GST_cache_clear()
206 * @param size the size of the cache 208 * @param size the size of the cache
207 */ 209 */
208void 210void
209GST_cache_init(unsigned int size) 211GST_cache_init (unsigned int size)
210{ 212{
211 if (0 == size) 213 if (0 == size)
212 return; 214 return;
213 cache_size = size; 215 cache_size = size;
214 cache = GNUNET_CONTAINER_multihashmap32_create(cache_size); 216 cache = GNUNET_CONTAINER_multihashmap32_create (cache_size);
215} 217}
216 218
217 219
@@ -222,21 +224,21 @@ GST_cache_init(unsigned int size)
222 * @return the HELLO message; NULL if not found 224 * @return the HELLO message; NULL if not found
223 */ 225 */
224const struct GNUNET_MessageHeader * 226const struct GNUNET_MessageHeader *
225GST_cache_lookup_hello(const unsigned int peer_id) 227GST_cache_lookup_hello (const unsigned int peer_id)
226{ 228{
227 struct CacheEntry *entry; 229 struct CacheEntry *entry;
228 230
229 LOG_DEBUG("Looking up HELLO for peer %u\n", peer_id); 231 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
230 if (NULL == cache) 232 if (NULL == cache)
231 { 233 {
232 LOG_DEBUG("Caching disabled\n"); 234 LOG_DEBUG ("Caching disabled\n");
233 return NULL; 235 return NULL;
234 } 236 }
235 entry = cache_lookup(peer_id); 237 entry = cache_lookup (peer_id);
236 if (NULL == entry) 238 if (NULL == entry)
237 return NULL; 239 return NULL;
238 if (NULL != entry->hello) 240 if (NULL != entry->hello)
239 LOG_DEBUG("HELLO found for peer %u\n", peer_id); 241 LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
240 return entry->hello; 242 return entry->hello;
241} 243}
242 244
@@ -249,18 +251,18 @@ GST_cache_lookup_hello(const unsigned int peer_id)
249 * @param hello the HELLO message 251 * @param hello the HELLO message
250 */ 252 */
251void 253void
252GST_cache_add_hello(const unsigned int peer_id, 254GST_cache_add_hello (const unsigned int peer_id,
253 const struct GNUNET_MessageHeader *hello) 255 const struct GNUNET_MessageHeader *hello)
254{ 256{
255 struct CacheEntry *entry; 257 struct CacheEntry *entry;
256 258
257 if (NULL == cache) 259 if (NULL == cache)
258 return; 260 return;
259 entry = cache_lookup(peer_id); 261 entry = cache_lookup (peer_id);
260 if (NULL == entry) 262 if (NULL == entry)
261 entry = add_entry(peer_id); 263 entry = add_entry (peer_id);
262 GNUNET_free_non_null(entry->hello); 264 GNUNET_free_non_null (entry->hello);
263 entry->hello = GNUNET_copy_message(hello); 265 entry->hello = GNUNET_copy_message (hello);
264} 266}
265 267
266/* end of gnunet-service-testbed_hc.c */ 268/* end of gnunet-service-testbed_hc.c */