summaryrefslogtreecommitdiffstats
path: root/src/hash.c
blob: cf64df445748cfe51851f379e53809cbc4374f09 (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
/*
 * (C) 2006-2007 by Pablo Neira Ayuso <pablo@netfilter.org>
 * 
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Description: generic hash table implementation
 */

#include "hash.h"
#include "slist.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>

struct hashtable_node *hashtable_alloc_node(int datasize, void *data)
{
	struct hashtable_node *n;
	int size = sizeof(struct hashtable_node) + datasize;

	n = malloc(size);
	if (!n)
		return NULL;
	memset(n, 0, size);
	memcpy(n->data, data, datasize);

	return n;
}

void hashtable_destroy_node(struct hashtable_node *h)
{
	free(h);
}

struct hashtable *
hashtable_create(int hashsize, int limit, int datasize,
		 uint32_t (*hash)(const void *data, struct hashtable *table),
		 int (*compare)(const void *data1, const void *data2))
{
	int i;
	struct hashtable *h;
	int size = sizeof(struct hashtable)
		   + hashsize * sizeof(struct slist_head);

	h = (struct hashtable *) malloc(size);
	if (!h) {
		errno = ENOMEM;
		return NULL;
	}

	memset(h, 0, size);
	for (i=0; i<hashsize; i++)
		INIT_SLIST_HEAD(h->members[i]);

	h->hashsize = hashsize;
	h->limit = limit;
	h->datasize = datasize;
	h->hash = hash;
	h->compare = compare;

	return h;
}

void hashtable_destroy(struct hashtable *h)
{
	hashtable_flush(h);
	free(h);
}

void *hashtable_add(struct hashtable *table, void *data)
{
	struct slist_head *e;
	struct hashtable_node *n;
	uint32_t id;

	/* hash table is full */
	if (table->count >= table->limit) {
		errno = ENOSPC;
		return NULL;
	}

	id = table->hash(data, table);

	slist_for_each(e, &table->members[id]) {
		n = slist_entry(e, struct hashtable_node, head);
		if (table->compare(n->data, data)) {
			errno = EEXIST;
			return NULL;
		}
	}

	n = hashtable_alloc_node(table->datasize, data);
	if (n == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	slist_add(&table->members[id], &n->head);
	table->count++;

	return n->data;
}

void *hashtable_test(struct hashtable *table, const void *data)
{
	struct slist_head *e;
	uint32_t id;
	struct hashtable_node *n;

	id = table->hash(data, table);

	slist_for_each(e, &table->members[id]) {
		n = slist_entry(e, struct hashtable_node, head);
		if (table->compare(n->data, data))
			return n->data;
	}

	errno = ENOENT;
	return NULL;
}

int hashtable_del(struct hashtable *table, void *data)
{
	struct slist_head *e, *next, *prev;
	uint32_t id;
	struct hashtable_node *n;

	id = table->hash(data, table);

	slist_for_each_safe(e, prev, next, &table->members[id]) {
		n = slist_entry(e, struct hashtable_node, head);
		if (table->compare(n->data, data)) {
			slist_del(e, prev);
			hashtable_destroy_node(n);
			table->count--;
			return 0;
		}
	}
	errno = ENOENT;
	return -1;
}

int hashtable_flush(struct hashtable *table)
{
	uint32_t i;
	struct slist_head *e, *next, *prev;
	struct hashtable_node *n;

	for (i=0; i < table->hashsize; i++)
		slist_for_each_safe(e, prev, next, &table->members[i]) {
			n = slist_entry(e, struct hashtable_node, head);
			slist_del(e, prev);
			hashtable_destroy_node(n);
		}

	table->count = 0;
	
	return 0;
}

int hashtable_iterate(struct hashtable *table, void *data,
		      int (*iterate)(void *data1, void *data2))
{
	uint32_t i;
	struct slist_head *e, *next, *prev;
	struct hashtable_node *n;

	for (i=0; i < table->hashsize; i++) {
		slist_for_each_safe(e, prev, next, &table->members[i]) {
			n = slist_entry(e, struct hashtable_node, head);
			if (iterate(data, n->data) == -1)
				return -1;
		}
	}
	return 0;
}

unsigned int hashtable_counter(const struct hashtable *table)
{
	return table->count;
}