summaryrefslogtreecommitdiff
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.c147
1 files changed, 73 insertions, 74 deletions
diff --git a/src/testbed/gnunet-service-testbed_cache.c b/src/testbed/gnunet-service-testbed_cache.c
index 80b0aed0c..901065da1 100644
--- a/src/testbed/gnunet-service-testbed_cache.c
+++ b/src/testbed/gnunet-service-testbed_cache.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2008--2013 GNUnet e.V. 3 Copyright (C) 2008--2013 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file testbed/gnunet-service-testbed_cache.c 22 * @file testbed/gnunet-service-testbed_cache.c
@@ -31,15 +31,14 @@
31#ifdef LOG 31#ifdef LOG
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{
43 /** 42 /**
44 * DLL next ptr for least recently used cache entries 43 * DLL next ptr for least recently used cache entries
45 */ 44 */
@@ -97,16 +96,16 @@ static unsigned int cache_size;
97 * @return the HELLO message; NULL if not found 96 * @return the HELLO message; NULL if not found
98 */ 97 */
99static struct CacheEntry * 98static struct CacheEntry *
100cache_lookup (unsigned int peer_id) 99cache_lookup(unsigned int peer_id)
101{ 100{
102 struct CacheEntry *entry; 101 struct CacheEntry *entry;
103 102
104 GNUNET_assert (NULL != cache); 103 GNUNET_assert(NULL != cache);
105 entry = GNUNET_CONTAINER_multihashmap32_get (cache, peer_id); 104 entry = GNUNET_CONTAINER_multihashmap32_get(cache, peer_id);
106 if (NULL == entry) 105 if (NULL == entry)
107 return NULL; 106 return NULL;
108 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry); 107 GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, entry);
109 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry); 108 GNUNET_CONTAINER_DLL_insert_tail(cache_head, cache_tail, entry);
110 return entry; 109 return entry;
111} 110}
112 111
@@ -117,11 +116,11 @@ cache_lookup (unsigned int peer_id)
117 * @param entry the cache entry to free 116 * @param entry the cache entry to free
118 */ 117 */
119static void 118static void
120free_entry (struct CacheEntry *entry) 119free_entry(struct CacheEntry *entry)
121{ 120{
122 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry); 121 GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, entry);
123 GNUNET_free_non_null (entry->hello); 122 GNUNET_free_non_null(entry->hello);
124 GNUNET_free (entry); 123 GNUNET_free(entry);
125} 124}
126 125
127 126
@@ -132,29 +131,29 @@ free_entry (struct CacheEntry *entry)
132 * @return the newly created entry 131 * @return the newly created entry
133 */ 132 */
134static struct CacheEntry * 133static struct CacheEntry *
135add_entry (unsigned int peer_id) 134add_entry(unsigned int peer_id)
136{ 135{
137 struct CacheEntry *entry; 136 struct CacheEntry *entry;
138 137
139 GNUNET_assert (NULL != cache); 138 GNUNET_assert(NULL != cache);
140 if (cache_size == GNUNET_CONTAINER_multihashmap32_size (cache)) 139 if (cache_size == GNUNET_CONTAINER_multihashmap32_size(cache))
141 { 140 {
142 /* remove the LRU head */ 141 /* remove the LRU head */
143 entry = cache_head; 142 entry = cache_head;
144 GNUNET_assert (GNUNET_OK == 143 GNUNET_assert(GNUNET_OK ==
145 GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t) 144 GNUNET_CONTAINER_multihashmap32_remove(cache, (uint32_t)
146 entry->peer_id, 145 entry->peer_id,
147 entry)); 146 entry));
148 free_entry (entry); 147 free_entry(entry);
149 } 148 }
150 entry = GNUNET_new (struct CacheEntry); 149 entry = GNUNET_new(struct CacheEntry);
151 entry->peer_id = peer_id; 150 entry->peer_id = peer_id;
152 GNUNET_assert (GNUNET_OK == 151 GNUNET_assert(GNUNET_OK ==
153 GNUNET_CONTAINER_multihashmap32_put (cache, 152 GNUNET_CONTAINER_multihashmap32_put(cache,
154 (uint32_t) peer_id, 153 (uint32_t)peer_id,
155 entry, 154 entry,
156 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); 155 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
157 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry); 156 GNUNET_CONTAINER_DLL_insert_tail(cache_head, cache_tail, entry);
158 return entry; 157 return entry;
159} 158}
160 159
@@ -170,14 +169,14 @@ add_entry (unsigned int peer_id)
170 * GNUNET_NO if not. 169 * GNUNET_NO if not.
171 */ 170 */
172static int 171static int
173cache_clear_iterator (void *cls, uint32_t key, void *value) 172cache_clear_iterator(void *cls, uint32_t key, void *value)
174{ 173{
175 struct CacheEntry *entry = value; 174 struct CacheEntry *entry = value;
176 175
177 GNUNET_assert (NULL != entry); 176 GNUNET_assert(NULL != entry);
178 GNUNET_assert (GNUNET_YES == 177 GNUNET_assert(GNUNET_YES ==
179 GNUNET_CONTAINER_multihashmap32_remove (cache, key, value)); 178 GNUNET_CONTAINER_multihashmap32_remove(cache, key, value));
180 free_entry (entry); 179 free_entry(entry);
181 return GNUNET_YES; 180 return GNUNET_YES;
182} 181}
183 182
@@ -186,15 +185,15 @@ cache_clear_iterator (void *cls, uint32_t key, void *value)
186 * Clear cache 185 * Clear cache
187 */ 186 */
188void 187void
189GST_cache_clear () 188GST_cache_clear()
190{ 189{
191 if (NULL != cache) 190 if (NULL != cache)
192 { 191 {
193 GNUNET_CONTAINER_multihashmap32_iterate (cache, &cache_clear_iterator, NULL); 192 GNUNET_CONTAINER_multihashmap32_iterate(cache, &cache_clear_iterator, NULL);
194 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (cache)); 193 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap32_size(cache));
195 GNUNET_CONTAINER_multihashmap32_destroy (cache); 194 GNUNET_CONTAINER_multihashmap32_destroy(cache);
196 cache = NULL; 195 cache = NULL;
197 } 196 }
198 cache_size = 0; 197 cache_size = 0;
199 cache_head = NULL; 198 cache_head = NULL;
200 cache_tail = NULL; 199 cache_tail = NULL;
@@ -207,12 +206,12 @@ GST_cache_clear ()
207 * @param size the size of the cache 206 * @param size the size of the cache
208 */ 207 */
209void 208void
210GST_cache_init (unsigned int size) 209GST_cache_init(unsigned int size)
211{ 210{
212 if (0 == size) 211 if (0 == size)
213 return; 212 return;
214 cache_size = size; 213 cache_size = size;
215 cache = GNUNET_CONTAINER_multihashmap32_create (cache_size); 214 cache = GNUNET_CONTAINER_multihashmap32_create(cache_size);
216} 215}
217 216
218 217
@@ -223,21 +222,21 @@ GST_cache_init (unsigned int size)
223 * @return the HELLO message; NULL if not found 222 * @return the HELLO message; NULL if not found
224 */ 223 */
225const struct GNUNET_MessageHeader * 224const struct GNUNET_MessageHeader *
226GST_cache_lookup_hello (const unsigned int peer_id) 225GST_cache_lookup_hello(const unsigned int peer_id)
227{ 226{
228 struct CacheEntry *entry; 227 struct CacheEntry *entry;
229 228
230 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id); 229 LOG_DEBUG("Looking up HELLO for peer %u\n", peer_id);
231 if (NULL == cache) 230 if (NULL == cache)
232 { 231 {
233 LOG_DEBUG ("Caching disabled\n"); 232 LOG_DEBUG("Caching disabled\n");
234 return NULL; 233 return NULL;
235 } 234 }
236 entry = cache_lookup (peer_id); 235 entry = cache_lookup(peer_id);
237 if (NULL == entry) 236 if (NULL == entry)
238 return NULL; 237 return NULL;
239 if (NULL != entry->hello) 238 if (NULL != entry->hello)
240 LOG_DEBUG ("HELLO found for peer %u\n", peer_id); 239 LOG_DEBUG("HELLO found for peer %u\n", peer_id);
241 return entry->hello; 240 return entry->hello;
242} 241}
243 242
@@ -250,18 +249,18 @@ GST_cache_lookup_hello (const unsigned int peer_id)
250 * @param hello the HELLO message 249 * @param hello the HELLO message
251 */ 250 */
252void 251void
253GST_cache_add_hello (const unsigned int peer_id, 252GST_cache_add_hello(const unsigned int peer_id,
254 const struct GNUNET_MessageHeader *hello) 253 const struct GNUNET_MessageHeader *hello)
255{ 254{
256 struct CacheEntry *entry; 255 struct CacheEntry *entry;
257 256
258 if (NULL == cache) 257 if (NULL == cache)
259 return; 258 return;
260 entry = cache_lookup (peer_id); 259 entry = cache_lookup(peer_id);
261 if (NULL == entry) 260 if (NULL == entry)
262 entry = add_entry (peer_id); 261 entry = add_entry(peer_id);
263 GNUNET_free_non_null (entry->hello); 262 GNUNET_free_non_null(entry->hello);
264 entry->hello = GNUNET_copy_message (hello); 263 entry->hello = GNUNET_copy_message(hello);
265} 264}
266 265
267/* end of gnunet-service-testbed_hc.c */ 266/* end of gnunet-service-testbed_hc.c */