rgss 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.clang-format +6 -0
  3. data/.gitignore +167 -0
  4. data/.yardopts +6 -0
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/Rakefile +9 -0
  9. data/ext/rgss/cglm-v0.7.9.tar.gz +0 -0
  10. data/ext/rgss/color.c +599 -0
  11. data/ext/rgss/entity.c +373 -0
  12. data/ext/rgss/extconf.rb +53 -0
  13. data/ext/rgss/font.c +135 -0
  14. data/ext/rgss/game.c +469 -0
  15. data/ext/rgss/game.h +99 -0
  16. data/ext/rgss/gl.c +3217 -0
  17. data/ext/rgss/glad.c +1140 -0
  18. data/ext/rgss/glad.h +2129 -0
  19. data/ext/rgss/glfw.c +1453 -0
  20. data/ext/rgss/graphics.c +324 -0
  21. data/ext/rgss/image.c +274 -0
  22. data/ext/rgss/input.c +745 -0
  23. data/ext/rgss/khrplatform.h +290 -0
  24. data/ext/rgss/mat4.c +279 -0
  25. data/ext/rgss/pax_global_header +1 -0
  26. data/ext/rgss/point.c +253 -0
  27. data/ext/rgss/rect.c +449 -0
  28. data/ext/rgss/rgss.c +56 -0
  29. data/ext/rgss/rgss.h +241 -0
  30. data/ext/rgss/stb_image.h +7762 -0
  31. data/ext/rgss/stb_image_write.h +1690 -0
  32. data/ext/rgss/stb_rect_pack.h +628 -0
  33. data/ext/rgss/stb_truetype.h +5011 -0
  34. data/ext/rgss/utf8.h +1652 -0
  35. data/ext/rgss/uthash.h +1133 -0
  36. data/ext/rgss/vec.c +114 -0
  37. data/ext/rgss/vec.h +192 -0
  38. data/ext/rgss/vec2.c +489 -0
  39. data/ext/rgss/vec3.c +751 -0
  40. data/ext/rgss/vec4.c +681 -0
  41. data/lib/rgss.rb +140 -0
  42. data/lib/rgss/batch.rb +57 -0
  43. data/lib/rgss/blend.rb +47 -0
  44. data/lib/rgss/game_object.rb +28 -0
  45. data/lib/rgss/plane.rb +95 -0
  46. data/lib/rgss/renderable.rb +158 -0
  47. data/lib/rgss/rgss.so +0 -0
  48. data/lib/rgss/shader.rb +94 -0
  49. data/lib/rgss/shaders/sprite-frag.glsl +40 -0
  50. data/lib/rgss/shaders/sprite-vert.glsl +17 -0
  51. data/lib/rgss/sprite.rb +139 -0
  52. data/lib/rgss/stubs/color.rb +318 -0
  53. data/lib/rgss/stubs/gl.rb +1999 -0
  54. data/lib/rgss/stubs/glfw.rb +626 -0
  55. data/lib/rgss/stubs/rect.rb +324 -0
  56. data/lib/rgss/stubs/rpg.rb +267 -0
  57. data/lib/rgss/stubs/tone.rb +65 -0
  58. data/lib/rgss/texture.rb +132 -0
  59. data/lib/rgss/tilemap.rb +116 -0
  60. data/lib/rgss/version.rb +3 -0
  61. data/lib/rgss/viewport.rb +67 -0
  62. data/rgss.gemspec +44 -0
  63. data/test.png +0 -0
  64. metadata +178 -0
