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
|
/*
** Zabbix
** Copyright (C) 2001-2014 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include <stddef.h>
#include "common.h"
#include "log.h"
#include "zbxalgo.h"
static void __hashset_free_entry(zbx_hashset_t *hs, ZBX_HASHSET_ENTRY_T *entry);
#define CRIT_LOAD_FACTOR 4/5
#define SLOT_GROWTH_FACTOR 3/2
/* private hashset functions */
static void __hashset_free_entry(zbx_hashset_t *hs, ZBX_HASHSET_ENTRY_T *entry)
{
hs->mem_free_func(entry);
}
/* public hashset interface */
void zbx_hashset_create(zbx_hashset_t *hs, size_t init_size,
zbx_hash_func_t hash_func,
zbx_compare_func_t compare_func)
{
zbx_hashset_create_ext(hs, init_size, hash_func, compare_func,
ZBX_DEFAULT_MEM_MALLOC_FUNC,
ZBX_DEFAULT_MEM_REALLOC_FUNC,
ZBX_DEFAULT_MEM_FREE_FUNC);
}
void zbx_hashset_create_ext(zbx_hashset_t *hs, size_t init_size,
zbx_hash_func_t hash_func,
zbx_compare_func_t compare_func,
zbx_mem_malloc_func_t mem_malloc_func,
zbx_mem_realloc_func_t mem_realloc_func,
zbx_mem_free_func_t mem_free_func)
{
int nslots = next_prime(init_size);
if (NULL == (hs->slots = mem_malloc_func(NULL, nslots * sizeof(ZBX_HASHSET_ENTRY_T *))))
return;
hs->num_data = 0;
hs->num_slots = nslots;
memset(hs->slots, 0, hs->num_slots * sizeof(ZBX_HASHSET_ENTRY_T *));
hs->hash_func = hash_func;
hs->compare_func = compare_func;
hs->mem_malloc_func = mem_malloc_func;
hs->mem_realloc_func = mem_realloc_func;
hs->mem_free_func = mem_free_func;
}
void zbx_hashset_destroy(zbx_hashset_t *hs)
{
int i;
ZBX_HASHSET_ENTRY_T *entry, *next_entry;
for (i = 0; i < hs->num_slots; i++)
{
entry = hs->slots[i];
while (NULL != entry)
{
next_entry = entry->next;
__hashset_free_entry(hs, entry);
entry = next_entry;
}
}
hs->num_data = 0;
hs->num_slots = 0;
hs->mem_free_func(hs->slots);
hs->slots = NULL;
hs->hash_func = NULL;
hs->compare_func = NULL;
hs->mem_malloc_func = NULL;
hs->mem_realloc_func = NULL;
hs->mem_free_func = NULL;
}
void *zbx_hashset_insert(zbx_hashset_t *hs, const void *data, size_t size)
{
return zbx_hashset_insert_ext(hs, data, size, 0);
}
void *zbx_hashset_insert_ext(zbx_hashset_t *hs, const void *data, size_t size, size_t offset)
{
int slot;
zbx_hash_t hash;
ZBX_HASHSET_ENTRY_T *entry;
hash = hs->hash_func(data);
slot = hash % hs->num_slots;
entry = hs->slots[slot];
while (NULL != entry)
{
if (entry->hash == hash && hs->compare_func(entry->data, data) == 0)
break;
entry = entry->next;
}
if (NULL == entry)
{
if (hs->num_data + 1 >= hs->num_slots * CRIT_LOAD_FACTOR)
{
int inc_slots, new_slot;
void *slots;
ZBX_HASHSET_ENTRY_T **prev_next, *curr_entry, *tmp;
inc_slots = next_prime(MAX(hs->num_slots + 1, hs->num_slots * SLOT_GROWTH_FACTOR));
if (NULL == (slots = hs->mem_realloc_func(hs->slots, inc_slots * sizeof(ZBX_HASHSET_ENTRY_T *))))
return NULL;
hs->slots = slots;
memset(hs->slots + hs->num_slots, 0, (inc_slots - hs->num_slots) * sizeof(ZBX_HASHSET_ENTRY_T *));
for (slot = 0; slot < hs->num_slots; slot++)
{
prev_next = &hs->slots[slot];
curr_entry = hs->slots[slot];
while (NULL != curr_entry)
{
if (slot != (new_slot = curr_entry->hash % inc_slots))
{
tmp = curr_entry->next;
curr_entry->next = hs->slots[new_slot];
hs->slots[new_slot] = curr_entry;
*prev_next = tmp;
curr_entry = tmp;
}
else
{
prev_next = &curr_entry->next;
curr_entry = curr_entry->next;
}
}
}
hs->num_slots = inc_slots;
/* recalculate new slot */
slot = hash % hs->num_slots;
}
if (NULL == (entry = hs->mem_malloc_func(NULL, offsetof(ZBX_HASHSET_ENTRY_T, data) + size)))
return NULL;
memcpy((char *)entry->data + offset, (const char *)data + offset, size - offset);
entry->hash = hash;
entry->next = hs->slots[slot];
hs->slots[slot] = entry;
hs->num_data++;
}
return entry->data;
}
void *zbx_hashset_search(zbx_hashset_t *hs, const void *data)
{
int slot;
zbx_hash_t hash;
ZBX_HASHSET_ENTRY_T *entry;
hash = hs->hash_func(data);
slot = hash % hs->num_slots;
entry = hs->slots[slot];
while (NULL != entry)
{
if (entry->hash == hash && hs->compare_func(entry->data, data) == 0)
break;
entry = entry->next;
}
return (NULL != entry ? entry->data : NULL);
}
void zbx_hashset_remove(zbx_hashset_t *hs, const void *data)
{
int slot;
zbx_hash_t hash;
ZBX_HASHSET_ENTRY_T *entry;
hash = hs->hash_func(data);
slot = hash % hs->num_slots;
entry = hs->slots[slot];
if (NULL != entry)
{
if (entry->hash == hash && hs->compare_func(entry->data, data) == 0)
{
hs->slots[slot] = entry->next;
__hashset_free_entry(hs, entry);
hs->num_data--;
}
else
{
ZBX_HASHSET_ENTRY_T *prev_entry;
prev_entry = entry;
entry = entry->next;
while (NULL != entry)
{
if (entry->hash == hash && hs->compare_func(entry->data, data) == 0)
{
prev_entry->next = entry->next;
__hashset_free_entry(hs, entry);
hs->num_data--;
break;
}
prev_entry = entry;
entry = entry->next;
}
}
}
}
void zbx_hashset_clear(zbx_hashset_t *hs)
{
int slot;
ZBX_HASHSET_ENTRY_T *entry;
for (slot = 0; slot < hs->num_slots; slot++)
{
while (NULL != hs->slots[slot])
{
entry = hs->slots[slot];
hs->slots[slot] = entry->next;
__hashset_free_entry(hs, entry);
}
}
hs->num_data = 0;
}
#define ITER_START (-1)
#define ITER_FINISH (-2)
void zbx_hashset_iter_reset(zbx_hashset_t *hs, zbx_hashset_iter_t *iter)
{
iter->hashset = hs;
iter->slot = ITER_START;
}
void *zbx_hashset_iter_next(zbx_hashset_iter_t *iter)
{
if (ITER_FINISH == iter->slot)
return NULL;
if (ITER_START != iter->slot && NULL != iter->entry && NULL != iter->entry->next)
{
iter->entry = iter->entry->next;
return iter->entry->data;
}
while (1)
{
iter->slot++;
if (iter->slot == iter->hashset->num_slots)
{
iter->slot = ITER_FINISH;
return NULL;
}
if (NULL != iter->hashset->slots[iter->slot])
{
iter->entry = iter->hashset->slots[iter->slot];
return iter->entry->data;
}
}
}
void zbx_hashset_iter_remove(zbx_hashset_iter_t *iter)
{
if (ITER_START == iter->slot || ITER_FINISH == iter->slot || NULL == iter->entry)
{
zabbix_log(LOG_LEVEL_CRIT, "removing a hashset entry through a bad iterator");
exit(EXIT_FAILURE);
}
if (iter->hashset->slots[iter->slot] == iter->entry)
{
iter->hashset->slots[iter->slot] = iter->entry->next;
__hashset_free_entry(iter->hashset, iter->entry);
iter->hashset->num_data--;
iter->slot--;
iter->entry = NULL;
}
else
{
ZBX_HASHSET_ENTRY_T *prev_entry = iter->hashset->slots[iter->slot];
while (prev_entry->next != iter->entry)
prev_entry = prev_entry->next;
prev_entry->next = iter->entry->next;
__hashset_free_entry(iter->hashset, iter->entry);
iter->hashset->num_data--;
iter->entry = prev_entry;
}
}
|