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