• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef HASHMAP_H
2 # define HASHMAP_H
3 
4 # include <stdlib.h>
5 # include <stdint.h>
6 
7 #ifndef __GNUC_PREREQ
8 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
9 #define __GNUC_PREREQ(maj, min) \
10 	((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
11 #else
12 #define __GNUC_PREREQ(maj, min) 0
13 #endif
14 #endif
15 
16 struct ext2fs_hashmap {
17 	uint32_t size;
18 	uint32_t(*hash)(const void *key, size_t len);
19 	void(*free)(void*);
20 	struct ext2fs_hashmap_entry *first;
21 	struct ext2fs_hashmap_entry *last;
22 	struct ext2fs_hashmap_entry {
23 		void *data;
24 		const void *key;
25 		size_t key_len;
26 		struct ext2fs_hashmap_entry *next;
27 		struct ext2fs_hashmap_entry *list_next;
28 		struct ext2fs_hashmap_entry *list_prev;
29 #if __GNUC_PREREQ (4, 8)
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wpedantic"
32 #endif
33 	} *entries[0];
34 #if __GNUC_PREREQ (4, 8)
35 #pragma GCC diagnostic pop
36 #endif
37 };
38 
39 struct ext2fs_hashmap *ext2fs_hashmap_create(
40 				uint32_t(*hash_fct)(const void*, size_t),
41 				void(*free_fct)(void*), size_t size);
42 void ext2fs_hashmap_add(struct ext2fs_hashmap *h, void *data, const void *key,
43 			size_t key_len);
44 void *ext2fs_hashmap_lookup(struct ext2fs_hashmap *h, const void *key,
45 			    size_t key_len);
46 void *ext2fs_hashmap_iter_in_order(struct ext2fs_hashmap *h,
47 				   struct ext2fs_hashmap_entry **it);
48 void ext2fs_hashmap_del(struct ext2fs_hashmap *h,
49 			struct ext2fs_hashmap_entry *e);
50 void ext2fs_hashmap_free(struct ext2fs_hashmap *h);
51 
52 uint32_t ext2fs_djb2_hash(const void *str, size_t size);
53 
54 #endif /* !HASHMAP_H */
55