5 * Generic implementation of hash-based key-value mappings.
6 * See Documentation/technical/api-hashmap.txt.
11 extern unsigned int strhash(const char *buf
);
12 extern unsigned int strihash(const char *buf
);
13 extern unsigned int memhash(const void *buf
, size_t len
);
14 extern unsigned int memihash(const void *buf
, size_t len
);
15 extern unsigned int memihash_cont(unsigned int hash_seed
, const void *buf
, size_t len
);
17 static inline unsigned int sha1hash(const unsigned char *sha1
)
20 * Equivalent to 'return *(unsigned int *)sha1;', but safe on
21 * platforms that don't support unaligned reads.
24 memcpy(&hash
, sha1
, sizeof(hash
));
30 struct hashmap_entry
{
31 struct hashmap_entry
*next
;
35 typedef int (*hashmap_cmp_fn
)(const void *entry
, const void *entry_or_key
,
39 struct hashmap_entry
**table
;
41 unsigned int size
, tablesize
, grow_at
, shrink_at
;
46 struct hashmap_entry
*next
;
47 unsigned int tablepos
;
50 /* hashmap functions */
52 extern void hashmap_init(struct hashmap
*map
, hashmap_cmp_fn equals_function
,
54 extern void hashmap_free(struct hashmap
*map
, int free_entries
);
56 /* hashmap_entry functions */
58 static inline void hashmap_entry_init(void *entry
, unsigned int hash
)
60 struct hashmap_entry
*e
= entry
;
64 extern void *hashmap_get(const struct hashmap
*map
, const void *key
,
66 extern void *hashmap_get_next(const struct hashmap
*map
, const void *entry
);
67 extern void hashmap_add(struct hashmap
*map
, void *entry
);
68 extern void *hashmap_put(struct hashmap
*map
, void *entry
);
69 extern void *hashmap_remove(struct hashmap
*map
, const void *key
,
72 static inline void *hashmap_get_from_hash(const struct hashmap
*map
,
73 unsigned int hash
, const void *keydata
)
75 struct hashmap_entry key
;
76 hashmap_entry_init(&key
, hash
);
77 return hashmap_get(map
, &key
, keydata
);
80 /* hashmap_iter functions */
82 extern void hashmap_iter_init(struct hashmap
*map
, struct hashmap_iter
*iter
);
83 extern void *hashmap_iter_next(struct hashmap_iter
*iter
);
84 static inline void *hashmap_iter_first(struct hashmap
*map
,
85 struct hashmap_iter
*iter
)
87 hashmap_iter_init(map
, iter
);
88 return hashmap_iter_next(iter
);
91 /* string interning */
93 extern const void *memintern(const void *data
, size_t len
);
94 static inline const char *strintern(const char *string
)
96 return memintern(string
, strlen(string
));