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
|
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// 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 <string.h>
struct nni_idhash_entry {
uint64_t ihe_key;
void * ihe_val;
uint32_t ihe_skips;
};
struct nni_idhash {
size_t ih_cap;
size_t ih_count;
size_t ih_load;
size_t ih_minload; // considers placeholders
size_t ih_maxload;
uint64_t ih_minval;
uint64_t ih_maxval;
uint64_t ih_dynval;
nni_idhash_entry *ih_entries;
nni_mtx ih_mtx;
};
int
nni_idhash_init(nni_idhash **hp)
{
nni_idhash *h;
if ((h = NNI_ALLOC_STRUCT(h)) == NULL) {
return (NNG_ENOMEM);
}
nni_mtx_init(&h->ih_mtx);
h->ih_entries = NULL;
h->ih_count = 0;
h->ih_load = 0;
h->ih_cap = 0;
h->ih_maxload = 0;
h->ih_minload = 0; // never shrink below this
h->ih_minval = 0;
h->ih_maxval = 0xffffffff;
h->ih_dynval = 0;
*hp = h;
return (0);
}
void
nni_idhash_fini(nni_idhash *h)
{
if (h != NULL) {
if (h->ih_entries != NULL) {
NNI_FREE_STRUCTS(h->ih_entries, h->ih_cap);
h->ih_entries = NULL;
h->ih_cap = h->ih_count = 0;
h->ih_load = h->ih_minload = h->ih_maxload = 0;
}
nni_mtx_fini(&h->ih_mtx);
NNI_FREE_STRUCT(h);
}
}
void
nni_idhash_set_limits(
nni_idhash *h, uint64_t minval, uint64_t maxval, uint64_t start)
{
if (start < minval) {
start = minval;
}
if (start > maxval) {
start = maxval;
}
nni_mtx_lock(&h->ih_mtx);
h->ih_minval = minval;
h->ih_maxval = maxval;
h->ih_dynval = start;
NNI_ASSERT(minval < maxval);
NNI_ASSERT(start >= minval);
NNI_ASSERT(start <= maxval);
nni_mtx_unlock(&h->ih_mtx);
}
// Inspired by Python dict implementation. This probe will visit every
// cell. We always hash consecutively assigned IDs.
#define NNI_IDHASH_NEXTPROBE(h, j) ((((j) *5) + 1) & (h->ih_cap - 1))
#define NNI_IDHASH_INDEX(h, j) \
(((j & 0xffffffff) ^ (j >> 32)) & (h->ih_cap - 1))
static int
nni_hash_find(nni_idhash *h, uint64_t id, void **valp)
{
uint32_t index = NNI_IDHASH_INDEX(h, id);
if (h->ih_count == 0) {
return (NNG_ENOENT);
}
for (;;) {
if ((h->ih_entries[index].ihe_val == NULL) &&
(h->ih_entries[index].ihe_skips == 0)) {
return (NNG_ENOENT);
}
if (h->ih_entries[index].ihe_key == id) {
*valp = h->ih_entries[index].ihe_val;
return (0);
}
index = NNI_IDHASH_NEXTPROBE(h, index);
}
}
int
nni_idhash_find(nni_idhash *h, uint64_t id, void **valp)
{
int rv;
nni_mtx_lock(&h->ih_mtx);
rv = nni_hash_find(h, id, valp);
nni_mtx_unlock(&h->ih_mtx);
return (rv);
}
static int
nni_hash_resize(nni_idhash *h)
{
size_t newsize;
size_t oldsize;
nni_idhash_entry *newents;
nni_idhash_entry *oldents;
uint32_t i;
if ((h->ih_load < h->ih_maxload) && (h->ih_load >= h->ih_minload)) {
// No resize needed.
return (0);
}
oldsize = h->ih_cap;
newsize = 8;
while (newsize < (h->ih_count * 2)) {
newsize *= 2;
}
oldents = h->ih_entries;
newents = NNI_ALLOC_STRUCTS(newents, newsize);
if (newents == NULL) {
return (NNG_ENOMEM);
}
memset(newents, 0, sizeof(nni_idhash_entry) * newsize);
h->ih_entries = newents;
h->ih_cap = newsize;
if (newsize > 8) {
h->ih_minload = newsize / 8;
h->ih_maxload = newsize * 2 / 3;
} else {
h->ih_minload = 0;
h->ih_maxload = 5;
}
for (i = 0; i < oldsize; i++) {
size_t index;
if (oldents[i].ihe_val == NULL) {
continue;
}
index = oldents[i].ihe_key & (newsize - 1);
for (;;) {
if (newents[index].ihe_val == NULL) {
h->ih_load++;
newents[index].ihe_val = oldents[i].ihe_val;
newents[index].ihe_key = oldents[i].ihe_key;
break;
}
newents[index].ihe_skips++;
index = NNI_IDHASH_NEXTPROBE(h, index);
}
}
if (oldsize != 0) {
NNI_FREE_STRUCTS(oldents, oldsize);
}
return (0);
}
int
nni_idhash_remove(nni_idhash *h, uint64_t id)
{
int rv;
void * val;
size_t index;
nni_mtx_lock(&h->ih_mtx);
// First check that it is in the table. This may double the
// lookup time, but it means that if we get past this then we KNOW
// we are going to delete an element.
if ((rv = nni_hash_find(h, id, &val)) != 0) {
nni_mtx_unlock(&h->ih_mtx);
return (rv);
}
index = NNI_IDHASH_INDEX(h, id);
for (;;) {
nni_idhash_entry *ent = &h->ih_entries[index];
if (ent->ihe_key == id) {
ent->ihe_val = NULL;
if (ent->ihe_skips == 0) {
h->ih_load--;
}
h->ih_count--;
break;
}
if (ent->ihe_skips < 1) {
nni_panic("Skips should be nonzero!");
}
ent->ihe_skips--;
if ((ent->ihe_skips == 0) && (ent->ihe_val == NULL)) {
h->ih_load--;
}
index = NNI_IDHASH_NEXTPROBE(h, index);
}
// Shrink -- but it's ok if we can't.
(void) nni_hash_resize(h);
nni_mtx_unlock(&h->ih_mtx);
return (0);
}
static int
nni_hash_insert(nni_idhash *h, uint64_t id, void *val)
{
size_t index;
// Try to resize. If we can't, but we still have room, go ahead
// and store it.
if ((nni_hash_resize(h) != 0) && (h->ih_count >= (h->ih_cap - 1))) {
return (NNG_ENOMEM);
}
index = NNI_IDHASH_INDEX(h, id);
for (;;) {
nni_idhash_entry *ent = &h->ih_entries[index];
if ((ent->ihe_val == NULL) || (ent->ihe_key == id)) {
if (ent->ihe_val == NULL) {
h->ih_count++;
h->ih_load++;
}
ent->ihe_key = id;
ent->ihe_val = val;
return (0);
}
ent->ihe_skips++;
index = NNI_IDHASH_NEXTPROBE(h, index);
}
}
int
nni_idhash_insert(nni_idhash *h, uint64_t id, void *val)
{
int rv;
nni_mtx_lock(&h->ih_mtx);
rv = nni_hash_insert(h, id, val);
nni_mtx_unlock(&h->ih_mtx);
return (rv);
}
int
nni_idhash_alloc(nni_idhash *h, uint64_t *idp, void *val)
{
uint64_t id;
void * scrap;
int rv;
nni_mtx_lock(&h->ih_mtx);
if (h->ih_count > (h->ih_maxval - h->ih_minval)) {
// Really more like ENOSPC.. the table is filled to max.
nni_mtx_unlock(&h->ih_mtx);
return (NNG_ENOMEM);
}
for (;;) {
id = h->ih_dynval;
h->ih_dynval++;
if (h->ih_dynval > h->ih_maxval) {
h->ih_dynval = h->ih_minval;
}
if (nni_hash_find(h, id, &scrap) == NNG_ENOENT) {
break;
}
}
rv = nni_hash_insert(h, id, val);
if (rv == 0) {
*idp = id;
}
nni_mtx_unlock(&h->ih_mtx);
return (rv);
}
size_t
nni_idhash_count(nni_idhash *h)
{
size_t num;
nni_mtx_lock(&h->ih_mtx);
num = h->ih_count;
nni_mtx_unlock(&h->ih_mtx);
return (num);
}
|