summaryrefslogtreecommitdiff
path: root/src/utils/hash.c
blob: aa01137828ea2402327247411a20479d5c94381c (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
/*
 * This file is part of LibParserUtils.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <stdio.h>
#include <string.h>

#include <parserutils/utils/hash.h>

#include "utils/chunkarray.h"

typedef struct slot_table slot_table;

struct parserutils_hash {
	slot_table *slots;

	parserutils_chunkarray *data;

	parserutils_alloc alloc;
	void *pw;
};

struct slot_table {
#define DEFAULT_SLOTS (1<<6)
	size_t n_slots;
	size_t n_used;

	const parserutils_chunkarray_entry *slots[];
};

static inline uint32_t _hash(const uint8_t *data, size_t len);
static inline int _cmp(const uint8_t *a, size_t alen, 
		const uint8_t *b, size_t blen);
static inline void grow_slots(parserutils_hash *hash);

/**
 * Create a hash
 *
 * \param alloc  Memory (de)allocation function
 * \param pw     Pointer to client-specific private data
 * \param hash   Pointer to location to receive result
 * \return PARSERUTILS_OK on success, appropriate error otherwise
 */
parserutils_error parserutils_hash_create(parserutils_alloc alloc, void *pw,
		parserutils_hash **hash)
{
	parserutils_hash *h;
	parserutils_error error;

	if (alloc == NULL || hash == NULL)
		return PARSERUTILS_BADPARM;

	h = alloc(0, sizeof(parserutils_hash), pw);
	if (h == NULL)
		return PARSERUTILS_NOMEM;

	h->slots = alloc(0, sizeof(slot_table) + 
			DEFAULT_SLOTS * sizeof(parserutils_chunkarray_entry *),
			pw);
	if (h->slots == NULL) {
		alloc(h, 0, pw);
		return PARSERUTILS_NOMEM;
	}

	memset(h->slots, 0, sizeof(slot_table) + 
			DEFAULT_SLOTS * sizeof(parserutils_chunkarray_entry *));
	h->slots->n_slots = DEFAULT_SLOTS;

	error = parserutils_chunkarray_create(alloc, pw, &h->data);
	if (error != PARSERUTILS_OK) {
		alloc(h->slots, 0, pw);
		alloc(h, 0, pw);
		return error;
	}

	h->alloc = alloc;
	h->pw = pw;

	*hash = h;

	return PARSERUTILS_OK;
}

/**
 * Destroy a hash
 *
 * \param hash  The hash to destroy
 * \return PARSERUTILS_OK on success, appropriate error otherwise
 */
parserutils_error parserutils_hash_destroy(parserutils_hash *hash)
{
	if (hash == NULL)
		return PARSERUTILS_BADPARM;

	parserutils_chunkarray_destroy(hash->data);

	hash->alloc(hash->slots, 0, hash->pw);

	hash->alloc(hash, 0, hash->pw);

	return PARSERUTILS_OK;
}

/**
 * Insert an item into a hash
 *
 * \param hash      The hash to insert into
 * \param data      Pointer to data
 * \param len       Length, in bytes, of data
 * \param inserted  Pointer to location to receive pointer to data in hash
 * \return PARSERUTILS_OK on success, appropriate error otherwise
 */
parserutils_error parserutils_hash_insert(parserutils_hash *hash,
		const uint8_t *data, uint16_t len,
		const parserutils_hash_entry **inserted)
{
	uint32_t index, mask;
	slot_table *slots;
	const parserutils_chunkarray_entry **entries;
	const parserutils_chunkarray_entry *entry;
	parserutils_error error;

	if (hash == NULL || data == NULL || inserted == NULL)
		return PARSERUTILS_BADPARM;

	slots = hash->slots;
	entries = slots->slots;

	/* Find index */
	mask = slots->n_slots - 1;
	index = _hash(data, len) & mask;

	/* Use linear probing to resolve collisions */
	while ((entry = entries[index]) != NULL) {
		/* If this data is already in the hash, return it */
		if (_cmp(data, len, entry->data, entry->length) == 0) {
			(*inserted) = (const parserutils_hash_entry *) entry;
			return PARSERUTILS_OK;
		}

		index = (index + 1) & mask;
	}

	/* The data isn't in the hash. Index identifies the slot to use */
	error = parserutils_chunkarray_insert(hash->data, data, len, &entry);
	if (error != PARSERUTILS_OK)
		return error;

	entries[index] = entry;
	(*inserted) = (const parserutils_hash_entry *) entry;

	/* If the table is 75% full, then increase its size */
	if (++slots->n_used >= (slots->n_slots >> 1) + (slots->n_slots >> 2))
		grow_slots(hash);

	return PARSERUTILS_OK;
}

/******************************************************************************
 * Private functions                                                          *
 ******************************************************************************/

/**
 * Hsieh hash function
 *
 * \param data  Pointer to data to hash
 * \param len   Length, in bytes, of data
 * \return hash value
 */
uint32_t _hash(const uint8_t *data, size_t len)
{
	uint32_t hash = len, tmp;
	int rem = len & 3;

#define read16(d) ((((uint32_t)((d)[1])) << 8) | ((uint32_t)((d)[0])))

	for (len = len >> 2; len > 0; len--) {
		hash += read16(data);
		tmp   = (read16(data + 2) << 11) ^ hash;
		hash  = (hash << 16) ^ tmp;
		data += 4;
		hash += hash >> 11;
	}

	switch (rem) {
	case 3:
		hash += read16(data);
		hash ^= hash << 16;
		hash ^= data[2] << 18;
		hash += hash >> 11;
		break;
	case 2:
		hash += read16(data);
		hash ^= hash << 11;
		hash += hash >> 17;
		break;
	case 1:
		hash += data[0];
		hash ^= hash << 10;
		hash += hash >> 1;
	}

#undef read16

	hash ^= hash << 3;
	hash += hash >> 5;
	hash ^= hash << 4;
	hash += hash >> 17;
	hash ^= hash << 25;
	hash += hash >> 6;

	return hash;
}

/**
 * Comparator for hash entries
 *
 * \param a     First item to consider 
 * \param alen  Length, in bytes, of a
 * \param b     Second item to consider
 * \param blen  Length, in bytes, of b
 * \return <0 iff a<b, ==0 iff a=b, >0 iff a>b
 */
int _cmp(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
{
	int result;

	/* Check for identity first */
	if (a == b)
		return 0;

	/* Sort first by length, then by data equality */
	if ((result = alen - blen) == 0)
		result = memcmp(a, b, alen);

	return result;
}

/**
 * Increase the size of the slot table
 *
 * \param hash  The hash to process
 */
void grow_slots(parserutils_hash *hash)
{
	slot_table *s;
	size_t new_size;

	if (hash == NULL)
		return;

	new_size = hash->slots->n_slots << 1;

	/* Create new slot table */
	s = hash->alloc(0, sizeof(slot_table) + 
			new_size * sizeof(parserutils_chunkarray_entry *),
			hash->pw);
	if (s == NULL)
		return;

	memset(s, 0, sizeof(slot_table) + 
			new_size * sizeof(parserutils_chunkarray_entry *));
	s->n_slots = new_size;

	/* Now, populate it with the contents of the current one */
	for (uint32_t i = 0; i < hash->slots->n_slots; i++) {
		const parserutils_chunkarray_entry *e = hash->slots->slots[i];

		if (e == NULL)
			continue;

		/* Find new index */
		uint32_t mask = s->n_slots - 1;
		uint32_t index = _hash(e->data, e->length) & mask;

		/* Use linear probing to resolve collisions */
		while (s->slots[index] != NULL)
			index = (index + 1) & mask;

		/* Insert into new slot table */
		s->slots[index] = e;
		s->n_used++;
	}

	/* Destroy current table, and replace it with the new one */
	hash->alloc(hash->slots, 0, hash->pw);
	hash->slots = s;

	return;
}

extern void parserutils_chunkarray_dump(parserutils_chunkarray *array);
extern void parserutils_hash_dump(parserutils_hash *hash);

/**
 * Dump details of a hash to stdout
 *
 * \param hash  The hash to dump
 */
void parserutils_hash_dump(parserutils_hash *hash)
{
	printf("%zu slots used (of %zu => %f%%)\n", hash->slots->n_used,
		hash->slots->n_slots, 
		hash->slots->n_used * 100 / (float) hash->slots->n_slots);

	printf("Data:\n");
	parserutils_chunkarray_dump(hash->data);

	printf("Hash structures: %zu\n", 
		sizeof(parserutils_hash) + sizeof(slot_table) + 
		hash->slots->n_slots * sizeof(parserutils_chunkarray_entry *));
}