summaryrefslogtreecommitdiff
path: root/src/hash.c
blob: fe6a047fcebe02ab2112de33f87feabe52972429 (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
/*
 * (C) 2006-2009 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 <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

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

	h = (struct hashtable *) calloc(size, 1);
	if (h == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	for (i=0; i<hashsize; i++)
		INIT_LIST_HEAD(&h->members[i]);

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

	return h;
}

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

int hashtable_hash(const struct hashtable *table, const void *data)
{
	return table->hash(data, table);
}

struct hashtable_node *
hashtable_find(const struct hashtable *table, const void *data, int id)
{
	struct list_head *e;
	struct hashtable_node *n;

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

int hashtable_add(struct hashtable *table, struct hashtable_node *n, int id)
{
	/* hash table is full */
	if (table->count >= table->limit) {
		errno = ENOSPC;
		return -1;
	}
	list_add(&n->head, &table->members[id]);
	table->count++;
	return 0;
}

void hashtable_del(struct hashtable *table, struct hashtable_node *n)
{
	list_del(&n->head);
	table->count--;
}

int hashtable_flush(struct hashtable *table)
{
	uint32_t i;
	struct list_head *e, *tmp;
	struct hashtable_node *n;

	for (i=0; i < table->hashsize; i++) {
		list_for_each_safe(e, tmp, &table->members[i]) {
			n = list_entry(e, struct hashtable_node, head);
			free(n);
		}
	}
	return 0;
}

int
hashtable_iterate_limit(struct hashtable *table, void *data,
			uint32_t from, uint32_t steps,
		        int (*iterate)(void *data1, void *n))
{
	uint32_t i;
	struct list_head *e, *tmp;
	struct hashtable_node *n;

	for (i=from; i < table->hashsize && i < from+steps; i++) {
		list_for_each_safe(e, tmp, &table->members[i]) {
			n = list_entry(e, struct hashtable_node, head);
			if (iterate(data, n) == -1)
				return -1;
		}
	}
	return i;
}

int hashtable_iterate(struct hashtable *table, void *data,
		      int (*iterate)(void *data1, void *n))
{
	return hashtable_iterate_limit(table, data, 0, UINT_MAX, iterate);
}

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