forked from OAID/Tengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_impl.c
371 lines (288 loc) · 7.89 KB
/
hash_impl.c
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* License); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, OPEN AI LAB
* Author: [email protected]
*/
#include <string.h>
#include "hash_impl.h"
static inline void init_bucket(struct hash_bucket* b, int idx)
{
b->entry_count = 0;
b->idx = idx;
init_list(&b->head);
init_lock(&b->lock);
#ifdef HASH_STATS
b->del_count = 0;
b->hit_count = 0;
b->ins_count = 0;
b->search_count = 0;
#endif
}
static void release_hash_entry(struct hash_impl* h, struct hash_entry* e)
{
struct hash_bucket* b = e->bucket;
#ifdef HASH_STATS
b->del_count++;
b->entry_count--;
#endif
h->entry_num--;
remove_list(&e->link);
if (h->cpy_key)
sys_free(e->key);
if (h->free_func)
h->free_func(e->data);
sys_free(e);
}
static void release_hash(struct hash* t)
{
struct hash_impl* h = ( struct hash_impl* )t;
for (int i = 0; i < h->bucket_size; i++)
{
struct hash_entry* pos;
struct hash_entry* dummy;
struct hash_bucket* b = &h->bucket[i];
if (h->mt_safe)
lock(&b->lock);
list_for_each_entry_safe(pos, dummy, &b->head, link) release_hash_entry(h, pos);
if (h->mt_safe)
unlock(&b->lock);
}
sys_free(h->bucket);
sys_free(h);
}
static void config_hash(struct hash* t, int cpy_key, free_data_t func, int mt_safe, int max_num)
{
struct hash_impl* h = ( struct hash_impl* )t;
if (cpy_key >= 0)
h->cpy_key = cpy_key;
if (func != NULL)
h->free_func = func;
if (mt_safe > 0)
h->mt_safe = mt_safe;
if (max_num > 0)
h->max_num = max_num;
}
static inline struct hash_bucket* find_bucket(struct hash_impl* h, const void* key, int key_size)
{
unsigned int hash_int = h->hash_func(key, key_size);
int bucket_idx = hash_int % h->bucket_size;
return &h->bucket[bucket_idx];
}
static inline int compare_hash_key(const void* key0, int key0_size, const void* key1, int key1_size)
{
if (key0_size != key1_size)
return -1;
return memcmp(key0, key1, key0_size);
}
static struct hash_entry* find_entry(struct hash* t, const void* key, int key_size)
{
struct hash_impl* h = ( struct hash_impl* )t;
struct hash_bucket* b = find_bucket(h, key, key_size);
struct hash_entry* e;
int found = 0;
if (h->mt_safe)
lock(&b->lock);
#ifdef HASH_STATS
b->search_count++;
#endif
list_entry_for_each(e, &b->head, link)
{
if (compare_hash_key(e->key, e->key_size, key, key_size) == 0)
{
found = 1;
#ifdef HASH_STATS
b->hit_count++;
#endif
break;
}
}
if (h->mt_safe)
unlock(&b->lock);
if (found)
return e;
else
return NULL;
}
static hash_entry_t wrapper_find_entry(struct hash* t, const void* key, int key_size)
{
return find_entry(t, key, key_size);
}
static int insert_hash(struct hash* t, const void* key, int key_size, void* data)
{
struct hash_impl* h = ( struct hash_impl* )t;
struct hash_bucket* b = find_bucket(h, key, key_size);
struct hash_entry* e;
if (h->mt_safe)
lock(&b->lock);
list_entry_for_each(e, &b->head, link)
{
if (compare_hash_key(e->key, e->key_size, key, key_size) == 0)
{
// already exists, report error
if (h->mt_safe)
unlock(&b->lock);
return -1;
}
}
e = ( struct hash_entry* )sys_malloc(sizeof(struct hash_entry));
/* handle key */
e->key_size = key_size;
if (h->cpy_key)
{
e->key = sys_malloc(key_size);
memcpy(e->key, key, key_size);
}
else
e->key = ( void* )key;
e->data = data;
e->bucket = b;
h->entry_num++;
#ifdef HASH_STATS
b->ins_count++;
#endif
append_list(&e->link, &b->head);
if (h->mt_safe)
unlock(&b->lock);
return 0;
}
static int delete_hash(struct hash* t, const void* key, int key_size)
{
struct hash_impl* h = ( struct hash_impl* )t;
struct hash_bucket* b = find_bucket(h, key, key_size);
struct hash_entry* e;
int found = 0;
if (h->mt_safe)
lock(&b->lock);
list_entry_for_each(e, &b->head, link)
{
if (compare_hash_key(e->key, e->key_size, key, key_size) == 0)
{
found = 1;
break;
}
}
if (!found)
{
if (h->mt_safe)
unlock(&b->lock);
return -1;
}
release_hash_entry(h, e);
if (h->mt_safe)
unlock(&b->lock);
return 0;
}
static void* find_hash(struct hash* t, const void* key, int key_size)
{
struct hash_entry* e = find_entry(t, key, key_size);
if (e == NULL)
return NULL;
return e->data;
}
static void* get_data(hash_entry_t* e)
{
struct hash_entry* entry = ( struct hash_entry* )e;
return entry->data;
}
static void remove_entry(struct hash* t, hash_entry_t* e)
{
struct hash_impl* h = ( struct hash_impl* )t;
struct hash_entry* entry = ( struct hash_entry* )e;
struct hash_bucket* b = entry->bucket;
if (h->mt_safe)
lock(&b->lock);
release_hash_entry(h, entry);
if (h->mt_safe)
unlock(&b->lock);
}
static int get_entry_num(struct hash* t)
{
struct hash_impl* h = ( struct hash_impl* )t;
return h->entry_num;
}
static void reset_seq_access(struct hash* t)
{
struct hash_impl* h = ( struct hash_impl* )t;
h->seq_ptr = NULL;
}
static hash_entry_t get_next_entry(struct hash* t)
{
struct hash_impl* h = ( struct hash_impl* )t;
struct hash_entry* e = NULL;
int start_bucket = 0;
struct hash_bucket* b;
if (h->seq_ptr)
{
b = h->seq_ptr->bucket;
if (!list_entry_is_last(h->seq_ptr, &b->head, link))
{
h->seq_ptr = list_entry_next(h->seq_ptr, link);
return h->seq_ptr;
}
// move to next bucket
start_bucket = b->idx + 1;
}
else
start_bucket = 0;
for (int i = start_bucket; i < h->bucket_size; i++)
{
b = &h->bucket[i];
if (!list_empty(&b->head))
{
e = list_entry_head(&b->head, struct hash_entry, link);
h->seq_ptr = e;
break;
}
}
return e;
}
static int init_hash(struct hash* t, int bucket_size, hash_key_t func)
{
struct hash_impl* h = ( struct hash_impl* )t;
h->bucket_size = bucket_size;
h->hash_func = func;
h->free_func = NULL;
h->mt_safe = 1;
h->cpy_key = 1;
h->entry_num = 0;
h->max_num = -1;
h->seq_ptr = NULL;
h->bucket = sys_malloc(sizeof(struct hash_bucket) * bucket_size);
for (int i = 0; i < bucket_size; i++)
init_bucket(&h->bucket[i], i);
return 0;
}
struct hash* create_hash_impl(void)
{
struct hash* h = sys_malloc(sizeof(struct hash_impl));
h->init = init_hash;
h->release = release_hash;
h->config = config_hash;
h->insert = insert_hash;
h->delete = delete_hash;
h->find = find_hash;
h->find_entry = wrapper_find_entry;
h->get_data = get_data;
h->remove_entry = remove_entry;
h->get_entry_num = get_entry_num;
h->reset_seq_access = reset_seq_access;
h->get_next_entry = get_next_entry;
return h;
}