4 struct ext2fs_hashmap {
6 uint32_t(*hash)(const void *key, size_t len);
8 struct ext2fs_hashmap_entry *first;
9 struct ext2fs_hashmap_entry *last;
10 #if __GNUC_PREREQ (4, 8)
11 #pragma GCC diagnostic push
12 #pragma GCC diagnostic ignored "-Wpedantic"
14 struct ext2fs_hashmap_entry *entries[0];
15 #if __GNUC_PREREQ (4, 8)
16 #pragma GCC diagnostic pop
20 uint32_t ext2fs_djb2_hash(const void *str, size_t size)
28 hash = ((hash << 5) + hash) + c;
33 struct ext2fs_hashmap *ext2fs_hashmap_create(
34 uint32_t(*hash_fct)(const void*, size_t),
35 void(*free_fct)(void*), size_t size)
37 struct ext2fs_hashmap *h = calloc(sizeof(struct ext2fs_hashmap) +
38 sizeof(struct ext2fs_hashmap_entry) * size, 1);
42 h->first = h->last = NULL;
46 void ext2fs_hashmap_add(struct ext2fs_hashmap *h, void *data, const void *key,
49 uint32_t hash = h->hash(key, key_len) % h->size;
50 struct ext2fs_hashmap_entry *e = malloc(sizeof(*e));
55 e->next = h->entries[hash];
59 e->list_next = h->first;
61 h->first->list_prev = e;
67 void *ext2fs_hashmap_lookup(struct ext2fs_hashmap *h, const void *key,
70 struct ext2fs_hashmap_entry *iter;
71 uint32_t hash = h->hash(key, key_len) % h->size;
73 for (iter = h->entries[hash]; iter; iter = iter->next)
74 if (iter->key_len == key_len && !memcmp(iter->key, key, key_len))
79 void *ext2fs_hashmap_iter_in_order(struct ext2fs_hashmap *h,
80 struct ext2fs_hashmap_entry **it)
82 *it = *it ? (*it)->list_next : h->first;
83 return *it ? (*it)->data : NULL;
86 void ext2fs_hashmap_free(struct ext2fs_hashmap *h)
90 for (i = 0; i < h->size; ++i) {
91 struct ext2fs_hashmap_entry *it = h->entries[i];
93 struct ext2fs_hashmap_entry *tmp = it->next;