@@ -0,0 +1,1133 @@
1
+ /*
2
+ Copyright (c) 2003-2020, Troy D. Hanson http://troydhanson.github.com/uthash/
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
13
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
15
+ OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
+ */
23
+
24
+ #ifndef UTHASH_H
25
+ #define UTHASH_H
26
+
27
+ #define UTHASH_VERSION 2.2.0
28
+
29
+ #include <string.h> /* memcmp, memset, strlen */
30
+ #include <stddef.h> /* ptrdiff_t */
31
+ #include <stdlib.h> /* exit */
32
+
33
+ #if defined(HASH_DEFINE_OWN_STDINT) && HASH_DEFINE_OWN_STDINT
34
+ /* This codepath is provided for backward compatibility, but I plan to remove it. */
35
+ #warning "HASH_DEFINE_OWN_STDINT is deprecated; please use HASH_NO_STDINT instead"
36
+ typedef unsigned int uint32_t;
37
+ typedef unsigned char uint8_t;
38
+ #elif defined(HASH_NO_STDINT) && HASH_NO_STDINT
39
+ #else
40
+ #include <stdint.h> /* uint8_t, uint32_t */
41
+ #endif
42
+
43
+ /* These macros use decltype or the earlier __typeof GNU extension.
44
+ As decltype is only available in newer compilers (VS2010 or gcc 4.3+
45
+ when compiling c++ source) this code uses whatever method is needed
46
+ or, for VS2008 where neither is available, uses casting workarounds. */
47
+ #if !defined(DECLTYPE) && !defined(NO_DECLTYPE)
48
+ #if defined(_MSC_VER) /* MS compiler */
49
+ #if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */
50
+ #define DECLTYPE(x) (decltype(x))
51
+ #else /* VS2008 or older (or VS2010 in C mode) */
52
+ #define NO_DECLTYPE
53
+ #endif
54
+ #elif defined(__BORLANDC__) || defined(__ICCARM__) || defined(__LCC__) || defined(__WATCOMC__)
55
+ #define NO_DECLTYPE
56
+ #else /* GNU, Sun and other compilers */
57
+ #define DECLTYPE(x) (__typeof(x))
58
+ #endif
59
+ #endif
60
+
61
+ #ifdef NO_DECLTYPE
62
+ #define DECLTYPE(x)
63
+ #define DECLTYPE_ASSIGN(dst,src) \
64
+ do { \
65
+ char **_da_dst = (char**)(&(dst)); \
66
+ *_da_dst = (char*)(src); \
67
+ } while (0)
68
+ #else
69
+ #define DECLTYPE_ASSIGN(dst,src) \
70
+ do { \
71
+ (dst) = DECLTYPE(dst)(src); \
72
+ } while (0)
73
+ #endif
74
+
75
+ #ifndef uthash_malloc
76
+ #define uthash_malloc(sz) malloc(sz) /* malloc fcn */
77
+ #endif
78
+ #ifndef uthash_free
79
+ #define uthash_free(ptr,sz) free(ptr) /* free fcn */
80
+ #endif
81
+ #ifndef uthash_bzero
82
+ #define uthash_bzero(a,n) memset(a,'\0',n)
83
+ #endif
84
+ #ifndef uthash_strlen
85
+ #define uthash_strlen(s) strlen(s)
86
+ #endif
87
+
88
+ #ifndef HASH_FUNCTION
89
+ #define HASH_FUNCTION(keyptr,keylen,hashv) HASH_JEN(keyptr, keylen, hashv)
90
+ #endif
91
+
92
+ #ifndef HASH_KEYCMP
93
+ #define HASH_KEYCMP(a,b,n) memcmp(a,b,n)
94
+ #endif
95
+
96
+ #ifndef uthash_noexpand_fyi
97
+ #define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */
98
+ #endif
99
+ #ifndef uthash_expand_fyi
100
+ #define uthash_expand_fyi(tbl) /* can be defined to log expands */
101
+ #endif
102
+
103
+ #ifndef HASH_NONFATAL_OOM
104
+ #define HASH_NONFATAL_OOM 0
105
+ #endif
106
+
107
+ #if HASH_NONFATAL_OOM
108
+ /* malloc failures can be recovered from */
109
+
110
+ #ifndef uthash_nonfatal_oom
111
+ #define uthash_nonfatal_oom(obj) do {} while (0) /* non-fatal OOM error */
112
+ #endif
113
+
114
+ #define HASH_RECORD_OOM(oomed) do { (oomed) = 1; } while (0)
115
+ #define IF_HASH_NONFATAL_OOM(x) x
116
+
117
+ #else
118
+ /* malloc failures result in lost memory, hash tables are unusable */
119
+
120
+ #ifndef uthash_fatal
121
+ #define uthash_fatal(msg) exit(-1) /* fatal OOM error */
122
+ #endif
123
+
124
+ #define HASH_RECORD_OOM(oomed) uthash_fatal("out of memory")
125
+ #define IF_HASH_NONFATAL_OOM(x)
126
+
127
+ #endif
128
+
129
+ /* initial number of buckets */
130
+ #define HASH_INITIAL_NUM_BUCKETS 32U /* initial number of buckets */
131
+ #define HASH_INITIAL_NUM_BUCKETS_LOG2 5U /* lg2 of initial number of buckets */
132
+ #define HASH_BKT_CAPACITY_THRESH 10U /* expand when bucket count reaches */
133
+
134
+ /* calculate the element whose hash handle address is hhp */
135
+ #define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho)))
136
+ /* calculate the hash handle from element address elp */
137
+ #define HH_FROM_ELMT(tbl,elp) ((UT_hash_handle*)(void*)(((char*)(elp)) + ((tbl)->hho)))
138
+
139
+ #define HASH_ROLLBACK_BKT(hh, head, itemptrhh) \
140
+ do { \
141
+ struct UT_hash_handle *_hd_hh_item = (itemptrhh); \
142
+ unsigned _hd_bkt; \
143
+ HASH_TO_BKT(_hd_hh_item->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \
144
+ (head)->hh.tbl->buckets[_hd_bkt].count++; \
145
+ _hd_hh_item->hh_next = NULL; \
146
+ _hd_hh_item->hh_prev = NULL; \
147
+ } while (0)
148
+
149
+ #define HASH_VALUE(keyptr,keylen,hashv) \
150
+ do { \
151
+ HASH_FUNCTION(keyptr, keylen, hashv); \
152
+ } while (0)
153
+
154
+ #define HASH_FIND_BYHASHVALUE(hh,head,keyptr,keylen,hashval,out) \
155
+ do { \
156
+ (out) = NULL; \
157
+ if (head) { \
158
+ unsigned _hf_bkt; \
159
+ HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _hf_bkt); \
160
+ if (HASH_BLOOM_TEST((head)->hh.tbl, hashval) != 0) { \
161
+ HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], keyptr, keylen, hashval, out); \
162
+ } \
163
+ } \
164
+ } while (0)
165
+
166
+ #define HASH_FIND(hh,head,keyptr,keylen,out) \
167
+ do { \
168
+ (out) = NULL; \
169
+ if (head) { \
170
+ unsigned _hf_hashv; \
171
+ HASH_VALUE(keyptr, keylen, _hf_hashv); \
172
+ HASH_FIND_BYHASHVALUE(hh, head, keyptr, keylen, _hf_hashv, out); \
173
+ } \
174
+ } while (0)
175
+
176
+ #ifdef HASH_BLOOM
177
+ #define HASH_BLOOM_BITLEN (1UL << HASH_BLOOM)
178
+ #define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8UL) + (((HASH_BLOOM_BITLEN%8UL)!=0UL) ? 1UL : 0UL)
179
+ #define HASH_BLOOM_MAKE(tbl,oomed) \
180
+ do { \
181
+ (tbl)->bloom_nbits = HASH_BLOOM; \
182
+ (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \
183
+ if (!(tbl)->bloom_bv) { \
184
+ HASH_RECORD_OOM(oomed); \
185
+ } else { \
186
+ uthash_bzero((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \
187
+ (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \
188
+ } \
189
+ } while (0)
190
+
191
+ #define HASH_BLOOM_FREE(tbl) \
192
+ do { \
193
+ uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \
194
+ } while (0)
195
+
196
+ #define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8U] |= (1U << ((idx)%8U)))
197
+ #define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8U] & (1U << ((idx)%8U)))
198
+
199
+ #define HASH_BLOOM_ADD(tbl,hashv) \
200
+ HASH_BLOOM_BITSET((tbl)->bloom_bv, ((hashv) & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U)))
201
+
202
+ #define HASH_BLOOM_TEST(tbl,hashv) \
203
+ HASH_BLOOM_BITTEST((tbl)->bloom_bv, ((hashv) & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U)))
204
+
205
+ #else
206
+ #define HASH_BLOOM_MAKE(tbl,oomed)
207
+ #define HASH_BLOOM_FREE(tbl)
208
+ #define HASH_BLOOM_ADD(tbl,hashv)
209
+ #define HASH_BLOOM_TEST(tbl,hashv) (1)
210
+ #define HASH_BLOOM_BYTELEN 0U
211
+ #endif
212
+
213
+ #define HASH_MAKE_TABLE(hh,head,oomed) \
214
+ do { \
215
+ (head)->hh.tbl = (UT_hash_table*)uthash_malloc(sizeof(UT_hash_table)); \
216
+ if (!(head)->hh.tbl) { \
217
+ HASH_RECORD_OOM(oomed); \
218
+ } else { \
219
+ uthash_bzero((head)->hh.tbl, sizeof(UT_hash_table)); \
220
+ (head)->hh.tbl->tail = &((head)->hh); \
221
+ (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
222
+ (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \
223
+ (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \
224
+ (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \
225
+ HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \
226
+ (head)->hh.tbl->signature = HASH_SIGNATURE; \
227
+ if (!(head)->hh.tbl->buckets) { \
228
+ HASH_RECORD_OOM(oomed); \
229
+ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
230
+ } else { \
231
+ uthash_bzero((head)->hh.tbl->buckets, \
232
+ HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \
233
+ HASH_BLOOM_MAKE((head)->hh.tbl, oomed); \
234
+ IF_HASH_NONFATAL_OOM( \
235
+ if (oomed) { \
236
+ uthash_free((head)->hh.tbl->buckets, \
237
+ HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
238
+ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
239
+ } \
240
+ ) \
241
+ } \
242
+ } \
243
+ } while (0)
244
+
245
+ #define HASH_REPLACE_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,replaced,cmpfcn) \
246
+ do { \
247
+ (replaced) = NULL; \
248
+ HASH_FIND_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, replaced); \
249
+ if (replaced) { \
250
+ HASH_DELETE(hh, head, replaced); \
251
+ } \
252
+ HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, &((add)->fieldname), keylen_in, hashval, add, cmpfcn); \
253
+ } while (0)
254
+
255
+ #define HASH_REPLACE_BYHASHVALUE(hh,head,fieldname,keylen_in,hashval,add,replaced) \
256
+ do { \
257
+ (replaced) = NULL; \
258
+ HASH_FIND_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, replaced); \
259
+ if (replaced) { \
260
+ HASH_DELETE(hh, head, replaced); \
261
+ } \
262
+ HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, add); \
263
+ } while (0)
264
+
265
+ #define HASH_REPLACE(hh,head,fieldname,keylen_in,add,replaced) \
266
+ do { \
267
+ unsigned _hr_hashv; \
268
+ HASH_VALUE(&((add)->fieldname), keylen_in, _hr_hashv); \
269
+ HASH_REPLACE_BYHASHVALUE(hh, head, fieldname, keylen_in, _hr_hashv, add, replaced); \
270
+ } while (0)
271
+
272
+ #define HASH_REPLACE_INORDER(hh,head,fieldname,keylen_in,add,replaced,cmpfcn) \
273
+ do { \
274
+ unsigned _hr_hashv; \
275
+ HASH_VALUE(&((add)->fieldname), keylen_in, _hr_hashv); \
276
+ HASH_REPLACE_BYHASHVALUE_INORDER(hh, head, fieldname, keylen_in, _hr_hashv, add, replaced, cmpfcn); \
277
+ } while (0)
278
+
279
+ #define HASH_APPEND_LIST(hh, head, add) \
280
+ do { \
281
+ (add)->hh.next = NULL; \
282
+ (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \
283
+ (head)->hh.tbl->tail->next = (add); \
284
+ (head)->hh.tbl->tail = &((add)->hh); \
285
+ } while (0)
286
+
287
+ #define HASH_AKBI_INNER_LOOP(hh,head,add,cmpfcn) \
288
+ do { \
289
+ do { \
290
+ if (cmpfcn(DECLTYPE(head)(_hs_iter), add) > 0) { \
291
+ break; \
292
+ } \
293
+ } while ((_hs_iter = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->next)); \
294
+ } while (0)
295
+
296
+ #ifdef NO_DECLTYPE
297
+ #undef HASH_AKBI_INNER_LOOP
298
+ #define HASH_AKBI_INNER_LOOP(hh,head,add,cmpfcn) \
299
+ do { \
300
+ char *_hs_saved_head = (char*)(head); \
301
+ do { \
302
+ DECLTYPE_ASSIGN(head, _hs_iter); \
303
+ if (cmpfcn(head, add) > 0) { \
304
+ DECLTYPE_ASSIGN(head, _hs_saved_head); \
305
+ break; \
306
+ } \
307
+ DECLTYPE_ASSIGN(head, _hs_saved_head); \
308
+ } while ((_hs_iter = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->next)); \
309
+ } while (0)
310
+ #endif
311
+
312
+ #if HASH_NONFATAL_OOM
313
+
314
+ #define HASH_ADD_TO_TABLE(hh,head,keyptr,keylen_in,hashval,add,oomed) \
315
+ do { \
316
+ if (!(oomed)) { \
317
+ unsigned _ha_bkt; \
318
+ (head)->hh.tbl->num_items++; \
319
+ HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \
320
+ HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], hh, &(add)->hh, oomed); \
321
+ if (oomed) { \
322
+ HASH_ROLLBACK_BKT(hh, head, &(add)->hh); \
323
+ HASH_DELETE_HH(hh, head, &(add)->hh); \
324
+ (add)->hh.tbl = NULL; \
325
+ uthash_nonfatal_oom(add); \
326
+ } else { \
327
+ HASH_BLOOM_ADD((head)->hh.tbl, hashval); \
328
+ HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \
329
+ } \
330
+ } else { \
331
+ (add)->hh.tbl = NULL; \
332
+ uthash_nonfatal_oom(add); \
333
+ } \
334
+ } while (0)
335
+
336
+ #else
337
+
338
+ #define HASH_ADD_TO_TABLE(hh,head,keyptr,keylen_in,hashval,add,oomed) \
339
+ do { \
340
+ unsigned _ha_bkt; \
341
+ (head)->hh.tbl->num_items++; \
342
+ HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \
343
+ HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], hh, &(add)->hh, oomed); \
344
+ HASH_BLOOM_ADD((head)->hh.tbl, hashval); \
345
+ HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \
346
+ } while (0)
347
+
348
+ #endif
349
+
350
+
351
+ #define HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh,head,keyptr,keylen_in,hashval,add,cmpfcn) \
352
+ do { \
353
+ IF_HASH_NONFATAL_OOM( int _ha_oomed = 0; ) \
354
+ (add)->hh.hashv = (hashval); \
355
+ (add)->hh.key = (char*) (keyptr); \
356
+ (add)->hh.keylen = (unsigned) (keylen_in); \
357
+ if (!(head)) { \
358
+ (add)->hh.next = NULL; \
359
+ (add)->hh.prev = NULL; \
360
+ HASH_MAKE_TABLE(hh, add, _ha_oomed); \
361
+ IF_HASH_NONFATAL_OOM( if (!_ha_oomed) { ) \
362
+ (head) = (add); \
363
+ IF_HASH_NONFATAL_OOM( } ) \
364
+ } else { \
365
+ void *_hs_iter = (head); \
366
+ (add)->hh.tbl = (head)->hh.tbl; \
367
+ HASH_AKBI_INNER_LOOP(hh, head, add, cmpfcn); \
368
+ if (_hs_iter) { \
369
+ (add)->hh.next = _hs_iter; \
370
+ if (((add)->hh.prev = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->prev)) { \
371
+ HH_FROM_ELMT((head)->hh.tbl, (add)->hh.prev)->next = (add); \
372
+ } else { \
373
+ (head) = (add); \
374
+ } \
375
+ HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->prev = (add); \
376
+ } else { \
377
+ HASH_APPEND_LIST(hh, head, add); \
378
+ } \
379
+ } \
380
+ HASH_ADD_TO_TABLE(hh, head, keyptr, keylen_in, hashval, add, _ha_oomed); \
381
+ HASH_FSCK(hh, head, "HASH_ADD_KEYPTR_BYHASHVALUE_INORDER"); \
382
+ } while (0)
383
+
384
+ #define HASH_ADD_KEYPTR_INORDER(hh,head,keyptr,keylen_in,add,cmpfcn) \
385
+ do { \
386
+ unsigned _hs_hashv; \
387
+ HASH_VALUE(keyptr, keylen_in, _hs_hashv); \
388
+ HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, keyptr, keylen_in, _hs_hashv, add, cmpfcn); \
389
+ } while (0)
390
+
391
+ #define HASH_ADD_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,cmpfcn) \
392
+ HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, &((add)->fieldname), keylen_in, hashval, add, cmpfcn)
393
+
394
+ #define HASH_ADD_INORDER(hh,head,fieldname,keylen_in,add,cmpfcn) \
395
+ HASH_ADD_KEYPTR_INORDER(hh, head, &((add)->fieldname), keylen_in, add, cmpfcn)
396
+
397
+ #define HASH_ADD_KEYPTR_BYHASHVALUE(hh,head,keyptr,keylen_in,hashval,add) \
398
+ do { \
399
+ IF_HASH_NONFATAL_OOM( int _ha_oomed = 0; ) \
400
+ (add)->hh.hashv = (hashval); \
401
+ (add)->hh.key = (const void*) (keyptr); \
402
+ (add)->hh.keylen = (unsigned) (keylen_in); \
403
+ if (!(head)) { \
404
+ (add)->hh.next = NULL; \
405
+ (add)->hh.prev = NULL; \
406
+ HASH_MAKE_TABLE(hh, add, _ha_oomed); \
407
+ IF_HASH_NONFATAL_OOM( if (!_ha_oomed) { ) \
408
+ (head) = (add); \
409
+ IF_HASH_NONFATAL_OOM( } ) \
410
+ } else { \
411
+ (add)->hh.tbl = (head)->hh.tbl; \
412
+ HASH_APPEND_LIST(hh, head, add); \
413
+ } \
414
+ HASH_ADD_TO_TABLE(hh, head, keyptr, keylen_in, hashval, add, _ha_oomed); \
415
+ HASH_FSCK(hh, head, "HASH_ADD_KEYPTR_BYHASHVALUE"); \
416
+ } while (0)
417
+
418
+ #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \
419
+ do { \
420
+ unsigned _ha_hashv; \
421
+ HASH_VALUE(keyptr, keylen_in, _ha_hashv); \
422
+ HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, keyptr, keylen_in, _ha_hashv, add); \
423
+ } while (0)
424
+
425
+ #define HASH_ADD_BYHASHVALUE(hh,head,fieldname,keylen_in,hashval,add) \
426
+ HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, add)
427
+
428
+ #define HASH_ADD(hh,head,fieldname,keylen_in,add) \
429
+ HASH_ADD_KEYPTR(hh, head, &((add)->fieldname), keylen_in, add)
430
+
431
+ #define HASH_TO_BKT(hashv,num_bkts,bkt) \
432
+ do { \
433
+ bkt = ((hashv) & ((num_bkts) - 1U)); \
434
+ } while (0)
435
+
436
+ /* delete "delptr" from the hash table.
437
+ * "the usual" patch-up process for the app-order doubly-linked-list.
438
+ * The use of _hd_hh_del below deserves special explanation.
439
+ * These used to be expressed using (delptr) but that led to a bug
440
+ * if someone used the same symbol for the head and deletee, like
441
+ * HASH_DELETE(hh,users,users);
442
+ * We want that to work, but by changing the head (users) below
443
+ * we were forfeiting our ability to further refer to the deletee (users)
444
+ * in the patch-up process. Solution: use scratch space to
445
+ * copy the deletee pointer, then the latter references are via that
446
+ * scratch pointer rather than through the repointed (users) symbol.
447
+ */
448
+ #define HASH_DELETE(hh,head,delptr) \
449
+ HASH_DELETE_HH(hh, head, &(delptr)->hh)
450
+
451
+ #define HASH_DELETE_HH(hh,head,delptrhh) \
452
+ do { \
453
+ struct UT_hash_handle *_hd_hh_del = (delptrhh); \
454
+ if ((_hd_hh_del->prev == NULL) && (_hd_hh_del->next == NULL)) { \
455
+ HASH_BLOOM_FREE((head)->hh.tbl); \
456
+ uthash_free((head)->hh.tbl->buckets, \
457
+ (head)->hh.tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
458
+ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
459
+ (head) = NULL; \
460
+ } else { \
461
+ unsigned _hd_bkt; \
462
+ if (_hd_hh_del == (head)->hh.tbl->tail) { \
463
+ (head)->hh.tbl->tail = HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->prev); \
464
+ } \
465
+ if (_hd_hh_del->prev != NULL) { \
466
+ HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->prev)->next = _hd_hh_del->next; \
467
+ } else { \
468
+ DECLTYPE_ASSIGN(head, _hd_hh_del->next); \
469
+ } \
470
+ if (_hd_hh_del->next != NULL) { \
471
+ HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->next)->prev = _hd_hh_del->prev; \
472
+ } \
473
+ HASH_TO_BKT(_hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \
474
+ HASH_DEL_IN_BKT((head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \
475
+ (head)->hh.tbl->num_items--; \
476
+ } \
477
+ HASH_FSCK(hh, head, "HASH_DELETE_HH"); \
478
+ } while (0)
479
+
480
+ /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */
481
+ #define HASH_FIND_STR(head,findstr,out) \
482
+ do { \
483
+ unsigned _uthash_hfstr_keylen = (unsigned)uthash_strlen(findstr); \
484
+ HASH_FIND(hh, head, findstr, _uthash_hfstr_keylen, out); \
485
+ } while (0)
486
+ #define HASH_ADD_STR(head,strfield,add) \
487
+ do { \
488
+ unsigned _uthash_hastr_keylen = (unsigned)uthash_strlen((add)->strfield); \
489
+ HASH_ADD(hh, head, strfield[0], _uthash_hastr_keylen, add); \
490
+ } while (0)
491
+ #define HASH_REPLACE_STR(head,strfield,add,replaced) \
492
+ do { \
493
+ unsigned _uthash_hrstr_keylen = (unsigned)uthash_strlen((add)->strfield); \
494
+ HASH_REPLACE(hh, head, strfield[0], _uthash_hrstr_keylen, add, replaced); \
495
+ } while (0)
496
+ #define HASH_FIND_INT(head,findint,out) \
497
+ HASH_FIND(hh,head,findint,sizeof(int),out)
498
+ #define HASH_ADD_INT(head,intfield,add) \
499
+ HASH_ADD(hh,head,intfield,sizeof(int),add)
500
+ #define HASH_REPLACE_INT(head,intfield,add,replaced) \
501
+ HASH_REPLACE(hh,head,intfield,sizeof(int),add,replaced)
502
+ #define HASH_FIND_PTR(head,findptr,out) \
503
+ HASH_FIND(hh,head,findptr,sizeof(void *),out)
504
+ #define HASH_ADD_PTR(head,ptrfield,add) \
505
+ HASH_ADD(hh,head,ptrfield,sizeof(void *),add)
506
+ #define HASH_REPLACE_PTR(head,ptrfield,add,replaced) \
507
+ HASH_REPLACE(hh,head,ptrfield,sizeof(void *),add,replaced)
508
+ #define HASH_DEL(head,delptr) \
509
+ HASH_DELETE(hh,head,delptr)
510
+
511
+ /* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined.
512
+ * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined.
513
+ */
514
+ #ifdef HASH_DEBUG
515
+ #include <stdio.h> /* fprintf, stderr */
516
+ #define HASH_OOPS(...) do { fprintf(stderr, __VA_ARGS__); exit(-1); } while (0)
517
+ #define HASH_FSCK(hh,head,where) \
518
+ do { \
519
+ struct UT_hash_handle *_thh; \
520
+ if (head) { \
521
+ unsigned _bkt_i; \
522
+ unsigned _count = 0; \
523
+ char *_prev; \
524
+ for (_bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; ++_bkt_i) { \
525
+ unsigned _bkt_count = 0; \
526
+ _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \
527
+ _prev = NULL; \
528
+ while (_thh) { \
529
+ if (_prev != (char*)(_thh->hh_prev)) { \
530
+ HASH_OOPS("%s: invalid hh_prev %p, actual %p\n", \
531
+ (where), (void*)_thh->hh_prev, (void*)_prev); \
532
+ } \
533
+ _bkt_count++; \
534
+ _prev = (char*)(_thh); \
535
+ _thh = _thh->hh_next; \
536
+ } \
537
+ _count += _bkt_count; \
538
+ if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \
539
+ HASH_OOPS("%s: invalid bucket count %u, actual %u\n", \
540
+ (where), (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \
541
+ } \
542
+ } \
543
+ if (_count != (head)->hh.tbl->num_items) { \
544
+ HASH_OOPS("%s: invalid hh item count %u, actual %u\n", \
545
+ (where), (head)->hh.tbl->num_items, _count); \
546
+ } \
547
+ _count = 0; \
548
+ _prev = NULL; \
549
+ _thh = &(head)->hh; \
550
+ while (_thh) { \
551
+ _count++; \
552
+ if (_prev != (char*)_thh->prev) { \
553
+ HASH_OOPS("%s: invalid prev %p, actual %p\n", \
554
+ (where), (void*)_thh->prev, (void*)_prev); \
555
+ } \
556
+ _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \
557
+ _thh = (_thh->next ? HH_FROM_ELMT((head)->hh.tbl, _thh->next) : NULL); \
558
+ } \
559
+ if (_count != (head)->hh.tbl->num_items) { \
560
+ HASH_OOPS("%s: invalid app item count %u, actual %u\n", \
561
+ (where), (head)->hh.tbl->num_items, _count); \
562
+ } \
563
+ } \
564
+ } while (0)
565
+ #else
566
+ #define HASH_FSCK(hh,head,where)
567
+ #endif
568
+
569
+ /* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to
570
+ * the descriptor to which this macro is defined for tuning the hash function.
571
+ * The app can #include <unistd.h> to get the prototype for write(2). */
572
+ #ifdef HASH_EMIT_KEYS
573
+ #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \
574
+ do { \
575
+ unsigned _klen = fieldlen; \
576
+ write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \
577
+ write(HASH_EMIT_KEYS, keyptr, (unsigned long)fieldlen); \
578
+ } while (0)
579
+ #else
580
+ #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen)
581
+ #endif
582
+
583
+ /* The Bernstein hash function, used in Perl prior to v5.6. Note (x<<5+x)=x*33. */
584
+ #define HASH_BER(key,keylen,hashv) \
585
+ do { \
586
+ unsigned _hb_keylen = (unsigned)keylen; \
587
+ const unsigned char *_hb_key = (const unsigned char*)(key); \
588
+ (hashv) = 0; \
589
+ while (_hb_keylen-- != 0U) { \
590
+ (hashv) = (((hashv) << 5) + (hashv)) + *_hb_key++; \
591
+ } \
592
+ } while (0)
593
+
594
+
595
+ /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at
596
+ * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */
597
+ #define HASH_SAX(key,keylen,hashv) \
598
+ do { \
599
+ unsigned _sx_i; \
600
+ const unsigned char *_hs_key = (const unsigned char*)(key); \
601
+ hashv = 0; \
602
+ for (_sx_i=0; _sx_i < keylen; _sx_i++) { \
603
+ hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \
604
+ } \
605
+ } while (0)
606
+ /* FNV-1a variation */
607
+ #define HASH_FNV(key,keylen,hashv) \
608
+ do { \
609
+ unsigned _fn_i; \
610
+ const unsigned char *_hf_key = (const unsigned char*)(key); \
611
+ (hashv) = 2166136261U; \
612
+ for (_fn_i=0; _fn_i < keylen; _fn_i++) { \
613
+ hashv = hashv ^ _hf_key[_fn_i]; \
614
+ hashv = hashv * 16777619U; \
615
+ } \
616
+ } while (0)
617
+
618
+ #define HASH_OAT(key,keylen,hashv) \
619
+ do { \
620
+ unsigned _ho_i; \
621
+ const unsigned char *_ho_key=(const unsigned char*)(key); \
622
+ hashv = 0; \
623
+ for(_ho_i=0; _ho_i < keylen; _ho_i++) { \
624
+ hashv += _ho_key[_ho_i]; \
625
+ hashv += (hashv << 10); \
626
+ hashv ^= (hashv >> 6); \
627
+ } \
628
+ hashv += (hashv << 3); \
629
+ hashv ^= (hashv >> 11); \
630
+ hashv += (hashv << 15); \
631
+ } while (0)
632
+
633
+ #define HASH_JEN_MIX(a,b,c) \
634
+ do { \
635
+ a -= b; a -= c; a ^= ( c >> 13 ); \
636
+ b -= c; b -= a; b ^= ( a << 8 ); \
637
+ c -= a; c -= b; c ^= ( b >> 13 ); \
638
+ a -= b; a -= c; a ^= ( c >> 12 ); \
639
+ b -= c; b -= a; b ^= ( a << 16 ); \
640
+ c -= a; c -= b; c ^= ( b >> 5 ); \
641
+ a -= b; a -= c; a ^= ( c >> 3 ); \
642
+ b -= c; b -= a; b ^= ( a << 10 ); \
643
+ c -= a; c -= b; c ^= ( b >> 15 ); \
644
+ } while (0)
645
+
646
+ #define HASH_JEN(key,keylen,hashv) \
647
+ do { \
648
+ unsigned _hj_i,_hj_j,_hj_k; \
649
+ unsigned const char *_hj_key=(unsigned const char*)(key); \
650
+ hashv = 0xfeedbeefu; \
651
+ _hj_i = _hj_j = 0x9e3779b9u; \
652
+ _hj_k = (unsigned)(keylen); \
653
+ while (_hj_k >= 12U) { \
654
+ _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \
655
+ + ( (unsigned)_hj_key[2] << 16 ) \
656
+ + ( (unsigned)_hj_key[3] << 24 ) ); \
657
+ _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \
658
+ + ( (unsigned)_hj_key[6] << 16 ) \
659
+ + ( (unsigned)_hj_key[7] << 24 ) ); \
660
+ hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \
661
+ + ( (unsigned)_hj_key[10] << 16 ) \
662
+ + ( (unsigned)_hj_key[11] << 24 ) ); \
663
+ \
664
+ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
665
+ \
666
+ _hj_key += 12; \
667
+ _hj_k -= 12U; \
668
+ } \
669
+ hashv += (unsigned)(keylen); \
670
+ switch ( _hj_k ) { \
671
+ case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); /* FALLTHROUGH */ \
672
+ case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); /* FALLTHROUGH */ \
673
+ case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); /* FALLTHROUGH */ \
674
+ case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); /* FALLTHROUGH */ \
675
+ case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); /* FALLTHROUGH */ \
676
+ case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); /* FALLTHROUGH */ \
677
+ case 5: _hj_j += _hj_key[4]; /* FALLTHROUGH */ \
678
+ case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); /* FALLTHROUGH */ \
679
+ case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); /* FALLTHROUGH */ \
680
+ case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); /* FALLTHROUGH */ \
681
+ case 1: _hj_i += _hj_key[0]; \
682
+ } \
683
+ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
684
+ } while (0)
685
+
686
+ /* The Paul Hsieh hash function */
687
+ #undef get16bits
688
+ #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
689
+ || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
690
+ #define get16bits(d) (*((const uint16_t *) (d)))
691
+ #endif
692
+
693
+ #if !defined (get16bits)
694
+ #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \
695
+ +(uint32_t)(((const uint8_t *)(d))[0]) )
696
+ #endif
697
+ #define HASH_SFH(key,keylen,hashv) \
698
+ do { \
699
+ unsigned const char *_sfh_key=(unsigned const char*)(key); \
700
+ uint32_t _sfh_tmp, _sfh_len = (uint32_t)keylen; \
701
+ \
702
+ unsigned _sfh_rem = _sfh_len & 3U; \
703
+ _sfh_len >>= 2; \
704
+ hashv = 0xcafebabeu; \
705
+ \
706
+ /* Main loop */ \
707
+ for (;_sfh_len > 0U; _sfh_len--) { \
708
+ hashv += get16bits (_sfh_key); \
709
+ _sfh_tmp = ((uint32_t)(get16bits (_sfh_key+2)) << 11) ^ hashv; \
710
+ hashv = (hashv << 16) ^ _sfh_tmp; \
711
+ _sfh_key += 2U*sizeof (uint16_t); \
712
+ hashv += hashv >> 11; \
713
+ } \
714
+ \
715
+ /* Handle end cases */ \
716
+ switch (_sfh_rem) { \
717
+ case 3: hashv += get16bits (_sfh_key); \
718
+ hashv ^= hashv << 16; \
719
+ hashv ^= (uint32_t)(_sfh_key[sizeof (uint16_t)]) << 18; \
720
+ hashv += hashv >> 11; \
721
+ break; \
722
+ case 2: hashv += get16bits (_sfh_key); \
723
+ hashv ^= hashv << 11; \
724
+ hashv += hashv >> 17; \
725
+ break; \
726
+ case 1: hashv += *_sfh_key; \
727
+ hashv ^= hashv << 10; \
728
+ hashv += hashv >> 1; \
729
+ } \
730
+ \
731
+ /* Force "avalanching" of final 127 bits */ \
732
+ hashv ^= hashv << 3; \
733
+ hashv += hashv >> 5; \
734
+ hashv ^= hashv << 4; \
735
+ hashv += hashv >> 17; \
736
+ hashv ^= hashv << 25; \
737
+ hashv += hashv >> 6; \
738
+ } while (0)
739
+
740
+ /* iterate over items in a known bucket to find desired item */
741
+ #define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,hashval,out) \
742
+ do { \
743
+ if ((head).hh_head != NULL) { \
744
+ DECLTYPE_ASSIGN(out, ELMT_FROM_HH(tbl, (head).hh_head)); \
745
+ } else { \
746
+ (out) = NULL; \
747
+ } \
748
+ while ((out) != NULL) { \
749
+ if ((out)->hh.hashv == (hashval) && (out)->hh.keylen == (keylen_in)) { \
750
+ if (HASH_KEYCMP((out)->hh.key, keyptr, keylen_in) == 0) { \
751
+ break; \
752
+ } \
753
+ } \
754
+ if ((out)->hh.hh_next != NULL) { \
755
+ DECLTYPE_ASSIGN(out, ELMT_FROM_HH(tbl, (out)->hh.hh_next)); \
756
+ } else { \
757
+ (out) = NULL; \
758
+ } \
759
+ } \
760
+ } while (0)
761
+
762
+ /* add an item to a bucket */
763
+ #define HASH_ADD_TO_BKT(head,hh,addhh,oomed) \
764
+ do { \
765
+ UT_hash_bucket *_ha_head = &(head); \
766
+ _ha_head->count++; \
767
+ (addhh)->hh_next = _ha_head->hh_head; \
768
+ (addhh)->hh_prev = NULL; \
769
+ if (_ha_head->hh_head != NULL) { \
770
+ _ha_head->hh_head->hh_prev = (addhh); \
771
+ } \
772
+ _ha_head->hh_head = (addhh); \
773
+ if ((_ha_head->count >= ((_ha_head->expand_mult + 1U) * HASH_BKT_CAPACITY_THRESH)) \
774
+ && !(addhh)->tbl->noexpand) { \
775
+ HASH_EXPAND_BUCKETS(addhh,(addhh)->tbl, oomed); \
776
+ IF_HASH_NONFATAL_OOM( \
777
+ if (oomed) { \
778
+ HASH_DEL_IN_BKT(head,addhh); \
779
+ } \
780
+ ) \
781
+ } \
782
+ } while (0)
783
+
784
+ /* remove an item from a given bucket */
785
+ #define HASH_DEL_IN_BKT(head,delhh) \
786
+ do { \
787
+ UT_hash_bucket *_hd_head = &(head); \
788
+ _hd_head->count--; \
789
+ if (_hd_head->hh_head == (delhh)) { \
790
+ _hd_head->hh_head = (delhh)->hh_next; \
791
+ } \
792
+ if ((delhh)->hh_prev) { \
793
+ (delhh)->hh_prev->hh_next = (delhh)->hh_next; \
794
+ } \
795
+ if ((delhh)->hh_next) { \
796
+ (delhh)->hh_next->hh_prev = (delhh)->hh_prev; \
797
+ } \
798
+ } while (0)
799
+
800
+ /* Bucket expansion has the effect of doubling the number of buckets
801
+ * and redistributing the items into the new buckets. Ideally the
802
+ * items will distribute more or less evenly into the new buckets
803
+ * (the extent to which this is true is a measure of the quality of
804
+ * the hash function as it applies to the key domain).
805
+ *
806
+ * With the items distributed into more buckets, the chain length
807
+ * (item count) in each bucket is reduced. Thus by expanding buckets
808
+ * the hash keeps a bound on the chain length. This bounded chain
809
+ * length is the essence of how a hash provides constant time lookup.
810
+ *
811
+ * The calculation of tbl->ideal_chain_maxlen below deserves some
812
+ * explanation. First, keep in mind that we're calculating the ideal
813
+ * maximum chain length based on the *new* (doubled) bucket count.
814
+ * In fractions this is just n/b (n=number of items,b=new num buckets).
815
+ * Since the ideal chain length is an integer, we want to calculate
816
+ * ceil(n/b). We don't depend on floating point arithmetic in this
817
+ * hash, so to calculate ceil(n/b) with integers we could write
818
+ *
819
+ * ceil(n/b) = (n/b) + ((n%b)?1:0)
820
+ *
821
+ * and in fact a previous version of this hash did just that.
822
+ * But now we have improved things a bit by recognizing that b is
823
+ * always a power of two. We keep its base 2 log handy (call it lb),
824
+ * so now we can write this with a bit shift and logical AND:
825
+ *
826
+ * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0)
827
+ *
828
+ */
829
+ #define HASH_EXPAND_BUCKETS(hh,tbl,oomed) \
830
+ do { \
831
+ unsigned _he_bkt; \
832
+ unsigned _he_bkt_i; \
833
+ struct UT_hash_handle *_he_thh, *_he_hh_nxt; \
834
+ UT_hash_bucket *_he_new_buckets, *_he_newbkt; \
835
+ _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \
836
+ sizeof(struct UT_hash_bucket) * (tbl)->num_buckets * 2U); \
837
+ if (!_he_new_buckets) { \
838
+ HASH_RECORD_OOM(oomed); \
839
+ } else { \
840
+ uthash_bzero(_he_new_buckets, \
841
+ sizeof(struct UT_hash_bucket) * (tbl)->num_buckets * 2U); \
842
+ (tbl)->ideal_chain_maxlen = \
843
+ ((tbl)->num_items >> ((tbl)->log2_num_buckets+1U)) + \
844
+ ((((tbl)->num_items & (((tbl)->num_buckets*2U)-1U)) != 0U) ? 1U : 0U); \
845
+ (tbl)->nonideal_items = 0; \
846
+ for (_he_bkt_i = 0; _he_bkt_i < (tbl)->num_buckets; _he_bkt_i++) { \
847
+ _he_thh = (tbl)->buckets[ _he_bkt_i ].hh_head; \
848
+ while (_he_thh != NULL) { \
849
+ _he_hh_nxt = _he_thh->hh_next; \
850
+ HASH_TO_BKT(_he_thh->hashv, (tbl)->num_buckets * 2U, _he_bkt); \
851
+ _he_newbkt = &(_he_new_buckets[_he_bkt]); \
852
+ if (++(_he_newbkt->count) > (tbl)->ideal_chain_maxlen) { \
853
+ (tbl)->nonideal_items++; \
854
+ if (_he_newbkt->count > _he_newbkt->expand_mult * (tbl)->ideal_chain_maxlen) { \
855
+ _he_newbkt->expand_mult++; \
856
+ } \
857
+ } \
858
+ _he_thh->hh_prev = NULL; \
859
+ _he_thh->hh_next = _he_newbkt->hh_head; \
860
+ if (_he_newbkt->hh_head != NULL) { \
861
+ _he_newbkt->hh_head->hh_prev = _he_thh; \
862
+ } \
863
+ _he_newbkt->hh_head = _he_thh; \
864
+ _he_thh = _he_hh_nxt; \
865
+ } \
866
+ } \
867
+ uthash_free((tbl)->buckets, (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \
868
+ (tbl)->num_buckets *= 2U; \
869
+ (tbl)->log2_num_buckets++; \
870
+ (tbl)->buckets = _he_new_buckets; \
871
+ (tbl)->ineff_expands = ((tbl)->nonideal_items > ((tbl)->num_items >> 1)) ? \
872
+ ((tbl)->ineff_expands+1U) : 0U; \
873
+ if ((tbl)->ineff_expands > 1U) { \
874
+ (tbl)->noexpand = 1; \
875
+ uthash_noexpand_fyi(tbl); \
876
+ } \
877
+ uthash_expand_fyi(tbl); \
878
+ } \
879
+ } while (0)
880
+
881
+
882
+ /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */
883
+ /* Note that HASH_SORT assumes the hash handle name to be hh.
884
+ * HASH_SRT was added to allow the hash handle name to be passed in. */
885
+ #define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn)
886
+ #define HASH_SRT(hh,head,cmpfcn) \
887
+ do { \
888
+ unsigned _hs_i; \
889
+ unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \
890
+ struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \
891
+ if (head != NULL) { \
892
+ _hs_insize = 1; \
893
+ _hs_looping = 1; \
894
+ _hs_list = &((head)->hh); \
895
+ while (_hs_looping != 0U) { \
896
+ _hs_p = _hs_list; \
897
+ _hs_list = NULL; \
898
+ _hs_tail = NULL; \
899
+ _hs_nmerges = 0; \
900
+ while (_hs_p != NULL) { \
901
+ _hs_nmerges++; \
902
+ _hs_q = _hs_p; \
903
+ _hs_psize = 0; \
904
+ for (_hs_i = 0; _hs_i < _hs_insize; ++_hs_i) { \
905
+ _hs_psize++; \
906
+ _hs_q = ((_hs_q->next != NULL) ? \
907
+ HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \
908
+ if (_hs_q == NULL) { \
909
+ break; \
910
+ } \
911
+ } \
912
+ _hs_qsize = _hs_insize; \
913
+ while ((_hs_psize != 0U) || ((_hs_qsize != 0U) && (_hs_q != NULL))) { \
914
+ if (_hs_psize == 0U) { \
915
+ _hs_e = _hs_q; \
916
+ _hs_q = ((_hs_q->next != NULL) ? \
917
+ HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \
918
+ _hs_qsize--; \
919
+ } else if ((_hs_qsize == 0U) || (_hs_q == NULL)) { \
920
+ _hs_e = _hs_p; \
921
+ if (_hs_p != NULL) { \
922
+ _hs_p = ((_hs_p->next != NULL) ? \
923
+ HH_FROM_ELMT((head)->hh.tbl, _hs_p->next) : NULL); \
924
+ } \
925
+ _hs_psize--; \
926
+ } else if ((cmpfcn( \
927
+ DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl, _hs_p)), \
928
+ DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl, _hs_q)) \
929
+ )) <= 0) { \
930
+ _hs_e = _hs_p; \
931
+ if (_hs_p != NULL) { \
932
+ _hs_p = ((_hs_p->next != NULL) ? \
933
+ HH_FROM_ELMT((head)->hh.tbl, _hs_p->next) : NULL); \
934
+ } \
935
+ _hs_psize--; \
936
+ } else { \
937
+ _hs_e = _hs_q; \
938
+ _hs_q = ((_hs_q->next != NULL) ? \
939
+ HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \
940
+ _hs_qsize--; \
941
+ } \
942
+ if ( _hs_tail != NULL ) { \
943
+ _hs_tail->next = ((_hs_e != NULL) ? \
944
+ ELMT_FROM_HH((head)->hh.tbl, _hs_e) : NULL); \
945
+ } else { \
946
+ _hs_list = _hs_e; \
947
+ } \
948
+ if (_hs_e != NULL) { \
949
+ _hs_e->prev = ((_hs_tail != NULL) ? \
950
+ ELMT_FROM_HH((head)->hh.tbl, _hs_tail) : NULL); \
951
+ } \
952
+ _hs_tail = _hs_e; \
953
+ } \
954
+ _hs_p = _hs_q; \
955
+ } \
956
+ if (_hs_tail != NULL) { \
957
+ _hs_tail->next = NULL; \
958
+ } \
959
+ if (_hs_nmerges <= 1U) { \
960
+ _hs_looping = 0; \
961
+ (head)->hh.tbl->tail = _hs_tail; \
962
+ DECLTYPE_ASSIGN(head, ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \
963
+ } \
964
+ _hs_insize *= 2U; \
965
+ } \
966
+ HASH_FSCK(hh, head, "HASH_SRT"); \
967
+ } \
968
+ } while (0)
969
+
970
+ /* This function selects items from one hash into another hash.
971
+ * The end result is that the selected items have dual presence
972
+ * in both hashes. There is no copy of the items made; rather
973
+ * they are added into the new hash through a secondary hash
974
+ * hash handle that must be present in the structure. */
975
+ #define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \
976
+ do { \
977
+ unsigned _src_bkt, _dst_bkt; \
978
+ void *_last_elt = NULL, *_elt; \
979
+ UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \
980
+ ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \
981
+ if ((src) != NULL) { \
982
+ for (_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \
983
+ for (_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \
984
+ _src_hh != NULL; \
985
+ _src_hh = _src_hh->hh_next) { \
986
+ _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \
987
+ if (cond(_elt)) { \
988
+ IF_HASH_NONFATAL_OOM( int _hs_oomed = 0; ) \
989
+ _dst_hh = (UT_hash_handle*)(void*)(((char*)_elt) + _dst_hho); \
990
+ _dst_hh->key = _src_hh->key; \
991
+ _dst_hh->keylen = _src_hh->keylen; \
992
+ _dst_hh->hashv = _src_hh->hashv; \
993
+ _dst_hh->prev = _last_elt; \
994
+ _dst_hh->next = NULL; \
995
+ if (_last_elt_hh != NULL) { \
996
+ _last_elt_hh->next = _elt; \
997
+ } \
998
+ if ((dst) == NULL) { \
999
+ DECLTYPE_ASSIGN(dst, _elt); \
1000
+ HASH_MAKE_TABLE(hh_dst, dst, _hs_oomed); \
1001
+ IF_HASH_NONFATAL_OOM( \
1002
+ if (_hs_oomed) { \
1003
+ uthash_nonfatal_oom(_elt); \
1004
+ (dst) = NULL; \
1005
+ continue; \
1006
+ } \
1007
+ ) \
1008
+ } else { \
1009
+ _dst_hh->tbl = (dst)->hh_dst.tbl; \
1010
+ } \
1011
+ HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \
1012
+ HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt], hh_dst, _dst_hh, _hs_oomed); \
1013
+ (dst)->hh_dst.tbl->num_items++; \
1014
+ IF_HASH_NONFATAL_OOM( \
1015
+ if (_hs_oomed) { \
1016
+ HASH_ROLLBACK_BKT(hh_dst, dst, _dst_hh); \
1017
+ HASH_DELETE_HH(hh_dst, dst, _dst_hh); \
1018
+ _dst_hh->tbl = NULL; \
1019
+ uthash_nonfatal_oom(_elt); \
1020
+ continue; \
1021
+ } \
1022
+ ) \
1023
+ HASH_BLOOM_ADD(_dst_hh->tbl, _dst_hh->hashv); \
1024
+ _last_elt = _elt; \
1025
+ _last_elt_hh = _dst_hh; \
1026
+ } \
1027
+ } \
1028
+ } \
1029
+ } \
1030
+ HASH_FSCK(hh_dst, dst, "HASH_SELECT"); \
1031
+ } while (0)
1032
+
1033
+ #define HASH_CLEAR(hh,head) \
1034
+ do { \
1035
+ if ((head) != NULL) { \
1036
+ HASH_BLOOM_FREE((head)->hh.tbl); \
1037
+ uthash_free((head)->hh.tbl->buckets, \
1038
+ (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \
1039
+ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
1040
+ (head) = NULL; \
1041
+ } \
1042
+ } while (0)
1043
+
1044
+ #define HASH_OVERHEAD(hh,head) \
1045
+ (((head) != NULL) ? ( \
1046
+ (size_t)(((head)->hh.tbl->num_items * sizeof(UT_hash_handle)) + \
1047
+ ((head)->hh.tbl->num_buckets * sizeof(UT_hash_bucket)) + \
1048
+ sizeof(UT_hash_table) + \
1049
+ (HASH_BLOOM_BYTELEN))) : 0U)
1050
+
1051
+ #ifdef NO_DECLTYPE
1052
+ #define HASH_ITER(hh,head,el,tmp) \
1053
+ for(((el)=(head)), ((*(char**)(&(tmp)))=(char*)((head!=NULL)?(head)->hh.next:NULL)); \
1054
+ (el) != NULL; ((el)=(tmp)), ((*(char**)(&(tmp)))=(char*)((tmp!=NULL)?(tmp)->hh.next:NULL)))
1055
+ #else
1056
+ #define HASH_ITER(hh,head,el,tmp) \
1057
+ for(((el)=(head)), ((tmp)=DECLTYPE(el)((head!=NULL)?(head)->hh.next:NULL)); \
1058
+ (el) != NULL; ((el)=(tmp)), ((tmp)=DECLTYPE(el)((tmp!=NULL)?(tmp)->hh.next:NULL)))
1059
+ #endif
1060
+
1061
+ /* obtain a count of items in the hash */
1062
+ #define HASH_COUNT(head) HASH_CNT(hh,head)
1063
+ #define HASH_CNT(hh,head) ((head != NULL)?((head)->hh.tbl->num_items):0U)
1064
+
1065
+ typedef struct UT_hash_bucket {
1066
+ struct UT_hash_handle *hh_head;
1067
+ unsigned count;
1068
+
1069
+ /* expand_mult is normally set to 0. In this situation, the max chain length
1070
+ * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If
1071
+ * the bucket's chain exceeds this length, bucket expansion is triggered).
1072
+ * However, setting expand_mult to a non-zero value delays bucket expansion
1073
+ * (that would be triggered by additions to this particular bucket)
1074
+ * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH.
1075
+ * (The multiplier is simply expand_mult+1). The whole idea of this
1076
+ * multiplier is to reduce bucket expansions, since they are expensive, in
1077
+ * situations where we know that a particular bucket tends to be overused.
1078
+ * It is better to let its chain length grow to a longer yet-still-bounded
1079
+ * value, than to do an O(n) bucket expansion too often.
1080
+ */
1081
+ unsigned expand_mult;
1082
+
1083
+ } UT_hash_bucket;
1084
+
1085
+ /* random signature used only to find hash tables in external analysis */
1086
+ #define HASH_SIGNATURE 0xa0111fe1u
1087
+ #define HASH_BLOOM_SIGNATURE 0xb12220f2u
1088
+
1089
+ typedef struct UT_hash_table {
1090
+ UT_hash_bucket *buckets;
1091
+ unsigned num_buckets, log2_num_buckets;
1092
+ unsigned num_items;
1093
+ struct UT_hash_handle *tail; /* tail hh in app order, for fast append */
1094
+ ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */
1095
+
1096
+ /* in an ideal situation (all buckets used equally), no bucket would have
1097
+ * more than ceil(#items/#buckets) items. that's the ideal chain length. */
1098
+ unsigned ideal_chain_maxlen;
1099
+
1100
+ /* nonideal_items is the number of items in the hash whose chain position
1101
+ * exceeds the ideal chain maxlen. these items pay the penalty for an uneven
1102
+ * hash distribution; reaching them in a chain traversal takes >ideal steps */
1103
+ unsigned nonideal_items;
1104
+
1105
+ /* ineffective expands occur when a bucket doubling was performed, but
1106
+ * afterward, more than half the items in the hash had nonideal chain
1107
+ * positions. If this happens on two consecutive expansions we inhibit any
1108
+ * further expansion, as it's not helping; this happens when the hash
1109
+ * function isn't a good fit for the key domain. When expansion is inhibited
1110
+ * the hash will still work, albeit no longer in constant time. */
1111
+ unsigned ineff_expands, noexpand;
1112
+
1113
+ uint32_t signature; /* used only to find hash tables in external analysis */
1114
+ #ifdef HASH_BLOOM
1115
+ uint32_t bloom_sig; /* used only to test bloom exists in external analysis */
1116
+ uint8_t *bloom_bv;
1117
+ uint8_t bloom_nbits;
1118
+ #endif
1119
+
1120
+ } UT_hash_table;
1121
+
1122
+ typedef struct UT_hash_handle {
1123
+ struct UT_hash_table *tbl;
1124
+ void *prev; /* prev element in app order */
1125
+ void *next; /* next element in app order */
1126
+ struct UT_hash_handle *hh_prev; /* previous hh in bucket order */
1127
+ struct UT_hash_handle *hh_next; /* next hh in bucket order */
1128
+ const void *key; /* ptr to enclosing struct's key */
1129
+ unsigned keylen; /* enclosing struct's key len */
1130
+ unsigned hashv; /* result of hash-fcn(key) */
1131
+ } UT_hash_handle;
1132
+
1133
+ #endif /* UTHASH_H */