summaryrefslogtreecommitdiff
path: root/src/libstrongswan/collections/hashtable.c
blob: 1003aa0fa7f6c6ad91228fff009a78436c50cc94 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * Copyright (C) 2008-2012 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */


#include "hashtable.h"

#include <utils/chunk.h>

/** The maximum capacity of the hash table (MUST be a power of 2) */
#define MAX_CAPACITY (1 << 30)

typedef struct pair_t pair_t;

/**
 * This pair holds a pointer to the key and value it represents.
 */
struct pair_t {
	/**
	 * Key of a hash table item.
	 */
	void *key;

	/**
	 * Value of a hash table item.
	 */
	void *value;

	/**
	 * Cached hash (used in case of a resize).
	 */
	u_int hash;

	/**
	 * Next pair in an overflow list.
	 */
	pair_t *next;
};

/**
 * Creates an empty pair object.
 */
static inline pair_t *pair_create(void *key, void *value, u_int hash)
{
	pair_t *this;

	INIT(this,
		.key = key,
		.value = value,
		.hash = hash,
	);

	return this;
}

typedef struct private_hashtable_t private_hashtable_t;

/**
 * Private data of a hashtable_t object.
 *
 */
struct private_hashtable_t {
	/**
	 * Public part of hash table.
	 */
	hashtable_t public;

	/**
	 * The number of items in the hash table.
	 */
	u_int count;

	/**
	 * The current capacity of the hash table (always a power of 2).
	 */
	u_int capacity;

	/**
	 * The current mask to calculate the row index (capacity - 1).
	 */
	u_int mask;

	/**
	 * The load factor.
	 */
	float load_factor;

	/**
	 * The actual table.
	 */
	pair_t **table;

	/**
	 * The hashing function.
	 */
	hashtable_hash_t hash;

	/**
	 * The equality function.
	 */
	hashtable_equals_t equals;
};

typedef struct private_enumerator_t private_enumerator_t;

/**
 * hash table enumerator implementation
 */
struct private_enumerator_t {

	/**
	 * implements enumerator interface
	 */
	enumerator_t enumerator;

	/**
	 * associated hash table
	 */
	private_hashtable_t *table;

	/**
	 * current row index
	 */
	u_int row;

	/**
	 * number of remaining items in hashtable
	 */
	u_int count;

	/**
	 * current pair
	 */
	pair_t *current;

	/**
	 * previous pair (used by remove_at)
	 */
	pair_t *prev;
};

/*
 * See header.
 */
u_int hashtable_hash_ptr(void *key)
{
	return chunk_hash(chunk_from_thing(key));
}

/*
 * See header.
 */
u_int hashtable_hash_str(void *key)
{
	return chunk_hash(chunk_from_str((char*)key));
}

/*
 * See header.
 */
bool hashtable_equals_ptr(void *key, void *other_key)
{
	return key == other_key;
}

/*
 * See header.
 */
bool hashtable_equals_str(void *key, void *other_key)
{
	return streq(key, other_key);
}

/**
 * This function returns the next-highest power of two for the given number.
 * The algorithm works by setting all bits on the right-hand side of the most
 * significant 1 to 1 and then increments the whole number so it rolls over
 * to the nearest power of two. Note: returns 0 for n == 0
 */
static u_int get_nearest_powerof2(u_int n)
{
	u_int i;

	--n;
	for (i = 1; i < sizeof(u_int) * 8; i <<= 1)
	{
		n |= n >> i;
	}
	return ++n;
}

/**
 * Init hash table parameters
 */
static void init_hashtable(private_hashtable_t *this, u_int capacity)
{
	capacity = max(1, min(capacity, MAX_CAPACITY));
	this->capacity = get_nearest_powerof2(capacity);
	this->mask = this->capacity - 1;
	this->load_factor = 0.75;

	this->table = calloc(this->capacity, sizeof(pair_t*));
}

/**
 * Double the size of the hash table and rehash all the elements.
 */
static void rehash(private_hashtable_t *this)
{
	pair_t **old_table;
	u_int row, old_capacity;

	if (this->capacity >= MAX_CAPACITY)
	{
		return;
	}

	old_capacity = this->capacity;
	old_table = this->table;

	init_hashtable(this, old_capacity << 1);

	for (row = 0; row < old_capacity; row++)
	{
		pair_t *pair, *next;
		u_int new_row;

		pair = old_table[row];
		while (pair)
		{	/* insert pair at the front of new bucket*/
			next = pair->next;
			new_row = pair->hash & this->mask;
			pair->next = this->table[new_row];
			this->table[new_row] = pair;
			pair = next;
		}
	}
	free(old_table);
}

