GNUnet  0.10.x
gnunet-service-testbed_cache.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2008--2013 GNUnet e.V.
4 
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
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
26 #include "gnunet-service-testbed.h"
27 
31 #ifdef LOG
32 #undef LOG
33 #endif
34 #define LOG(kind,...) \
35  GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__)
36 
37 
41 struct CacheEntry
42 {
46  struct CacheEntry *next;
47 
51  struct CacheEntry *prev;
52 
57 
61  unsigned int peer_id;
62 };
63 
64 
69 
76 static struct CacheEntry *cache_head;
77 
84 static struct CacheEntry *cache_tail;
85 
89 static unsigned int cache_size;
90 
91 
99 static struct CacheEntry *
100 cache_lookup (unsigned int peer_id)
101 {
102  struct CacheEntry *entry;
103 
104  GNUNET_assert (NULL != cache);
105  entry = GNUNET_CONTAINER_multihashmap32_get (cache, peer_id);
106  if (NULL == entry)
107  return NULL;
108  GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
109  GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
110  return entry;
111 }
112 
113 
119 static void
120 free_entry (struct CacheEntry *entry)
121 {
122  GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
123  GNUNET_free_non_null (entry->hello);
124  GNUNET_free (entry);
125 }
126 
127 
134 static struct CacheEntry *
135 add_entry (unsigned int peer_id)
136 {
137  struct CacheEntry *entry;
138 
139  GNUNET_assert (NULL != cache);
141  {
142  /* remove the LRU head */
143  entry = cache_head;
145  GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t)
146  entry->peer_id,
147  entry));
148  free_entry (entry);
149  }
150  entry = GNUNET_new (struct CacheEntry);
151  entry->peer_id = peer_id;
154  (uint32_t) peer_id,
155  entry,
157  GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
158  return entry;
159 }
160 
161 
172 static int
173 cache_clear_iterator (void *cls, uint32_t key, void *value)
174 {
175  struct CacheEntry *entry = value;
176 
177  GNUNET_assert (NULL != entry);
179  GNUNET_CONTAINER_multihashmap32_remove (cache, key, value));
180  free_entry (entry);
181  return GNUNET_YES;
182 }
183 
184 
188 void
190 {
191  if (NULL != cache)
192  {
196  cache = NULL;
197  }
198  cache_size = 0;
199  cache_head = NULL;
200  cache_tail = NULL;
201 }
202 
203 
209 void
210 GST_cache_init (unsigned int size)
211 {
212  if (0 == size)
213  return;
214  cache_size = size;
216 }
217 
218 
225 const struct GNUNET_MessageHeader *
226 GST_cache_lookup_hello (const unsigned int peer_id)
227 {
228  struct CacheEntry *entry;
229 
230  LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
231  if (NULL == cache)
232  {
233  LOG_DEBUG ("Caching disabled\n");
234  return NULL;
235  }
236  entry = cache_lookup (peer_id);
237  if (NULL == entry)
238  return NULL;
239  if (NULL != entry->hello)
240  LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
241  return entry->hello;
242 }
243 
244 
252 void
253 GST_cache_add_hello (const unsigned int peer_id,
254  const struct GNUNET_MessageHeader *hello)
255 {
256  struct CacheEntry *entry;
257 
258  if (NULL == cache)
259  return;
260  entry = cache_lookup (peer_id);
261  if (NULL == entry)
262  entry = add_entry (peer_id);
263  GNUNET_free_non_null (entry->hello);
264  entry->hello = GNUNET_copy_message (hello);
265 }
266 
267 /* end of gnunet-service-testbed_hc.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct CacheEntry * next
DLL next ptr for least recently used cache entries.
data structures shared amongst components of TESTBED service
static struct CacheEntry * cache_head
DLL head for least recently used cache entries; least recently used cache items are at the head...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct CacheEntry * add_entry(unsigned int peer_id)
Creates a new cache entry and then puts it into the cache&#39;s hashtable.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void free_entry(struct CacheEntry *entry)
Free the resources occupied by a cache entry.
int GNUNET_CONTAINER_multihashmap32_put(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
struct CacheEntry * prev
DLL prev ptr for least recently used cache entries.
int GNUNET_CONTAINER_multihashmap32_remove(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, const void *value)
Remove the given key-value pair from the map.
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
static char * value
Value of the record to add/remove.
void * GNUNET_CONTAINER_multihashmap32_get(const struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key)
Given a key find a value in the map matching the key.
const struct GNUNET_MessageHeader * GST_cache_lookup_hello(const unsigned int peer_id)
Looks up in the hello cache and returns the HELLO of the given peer.
void GST_cache_init(unsigned int size)
Initializes the cache.
unsigned int GNUNET_CONTAINER_multihashmap32_size(const struct GNUNET_CONTAINER_MultiHashMap32 *map)
Get the number of key-value pairs in the map.
Internal representation of the hash map.
static int cache_clear_iterator(void *cls, uint32_t key, void *value)
Iterator over hash map entries.
struct GNUNET_CONTAINER_MultiHashMap32 * GNUNET_CONTAINER_multihashmap32_create(unsigned int len)
Create a 32-bit key multi hash map.
void GNUNET_CONTAINER_multihashmap32_destroy(struct GNUNET_CONTAINER_MultiHashMap32 *map)
Destroy a 32-bit key hash map.
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
static struct CacheEntry * cache_tail
DLL tail for least recently used cache entries; recently used cache items are at the tail...
int GNUNET_CONTAINER_multihashmap32_iterate(struct GNUNET_CONTAINER_MultiHashMap32 *map, GNUNET_CONTAINER_HashMapIterator32 it, void *it_cls)
Iterate over all entries in the map.
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
static unsigned int cache_size
Maximum number of elements to cache.
struct GNUNET_MessageHeader * hello
The HELLO message.
static struct CacheEntry * cache_lookup(unsigned int peer_id)
Looks up in the cache and returns the entry.
Header for all communications.
#define GNUNET_YES
Definition: gnunet_common.h:80
void GST_cache_add_hello(const unsigned int peer_id, const struct GNUNET_MessageHeader *hello)
Caches the HELLO of the given peer.
#define LOG_DEBUG(msg)
unsigned int peer_id
The id of the peer this entry corresponds to.
void GST_cache_clear()
Clear cache.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_MessageHeader * GNUNET_copy_message(const struct GNUNET_MessageHeader *msg)
Create a copy of the given message.