ht.c 6.83 KB
Newer Older
1
/*
2
 * Copyright (C) 2016, 2017  Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
 */

9
10
#include <config.h>

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string.h>

#include <isc/hash.h>
#include <isc/ht.h>
#include <isc/types.h>
#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/result.h>
#include <isc/util.h>


typedef struct isc_ht_node isc_ht_node_t;

#define ISC_HT_MAGIC			ISC_MAGIC('H', 'T', 'a', 'b')
#define ISC_HT_VALID(ht)		ISC_MAGIC_VALID(ht, ISC_HT_MAGIC)

struct isc_ht_node {
	void *value;
	isc_ht_node_t *next;
	size_t keysize;
31
	unsigned char key[FLEXIBLE_ARRAY_MEMBER];
32
33
34
35
36
37
38
39
40
41
42
43
44
};

struct isc_ht {
	unsigned int magic;
	isc_mem_t *mctx;
	size_t size;
	size_t mask;
	unsigned int count;
	isc_ht_node_t **table;
};

struct isc_ht_iter {
	isc_ht_t *ht;
Francis Dupont's avatar
Francis Dupont committed
45
	size_t i;
46
47
48
49
50
51
	isc_ht_node_t *cur;
};

isc_result_t
isc_ht_init(isc_ht_t **htp, isc_mem_t *mctx, isc_uint8_t bits) {
	isc_ht_t *ht = NULL;
52
	size_t i;
53
54
55
56
57
58
59
60
61
62
63
64
65

	REQUIRE(htp != NULL && *htp == NULL);
	REQUIRE(mctx != NULL);
	REQUIRE(bits >= 1 && bits <= (sizeof(size_t)*8 - 1));

	ht = isc_mem_get(mctx, sizeof(struct isc_ht));
	if (ht == NULL) {
		return (ISC_R_NOMEMORY);
	}

	ht->mctx = NULL;
	isc_mem_attach(mctx, &ht->mctx);

Mark Andrews's avatar
Mark Andrews committed
66
67
	ht->size = ((size_t)1<<bits);
	ht->mask = ((size_t)1<<bits)-1;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	ht->count = 0;

	ht->table = isc_mem_get(ht->mctx, ht->size * sizeof(isc_ht_node_t*));
	if (ht->table == NULL) {
		isc_mem_putanddetach(&ht->mctx, ht, sizeof(struct isc_ht));
		return (ISC_R_NOMEMORY);
	}

	for (i = 0; i < ht->size; i++) {
		ht->table[i] = NULL;
	}

	ht->magic = ISC_HT_MAGIC;

	*htp = ht;
	return (ISC_R_SUCCESS);
}

void
isc_ht_destroy(isc_ht_t **htp) {
	isc_ht_t *ht;
89
	size_t i;
90
91
92
93
94
95
96
97
98
99
100
101
102
103

	REQUIRE(htp != NULL);

	ht = *htp;
	REQUIRE(ISC_HT_VALID(ht));

	ht->magic = 0;

	for (i = 0; i < ht->size; i++) {
		isc_ht_node_t *node = ht->table[i];
		while (node != NULL) {
			isc_ht_node_t *next = node->next;
			ht->count--;
			isc_mem_put(ht->mctx, node,
104
105
				    offsetof(isc_ht_node_t, key) +
				    node->keysize);
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
			node = next;
		}
	}

	INSIST(ht->count == 0);

	isc_mem_put(ht->mctx, ht->table, ht->size * sizeof(isc_ht_node_t*));
	isc_mem_putanddetach(&ht->mctx, ht, sizeof(struct isc_ht));

	*htp = NULL;
}

isc_result_t
isc_ht_add(isc_ht_t *ht, const unsigned char *key,
	   isc_uint32_t keysize, void *value)
{
	isc_ht_node_t *node;
	isc_uint32_t hash;

	REQUIRE(ISC_HT_VALID(ht));
	REQUIRE(key != NULL && keysize > 0);

	hash = isc_hash_function(key, keysize, ISC_TRUE, NULL);
	node = ht->table[hash & ht->mask];
	while (node != NULL) {
		if (keysize == node->keysize &&
Francis Dupont's avatar
Francis Dupont committed
132
		    memcmp(key, node->key, keysize) == 0) {
133
134
135
136
137
			return (ISC_R_EXISTS);
		}
		node = node->next;
	}

138
	node = isc_mem_get(ht->mctx, offsetof(isc_ht_node_t, key) + keysize);
Francis Dupont's avatar
Francis Dupont committed
139
	if (node == NULL)
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
		return (ISC_R_NOMEMORY);

	memmove(node->key, key, keysize);
	node->keysize = keysize;
	node->next = ht->table[hash & ht->mask];
	node->value = value;

	ht->count++;
	ht->table[hash & ht->mask] = node;
	return (ISC_R_SUCCESS);
}

isc_result_t
isc_ht_find(const isc_ht_t *ht, const unsigned char *key,
	    isc_uint32_t keysize, void **valuep)
{
	isc_ht_node_t *node;
	isc_uint32_t hash;

	REQUIRE(ISC_HT_VALID(ht));
	REQUIRE(key != NULL && keysize > 0);

	hash = isc_hash_function(key, keysize, ISC_TRUE, NULL);
	node = ht->table[hash & ht->mask];
	while (node != NULL) {
		if (keysize == node->keysize &&
166
167
168
169
		    memcmp(key, node->key, keysize) == 0)
		{
			if (valuep != NULL)
				*valuep = node->value;
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
			return (ISC_R_SUCCESS);
		}
		node = node->next;
	}

	return (ISC_R_NOTFOUND);
}

