7
# define HASH_VALUE_CAST(table) (typeof((table)._value))
9
# define HASH_VALUE_CAST(table)
4
12
/* Returns hash code. */
5
13
typedef unsigned int hash_callback_t(const void *p);
6
14
/* Returns 0 if the pointers are equal. */
7
15
typedef int hash_cmp_callback_t(const void *p1, const void *p2);
9
17
/* Create a new hash table. If initial_size is 0, the default value is used.
10
If hash_cb or key_compare_cb is NULL, direct hashing/comparing is used.
12
18
table_pool is used to allocate/free large hash tables, node_pool is used
13
19
for smaller allocations and can also be alloconly pool. The pools must not
14
20
be free'd before hash_table_destroy() is called. */
16
hash_table_create(pool_t table_pool, pool_t node_pool, unsigned int initial_size,
17
hash_callback_t *hash_cb, hash_cmp_callback_t *key_compare_cb);
21
void hash_table_create(struct hash_table **table_r, pool_t node_pool,
22
unsigned int initial_size,
23
hash_callback_t *hash_cb,
24
hash_cmp_callback_t *key_compare_cb);
25
#if defined (__GNUC__) && !defined(__cplusplus)
26
# define hash_table_create(table, pool, size, hash_cb, key_cmp_cb) \
27
({(void)COMPILE_ERROR_IF_TRUE( \
28
sizeof((*table)._key) != sizeof(void *) || \
29
sizeof((*table)._value) != sizeof(void *)); \
30
(void)COMPILE_ERROR_IF_TRUE( \
31
!__builtin_types_compatible_p(typeof(&key_cmp_cb), \
32
int (*)(typeof((*table)._key), typeof((*table)._key))) && \
33
!__builtin_types_compatible_p(typeof(&key_cmp_cb), \
34
int (*)(typeof((*table)._const_key), typeof((*table)._const_key)))); \
35
(void)COMPILE_ERROR_IF_TRUE( \
36
!__builtin_types_compatible_p(typeof(&hash_cb), \
37
unsigned int (*)(typeof((*table)._key))) && \
38
!__builtin_types_compatible_p(typeof(&hash_cb), \
39
unsigned int (*)(typeof((*table)._const_key)))); \
40
hash_table_create(&(*table)._table, pool, size, \
41
(hash_callback_t *)hash_cb, \
42
(hash_cmp_callback_t *)key_cmp_cb);})
44
# define hash_table_create(table, pool, size, hash_cb, key_cmp_cb) \
45
hash_table_create(&(*table)._table, pool, size, \
46
(hash_callback_t *)hash_cb, \
47
(hash_cmp_callback_t *)key_cmp_cb)
50
/* Create hash table where comparisons are done directly with the pointers. */
51
void hash_table_create_direct(struct hash_table **table_r, pool_t node_pool,
52
unsigned int initial_size);
53
#if defined (__GNUC__) && !defined(__cplusplus)
54
# define hash_table_create_direct(table, pool, size) \
55
({(void)COMPILE_ERROR_IF_TRUE( \
56
sizeof((*table)._key) != sizeof(void *) || \
57
sizeof((*table)._value) != sizeof(void *)); \
58
hash_table_create_direct(&(*table)._table, pool, size);})
60
# define hash_table_create_direct(table, pool, size) \
61
hash_table_create_direct(&(*table)._table, pool, size)
64
#define hash_table_is_created(table) \
65
((table)._table != NULL)
18
67
void hash_table_destroy(struct hash_table **table);
68
#define hash_table_destroy(table) \
69
hash_table_destroy(&(*table)._table)
19
70
/* Remove all nodes from hash table. If free_collisions is TRUE, the
20
memory allocated from node_pool is freed, or discarded with
71
memory allocated from node_pool is freed, or discarded with alloconly pools.
72
WARNING: If you p_clear() the node_pool, the free_collisions must be TRUE. */
22
73
void hash_table_clear(struct hash_table *table, bool free_collisions);
74
#define hash_table_clear(table, free_collisions) \
75
hash_table_clear((table)._table, free_collisions)
24
77
void *hash_table_lookup(const struct hash_table *table, const void *key) ATTR_PURE;
78
#define hash_table_lookup(table, key) \
79
HASH_VALUE_CAST(table)hash_table_lookup((table)._table, \
80
(const void *)((const char *)(key) + COMPILE_ERROR_IF_TYPES2_NOT_COMPATIBLE((table)._key, (table)._const_key, key)))
25
82
bool hash_table_lookup_full(const struct hash_table *table,
26
83
const void *lookup_key,
27
void **orig_key, void **value);
84
void **orig_key_r, void **value_r);
86
# define hash_table_lookup_full(table, lookup_key, orig_key_r, value_r) \
87
hash_table_lookup_full((table)._table, \
88
(void *)((const char *)(lookup_key) + COMPILE_ERROR_IF_TYPES2_NOT_COMPATIBLE((table)._const_key, (table)._key, lookup_key)), \
89
(void *)((orig_key_r) + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE((table)._keyp, orig_key_r) + \
90
COMPILE_ERROR_IF_TRUE(sizeof(*orig_key_r) != sizeof(void *))), \
91
(void *)((value_r) + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE((table)._valuep, value_r) + \
92
COMPILE_ERROR_IF_TRUE(sizeof(*value_r) != sizeof(void *))))
94
/* C++ requires (void **) casting, but that's not possible with strict
95
aliasing, so .. we'll just disable the type checks */
96
# define hash_table_lookup_full(table, lookup_key, orig_key_r, value_r) \
97
hash_table_lookup_full((table)._table, lookup_key, orig_key_r, value_r)
29
100
/* Insert/update node in hash table. The difference is that hash_table_insert()
30
101
replaces the key in table to given one, while hash_table_update() doesnt. */
31
102
void hash_table_insert(struct hash_table *table, void *key, void *value);
32
103
void hash_table_update(struct hash_table *table, void *key, void *value);
104
#define hash_table_insert(table, key, value) \
105
hash_table_insert((table)._table, \
106
(void *)((char*)(key) + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE((table)._key, key)), \
107
(void *)((char*)(value) + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE((table)._value, value)))
108
#define hash_table_update(table, key, value) \
109
hash_table_update((table)._table, \
110
(void *)((char *)(key) + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE((table)._key, key)), \
111
(void *)((char *)(value) + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE((table)._value, value)))
34
113
void hash_table_remove(struct hash_table *table, const void *key);
114
#define hash_table_remove(table, key) \
115
hash_table_remove((table)._table, \
116
(const void *)((const char *)(key) + COMPILE_ERROR_IF_TYPES2_NOT_COMPATIBLE((table)._const_key, (table)._key, key)))
35
117
unsigned int hash_table_count(const struct hash_table *table) ATTR_PURE;
118
#define hash_table_count(table) \
119
hash_table_count((table)._table)
37
121
/* Iterates through all nodes in hash table. You may safely call hash_table_*()
38
122
functions while iterating, but if you add any new nodes, they may or may
39
123
not be called for in this iteration. */
40
124
struct hash_iterate_context *hash_table_iterate_init(struct hash_table *table);
125
#define hash_table_iterate_init(table) \
126
hash_table_iterate_init((table)._table)
41
127
bool hash_table_iterate(struct hash_iterate_context *ctx,
42
128
void **key_r, void **value_r);
130
# define hash_table_iterate(ctx, table, key_r, value_r) \
131
hash_table_iterate(ctx, \
132
(void *)((key_r) + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE((table)._keyp, key_r) + \
133
COMPILE_ERROR_IF_TRUE(sizeof(*key_r) != sizeof(void *)) + \
134
COMPILE_ERROR_IF_TRUE(sizeof(*value_r) != sizeof(void *))), \
135
(void *)((value_r) + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE((table)._valuep, value_r)))
137
/* C++ requires (void **) casting, but that's not possible with strict
138
aliasing, so .. we'll just disable the type checks */
139
# define hash_table_iterate(ctx, table, key_r, value_r) \
140
hash_table_iterate(ctx, key_r, value_r)
43
143
void hash_table_iterate_deinit(struct hash_iterate_context **ctx);
45
145
/* Hash table isn't resized, and removed nodes aren't removed from
46
146
the list while hash table is freezed. Supports nesting. */
47
147
void hash_table_freeze(struct hash_table *table);
48
148
void hash_table_thaw(struct hash_table *table);
149
#define hash_table_freeze(table) \
150
hash_table_freeze((table)._table)
151
#define hash_table_thaw(table) \
152
hash_table_thaw((table)._table)
50
154
/* Copy all nodes from one hash table to another */
51
155
void hash_table_copy(struct hash_table *dest, struct hash_table *src);
156
#define hash_table_copy(table1, table2) \
157
hash_table_copy((table1)._table, (table2)._table)
53
159
/* hash function for strings */
54
unsigned int str_hash(const void *p) ATTR_PURE;
55
unsigned int strcase_hash(const void *p) ATTR_PURE;
160
unsigned int str_hash(const char *p) ATTR_PURE;
161
unsigned int strcase_hash(const char *p) ATTR_PURE;
56
162
/* a generic hash for a given memory block */
57
163
unsigned int mem_hash(const void *p, unsigned int size) ATTR_PURE;