GNUnet  0.20.0
container_multihashmap.c File Reference

hash map where the same key may be present multiple times More...

#include "gnunet_common.h"
#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for container_multihashmap.c:

Go to the source code of this file.

Data Structures

struct  BigMapEntry
 An entry in the hash map with the full key. More...
 
struct  SmallMapEntry
 An entry in the hash map with just a pointer to the key. More...
 
union  MapEntry
 Entry in the map. More...
 
struct  GNUNET_CONTAINER_MultiHashMap
 Internal representation of the hash map. More...
 
struct  GNUNET_CONTAINER_MultiHashMapIterator
 Cursor into a multihashmap. More...
 

Macros

#define LOG(kind, ...)    GNUNET_log_from (kind, "util-container-multihashmap", __VA_ARGS__)
 
#define NEXT_CACHE_SIZE   16
 Maximum recursion depth for callbacks of GNUNET_CONTAINER_multihashmap_get_multiple() themselves s again calling GNUNET_CONTAINER_multihashmap_get_multiple(). More...
 

Functions

struct GNUNET_CONTAINER_MultiHashMapGNUNET_CONTAINER_multihashmap_create (unsigned int len, int do_not_copy_keys)
 Create a multi hash map. More...
 
void GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap *map)
 Destroy a hash map. More...
 
static unsigned int idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
 Compute the index of the bucket for the given key. More...
 
unsigned int GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap *map)
 Get the number of key-value pairs in the map. More...
 
void * GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
 Given a key find a value in the map matching the key. More...
 
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_iterate (struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
 Iterate over all entries in the map. More...
 
static void update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map, const struct BigMapEntry *bme)
 We are about to free() the bme, make sure it is not in the list of next values for any iterator in the map's next_cache. More...
 
static void update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map, const struct SmallMapEntry *sme)
 We are about to free() the sme, make sure it is not in the list of next values for any iterator in the map's next_cache. More...
 
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
 Remove the given key-value pair from the map. More...
 
int GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
 Remove all entries for the given key from the map. More...
 
static int remove_all (void *cls, const struct GNUNET_HashCode *key, void *value)
 Callback used to remove all entries from the map. More...
 
unsigned int GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map)
 Remove all entries from the map. More...
 
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains (const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
 Check if the map contains any value under the given key (including values that are NULL). More...
 
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains_value (const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
 Check if the map contains the given value under the given key. More...
 
static void grow (struct GNUNET_CONTAINER_MultiHashMap *map)
 Grow the given map to a more appropriate size. More...
 
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
 Store a key-value pair in the map. More...
 
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_get_multiple (struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
 Iterate over all entries in the map that match a particular key. More...
 
unsigned int GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
 Call it on a random value from the map, or not at all if the map is empty. More...
 
struct GNUNET_CONTAINER_MultiHashMapIteratorGNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map)
 Create an iterator for a multihashmap. More...
 
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter, struct GNUNET_HashCode *key, const void **value)
 Retrieve the next element from the hash map at the iterator's position. More...
 
void GNUNET_CONTAINER_multihashmap_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
 Destroy a multihashmap iterator. More...
 

Detailed Description

hash map where the same key may be present multiple times

Author
Christian Grothoff

Definition in file container_multihashmap.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)     GNUNET_log_from (kind, "util-container-multihashmap", __VA_ARGS__)

Definition at line 31 of file container_multihashmap.c.

◆ NEXT_CACHE_SIZE

#define NEXT_CACHE_SIZE   16

Maximum recursion depth for callbacks of GNUNET_CONTAINER_multihashmap_get_multiple() themselves s again calling GNUNET_CONTAINER_multihashmap_get_multiple().

Should be totally excessive, but if violated we die.

Definition at line 40 of file container_multihashmap.c.

Function Documentation

◆ idx_of()

static unsigned int idx_of ( const struct GNUNET_CONTAINER_MultiHashMap map,
const struct GNUNET_HashCode key 
)
static

Compute the index of the bucket for the given key.

Parameters
maphash map for which to compute the index
keywhat key should the index be computed for
Returns
offset into the "map" array of "map"

Definition at line 265 of file container_multihashmap.c.

267 {
268  GNUNET_assert (map != NULL);
269  return (*(unsigned int *) key) % map->map_length;
270 }
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
unsigned int map_length
Length of the "map" array.

References GNUNET_assert, key, map, and GNUNET_CONTAINER_MultiPeerMap::map_length.

Referenced by GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_get_multiple(), and GNUNET_CONTAINER_multihashmap_remove_all().

Here is the caller graph for this function:

◆ update_next_cache_bme()

static void update_next_cache_bme ( struct GNUNET_CONTAINER_MultiHashMap map,
const struct BigMapEntry bme 
)
static

We are about to free() the bme, make sure it is not in the list of next values for any iterator in the map's next_cache.