isc_result_t
isc_ht_delete(isc_ht_t *ht, const unsigned char *key, isc_uint32_t keysize) {
	isc_ht_node_t *node, *prev;
	isc_uint32_t hash;

	REQUIRE(ISC_HT_VALID(ht));
	REQUIRE(key != NULL && keysize > 0);

	prev = NULL;
	hash = isc_hash_function(key, keysize, ISC_TRUE, NULL);
	node = ht->table[hash & ht->mask];
	while (node != NULL) {
		if (keysize == node->keysize &&
Francis Dupont's avatar
Francis Dupont committed
191
192
		    memcmp(key, node->key, keysize) == 0) {
			if (prev == NULL)
193
				ht->table[hash & ht->mask] = node->next;
Francis Dupont's avatar
Francis Dupont committed
194
			else
195
196
				prev->next = node->next;
			isc_mem_put(ht->mctx, node,
197
198
				    offsetof(isc_ht_node_t, key) +
				    node->keysize);
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
			ht->count--;

			return (ISC_R_SUCCESS);
		}

		prev = node;
		node = node->next;
	}
	return (ISC_R_NOTFOUND);
}

isc_result_t
isc_ht_iter_create(isc_ht_t *ht, isc_ht_iter_t **itp) {
	isc_ht_iter_t *it;

	REQUIRE(ISC_HT_VALID(ht));
	REQUIRE(itp != NULL && *itp == NULL);

	it = isc_mem_get(ht->mctx, sizeof(isc_ht_iter_t));
	if (it == NULL)
		return (ISC_R_NOMEMORY);

	it->ht = ht;
	it->i = 0;
	it->cur = NULL;

	*itp = it;

	return (ISC_R_SUCCESS);
}

void
isc_ht_iter_destroy(isc_ht_iter_t **itp) {
	isc_ht_iter_t *it;
	isc_ht_t *ht;

	REQUIRE(itp != NULL && *itp != NULL);

	it = *itp;
	ht = it->ht;
	isc_mem_put(ht->mctx, it, sizeof(isc_ht_iter_t));

	*itp = NULL;
}

isc_result_t
isc_ht_iter_first(isc_ht_iter_t *it) {
	REQUIRE(it != NULL);

	it->i = 0;
	while (it->i < it->ht->size && it->ht->table[it->i] == NULL)
		it->i++;

Francis Dupont's avatar
Francis Dupont committed
252
	if (it->i == it->ht->size)
253
254
255
256
257
258
259
260
261
262
263
264
265
266
		return (ISC_R_NOMORE);

	it->cur = it->ht->table[it->i];

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_ht_iter_next(isc_ht_iter_t *it) {
	REQUIRE(it != NULL);
	REQUIRE(it->cur != NULL);

	it->cur = it->cur->next;
	if (it->cur == NULL) {
267
		do {
268
			it->i++;
269
270
		} while (it->i < it->ht->size && it->ht->table[it->i] == NULL);
		if (it->i >= it->ht->size)
271
272
273
274
275
276
277
			return (ISC_R_NOMORE);
		it->cur = it->ht->table[it->i];
	}

	return (ISC_R_SUCCESS);
}

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
isc_result_t
isc_ht_iter_delcurrent_next(isc_ht_iter_t *it) {
	isc_result_t result = ISC_R_SUCCESS;
	isc_ht_node_t *to_delete = NULL;
	isc_ht_node_t *prev = NULL;
	isc_ht_node_t *node = NULL;
	isc_uint32_t hash;
	isc_ht_t *ht;
	REQUIRE(it != NULL);
	REQUIRE(it->cur != NULL);
	to_delete = it->cur;
	ht = it->ht;

	it->cur = it->cur->next;
	if (it->cur == NULL) {
		do {
294
			it->i++;
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
		} while (it->i < ht->size && ht->table[it->i] == NULL);
		if (it->i >= ht->size)
			result = ISC_R_NOMORE;
		else
			it->cur = ht->table[it->i];
	}

	hash = isc_hash_function(to_delete->key, to_delete->keysize, ISC_TRUE,
				 NULL);
	node = ht->table[hash & ht->mask];
	while (node != to_delete) {
		prev = node;
		node = node->next;
		INSIST(node != NULL);
	}

Francis Dupont's avatar
Francis Dupont committed
311
	if (prev == NULL)
312
		ht->table[hash & ht->mask] = node->next;
Francis Dupont's avatar
Francis Dupont committed
313
	else
314
315
		prev->next = node->next;
	isc_mem_put(ht->mctx, node,
316
		    offsetof(isc_ht_node_t, key) + node->keysize);
317
318
319
320
321
	ht->count--;

	return (result);
}

322
323
324
325
326
327
328
void
isc_ht_iter_current(isc_ht_iter_t *it, void **valuep) {
	REQUIRE(it != NULL);
	REQUIRE(it->cur != NULL);
	*valuep = it->cur->value;
}

329
330
331
332
333
334
335
336
337
void
isc_ht_iter_currentkey(isc_ht_iter_t *it, unsigned char **key, size_t *keysize)
{
	REQUIRE(it != NULL);
	REQUIRE(it->cur != NULL);
	*key = it->cur->key;
	*keysize = it->cur->keysize;
}

338
339
340
341
342
343
unsigned int
isc_ht_count(isc_ht_t *ht) {
	REQUIRE(ISC_HT_VALID(ht));

	return(ht->count);
}