summaryrefslogtreecommitdiffstats
path: root/lib/isc/ht.c
blob: b3c6825f1f85a9597d72aca62318ea1f53ad4fd7 (plain)
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
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * 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/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#include <config.h>

#include <inttypes.h>
#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;
	unsigned char key[FLEXIBLE_ARRAY_MEMBER];
};

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;
	size_t i;
	isc_ht_node_t *cur;
};

isc_result_t
isc_ht_init(isc_ht_t **htp, isc_mem_t *mctx, uint8_t bits) {
	isc_ht_t *ht = NULL;
	size_t i;

	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);

	ht->size = ((size_t)1<<bits);
	ht->mask = ((size_t)1<<bits)-1;
	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;
	size_t i;

	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,
				    offsetof(isc_ht_node_t, key) +
				    node->keysize);
			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,
	   uint32_t keysize, void *value)
{
	isc_ht_node_t *node;
	uint32_t hash;

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

	hash = isc_hash_function(key, keysize, true, NULL);
	node = ht->table[hash & ht->mask];
	while (node != NULL) {
		if (keysize == node->keysize &&
		    memcmp(key, node->key, keysize) == 0) {
			return (ISC_R_EXISTS);
		}
		node = node->next;
	}

	node = isc_mem_get(ht->mctx, offsetof(isc_ht_node_t, key) + keysize);
	if (node == NULL)
		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,
	    uint32_t keysize, void **valuep)
{
	isc_ht_node_t *node;
	uint32_t hash;

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

	hash = isc_hash_function(key, keysize, true, NULL);
	node = ht->table[hash & ht->mask];
	while (node != NULL) {
		if (keysize == node->keysize &&
		    memcmp(key, node->key, keysize) == 0) {
			*valuep = node->value;
			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, uint32_t keysize) {
	isc_ht_node_t *node, *prev;
	uint32_t hash;

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

	prev = NULL;
	hash = isc_hash_function(key, keysize, true, NULL);
	node = ht->table[hash & ht->mask];
	while (node != NULL) {
		if (keysize == node->keysize &&
		    memcmp(key, node->key, keysize) == 0) {
			if (prev == NULL)
				ht->table[hash & ht->mask] = node->next;
			else
				prev->next = node->next;
			isc_mem_put(ht->mctx, node,
				    offsetof(isc_ht_node_t, key) +
				    node->keysize);
			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++;

	if (it->i == it->ht->size)
		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) {
		do {
			it->i++;
		} while (it->i < it->ht->size && it->ht->table[it->i] == NULL);
		if (it->i >= it->ht->size)
			return (ISC_R_NOMORE);
		it->cur = it->ht->table[it->i];
	}

	return (ISC_R_SUCCESS);
}

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;
	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 {
			it->i++;
		} 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, true,
				 NULL);
	node = ht->table[hash & ht->mask];
	while (node != to_delete) {
		prev = node;
		node = node->next;
		INSIST(node != NULL);
	}

	if (prev == NULL)
		ht->table[hash & ht->mask] = node->next;
	else
		prev->next = node->next;
	isc_mem_put(ht->mctx, node,
		    offsetof(isc_ht_node_t, key) + node->keysize);
	ht->count--;

	return (result);
}

void
isc_ht_iter_current(isc_ht_iter_t *it, void **valuep) {
	REQUIRE(it != NULL);
	REQUIRE(it->cur != NULL);
	*valuep = it->cur->value;
}

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;
}

unsigned int
isc_ht_count(isc_ht_t *ht) {
	REQUIRE(ISC_HT_VALID(ht));

	return(ht->count);
}