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
|
/* Copyright (c) 2024, Peter McGoron
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "uns.h"
#include "uns_hashtable.h"
enum {
HTBL_LEN_PTR,
HTBL_USED_PTR,
HTBL_HDR_LEN
};
static size_t get_size_t(Uns_GC gc, struct uns_ctr *root, size_t i)
{
size_t r;
void *p = uns_get(gc, root->p, i, NULL);
memcpy(&r, p, sizeof(r));
return r;
}
static void set_size_t(Uns_GC gc, struct uns_ctr *root, size_t i, size_t val)
{
void *p = uns_get(gc, root->p, i, NULL);
memcpy(p, &val, sizeof(val));
}
static void *index(Uns_GC gc, struct uns_ctr *htbl, size_t i)
{
return uns_get(gc, htbl->p, i + HTBL_HDR_LEN, NULL);
}
void uns_hashtable_alloc_into(Uns_GC gc, struct uns_ctr *r, size_t size)
{
unsigned char *p;
size_t i;
r->p = uns_alloc_rec(gc, size + HTBL_HDR_LEN, 0);
p = uns_alloc(gc, sizeof(size_t), 0);
uns_set(gc, r->p, HTBL_LEN_PTR, UNS_POINTER, p);
set_size_t(gc, r, HTBL_LEN_PTR, size);
p = uns_alloc(gc, sizeof(size_t), 0);
uns_set(gc, r->p, HTBL_USED_PTR, UNS_POINTER, p);
set_size_t(gc, r, HTBL_USED_PTR, 0);
/* Set entries to zero. */
for (i = HTBL_HDR_LEN; i <= size; i++)
uns_set(gc, r->p, i, UNS_POINTER, NULL);
}
size_t uns_hashtable_len(Uns_GC gc, struct uns_ctr *htbl)
{
return get_size_t(gc, htbl, HTBL_LEN_PTR);
}
size_t uns_hashtable_used(Uns_GC gc, struct uns_ctr *htbl)
{
return get_size_t(gc, htbl, HTBL_USED_PTR);
}
enum {
LENGTH,
STRING,
VALUE,
NEXT,
HTBL_ENTRY_LEN
};
static void alloc_holder(Uns_GC gc, struct uns_ctr *r, struct uns_ctr *string,
struct uns_ctr *value, size_t len)
{
unsigned char *alloc_ptr;
r->p = uns_alloc_rec(gc, HTBL_ENTRY_LEN, 0);
alloc_ptr = uns_alloc(gc, sizeof(size_t), 0);
memcpy(alloc_ptr, &len, sizeof(size_t));
uns_set(gc, r->p, LENGTH, UNS_POINTER, alloc_ptr);
uns_set(gc, r->p, STRING, UNS_POINTER, string->p);
uns_set(gc, r->p, VALUE, UNS_POINTER, value->p);
uns_set(gc, r->p, NEXT, UNS_POINTER, NULL);
}
static unsigned long fnv(unsigned char *p, size_t len)
{
unsigned long hash = 0x811c9dc5;
size_t i;
for (i = 0; i < len; i++) {
hash ^= p[i];
hash *= 16777619;
}
return hash;
}
static int search_container(Uns_GC gc, struct uns_ctr *cur_ctr,
unsigned char *str, size_t len)
{
unsigned char *ctr_str = uns_get(gc, cur_ctr->p, STRING, NULL);
size_t ctr_str_len = get_size_t(gc, cur_ctr, LENGTH);
void *next;
if (ctr_str_len != len || memcmp(ctr_str, str, len) != 0) {
/* strings do not match */
next = uns_get(gc, cur_ctr->p, NEXT, NULL);
if (!next)
return 0;
cur_ctr->p = next;
return search_container(gc, cur_ctr, str, len);
} else {
return 1;
}
}
static int search_for_container(Uns_GC gc, struct uns_ctr *htbl,
struct uns_ctr *container,
unsigned char *str, size_t len, size_t *hash_out)
{
size_t htbl_len = uns_hashtable_len(gc, htbl);
size_t hash = fnv(str, len) % htbl_len;
if (hash_out)
*hash_out = hash;
container->p = index(gc, htbl, hash);
if (!container->p)
return 0;
return search_container(gc, container, str, len);
}
/* This could be made better with internal pointers, but that is not
* portable.
*/
void uns_hashtable_del(Uns_GC gc, struct uns_ctr *htbl,
unsigned char *str, size_t len,
struct uns_ctr *old_value)
{
size_t hash = fnv(str, len) % uns_hashtable_len(gc, htbl);
struct uns_ctr rec = {0};
struct uns_ctr prevptr = {0};
old_value->p = NULL;
rec.p = index(gc, htbl, hash);
if (!rec.p) {
return;
}
/* Special case: first pointer */
if (len == get_size_t(gc, &rec, LENGTH) &&
memcmp(str, uns_get(gc, rec.p, STRING, NULL), len) == 0) {
old_value->p = uns_get(gc, rec.p, VALUE, NULL);
uns_set(gc, htbl->p, HTBL_HDR_LEN + hash, UNS_POINTER,
uns_get(gc, rec.p, NEXT, NULL));
}
prevptr = rec;
for (rec.p = uns_get(gc, rec.p, NEXT, NULL); rec.p;
rec.p = uns_get(gc, rec.p, NEXT, NULL)) {
if (len == get_size_t(gc, &rec, LENGTH)
&& memcmp(str, uns_get(gc, rec.p, VALUE, NULL), len) == 0) {
old_value->p = uns_get(gc, rec.p, VALUE, NULL);
uns_set(gc, prevptr.p, UNS_POINTER, NEXT,
uns_get(gc, rec.p, NEXT, NULL));
return;
}
}
}
void uns_hashtable_search(Uns_GC gc, struct uns_ctr *htbl,
unsigned char *str, size_t string_len,
struct uns_ctr *found)
{
int r;
r = search_for_container(gc, htbl, found, str, string_len, NULL);
if (r)
found->p = uns_get(gc, found->p, VALUE, NULL);
else
found->p = NULL;
}
void uns_hashtable_add(Uns_GC gc, struct uns_ctr *htbl,
struct uns_ctr *string, size_t string_len,
struct uns_ctr *value, struct uns_ctr *old_value)
{
struct uns_ctr container = {0};
struct uns_ctr next_container = {0};
size_t hash;
if (uns_hashtable_used(gc, htbl) >= uns_hashtable_len(gc, htbl)*7/10)
uns_hashtable_resize(gc, htbl, uns_hashtable_len(gc, htbl)*2);
if (search_for_container(gc, htbl, &container, (unsigned char *)string->p, string_len, &hash)) {
old_value->p = uns_get(gc, container.p, VALUE, NULL);
uns_set(gc, container.p, VALUE, UNS_POINTER, value->p);
return;
}
set_size_t(gc, htbl, HTBL_USED_PTR,
uns_hashtable_used(gc, htbl) + 1);
old_value->p = NULL;
uns_root_add(gc, &container);
uns_root_add(gc, &next_container);
alloc_holder(gc, &next_container, string, value, string_len);
if (container.p) {
uns_set(gc, container.p, NEXT, UNS_POINTER, next_container.p);
} else {
uns_set(gc, htbl->p, hash + HTBL_HDR_LEN, UNS_POINTER, next_container.p);
}
uns_root_remove(gc, &next_container);
uns_root_remove(gc, &container);
}
static void add_for_ent(Uns_GC gc, struct uns_ctr *htbl, void *ctrp)
{
struct uns_ctr ctr = {0};
struct uns_ctr str = {0};
size_t str_len;
struct uns_ctr value = {0};
struct uns_ctr old_value_should_never_happen = {0};
if (!ctrp)
return;
ctr.p = ctrp;
uns_root_add(gc, &ctr);
uns_root_add(gc, &str);
uns_root_add(gc, &value);
uns_root_add(gc, &old_value_should_never_happen);
str.p = uns_get(gc, ctr.p, STRING, NULL);
value.p = uns_get(gc, ctr.p, VALUE, NULL);
str_len = get_size_t(gc, &ctr, LENGTH);
uns_hashtable_add(gc, htbl, &str, str_len, &value,
&old_value_should_never_happen);
assert(!old_value_should_never_happen.p);
uns_root_remove(gc, &old_value_should_never_happen);
uns_root_remove(gc, &value);
uns_root_remove(gc, &str);
uns_root_remove(gc, &ctr);
ctrp = uns_get(gc, ctr.p, NEXT, NULL);
add_for_ent(gc, htbl, ctrp);
}
void uns_hashtable_resize(Uns_GC gc, struct uns_ctr *htbl, size_t newsize)
{
struct uns_ctr newtbl = {0};
size_t i;
size_t oldlen = uns_hashtable_len(gc, htbl);
uns_root_add(gc, &newtbl);
uns_hashtable_alloc_into(gc, &newtbl, newsize);
set_size_t(gc, &newtbl, HTBL_USED_PTR,
uns_hashtable_used(gc, &newtbl));
for (i = 0; i < oldlen; i++)
add_for_ent(gc, &newtbl, index(gc, htbl, i));
htbl->p = newtbl.p;
uns_root_remove(gc, &newtbl);
}
|