2 * Copyright © 2009,2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
34 #include "c99_compat.h"
48 struct hash_entry
*table
;
49 uint32_t (*key_hash_function
)(const void *key
);
50 bool (*key_equals_function
)(const void *a
, const void *b
);
51 const void *deleted_key
;
55 uint64_t rehash_magic
;
59 uint32_t deleted_entries
;
63 _mesa_hash_table_create(void *mem_ctx
,
64 uint32_t (*key_hash_function
)(const void *key
),
65 bool (*key_equals_function
)(const void *a
,
69 _mesa_hash_table_init(struct hash_table
*ht
,
71 uint32_t (*key_hash_function
)(const void *key
),
72 bool (*key_equals_function
)(const void *a
,
76 _mesa_hash_table_clone(struct hash_table
*src
, void *dst_mem_ctx
);
77 void _mesa_hash_table_destroy(struct hash_table
*ht
,
78 void (*delete_function
)(struct hash_entry
*entry
));
79 void _mesa_hash_table_clear(struct hash_table
*ht
,
80 void (*delete_function
)(struct hash_entry
*entry
));
81 void _mesa_hash_table_set_deleted_key(struct hash_table
*ht
,
82 const void *deleted_key
);
84 static inline uint32_t _mesa_hash_table_num_entries(struct hash_table
*ht
)
90 _mesa_hash_table_insert(struct hash_table
*ht
, const void *key
, void *data
);
92 _mesa_hash_table_insert_pre_hashed(struct hash_table
*ht
, uint32_t hash
,
93 const void *key
, void *data
);
95 _mesa_hash_table_search(struct hash_table
*ht
, const void *key
);
97 _mesa_hash_table_search_pre_hashed(struct hash_table
*ht
, uint32_t hash
,
99 void _mesa_hash_table_remove(struct hash_table
*ht
,
100 struct hash_entry
*entry
);
101 void _mesa_hash_table_remove_key(struct hash_table
*ht
,
104 struct hash_entry
*_mesa_hash_table_next_entry(struct hash_table
*ht
,
105 struct hash_entry
*entry
);
107 _mesa_hash_table_random_entry(struct hash_table
*ht
,
108 bool (*predicate
)(struct hash_entry
*entry
));
110 uint32_t _mesa_hash_data(const void *data
, size_t size
);
111 uint32_t _mesa_hash_string(const void *key
);
112 bool _mesa_key_string_equal(const void *a
, const void *b
);
113 bool _mesa_key_pointer_equal(const void *a
, const void *b
);
115 static inline uint32_t _mesa_key_hash_string(const void *key
)
117 return _mesa_hash_string((const char *)key
);
120 static inline uint32_t _mesa_hash_pointer(const void *pointer
)
122 uintptr_t num
= (uintptr_t) pointer
;
123 return (uint32_t) ((num
>> 2) ^ (num
>> 6) ^ (num
>> 10) ^ (num
>> 14));
127 _mesa_pointer_hash_table_create(void *mem_ctx
);
130 _mesa_fnv32_1a_offset_bias
= 2166136261u,
133 static inline uint32_t
134 _mesa_fnv32_1a_accumulate_block(uint32_t hash
, const void *data
, size_t size
)
136 const uint8_t *bytes
= (const uint8_t *)data
;
138 while (size
-- != 0) {
140 hash
= hash
* 0x01000193;
147 #define _mesa_fnv32_1a_accumulate(hash, expr) \
148 _mesa_fnv32_1a_accumulate_block(hash, &(expr), sizeof(expr))
151 * This foreach function is safe against deletion (which just replaces
152 * an entry's data with the deleted marker), but not against insertion
153 * (which may rehash the table, making entry a dangling pointer).
155 #define hash_table_foreach(ht, entry) \
156 for (struct hash_entry *entry = _mesa_hash_table_next_entry(ht, NULL); \
158 entry = _mesa_hash_table_next_entry(ht, entry))
161 hash_table_call_foreach(struct hash_table
*ht
,
162 void (*callback
)(const void *key
,
167 hash_table_foreach(ht
, entry
)
168 callback(entry
->key
, entry
->data
, closure
);
172 * Hash table wrapper which supports 64-bit keys.
174 struct hash_table_u64
{
175 struct hash_table
*table
;
176 void *freed_key_data
;
177 void *deleted_key_data
;
180 struct hash_table_u64
*
181 _mesa_hash_table_u64_create(void *mem_ctx
);
184 _mesa_hash_table_u64_destroy(struct hash_table_u64
*ht
,
185 void (*delete_function
)(struct hash_entry
*entry
));
188 _mesa_hash_table_u64_insert(struct hash_table_u64
*ht
, uint64_t key
,
192 _mesa_hash_table_u64_search(struct hash_table_u64
*ht
, uint64_t key
);
195 _mesa_hash_table_u64_remove(struct hash_table_u64
*ht
, uint64_t key
);
198 _mesa_hash_table_u64_clear(struct hash_table_u64
*ht
,
199 void (*delete_function
)(struct hash_entry
*entry
));
205 #endif /* _HASH_TABLE_H */