aboutsummaryrefslogtreecommitdiff
path: root/contrib/lib9p/hashtable.c
blob: d6558eb655989b621302ff3b97721bd8aa6995da (plain) (blame)
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
/*
 * Copyright 2016 Jakub Klama <jceel@FreeBSD.org>
 * All rights reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted providing that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/queue.h>
#include "lib9p_impl.h"
#include "hashtable.h"

static struct ht_item *ht_iter_advance(struct ht_iter *, struct ht_item *);

void
ht_init(struct ht *h, ssize_t size)
{
	ssize_t i;

	memset(h, 0, sizeof(struct ht));
	h->ht_nentries = size;
	h->ht_entries = l9p_calloc((size_t)size, sizeof(struct ht_entry));
	pthread_rwlock_init(&h->ht_rwlock, NULL);

	for (i = 0; i < size; i++)
		TAILQ_INIT(&h->ht_entries[i].hte_items);
}

void
ht_destroy(struct ht *h)
{
	struct ht_entry *he;
	struct ht_item *item, *tmp;
	ssize_t i;

	for (i = 0; i < h->ht_nentries; i++) {
		he = &h->ht_entries[i];
		TAILQ_FOREACH_SAFE(item, &he->hte_items, hti_link, tmp) {
			free(item);
		}
	}

	pthread_rwlock_destroy(&h->ht_rwlock);
	free(h->ht_entries);
	h->ht_entries = NULL;
}

void *
ht_find(struct ht *h, uint32_t hash)
{
	void *result;

	ht_rdlock(h);
	result = ht_find_locked(h, hash);
	ht_unlock(h);
	return (result);
}

void *
ht_find_locked(struct ht *h, uint32_t hash)
{
	struct ht_entry *entry;
	struct ht_item *item;

	entry = &h->ht_entries[hash % h->ht_nentries];

	TAILQ_FOREACH(item, &entry->hte_items, hti_link) {
		if (item->hti_hash == hash)
			return (item->hti_data);
	}

	return (NULL);
}

int
ht_add(struct ht *h, uint32_t hash, void *value)
{
	struct ht_entry *entry;
	struct ht_item *item;

	ht_wrlock(h);
	entry = &h->ht_entries[hash % h->ht_nentries];

	TAILQ_FOREACH(item, &entry->hte_items, hti_link) {
		if (item->hti_hash == hash) {
			errno = EEXIST;
			ht_unlock(h);
			return (-1);
		}
	}

	item = l9p_calloc(1, sizeof(struct ht_item));
	item->hti_hash = hash;
	item->hti_data = value;
	TAILQ_INSERT_TAIL(&entry->hte_items, item, hti_link);
	ht_unlock(h);

	return (0);
}

int
ht_remove(struct ht *h, uint32_t hash)
{
	int result;

	ht_wrlock(h);
	result = ht_remove_locked(h, hash);
	ht_unlock(h);
	return (result);
}

int
ht_remove_locked(struct ht *h, uint32_t hash)
{
	struct ht_entry *entry;
	struct ht_item *item, *tmp;
	ssize_t slot = hash % h->ht_nentries;

	entry = &h->ht_entries[slot];

	TAILQ_FOREACH_SAFE(item, &entry->hte_items, hti_link, tmp) {
		if (item->hti_hash == hash) {
			TAILQ_REMOVE(&entry->hte_items, item, hti_link);
			free(item);
			return (0);
		}
	}

	errno = ENOENT;
	return (-1);
}

/*
 * Inner workings for advancing the iterator.
 *
 * If we have a current item, that tells us how to find the
 * next item.  If not, we get the first item from the next
 * slot (well, the next slot with an item); in any case, we
 * record the new slot and return the next item.
 *
 * For bootstrapping, iter->htit_slot can be -1 to start
 * searching at slot 0.
 *
 * Caller must hold a lock on the table.
 */
static struct ht_item *
ht_iter_advance(struct ht_iter *iter, struct ht_item *cur)
{
	struct ht_item *next;
	struct ht *h;
	ssize_t slot;

	h = iter->htit_parent;

	if (cur == NULL)
		next = NULL;
	else
		next = TAILQ_NEXT(cur, hti_link);

	if (next == NULL) {
		slot = iter->htit_slot;
		while (++slot < h->ht_nentries) {
			next = TAILQ_FIRST(&h->ht_entries[slot].hte_items);
			if (next != NULL)
				break;
		}
		iter->htit_slot = slot;
	}
	return (next);
}

/*
 * Remove the current item - there must be one, or this is an
 * error.  This (necessarily) pre-locates the next item, so callers
 * must not use it on an actively-changing table.
 */
int
ht_remove_at_iter(struct ht_iter *iter)
{
	struct ht_item *item;
	struct ht *h;
	ssize_t slot;

	assert(iter != NULL);

	if ((item = iter->htit_curr) == NULL) {
		errno = EINVAL;
		return (-1);
	}

	/* remove the item from the table, saving the NEXT one */
	h = iter->htit_parent;
	ht_wrlock(h);
	slot = iter->htit_slot;
	iter->htit_next = ht_iter_advance(iter, item);
	TAILQ_REMOVE(&h->ht_entries[slot].hte_items, item, hti_link);
	ht_unlock(h);

	/* mark us as no longer on an item, then free it */
	iter->htit_curr = NULL;
	free(item);

	return (0);
}

/*
 * Initialize iterator.  Subsequent ht_next calls will find the
 * first item, then the next, and so on.  Callers should in general
 * not use this on actively-changing tables, though we do our best
 * to make it semi-sensible.
 */
void
ht_iter(struct ht *h, struct ht_iter *iter)
{

	iter->htit_parent = h;
	iter->htit_curr = NULL;
	iter->htit_next = NULL;
	iter->htit_slot = -1;	/* which will increment to 0 */
}

/*
 * Return the next item, which is the first item if we have not
 * yet been called on this iterator, or the next item if we have.
 */
void *
ht_next(struct ht_iter *iter)
{
	struct ht_item *item;
	struct ht *h;

	if ((item = iter->htit_next) == NULL) {
		/* no pre-loaded next; find next from current */
		h = iter->htit_parent;
		ht_rdlock(h);
		item = ht_iter_advance(iter, iter->htit_curr);
		ht_unlock(h);
	} else
		iter->htit_next = NULL;
	iter->htit_curr = item;
	return (item == NULL ? NULL : item->hti_data);
}