/src/e2fsprogs/lib/ext2fs/hashmap.c
Line | Count | Source |
1 | | #include "hashmap.h" |
2 | | #include <string.h> |
3 | | |
4 | | struct ext2fs_hashmap { |
5 | | uint32_t size; |
6 | | uint32_t(*hash)(const void *key, size_t len); |
7 | | void(*free)(void*); |
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" |
13 | | #endif |
14 | | struct ext2fs_hashmap_entry *entries[0]; |
15 | | #if __GNUC_PREREQ (4, 8) |
16 | | #pragma GCC diagnostic pop |
17 | | #endif |
18 | | }; |
19 | | |
20 | | uint32_t ext2fs_djb2_hash(const void *str, size_t size) |
21 | 0 | { |
22 | 0 | int c; |
23 | 0 | const char *s = str; |
24 | 0 | uint32_t hash = 5381; |
25 | |
|
26 | 0 | while (size-- > 0) { |
27 | 0 | c = *s++; |
28 | 0 | hash = ((hash << 5) + hash) + c; |
29 | 0 | } |
30 | 0 | return hash; |
31 | 0 | } |
32 | | |
33 | | struct ext2fs_hashmap *ext2fs_hashmap_create( |
34 | | uint32_t(*hash_fct)(const void*, size_t), |
35 | | void(*free_fct)(void*), size_t size) |
36 | 0 | { |
37 | 0 | struct ext2fs_hashmap *h = calloc(1, sizeof(struct ext2fs_hashmap) + |
38 | 0 | sizeof(struct ext2fs_hashmap_entry) * size); |
39 | 0 | if (!h) |
40 | 0 | return NULL; |
41 | | |
42 | 0 | h->size = size; |
43 | 0 | h->free = free_fct; |
44 | 0 | h->hash = hash_fct; |
45 | 0 | h->first = h->last = NULL; |
46 | 0 | return h; |
47 | 0 | } |
48 | | |
49 | | int ext2fs_hashmap_add(struct ext2fs_hashmap *h, |
50 | | void *data, const void *key, size_t key_len) |
51 | 0 | { |
52 | 0 | uint32_t hash = h->hash(key, key_len) % h->size; |
53 | 0 | struct ext2fs_hashmap_entry *e = malloc(sizeof(*e)); |
54 | |
|
55 | 0 | if (!e) |
56 | 0 | return -1; |
57 | | |
58 | 0 | e->data = data; |
59 | 0 | e->key = key; |
60 | 0 | e->key_len = key_len; |
61 | 0 | e->next = h->entries[hash]; |
62 | 0 | h->entries[hash] = e; |
63 | |
|
64 | 0 | e->list_prev = NULL; |
65 | 0 | e->list_next = h->first; |
66 | 0 | if (h->first) |
67 | 0 | h->first->list_prev = e; |
68 | 0 | h->first = e; |
69 | 0 | if (!h->last) |
70 | 0 | h->last = e; |
71 | |
|
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | | void *ext2fs_hashmap_lookup(struct ext2fs_hashmap *h, const void *key, |
76 | | size_t key_len) |
77 | 0 | { |
78 | 0 | struct ext2fs_hashmap_entry *iter; |
79 | 0 | uint32_t hash = h->hash(key, key_len) % h->size; |
80 | |
|
81 | 0 | for (iter = h->entries[hash]; iter; iter = iter->next) |
82 | 0 | if (iter->key_len == key_len && !memcmp(iter->key, key, key_len)) |
83 | 0 | return iter->data; |
84 | 0 | return NULL; |
85 | 0 | } |
86 | | |
87 | | void *ext2fs_hashmap_iter_in_order(struct ext2fs_hashmap *h, |
88 | | struct ext2fs_hashmap_entry **it) |
89 | 0 | { |
90 | 0 | *it = *it ? (*it)->list_next : h->first; |
91 | 0 | return *it ? (*it)->data : NULL; |
92 | 0 | } |
93 | | |
94 | | void ext2fs_hashmap_free(struct ext2fs_hashmap *h) |
95 | 0 | { |
96 | 0 | size_t i; |
97 | |
|
98 | 0 | for (i = 0; i < h->size; ++i) { |
99 | 0 | struct ext2fs_hashmap_entry *it = h->entries[i]; |
100 | 0 | while (it) { |
101 | 0 | struct ext2fs_hashmap_entry *tmp = it->next; |
102 | 0 | if (h->free) |
103 | 0 | h->free(it->data); |
104 | 0 | free(it); |
105 | 0 | it = tmp; |
106 | 0 | } |
107 | 0 | } |
108 | 0 | free(h); |
109 | 0 | } |