Parameters
mapthe map to check
bmethe entry that is about to be free'd

Definition at line 380 of file container_multihashmap.c.

382 {
383  for (unsigned int i = 0; i < map->next_cache_off; i++)
384  if (map->next_cache[i].bme == bme)
385  map->next_cache[i].bme = bme->next;
386 }
struct BigMapEntry * next
If there is a hash collision, we create a linked list.
unsigned int next_cache_off
Offset of next_cache entries in use, must be smaller than NEXT_CACHE_SIZE.
union MapEntry next_cache[16]
Map entries indicating iteration positions currently in use by GNUNET_CONTAINER_multihashmap_get_mult...
struct BigMapEntry * bme
Variant used if map entries contain the full key.

References MapEntry::bme, map, BigMapEntry::next, GNUNET_CONTAINER_MultiPeerMap::next_cache, and GNUNET_CONTAINER_MultiPeerMap::next_cache_off.

Referenced by GNUNET_CONTAINER_multihashmap_remove_all().

Here is the caller graph for this function:

◆ update_next_cache_sme()

static void update_next_cache_sme ( struct GNUNET_CONTAINER_MultiHashMap map,
const struct SmallMapEntry sme 
)
static

We are about to free() the sme, make sure it is not in the list of next values for any iterator in the map's next_cache.

Parameters
mapthe map to check
smethe entry that is about to be free'd

Definition at line 397 of file container_multihashmap.c.

399 {
400  for (unsigned int i = 0; i < map->next_cache_off; i++)
401  if (map->next_cache[i].sme == sme)
402  map->next_cache[i].sme = sme->next;
403 }
struct SmallMapEntry * next
If there is a hash collision, we create a linked list.
struct SmallMapEntry * sme
Variant used if map entries only contain a pointer to the key.

Referenced by GNUNET_CONTAINER_multihashmap_remove_all().

Here is the caller graph for this function:

◆ remove_all()

static int remove_all ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Callback used to remove all entries from the map.

Parameters
clsthe struct GNUNET_CONTAINER_MultiHashMap
keythe key
valuethe value
Returns
GNUNET_OK (continue to iterate)

Definition at line 553 of file container_multihashmap.c.

554 {
555  struct GNUNET_CONTAINER_MultiHashMap *map = cls;
556 
559  return GNUNET_OK;
560 }
static char * value
Value of the record to add/remove.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
@ GNUNET_OK
@ GNUNET_YES
Internal representation of the hash map.

References GNUNET_assert, GNUNET_CONTAINER_multihashmap_remove(), GNUNET_OK, GNUNET_YES, key, map, and value.

Referenced by GNUNET_CONTAINER_multihashmap_clear().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ grow()

static void grow ( struct GNUNET_CONTAINER_MultiHashMap map)
static

Grow the given map to a more appropriate size.

Parameters
mapthe hash map to grow

Definition at line 645 of file container_multihashmap.c.

646 {
647  union MapEntry *old_map;
648  union MapEntry *new_map;
649  unsigned int old_len;
650  unsigned int new_len;
651  unsigned int idx;
652 
653  old_map = map->map;
654  old_len = map->map_length;
655  GNUNET_assert (0 != old_len);
656  new_len = old_len * 2;
657  if (0 == new_len) /* 2^31 * 2 == 0 */
658  new_len = old_len; /* never use 0 */
659  if (new_len == old_len)
660  return; /* nothing changed */
661  new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
662  if (NULL == new_map)
663  return; /* grow not possible */
665  map->map_length = new_len;
666  map->map = new_map;
667  for (unsigned int i = 0; i < old_len; i++)
668  {
669  if (map->use_small_entries)
670  {
671  struct SmallMapEntry *sme;
672 
673  while (NULL != (sme = old_map[i].sme))
674  {
675  old_map[i].sme = sme->next;
676  idx = idx_of (map, sme->key);
677  sme->next = new_map[idx].sme;
678  new_map[idx].sme = sme;
679  }
680  }
681  else
682  {
683  struct BigMapEntry *bme;
684 
685  while (NULL != (bme = old_map[i].bme))
686  {
687  old_map[i].bme = bme->next;
688  idx = idx_of (map, &bme->key);
689  bme->next = new_map[idx].bme;
690  new_map[idx].bme = bme;
691  }
692  }
693  }
694  GNUNET_free (old_map);
695 }
static unsigned int idx_of(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Compute the index of the bucket for the given key.
#define GNUNET_malloc_large(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
An entry in the hash map with the full key.
unsigned int modification_counter
Counts the destructive modifications (grow, remove) to the map, so that iterators can check if they a...
union MapEntry * map
All of our buckets.
int use_small_entries
GNUNET_NO if the map entries are of type 'struct BigMapEntry', GNUNET_YES if the map entries are of t...
An entry in the hash map with just a pointer to the key.
Entry in the map.