METHOD(hashtable_t, put, void*,
	   private_hashtable_t *this, void *key, void *value)
{
	void *old_value = NULL;
	pair_t *pair;
	u_int hash, row;

	hash = this->hash(key);
	row = hash & this->mask;
	pair = this->table[row];
	while (pair)
	{	/* search existing bucket for key */
		if (this->equals(key, pair->key))
		{
			old_value = pair->value;
			pair->value = value;
			pair->key = key;
			break;
		}
		pair = pair->next;
	}
	if (!pair)
	{	/* insert at the front of bucket */
		pair = pair_create(key, value, hash);
		pair->next = this->table[row];
		this->table[row] = pair;
		this->count++;
	}
	if (this->count >= this->capacity * this->load_factor)
	{
		rehash(this);
	}
	return old_value;
}

static void *get_internal(private_hashtable_t *this, void *key,
						  hashtable_equals_t equals)
{
	void *value = NULL;
	pair_t *pair;

	if (!this->count)
	{	/* no need to calculate the hash */
		return NULL;
	}

	pair = this->table[this->hash(key) & this->mask];
	while (pair)
	{
		if (equals(key, pair->key))
		{
			value = pair->value;
			break;
		}
		pair = pair->next;
	}
	return value;
}

METHOD(hashtable_t, get, void*,
	   private_hashtable_t *this, void *key)
{
	return get_internal(this, key, this->equals);
}

METHOD(hashtable_t, get_match, void*,
	   private_hashtable_t *this, void *key, hashtable_equals_t match)
{
	return get_internal(this, key, match);
}

METHOD(hashtable_t, remove_, void*,
	   private_hashtable_t *this, void *key)
{
	void *value = NULL;
	pair_t *pair, *prev = NULL;
	u_int row;

	row = this->hash(key) & this->mask;
	pair = this->table[row];
	while (pair)
	{
		if (this->equals(key, pair->key))
		{
			if (prev)
			{
				prev->next = pair->next;
			}
			else
			{
				this->table[row] = pair->next;
			}
			value = pair->value;
			this->count--;
			free(pair);
			break;
		}
		prev = pair;
		pair = pair->next;
	}
	return value;
}

METHOD(hashtable_t, remove_at, void,
	   private_hashtable_t *this, private_enumerator_t *enumerator)
{
	if (enumerator->table == this && enumerator->current)
	{
		pair_t *current = enumerator->current;
		if (enumerator->prev)
		{
			enumerator->prev->next = current->next;
		}
		else
		{
			this->table[enumerator->row] = current->next;
		}
		enumerator->current = enumerator->prev;
		free(current);
		this->count--;
	}
}

METHOD(hashtable_t, get_count, u_int,
	   private_hashtable_t *this)
{
	return this->count;
}

METHOD(enumerator_t, enumerate, bool,
	   private_enumerator_t *this, void **key, void **value)
{
	while (this->count && this->row < this->table->capacity)
	{
		this->prev = this->current;
		if (this->current)
		{
			this->current = this->current->next;
		}
		else
		{
			this->current = this->table->table[this->row];
		}
		if (this->current)
		{
			if (key)
			{
				*key = this->current->key;
			}
			if (value)
			{
				*value = this->current->value;
			}
			this->count--;
			return TRUE;
		}
		this->row++;
	}
	return FALSE;
}

METHOD(hashtable_t, create_enumerator, enumerator_t*,
	   private_hashtable_t *this)
{
	private_enumerator_t *enumerator;

	INIT(enumerator,
		.enumerator = {
			.enumerate = (void*)_enumerate,
			.destroy = (void*)free,
		},
		.table = this,
		.count = this->count,
	);

	return &enumerator->enumerator;
}

METHOD(hashtable_t, destroy, void,
	   private_hashtable_t *this)
{
	pair_t *pair, *next;
	u_int row;

	for (row = 0; row < this->capacity; row++)
	{
		pair = this->table[row];
		while (pair)
		{
			next = pair->next;
			free(pair);
			pair = next;
		}
	}
	free(this->table);
	free(this);
}

/*
 * Described in header.
 */
hashtable_t *hashtable_create(hashtable_hash_t hash, hashtable_equals_t equals,
							  u_int capacity)
{
	private_hashtable_t *this;

	INIT(this,
		.public = {
			.put = _put,
			.get = _get,
			.get_match = _get_match,
			.remove = _remove_,
			.remove_at = (void*)_remove_at,
			.get_count = _get_count,
			.create_enumerator = _create_enumerator,
			.destroy = _destroy,
		},
		.hash = hash,
		.equals = equals,
	);

	init_hashtable(this, capacity);

	return &this->public;
}