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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
//
// Copyright 2016 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include "core/nng_impl.h"
#include "core/objhash.h"
#include <string.h>
// The details of the nni_objhash are "private".
struct nni_objhash {
size_t oh_cap;
size_t oh_count;
size_t oh_load;
size_t oh_minload; // considers placeholders
size_t oh_maxload;
uint32_t oh_minval;
uint32_t oh_maxval;
uint32_t oh_dynval;
nni_mtx oh_lock;
nni_cv oh_cv;
nni_objhash_node * oh_nodes;
nni_objhash_ctor oh_ctor;
nni_objhash_dtor oh_dtor;
};
struct nni_objhash_node {
uint32_t on_id; // the key
uint32_t on_skips; // indicates
uint32_t on_refcnt; // reference count
void * on_val; // pointer to user data
};
int
nni_objhash_init(nni_objhash **ohp, nni_objhash_ctor ctor,
nni_objhash_dtor dtor)
{
nni_objhash *oh;
int rv;
if ((ctor == NULL) || (dtor == NULL)) {
return (NNG_EINVAL);
}
if ((oh = NNI_ALLOC_STRUCT(oh)) == NULL) {
return (NNG_ENOMEM);
}
if ((rv = nni_mtx_init(&oh->oh_lock)) != 0) {
NNI_FREE_STRUCT(oh);
return (rv);
}
if ((rv = nni_cv_init(&oh->oh_cv, &oh->oh_lock)) != 0) {
nni_mtx_fini(&oh->oh_lock);
NNI_FREE_STRUCT(oh);
return (rv);
}
oh->oh_nodes = NULL;
oh->oh_count = 0;
oh->oh_load = 0;
oh->oh_cap = 0;
oh->oh_maxload = 0;
oh->oh_minload = 0; // never shrink below this
oh->oh_minval = 1;
oh->oh_maxval = 0x7fffffff;
oh->oh_dynval = nni_random() %
(oh->oh_maxval - oh->oh_minval) + oh->oh_minval;
oh->oh_ctor = ctor;
oh->oh_dtor = dtor;
*ohp = oh;
return (0);
}
void
nni_objhash_fini(nni_objhash *oh)
{
if (oh == NULL) {
return;
}
if (oh->oh_nodes != NULL) {
nni_free(oh->oh_nodes, oh->oh_cap * sizeof (nni_objhash_node));
oh->oh_nodes = NULL;
oh->oh_cap = oh->oh_count = 0;
oh->oh_load = oh->oh_minload = oh->oh_maxload = 0;
}
nni_cv_fini(&oh->oh_cv);
nni_mtx_fini(&oh->oh_lock);
NNI_FREE_STRUCT(oh);
}
// Inspired by Python dict implementation. This probe will visit every
// cell. We always hash consecutively assigned IDs.
#define NNI_OBJHASH_NEXTPROBE(h, j) \
((((j) * 5) + 1)& (h->oh_cap - 1))
// nni_objhash_find_node finds the object hash node associated with a given id.
// The object hash lock must be held by the caller.
static nni_objhash_node *
nni_objhash_find_node(nni_objhash *oh, uint32_t id)
{
uint32_t index;
nni_objhash_node *node;
if (oh->oh_count == 0) {
return (NULL);
}
index = id & (oh->oh_cap - 1);
for (;;) {
node = &oh->oh_nodes[index];
if ((node->on_val == NULL) && (node->on_skips == 0)) {
return (NULL);
}
if (node->on_id == id) {
return (node);
}
index = NNI_OBJHASH_NEXTPROBE(oh, index);
}
}
// nni_objhash_find looks up the object, and bumps the reference on it.
// The caller should drop the reference when done by calling nni_objhash_unref.
int
nni_objhash_find(nni_objhash *oh, uint32_t id, void **valp)
{
uint32_t index;
nni_objhash_node *node;
int rv;
nni_mtx_lock(&oh->oh_lock);
node = nni_objhash_find_node(oh, id);
if ((node != NULL) && (node->on_val != NULL)) {
*valp = node->on_val;
node->on_refcnt++;
rv = 0;
} else {
rv = NNG_ENOENT;
}
nni_mtx_unlock(&oh->oh_lock);
return (rv);
}
// Resize the object hash. This is called internally with the lock
// for the object hash held. Grow indicates that this is being called
// from a function that intends to add data, so extra space is needed.
static int
nni_objhash_resize(nni_objhash *oh, int grow)
{
size_t newsize;
size_t oldsize;
nni_objhash_node *newnodes;
nni_objhash_node *oldnodes;
uint32_t i;
if ((!grow) && (oh->oh_count == 0) && (oh->oh_cap != 0)) {
// Table is empty, and we are unrefing. Lets reclaim the
// space. Note that this means that allocations which
// fluctuate between one and zero are going to bang on the
// allocator a bit. Since such cases should not be very
// performance sensitive, this is probably okay.
nni_free(oh->oh_nodes, oh->oh_cap * sizeof (nni_objhash_node));
oh->oh_cap = 0;
oh->oh_nodes = NULL;
oh->oh_minload = 0;
oh->oh_maxload = 0;
return (0);
}
if ((oh->oh_load < oh->oh_maxload) && (oh->oh_load >= oh->oh_minload)) {
// No resize needed.
return (0);
}
oldsize = oh->oh_cap;
newsize = oh->oh_cap;
newsize = 8;
while (newsize < (oh->oh_count * 2)) {
newsize *= 2;
}
oldnodes = oh->oh_nodes;
newnodes = nni_alloc(sizeof (nni_objhash_node) * newsize);
if (newnodes == NULL) {
return (NNG_ENOMEM);
}
memset(newnodes, 0, sizeof (nni_objhash_node) * newsize);
oh->oh_nodes = newnodes;
oh->oh_cap = newsize;
if (newsize > 8) {
oh->oh_minload = newsize / 8;
oh->oh_maxload = newsize * 2 / 3;
} else {
oh->oh_minload = 0;
oh->oh_maxload = 5;
}
for (i = 0; i < oldsize; i++) {
uint32_t index;
if (oldnodes[i].on_val == NULL) {
continue;
}
index = oldnodes[i].on_id & (newsize - 1);
for (;;) {
if (newnodes[index].on_val == NULL) {
oh->oh_load++;
newnodes[index].on_val = oldnodes[i].on_val;
newnodes[index].on_id = oldnodes[i].on_id;
newnodes[index].on_refcnt =
oldnodes[i].on_refcnt;
break;
}
newnodes[index].on_skips++;
index = NNI_OBJHASH_NEXTPROBE(oh, index);
}
}
if (oldsize != 0) {
nni_free(oldnodes, sizeof (nni_objhash_node) * oldsize);
}
return (0);
}
void
nni_objhash_unref(nni_objhash *oh, uint32_t id)
{
int rv;
void *val;
uint32_t index;
nni_objhash_node *node;
nni_objhash_dtor dtor;
nni_mtx_lock(&oh->oh_lock);
dtor = oh->oh_dtor;
node = nni_objhash_find_node(oh, id);
NNI_ASSERT(node != NULL);
val = node->on_val;
NNI_ASSERT(node->on_refcnt > 0);
NNI_ASSERT(node->on_refcnt < 1000000); // reasonable limit, debug only
node->on_refcnt--;
if (node->on_refcnt != 0) {
if (node->on_refcnt == 1) {
nni_cv_wake(&oh->oh_cv);
}
// Still busy/referenced?
nni_mtx_unlock(&oh->oh_lock);
return;
}
index = id & (oh->oh_cap - 1);
for (;;) {
node = &oh->oh_nodes[index];
if (node->on_id == id) {
break;
}
NNI_ASSERT(node->on_skips != 0);
node->on_skips--;
if ((node->on_val == NULL) && (node->on_skips == 0)) {
oh->oh_load--;
}
index = NNI_OBJHASH_NEXTPROBE(oh, index);
}
NNI_ASSERT(node->on_val != NULL);
NNI_ASSERT(node->on_refcnt == 0);
NNI_ASSERT(node->on_id == id);
node->on_val = NULL;
oh->oh_count--;
if (node->on_skips == 0) {
oh->oh_load--;
}
// Reclaim the buffer if we want, but preserve the limits.
nni_objhash_resize(oh, 0);
nni_mtx_unlock(&oh->oh_lock);
// Now run the destructor.
dtor(val);
}
void
nni_objhash_unref_wait(nni_objhash *oh, uint32_t id)
{
int rv;
void *val;
uint32_t index;
nni_objhash_node *node;
nni_objhash_dtor dtor;
nni_mtx_lock(&oh->oh_lock);
dtor = oh->oh_dtor;
node = nni_objhash_find_node(oh, id);
NNI_ASSERT(node != NULL);
val = node->on_val;
while (node->on_refcnt != 1) {
nni_cv_wait(&oh->oh_cv);
}
node->on_refcnt--;
if (node->on_refcnt != 0) {
if (node->on_refcnt == 1) {
nni_cv_wake(&oh->oh_cv);
}
// Still busy/referenced?
nni_mtx_unlock(&oh->oh_lock);
return;
}
index = id & (oh->oh_cap - 1);
for (;;) {
node = &oh->oh_nodes[index];
if (node->on_id == id) {
break;
}
NNI_ASSERT(node->on_skips != 0);
node->on_skips--;
if ((node->on_val == NULL) && (node->on_skips == 0)) {
oh->oh_load--;
}
index = NNI_OBJHASH_NEXTPROBE(oh, index);
}
NNI_ASSERT(node->on_val != NULL);
NNI_ASSERT(node->on_refcnt == 0);
NNI_ASSERT(node->on_id == id);
node->on_val = NULL;
oh->oh_count--;
if (node->on_skips == 0) {
oh->oh_load--;
}
// Reclaim the buffer if we want, but preserve the limits.
nni_objhash_resize(oh, 0);
nni_mtx_unlock(&oh->oh_lock);
// Now run the destructor.
dtor(val);
}
// Allocate a new object hash entry. Note that this will execute the
// constructor with the object hash lock held. Consequently, code that
// runs the constructor must not run for long periods of time, since that
// can block all other uses of the object hash.
int
nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp)
{
uint32_t id;
uint32_t index;
nni_objhash_node *node;
nni_mtx_lock(&oh->oh_lock);
if (oh->oh_count > (oh->oh_maxval - oh->oh_minval)) {
// Really more like ENOSPC.. the table is filled to max.
nni_mtx_unlock(&oh->oh_lock);
return (NNG_ENOMEM);
}
nni_objhash_resize(oh, 1);
for (;;) {
id = oh->oh_dynval;
oh->oh_dynval++;
if ((oh->oh_dynval > oh->oh_maxval) ||
(oh->oh_dynval < oh->oh_minval)) {
oh->oh_dynval = oh->oh_minval;
}
if (nni_objhash_find_node(oh, id) == NULL) {
// We can use this ID, great!
break;
}
}
// We know the ID we're going to use, but we have to walk again,
// because we need to note whether we had to skip (probe), and mark
// them so they don't get nuked along the way.
// check to see if anything is located there.
index = id & (oh->oh_cap - 1);
for (;;) {
node = &oh->oh_nodes[index];
if (node->on_val == NULL) {
break;
}
NNI_ASSERT(node->on_id != id);
node->on_skips++;
index = NNI_OBJHASH_NEXTPROBE(oh, index);
}
node->on_id = id;
node->on_refcnt++;
node->on_val = oh->oh_ctor(id);
if (node->on_val == NULL) {
// Constructor failed; walk *again* to undo the skip increments.
node->on_refcnt--;
index = id & (oh->oh_cap - 1);
for (;;) {
node = &oh->oh_nodes[index];
if (node->on_val == NULL) {
NNI_ASSERT(node->on_id == id);
break;
}
NNI_ASSERT(node->on_skips != 0);
node->on_skips--;
index = NNI_OBJHASH_NEXTPROBE(oh, index);
}
nni_mtx_unlock(&oh->oh_lock);
return (NNG_ENOMEM); // no other return from ctor
}
oh->oh_count++;
if (node->on_skips == 0) {
oh->oh_load++;
}
*valp = node->on_val;
*idp = id;
nni_mtx_unlock(&oh->oh_lock);
return (0);
}
size_t
nni_objhash_count(nni_objhash *oh)
{
return (oh->oh_count);
}
|