1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/**************************************************************************
*
* Copyright 2008 VMware, Inc.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/**
* Key lookup/associative container.
*
* Like Jose's util_hash_table, based on CSO cache code for now.
*
* Author: Brian Paul
*/
#include "pipe/p_compiler.h"
#include "util/u_debug.h"
#include "cso_cache/cso_hash.h"
#include "util/u_memory.h"
#include "util/u_keymap.h"
struct keymap
{
struct cso_hash *cso;
unsigned key_size;
unsigned max_entries; /* XXX not obeyed net */
unsigned num_entries;
keymap_delete_func delete_func;
};
struct keymap_item
{
void *key, *value;
};
/**
* This the default key-delete function used when the client doesn't
* provide one.
*/
static void
default_delete_func(const struct keymap *map,
const void *key, void *data, void *user)
{
FREE((void*) data);
}
static inline struct keymap_item *
hash_table_item(struct cso_hash_iter iter)
{
return (struct keymap_item *) cso_hash_iter_data(iter);
}
/**
* Return 4-byte hash key for a block of bytes.
*/
static unsigned
hash(const void *key, unsigned keySize)
{
unsigned i, hash;
keySize /= 4; /* convert from bytes to uints */
hash = 0;
for (i = 0; i < keySize; i++) {
hash ^= (i + 1) * ((const unsigned *) key)[i];
}
/*hash = hash ^ (hash >> 11) ^ (hash >> 22);*/
return hash;
}
/**
* Create a new map.
* \param keySize size of the keys in bytes
* \param maxEntries max number of entries to allow (~0 = infinity)
* \param deleteFunc optional callback to call when entries
* are deleted/replaced
*/
struct keymap *
util_new_keymap(unsigned keySize, unsigned maxEntries,
keymap_delete_func deleteFunc)
{
struct keymap *map = MALLOC_STRUCT(keymap);
if (!map)
return NULL;
map->cso = cso_hash_create();
if (!map->cso) {
FREE(map);
return NULL;
}
map->max_entries = maxEntries;
map->num_entries = 0;
map->key_size = keySize;
map->delete_func = deleteFunc ? deleteFunc : default_delete_func;
return map;
}
/**
* Delete/free a keymap and all entries. The deleteFunc that was given at
* create time will be called for each entry.
* \param user user-provided pointer passed through to the delete callback
*/
void
util_delete_keymap(struct keymap *map, void *user)
{
util_keymap_remove_all(map, user);
cso_hash_delete(map->cso);
FREE(map);
}
static inline struct cso_hash_iter
hash_table_find_iter(const struct keymap *map, const void *key,
unsigned key_hash)
{
struct cso_hash_iter iter;
struct keymap_item *item;
iter = cso_hash_find(map->cso, key_hash);
while (!cso_hash_iter_is_null(iter)) {
item = (struct keymap_item *) cso_hash_iter_data(iter);
if (!memcmp(item->key, key, map->key_size))
break;
iter = cso_hash_iter_next(iter);
}
return iter;
}
static inline struct keymap_item *
hash_table_find_item(const struct keymap *map, const void *key,
unsigned key_hash)
{
struct cso_hash_iter iter = hash_table_find_iter(map, key, key_hash);
if (cso_hash_iter_is_null(iter)) {
return NULL;
}
else {
return hash_table_item(iter);
}
}
/**
* Insert a new key + data pointer into the table.
* Note: we create a copy of the key, but not the data!
* If the key is already present in the table, replace the existing
* entry (calling the delete callback on the previous entry).
* If the maximum capacity of the map is reached an old entry
* will be deleted (the delete callback will be called).
*/
boolean
util_keymap_insert(struct keymap *map, const void *key,
const void *data, void *user)
{
unsigned key_hash;
struct keymap_item *item;
struct cso_hash_iter iter;
assert(map);
if (!map)
return FALSE;
key_hash = hash(key, map->key_size);
item = hash_table_find_item(map, key, key_hash);
if (item) {
/* call delete callback for old entry/item */
map->delete_func(map, item->key, item->value, user);
item->value = (void *) data;
return TRUE;
}
item = MALLOC_STRUCT(keymap_item);
if (!item)
return FALSE;
item->key = mem_dup(key, map->key_size);
item->value = (void *) data;
iter = cso_hash_insert(map->cso, key_hash, item);
if (cso_hash_iter_is_null(iter)) {
FREE(item);
return FALSE;
}
map->num_entries++;
return TRUE;
}
/**
* Look up a key in the map and return the associated data pointer.
*/
const void *
util_keymap_lookup(const struct keymap *map, const void *key)
{
unsigned key_hash;
struct keymap_item *item;
assert(map);
if (!map)
return NULL;
key_hash = hash(key, map->key_size);
item = hash_table_find_item(map, key, key_hash);
if (!item)
return NULL;
return item->value;
}
/**
* Remove an entry from the map.
* The delete callback will be called if the given key/entry is found.
* \param user passed to the delete callback as the last param.
*/
void
util_keymap_remove(struct keymap *map, const void *key, void *user)
{
unsigned key_hash;
struct cso_hash_iter iter;
struct keymap_item *item;
assert(map);
if (!map)
return;
key_hash = hash(key, map->key_size);
iter = hash_table_find_iter(map, key, key_hash);
if (cso_hash_iter_is_null(iter))
return;
item = hash_table_item(iter);
assert(item);
if (!item)
return;
map->delete_func(map, item->key, item->value, user);
FREE(item->key);
FREE(item);
map->num_entries--;
cso_hash_erase(map->cso, iter);
}
/**
* Remove all entries from the map, calling the delete callback for each.
* \param user passed to the delete callback as the last param.
*/
void
util_keymap_remove_all(struct keymap *map, void *user)
{
struct cso_hash_iter iter;
struct keymap_item *item;
assert(map);
if (!map)
return;
iter = cso_hash_first_node(map->cso);
while (!cso_hash_iter_is_null(iter)) {
item = (struct keymap_item *)
cso_hash_take(map->cso, cso_hash_iter_key(iter));
map->delete_func(map, item->key, item->value, user);
FREE(item->key);
FREE(item);
iter = cso_hash_first_node(map->cso);
}
}
extern void
util_keymap_info(const struct keymap *map)
{
debug_printf("Keymap %p: %u of max %u entries\n",
(void *) map, map->num_entries, map->max_entries);
}
|