/src/jansson/src/hashtable.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org> |
3 | | * |
4 | | * This library is free software; you can redistribute it and/or modify |
5 | | * it under the terms of the MIT license. See LICENSE for details. |
6 | | */ |
7 | | |
8 | | #ifdef HAVE_CONFIG_H |
9 | | #include <jansson_private_config.h> |
10 | | #endif |
11 | | |
12 | | #include <stdlib.h> |
13 | | #include <string.h> |
14 | | |
15 | | #ifdef HAVE_STDINT_H |
16 | | #include <stdint.h> |
17 | | #endif |
18 | | |
19 | | #include "hashtable.h" |
20 | | #include "jansson_private.h" /* for container_of() */ |
21 | | #include <jansson_config.h> /* for JSON_INLINE */ |
22 | | |
23 | | #ifndef INITIAL_HASHTABLE_ORDER |
24 | | #define INITIAL_HASHTABLE_ORDER 3 |
25 | | #endif |
26 | | |
27 | | typedef struct hashtable_list list_t; |
28 | | typedef struct hashtable_pair pair_t; |
29 | | typedef struct hashtable_bucket bucket_t; |
30 | | |
31 | | extern volatile uint32_t hashtable_seed; |
32 | | |
33 | | /* Implementation of the hash function */ |
34 | | #include "lookup3.h" |
35 | | |
36 | 414k | #define list_to_pair(list_) container_of(list_, pair_t, list) |
37 | 55.9k | #define ordered_list_to_pair(list_) container_of(list_, pair_t, ordered_list) |
38 | 757k | #define hash_str(key, len) ((size_t)hashlittle((key), len, hashtable_seed)) |
39 | | |
40 | 456k | static JSON_INLINE void list_init(list_t *list) { |
41 | 456k | list->next = list; |
42 | 456k | list->prev = list; |
43 | 456k | } |
44 | | |
45 | 407k | static JSON_INLINE void list_insert(list_t *list, list_t *node) { |
46 | 407k | node->next = list; |
47 | 407k | node->prev = list->prev; |
48 | 407k | list->prev->next = node; |
49 | 407k | list->prev = node; |
50 | 407k | } |
51 | | |
52 | 233k | static JSON_INLINE void list_remove(list_t *list) { |
53 | 233k | list->prev->next = list->next; |
54 | 233k | list->next->prev = list->prev; |
55 | 233k | } |
56 | | |
57 | 1.00M | static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) { |
58 | 1.00M | return bucket->first == &hashtable->list && bucket->first == bucket->last; |
59 | 1.00M | } |
60 | | |
61 | 246k | static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket, list_t *list) { |
62 | 246k | if (bucket_is_empty(hashtable, bucket)) { |
63 | 182k | list_insert(&hashtable->list, list); |
64 | 182k | bucket->first = bucket->last = list; |
65 | 182k | } else { |
66 | 63.8k | list_insert(bucket->first, list); |
67 | 63.8k | bucket->first = list; |
68 | 63.8k | } |
69 | 246k | } |
70 | | |
71 | | static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket, |
72 | 757k | const char *key, size_t key_len, size_t hash) { |
73 | 757k | list_t *list; |
74 | 757k | pair_t *pair; |
75 | | |
76 | 757k | if (bucket_is_empty(hashtable, bucket)) |
77 | 519k | return NULL; |
78 | | |
79 | 238k | list = bucket->first; |
80 | 283k | while (1) { |
81 | 283k | pair = list_to_pair(list); |
82 | 283k | if (pair->hash == hash && pair->key_len == key_len && |
83 | 124k | memcmp(pair->key, key, key_len) == 0) |
84 | 124k | return pair; |
85 | | |
86 | 159k | if (list == bucket->last) |
87 | 114k | break; |
88 | | |
89 | 45.7k | list = list->next; |
90 | 45.7k | } |
91 | | |
92 | 114k | return NULL; |
93 | 238k | } |
94 | | |
95 | | /* returns 0 on success, -1 if key was not found */ |
96 | | static int hashtable_do_del(hashtable_t *hashtable, const char *key, size_t key_len, |
97 | 116k | size_t hash) { |
98 | 116k | pair_t *pair; |
99 | 116k | bucket_t *bucket; |
100 | 116k | size_t index; |
101 | | |
102 | 116k | index = hash & hashmask(hashtable->order); |
103 | 116k | bucket = &hashtable->buckets[index]; |
104 | | |
105 | 116k | pair = hashtable_find_pair(hashtable, bucket, key, key_len, hash); |
106 | 116k | if (!pair) |
107 | 0 | return -1; |
108 | | |
109 | 116k | if (&pair->list == bucket->first && &pair->list == bucket->last) |
110 | 86.3k | bucket->first = bucket->last = &hashtable->list; |
111 | | |
112 | 30.3k | else if (&pair->list == bucket->first) |
113 | 22.4k | bucket->first = pair->list.next; |
114 | | |
115 | 7.83k | else if (&pair->list == bucket->last) |
116 | 7.83k | bucket->last = pair->list.prev; |
117 | | |
118 | 116k | list_remove(&pair->list); |
119 | 116k | list_remove(&pair->ordered_list); |
120 | 116k | json_decref(pair->value); |
121 | | |
122 | 116k | jsonp_free(pair); |
123 | 116k | hashtable->size--; |
124 | | |
125 | 116k | return 0; |
126 | 116k | } |
127 | | |
128 | 65.2k | static void hashtable_do_clear(hashtable_t *hashtable) { |
129 | 65.2k | list_t *list, *next; |
130 | 65.2k | pair_t *pair; |
131 | | |
132 | 109k | for (list = hashtable->list.next; list != &hashtable->list; list = next) { |
133 | 43.7k | next = list->next; |
134 | 43.7k | pair = list_to_pair(list); |
135 | 43.7k | json_decref(pair->value); |
136 | 43.7k | jsonp_free(pair); |
137 | 43.7k | } |
138 | 65.2k | } |
139 | | |
140 | 4.59k | static int hashtable_do_rehash(hashtable_t *hashtable) { |
141 | 4.59k | list_t *list, *next; |
142 | 4.59k | pair_t *pair; |
143 | 4.59k | size_t i, index, new_size, new_order; |
144 | 4.59k | struct hashtable_bucket *new_buckets; |
145 | | |
146 | 4.59k | new_order = hashtable->order + 1; |
147 | 4.59k | new_size = hashsize(new_order); |
148 | | |
149 | 4.59k | new_buckets = jsonp_malloc(new_size * sizeof(bucket_t)); |
150 | 4.59k | if (!new_buckets) |
151 | 0 | return -1; |
152 | | |
153 | 4.59k | jsonp_free(hashtable->buckets); |
154 | 4.59k | hashtable->buckets = new_buckets; |
155 | 4.59k | hashtable->order = new_order; |
156 | | |
157 | 177k | for (i = 0; i < new_size; i++) { |
158 | 172k | hashtable->buckets[i].first = hashtable->buckets[i].last = &hashtable->list; |
159 | 172k | } |
160 | | |
161 | 4.59k | list = hashtable->list.next; |
162 | 4.59k | list_init(&hashtable->list); |
163 | | |
164 | 91.0k | for (; list != &hashtable->list; list = next) { |
165 | 86.4k | next = list->next; |
166 | 86.4k | pair = list_to_pair(list); |
167 | 86.4k | index = pair->hash % new_size; |
168 | 86.4k | insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list); |
169 | 86.4k | } |
170 | | |
171 | 4.59k | return 0; |
172 | 4.59k | } |
173 | | |
174 | 65.2k | int hashtable_init(hashtable_t *hashtable) { |
175 | 65.2k | size_t i; |
176 | | |
177 | 65.2k | hashtable->size = 0; |
178 | 65.2k | hashtable->order = INITIAL_HASHTABLE_ORDER; |
179 | 65.2k | hashtable->buckets = jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t)); |
180 | 65.2k | if (!hashtable->buckets) |
181 | 0 | return -1; |
182 | | |
183 | 65.2k | list_init(&hashtable->list); |
184 | 65.2k | list_init(&hashtable->ordered_list); |
185 | | |
186 | 587k | for (i = 0; i < hashsize(hashtable->order); i++) { |
187 | 522k | hashtable->buckets[i].first = hashtable->buckets[i].last = &hashtable->list; |
188 | 522k | } |
189 | | |
190 | 65.2k | return 0; |
191 | 65.2k | } |
192 | | |
193 | 65.2k | void hashtable_close(hashtable_t *hashtable) { |
194 | 65.2k | hashtable_do_clear(hashtable); |
195 | 65.2k | jsonp_free(hashtable->buckets); |
196 | 65.2k | } |
197 | | |
198 | 160k | static pair_t *init_pair(json_t *value, const char *key, size_t key_len, size_t hash) { |
199 | 160k | pair_t *pair; |
200 | | |
201 | | /* offsetof(...) returns the size of pair_t without the last, |
202 | | flexible member. This way, the correct amount is |
203 | | allocated. */ |
204 | | |
205 | 160k | if (key_len >= (size_t)-1 - offsetof(pair_t, key)) { |
206 | | /* Avoid an overflow if the key is very long */ |
207 | 0 | return NULL; |
208 | 0 | } |
209 | | |
210 | 160k | pair = jsonp_malloc(offsetof(pair_t, key) + key_len + 1); |
211 | | |
212 | 160k | if (!pair) |
213 | 0 | return NULL; |
214 | | |
215 | 160k | pair->hash = hash; |
216 | 160k | memcpy(pair->key, key, key_len); |
217 | 160k | pair->key[key_len] = '\0'; |
218 | 160k | pair->key_len = key_len; |
219 | 160k | pair->value = value; |
220 | | |
221 | 160k | list_init(&pair->list); |
222 | 160k | list_init(&pair->ordered_list); |
223 | | |
224 | 160k | return pair; |
225 | 160k | } |
226 | | |
227 | | int hashtable_set(hashtable_t *hashtable, const char *key, size_t key_len, |
228 | 161k | json_t *value) { |
229 | 161k | pair_t *pair; |
230 | 161k | bucket_t *bucket; |
231 | 161k | size_t hash, index; |
232 | | |
233 | | /* rehash if the load ratio exceeds 1 */ |
234 | 161k | if (hashtable->size >= hashsize(hashtable->order)) |
235 | 4.59k | if (hashtable_do_rehash(hashtable)) |
236 | 0 | return -1; |
237 | | |
238 | 161k | hash = hash_str(key, key_len); |
239 | 161k | index = hash & hashmask(hashtable->order); |
240 | 161k | bucket = &hashtable->buckets[index]; |
241 | 161k | pair = hashtable_find_pair(hashtable, bucket, key, key_len, hash); |
242 | | |
243 | 161k | if (pair) { |
244 | 1.16k | json_decref(pair->value); |
245 | 1.16k | pair->value = value; |
246 | 160k | } else { |
247 | 160k | pair = init_pair(value, key, key_len, hash); |
248 | | |
249 | 160k | if (!pair) |
250 | 0 | return -1; |
251 | | |
252 | 160k | insert_to_bucket(hashtable, bucket, &pair->list); |
253 | 160k | list_insert(&hashtable->ordered_list, &pair->ordered_list); |
254 | | |
255 | 160k | hashtable->size++; |
256 | 160k | } |
257 | 161k | return 0; |
258 | 161k | } |
259 | | |
260 | 479k | void *hashtable_get(hashtable_t *hashtable, const char *key, size_t key_len) { |
261 | 479k | pair_t *pair; |
262 | 479k | size_t hash; |
263 | 479k | bucket_t *bucket; |
264 | | |
265 | 479k | hash = hash_str(key, key_len); |
266 | 479k | bucket = &hashtable->buckets[hash & hashmask(hashtable->order)]; |
267 | | |
268 | 479k | pair = hashtable_find_pair(hashtable, bucket, key, key_len, hash); |
269 | 479k | if (!pair) |
270 | 473k | return NULL; |
271 | | |
272 | 6.17k | return pair->value; |
273 | 479k | } |
274 | | |
275 | 116k | int hashtable_del(hashtable_t *hashtable, const char *key, size_t key_len) { |
276 | 116k | size_t hash = hash_str(key, key_len); |
277 | 116k | return hashtable_do_del(hashtable, key, key_len, hash); |
278 | 116k | } |
279 | | |
280 | 0 | void hashtable_clear(hashtable_t *hashtable) { |
281 | 0 | size_t i; |
282 | |
|
283 | 0 | hashtable_do_clear(hashtable); |
284 | |
|
285 | 0 | for (i = 0; i < hashsize(hashtable->order); i++) { |
286 | 0 | hashtable->buckets[i].first = hashtable->buckets[i].last = &hashtable->list; |
287 | 0 | } |
288 | |
|
289 | 0 | list_init(&hashtable->list); |
290 | 0 | list_init(&hashtable->ordered_list); |
291 | 0 | hashtable->size = 0; |
292 | 0 | } |
293 | | |
294 | 18.1k | void *hashtable_iter(hashtable_t *hashtable) { |
295 | 18.1k | return hashtable_iter_next(hashtable, &hashtable->ordered_list); |
296 | 18.1k | } |
297 | | |
298 | 0 | void *hashtable_iter_at(hashtable_t *hashtable, const char *key, size_t key_len) { |
299 | 0 | pair_t *pair; |
300 | 0 | size_t hash; |
301 | 0 | bucket_t *bucket; |
302 | |
|
303 | 0 | hash = hash_str(key, key_len); |
304 | 0 | bucket = &hashtable->buckets[hash & hashmask(hashtable->order)]; |
305 | |
|
306 | 0 | pair = hashtable_find_pair(hashtable, bucket, key, key_len, hash); |
307 | 0 | if (!pair) |
308 | 0 | return NULL; |
309 | | |
310 | 0 | return &pair->ordered_list; |
311 | 0 | } |
312 | | |
313 | 36.8k | void *hashtable_iter_next(hashtable_t *hashtable, void *iter) { |
314 | 36.8k | list_t *list = (list_t *)iter; |
315 | 36.8k | if (list->next == &hashtable->ordered_list) |
316 | 18.1k | return NULL; |
317 | 18.6k | return list->next; |
318 | 36.8k | } |
319 | | |
320 | 18.6k | void *hashtable_iter_key(void *iter) { |
321 | 18.6k | pair_t *pair = ordered_list_to_pair((list_t *)iter); |
322 | 18.6k | return pair->key; |
323 | 18.6k | } |
324 | | |
325 | 18.6k | size_t hashtable_iter_key_len(void *iter) { |
326 | 18.6k | pair_t *pair = ordered_list_to_pair((list_t *)iter); |
327 | 18.6k | return pair->key_len; |
328 | 18.6k | } |
329 | | |
330 | 18.6k | void *hashtable_iter_value(void *iter) { |
331 | 18.6k | pair_t *pair = ordered_list_to_pair((list_t *)iter); |
332 | 18.6k | return pair->value; |
333 | 18.6k | } |
334 | | |
335 | 0 | void hashtable_iter_set(void *iter, json_t *value) { |
336 | 0 | pair_t *pair = ordered_list_to_pair((list_t *)iter); |
337 | |
|
338 | 0 | json_decref(pair->value); |
339 | 0 | pair->value = value; |
340 | 0 | } |