forked from mabu233/sdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxdebug_hash.c
More file actions
299 lines (250 loc) · 7.95 KB
/
xdebug_hash.c
File metadata and controls
299 lines (250 loc) · 7.95 KB
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
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2018 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.01 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| https://xdebug.org/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| derick@xdebug.org so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Sterling Hughes <sterling@php.net> |
+----------------------------------------------------------------------+
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "xdebug_hash.h"
#include "xdebug_llist.h"
/*
* Helper function to make a null terminated string from a key
*/
char *xdebug_hash_key_to_str(xdebug_hash_key* key, int* new_len)
{
char *tmp;
tmp = calloc (key->value.str.len + 1, 1);
memcpy(tmp, key->value.str.val, key->value.str.len);
*new_len = key->value.str.len;
return tmp;
}
static xdebug_ui32 xdebug_hash_str(const char *key, unsigned int key_length)
{
char *p = (char *) key;
char *end = (char *) key + key_length;
unsigned long h = 5381;
while (p < end) {
h += h << 5;
h ^= (unsigned long) *p++;
}
return h;
}
static xdebug_ui32 xdebug_hash_num(xdebug_ui32 key)
{
key += ~(key << 15);
key ^= (key >> 10);
key += (key << 3);
key ^= (key >> 6);
key += (key << 11);
key ^= (key >> 16);
return key;
}
static void hash_element_dtor(void *u, void *ele)
{
xdebug_hash_element *e = (xdebug_hash_element *) ele;
xdebug_hash *h = (xdebug_hash *) u;
if (e->key.type == XDEBUG_HASH_KEY_IS_STRING) {
free(e->key.value.str.val);
}
if (h->dtor) {
h->dtor(e->ptr);
}
free(e);
e = NULL;
}
xdebug_hash *xdebug_hash_alloc(int slots, xdebug_hash_dtor_t dtor)
{
xdebug_hash *h;
int i;
h = malloc(sizeof(xdebug_hash));
h->dtor = dtor;
h->sorter = NULL;
h->size = 0;
h->slots = slots;
h->table = (xdebug_llist **) malloc(slots * sizeof(xdebug_llist *));
for (i = 0; i < h->slots; ++i) {
h->table[i] = xdebug_llist_alloc((xdebug_llist_dtor) hash_element_dtor);
}
return h;
}
xdebug_hash *xdebug_hash_alloc_with_sort(int slots, xdebug_hash_dtor_t dtor, xdebug_hash_apply_sorter_t sorter)
{
xdebug_hash *h;
h = xdebug_hash_alloc(slots, dtor);
h->sorter = sorter;
return h;
}
#define FIND_SLOT(__h, __s_key, __s_key_len, __n_key) \
((__s_key ? xdebug_hash_str(__s_key, __s_key_len) : xdebug_hash_num(__n_key)) % (__h)->slots)
#define KEY_CREATE(__k, __s_key, __s_key_len, __n_key, __dup) \
if (__s_key) { \
if (__dup) { \
(__k)->value.str.val = (char *) malloc(__s_key_len); \
memcpy((__k)->value.str.val, __s_key, __s_key_len); \
} else { \
(__k)->value.str.val = __s_key; \
} \
(__k)->value.str.len = __s_key_len; \
(__k)->type = XDEBUG_HASH_KEY_IS_STRING; \
} else { \
(__k)->value.str.len = 0; \
(__k)->value.num = __n_key; \
(__k)->type = XDEBUG_HASH_KEY_IS_NUM; \
}
static int xdebug_hash_key_compare(xdebug_hash_key *key1, xdebug_hash_key *key2)
{
if (key1->type == XDEBUG_HASH_KEY_IS_NUM) {
if (key2->type == XDEBUG_HASH_KEY_IS_STRING)
return 0;
if (key1->value.num == key2->value.num)
return 1;
} else {
if (key2->type == XDEBUG_HASH_KEY_IS_NUM)
return 0;
if (key1->value.str.len == key2->value.str.len &&
*key1->value.str.val == *key2->value.str.val &&
memcmp(key1->value.str.val, key2->value.str.val, key1->value.str.len) == 0) {
return 1;
}
}
return 0;
}
int xdebug_hash_add_or_update(xdebug_hash *h, const char *str_key, unsigned int str_key_len, unsigned long num_key, const void *p)
{
xdebug_hash_element *e;
xdebug_hash_key tmp;
xdebug_llist *l;
xdebug_llist_element *le;
int slot;
slot = FIND_SLOT(h, str_key, str_key_len, num_key);
l = h->table[slot];
KEY_CREATE(&tmp, (char*) str_key, str_key_len, num_key, 0);
for (le = XDEBUG_LLIST_HEAD(l); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
if (xdebug_hash_key_compare(&tmp, &((xdebug_hash_element *) XDEBUG_LLIST_VALP(le))->key)) {
xdebug_hash_element *to_update = XDEBUG_LLIST_VALP(le);
if (h->dtor) {
h->dtor(to_update->ptr);
}
to_update->ptr = (void *) p;
return 1;
}
}
e = (xdebug_hash_element *) malloc(sizeof(xdebug_hash_element));
KEY_CREATE(&e->key, (char*) str_key, str_key_len, num_key, 1);
e->ptr = (void *) p;
if (xdebug_llist_insert_next(l, XDEBUG_LLIST_TAIL(l), e)) {
++h->size;
return 1;
} else {
return 0;
}
}
int xdebug_hash_extended_delete(xdebug_hash *h, const char *str_key, unsigned int str_key_len, xdebug_ui32 num_key)
{
xdebug_llist *l;
xdebug_llist_element *le;
xdebug_hash_key tmp;
int slot;
slot = FIND_SLOT(h, str_key, str_key_len, num_key);
l = h->table[slot];
KEY_CREATE(&tmp, (char*) str_key, str_key_len, num_key, 0);
for (le = XDEBUG_LLIST_HEAD(l); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
if (xdebug_hash_key_compare(&tmp, &((xdebug_hash_element *) XDEBUG_LLIST_VALP(le))->key)) {
xdebug_llist_remove(l, le, (void *) h);
--h->size;
return 1;
}
}
return 0;
}
int xdebug_hash_extended_find(xdebug_hash *h, const char *str_key, unsigned int str_key_len, xdebug_ui32 num_key, void **p)
{
xdebug_llist *l;
xdebug_llist_element *le;
xdebug_hash_key tmp;
int slot;
slot = FIND_SLOT(h, str_key, str_key_len, num_key);
l = h->table[slot];
KEY_CREATE(&tmp, (char*) str_key, str_key_len, num_key, 0);
for (le = XDEBUG_LLIST_HEAD(l); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
if (xdebug_hash_key_compare(&tmp, &((xdebug_hash_element *) XDEBUG_LLIST_VALP(le))->key)) {
*p = ((xdebug_hash_element *) XDEBUG_LLIST_VALP(le))->ptr;
return 1;
}
}
return 0;
}
void xdebug_hash_apply(xdebug_hash *h, void *user, void (*cb)(void *, xdebug_hash_element *))
{
xdebug_llist_element *le;
int i;
for (i = 0; i < h->slots; ++i) {
for (le = XDEBUG_LLIST_HEAD(h->table[i]); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
cb(user, (xdebug_hash_element *) XDEBUG_LLIST_VALP(le));
}
}
}
void xdebug_hash_apply_with_argument(xdebug_hash *h, void *user, void (*cb)(void *, xdebug_hash_element *, void *), void *argument)
{
xdebug_llist_element *le;
int i;
int num_items = 0;
xdebug_hash_element **pp_he_list;
if (h->sorter) {
for (i = 0; i < h->slots; ++i) {
for (le = XDEBUG_LLIST_HEAD(h->table[i]); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
num_items += 1;
}
}
pp_he_list = (xdebug_hash_element **) malloc((size_t) (num_items * sizeof(xdebug_hash_element **)));
if (pp_he_list) {
int j = 0;
for (i = 0; i < h->slots; ++i) {
for (le = XDEBUG_LLIST_HEAD(h->table[i]); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
pp_he_list[j++] = XDEBUG_LLIST_VALP(le);
}
}
qsort(pp_he_list, num_items, sizeof(xdebug_llist_element *), h->sorter);
for (i = 0; i < num_items; ++i) {
cb(user, pp_he_list[i], argument);
}
free((void *) pp_he_list);
return;
}
}
for (i = 0; i < h->slots; ++i) {
for (le = XDEBUG_LLIST_HEAD(h->table[i]); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
cb(user, (xdebug_hash_element *) XDEBUG_LLIST_VALP(le), argument);
}
}
}
void xdebug_hash_destroy(xdebug_hash *h)
{
int i;
for (i = 0; i < h->slots; ++i) {
xdebug_llist_destroy(h->table[i], (void *) h);
}
free(h->table);
free(h);
}
/*
* Local Variables:
* c-basic-offset: 4
* tab-width: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/