2008-12-03 09:32:16 +00:00
|
|
|
/*
|
2020-04-24 06:30:03 +00:00
|
|
|
* Copyright (C) 2008-2020 Tobias Brunner
|
2018-05-23 14:04:50 +00:00
|
|
|
* HSR Hochschule fuer Technik Rapperswil
|
2008-12-03 09:32:16 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2013-05-01 10:13:28 +00:00
|
|
|
#include <utils/chunk.h>
|
2020-04-24 08:56:50 +00:00
|
|
|
#include <utils/debug.h>
|
|
|
|
#ifdef HASHTABLE_PROFILER
|
|
|
|
#include <utils/backtrace.h>
|
|
|
|
#endif
|
2013-05-01 10:13:28 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
/** The minimum size of the hash table (MUST be a power of 2) */
|
|
|
|
#define MIN_SIZE 8
|
|
|
|
/** The maximum size of the hash table (MUST be a power of 2) */
|
|
|
|
#define MAX_SIZE (1 << 30)
|
2020-04-24 06:30:03 +00:00
|
|
|
/** Maximum load factor before the hash table is resized */
|
|
|
|
#define LOAD_FACTOR 0.75f
|
2008-12-03 09:32:16 +00:00
|
|
|
|
|
|
|
typedef struct pair_t pair_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This pair holds a pointer to the key and value it represents.
|
|
|
|
*/
|
|
|
|
struct pair_t {
|
2020-04-24 06:30:03 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* Key of a hash table item.
|
|
|
|
*/
|
2014-03-27 10:57:54 +00:00
|
|
|
const void *key;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* Value of a hash table item.
|
|
|
|
*/
|
|
|
|
void *value;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* Cached hash (used in case of a resize).
|
|
|
|
*/
|
|
|
|
u_int hash;
|
2011-05-23 16:35:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Next pair in an overflow list.
|
|
|
|
*/
|
|
|
|
pair_t *next;
|
2008-12-03 09:32:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an empty pair object.
|
|
|
|
*/
|
2014-03-27 10:57:54 +00:00
|
|
|
static inline pair_t *pair_create(const void *key, void *value, u_int hash)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2010-06-07 13:50:41 +00:00
|
|
|
pair_t *this;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-06-07 13:50:41 +00:00
|
|
|
INIT(this,
|
|
|
|
.key = key,
|
|
|
|
.value = value,
|
|
|
|
.hash = hash,
|
|
|
|
);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct private_hashtable_t private_hashtable_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private data of a hashtable_t object.
|
|
|
|
*/
|
|
|
|
struct private_hashtable_t {
|
2020-04-24 06:30:03 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* Public part of hash table.
|
|
|
|
*/
|
|
|
|
hashtable_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
2009-09-04 11:46:09 +00:00
|
|
|
* The number of items in the hash table.
|
2008-12-03 09:32:16 +00:00
|
|
|
*/
|
|
|
|
u_int count;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
2020-04-24 06:50:24 +00:00
|
|
|
* The current size of the hash table (always a power of 2).
|
2008-12-03 09:32:16 +00:00
|
|
|
*/
|
2020-04-24 06:50:24 +00:00
|
|
|
u_int size;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
2020-04-24 06:50:24 +00:00
|
|
|
* The current mask to calculate the row index (size - 1).
|
2008-12-03 09:32:16 +00:00
|
|
|
*/
|
|
|
|
u_int mask;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* The actual table.
|
|
|
|
*/
|
2011-05-23 16:35:21 +00:00
|
|
|
pair_t **table;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* The hashing function.
|
|
|
|
*/
|
|
|
|
hashtable_hash_t hash;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* The equality function.
|
|
|
|
*/
|
|
|
|
hashtable_equals_t equals;
|
2020-04-24 06:50:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alternative comparison function.
|
|
|
|
*/
|
|
|
|
hashtable_cmp_t cmp;
|
2020-04-24 08:56:50 +00:00
|
|
|
|
|
|
|
#ifdef HASHTABLE_PROFILER
|
|
|
|
/**
|
|
|
|
* Some stats to profile lookups in the table
|
|
|
|
*/
|
|
|
|
struct {
|
|
|
|
size_t count;
|
|
|
|
size_t probes;
|
|
|
|
size_t longest;
|
|
|
|
} success, failure;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stats on the memory usage of the table
|
|
|
|
*/
|
|
|
|
struct {
|
|
|
|
size_t count;
|
|
|
|
size_t size;
|
|
|
|
} max;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep track of where the hash table was created
|
|
|
|
*/
|
|
|
|
backtrace_t *backtrace;
|
|
|
|
#endif
|
2008-12-03 09:32:16 +00:00
|
|
|
};
|
|
|
|
|
2020-04-24 11:41:53 +00:00
|
|
|
typedef struct private_hashlist_t private_hashlist_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private data of a hashlist_t object.
|
|
|
|
*/
|
|
|
|
struct private_hashlist_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public part of hash table.
|
|
|
|
*/
|
|
|
|
hashlist_t public;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inherited private part of hash table (we get the public part too, but
|
|
|
|
* ignore it).
|
|
|
|
*/
|
|
|
|
private_hashtable_t super;
|
|
|
|
};
|
|
|
|
|
2020-04-24 08:56:50 +00:00
|
|
|
#ifdef HASHTABLE_PROFILER
|
|
|
|
|
|
|
|
#define lookup_start() \
|
|
|
|
u_int _lookup_probes = 0;
|
|
|
|
|
|
|
|
#define lookup_probing() \
|
|
|
|
_lookup_probes++;
|
|
|
|
|
|
|
|
#define _lookup_done(table, result) \
|
|
|
|
table->result.count++; \
|
|
|
|
table->result.probes += _lookup_probes; \
|
|
|
|
table->result.longest = max(table->result.longest, _lookup_probes);
|
|
|
|
|
|
|
|
#define lookup_success(table) _lookup_done(table, success);
|
|
|
|
#define lookup_failure(table) _lookup_done(table, failure);
|
|
|
|
|
|
|
|
#define profile_size(table) \
|
|
|
|
table->max.size = max(table->max.size, table->size);
|
|
|
|
#define profile_count(table) \
|
|
|
|
table->max.count = max(table->max.count, table->count);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define lookup_start(...) {}
|
|
|
|
#define lookup_probing(...) {}
|
|
|
|
#define lookup_success(...) {}
|
|
|
|
#define lookup_failure(...) {}
|
|
|
|
#define profile_size(...) {}
|
|
|
|
#define profile_count(...) {}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
typedef struct private_enumerator_t private_enumerator_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hash table enumerator implementation
|
|
|
|
*/
|
|
|
|
struct private_enumerator_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* implements enumerator interface
|
|
|
|
*/
|
|
|
|
enumerator_t enumerator;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* associated hash table
|
|
|
|
*/
|
|
|
|
private_hashtable_t *table;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* current row index
|
|
|
|
*/
|
|
|
|
u_int row;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-05-23 16:45:49 +00:00
|
|
|
/**
|
|
|
|
* number of remaining items in hashtable
|
|
|
|
*/
|
|
|
|
u_int count;
|
|
|
|
|
2010-06-07 14:36:26 +00:00
|
|
|
/**
|
|
|
|
* current pair
|
|
|
|
*/
|
2011-05-23 16:35:21 +00:00
|
|
|
pair_t *current;
|
2010-06-07 14:36:26 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
2011-05-23 16:35:21 +00:00
|
|
|
* previous pair (used by remove_at)
|
2008-12-03 09:32:16 +00:00
|
|
|
*/
|
2011-05-23 16:35:21 +00:00
|
|
|
pair_t *prev;
|
|
|
|
};
|
2008-12-03 09:32:16 +00:00
|
|
|
|
2013-05-01 10:13:28 +00:00
|
|
|
/*
|
|
|
|
* See header.
|
|
|
|
*/
|
2014-03-27 10:57:54 +00:00
|
|
|
u_int hashtable_hash_ptr(const void *key)
|
2013-05-01 10:13:28 +00:00
|
|
|
{
|
|
|
|
return chunk_hash(chunk_from_thing(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See header.
|
|
|
|
*/
|
2014-03-27 10:57:54 +00:00
|
|
|
u_int hashtable_hash_str(const void *key)
|
2013-05-01 10:13:28 +00:00
|
|
|
{
|
|
|
|
return chunk_hash(chunk_from_str((char*)key));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See header.
|
|
|
|
*/
|
2014-03-27 10:57:54 +00:00
|
|
|
bool hashtable_equals_ptr(const void *key, const void *other_key)
|
2013-05-01 10:13:28 +00:00
|
|
|
{
|
|
|
|
return key == other_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See header.
|
|
|
|
*/
|
2014-03-27 10:57:54 +00:00
|
|
|
bool hashtable_equals_str(const void *key, const void *other_key)
|
2013-05-01 10:13:28 +00:00
|
|
|
{
|
|
|
|
return streq(key, other_key);
|
|
|
|
}
|
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2009-10-26 15:08:14 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
--n;
|
2008-12-04 16:33:39 +00:00
|
|
|
for (i = 1; i < sizeof(u_int) * 8; i <<= 1)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
|
|
|
n |= n >> i;
|
|
|
|
}
|
|
|
|
return ++n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init hash table parameters
|
|
|
|
*/
|
2020-04-24 06:50:24 +00:00
|
|
|
static void init_hashtable(private_hashtable_t *this, u_int size)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:50:24 +00:00
|
|
|
size = max(MIN_SIZE, min(size, MAX_SIZE));
|
|
|
|
this->size = get_nearest_powerof2(size);
|
|
|
|
this->mask = this->size - 1;
|
2020-04-24 08:56:50 +00:00
|
|
|
profile_size(this);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
this->table = calloc(this->size, sizeof(pair_t*));
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Double the size of the hash table and rehash all the elements.
|
|
|
|
*/
|
|
|
|
static void rehash(private_hashtable_t *this)
|
|
|
|
{
|
2020-04-24 06:30:03 +00:00
|
|
|
pair_t **old_table, *to_move, *pair, *next;
|
2020-04-24 06:50:24 +00:00
|
|
|
u_int row, new_row, old_size;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
if (this->size >= MAX_SIZE)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
old_size = this->size;
|
2009-10-26 15:08:14 +00:00
|
|
|
old_table = this->table;
|
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
init_hashtable(this, old_size << 1);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
for (row = 0; row < old_size; row++)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:30:03 +00:00
|
|
|
to_move = old_table[row];
|
|
|
|
while (to_move)
|
|
|
|
{
|
2020-04-24 06:50:24 +00:00
|
|
|
pair_t *prev = NULL;
|
|
|
|
|
2020-04-24 06:30:03 +00:00
|
|
|
new_row = to_move->hash & this->mask;
|
|
|
|
pair = this->table[new_row];
|
2020-04-24 06:50:24 +00:00
|
|
|
while (pair)
|
2020-04-24 06:30:03 +00:00
|
|
|
{
|
2020-04-24 06:50:24 +00:00
|
|
|
if (this->cmp && this->cmp(to_move->key, pair->key) < 0)
|
2020-04-24 06:30:03 +00:00
|
|
|
{
|
2020-04-24 06:50:24 +00:00
|
|
|
break;
|
2020-04-24 06:30:03 +00:00
|
|
|
}
|
2020-04-24 06:50:24 +00:00
|
|
|
prev = pair;
|
|
|
|
pair = pair->next;
|
|
|
|
}
|
|
|
|
next = to_move->next;
|
|
|
|
to_move->next = NULL;
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
to_move->next = prev->next;
|
|
|
|
prev->next = to_move;
|
2020-04-24 06:30:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-24 06:50:24 +00:00
|
|
|
to_move->next = this->table[new_row];
|
2020-04-24 06:30:03 +00:00
|
|
|
this->table[new_row] = to_move;
|
|
|
|
}
|
|
|
|
to_move = next;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(old_table);
|
|
|
|
}
|
|
|
|
|
2020-04-24 06:30:03 +00:00
|
|
|
/**
|
|
|
|
* Find the pair with the given key, optionally returning the hash and previous
|
|
|
|
* (or last) pair in the bucket.
|
|
|
|
*/
|
|
|
|
static inline pair_t *find_key(private_hashtable_t *this, const void *key,
|
|
|
|
hashtable_equals_t equals, u_int *out_hash,
|
|
|
|
pair_t **out_prev)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:30:03 +00:00
|
|
|
pair_t *pair, *prev = NULL;
|
2020-04-24 06:50:24 +00:00
|
|
|
bool use_callback = equals != NULL;
|
2020-04-24 06:30:03 +00:00
|
|
|
u_int hash;
|
|
|
|
|
|
|
|
if (!this->count && !out_hash)
|
|
|
|
{ /* no need to calculate the hash if not requested */
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
equals = equals ?: this->equals;
|
2009-10-26 15:08:14 +00:00
|
|
|
hash = this->hash(key);
|
2020-04-24 06:30:03 +00:00
|
|
|
if (out_hash)
|
|
|
|
{
|
|
|
|
*out_hash = hash;
|
|
|
|
}
|
|
|
|
|
2020-04-24 08:56:50 +00:00
|
|
|
lookup_start();
|
|
|
|
|
2020-04-24 06:30:03 +00:00
|
|
|
pair = this->table[hash & this->mask];
|
2011-05-23 16:35:21 +00:00
|
|
|
while (pair)
|
2020-04-24 06:30:03 +00:00
|
|
|
{
|
2020-04-24 08:56:50 +00:00
|
|
|
lookup_probing();
|
2020-04-24 11:41:53 +00:00
|
|
|
/* when keys are sorted, we compare all items so we can abort earlier
|
2020-04-24 06:50:24 +00:00
|
|
|
* even if the hash does not match, but only as long as we don't
|
|
|
|
* have a callback */
|
|
|
|
if (!use_callback && this->cmp)
|
|
|
|
{
|
|
|
|
int cmp = this->cmp(key, pair->key);
|
|
|
|
if (cmp == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (cmp < 0)
|
|
|
|
{ /* no need to continue as the key we search is smaller */
|
|
|
|
pair = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (hash == pair->hash && equals(key, pair->key))
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
break;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2020-04-24 06:30:03 +00:00
|
|
|
prev = pair;
|
2011-05-23 16:35:21 +00:00
|
|
|
pair = pair->next;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2020-04-24 06:30:03 +00:00
|
|
|
if (out_prev)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:30:03 +00:00
|
|
|
*out_prev = prev;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2020-04-24 08:56:50 +00:00
|
|
|
if (pair)
|
|
|
|
{
|
|
|
|
lookup_success(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lookup_failure(this);
|
|
|
|
}
|
2020-04-24 06:30:03 +00:00
|
|
|
return pair;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:30:03 +00:00
|
|
|
METHOD(hashtable_t, put, void*,
|
|
|
|
private_hashtable_t *this, const void *key, void *value)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:30:03 +00:00
|
|
|
void *old_value = NULL;
|
|
|
|
pair_t *pair, *prev = NULL;
|
|
|
|
u_int hash;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
if (this->count >= this->size * LOAD_FACTOR)
|
2020-04-24 06:30:03 +00:00
|
|
|
{
|
|
|
|
rehash(this);
|
2012-09-21 06:49:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
pair = find_key(this, key, NULL, &hash, &prev);
|
2020-04-24 06:30:03 +00:00
|
|
|
if (pair)
|
|
|
|
{
|
|
|
|
old_value = pair->value;
|
|
|
|
pair->value = value;
|
|
|
|
pair->key = key;
|
|
|
|
}
|
|
|
|
else
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:30:03 +00:00
|
|
|
pair = pair_create(key, value, hash);
|
|
|
|
if (prev)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:50:24 +00:00
|
|
|
pair->next = prev->next;
|
2020-04-24 06:30:03 +00:00
|
|
|
prev->next = pair;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2020-04-24 06:30:03 +00:00
|
|
|
else
|
|
|
|
{
|
2020-04-24 06:50:24 +00:00
|
|
|
pair->next = this->table[hash & this->mask];
|
2020-04-24 06:30:03 +00:00
|
|
|
this->table[hash & this->mask] = pair;
|
|
|
|
}
|
|
|
|
this->count++;
|
2020-04-24 08:56:50 +00:00
|
|
|
profile_count(this);
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2020-04-24 06:30:03 +00:00
|
|
|
return old_value;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:30:03 +00:00
|
|
|
|
2012-05-24 10:54:38 +00:00
|
|
|
METHOD(hashtable_t, get, void*,
|
2013-08-27 14:37:41 +00:00
|
|
|
private_hashtable_t *this, const void *key)
|
2012-05-24 10:54:38 +00:00
|
|
|
{
|
2020-04-24 06:50:24 +00:00
|
|
|
pair_t *pair = find_key(this, key, NULL, NULL, NULL);
|
2020-04-24 06:30:03 +00:00
|
|
|
return pair ? pair->value : NULL;
|
2012-05-24 10:54:38 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 13:50:41 +00:00
|
|
|
METHOD(hashtable_t, remove_, void*,
|
2013-08-27 14:37:41 +00:00
|
|
|
private_hashtable_t *this, const void *key)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
|
|
|
void *value = NULL;
|
2011-05-23 16:35:21 +00:00
|
|
|
pair_t *pair, *prev = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
pair = find_key(this, key, NULL, NULL, &prev);
|
2020-04-24 06:30:03 +00:00
|
|
|
if (pair)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:30:03 +00:00
|
|
|
if (prev)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2020-04-24 06:30:03 +00:00
|
|
|
prev->next = pair->next;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2020-04-24 06:30:03 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this->table[pair->hash & this->mask] = pair->next;
|
|
|
|
}
|
|
|
|
value = pair->value;
|
|
|
|
free(pair);
|
|
|
|
this->count--;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-06-07 14:36:26 +00:00
|
|
|
METHOD(hashtable_t, remove_at, void,
|
2013-08-27 14:37:41 +00:00
|
|
|
private_hashtable_t *this, private_enumerator_t *enumerator)
|
2010-06-07 14:36:26 +00:00
|
|
|
{
|
|
|
|
if (enumerator->table == this && enumerator->current)
|
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
pair_t *current = enumerator->current;
|
|
|
|
if (enumerator->prev)
|
2010-06-07 14:36:26 +00:00
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
enumerator->prev->next = current->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->table[enumerator->row] = current->next;
|
2010-06-07 14:36:26 +00:00
|
|
|
}
|
2011-05-23 16:35:21 +00:00
|
|
|
enumerator->current = enumerator->prev;
|
|
|
|
free(current);
|
|
|
|
this->count--;
|
2010-06-07 14:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-07 13:50:41 +00:00
|
|
|
METHOD(hashtable_t, get_count, u_int,
|
2013-08-27 14:37:41 +00:00
|
|
|
private_hashtable_t *this)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
|
|
|
return this->count;
|
|
|
|
}
|
|
|
|
|
2010-06-07 13:50:41 +00:00
|
|
|
METHOD(enumerator_t, enumerate, bool,
|
2017-05-11 07:17:02 +00:00
|
|
|
private_enumerator_t *this, va_list args)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2017-05-11 07:17:02 +00:00
|
|
|
const void **key;
|
|
|
|
void **value;
|
|
|
|
|
|
|
|
VA_ARGS_VGET(args, key, value);
|
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
while (this->count && this->row < this->table->size)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
this->prev = this->current;
|
2008-12-03 09:32:16 +00:00
|
|
|
if (this->current)
|
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
this->current = this->current->next;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
this->current = this->table->table[this->row];
|
|
|
|
}
|
|
|
|
if (this->current)
|
|
|
|
{
|
|
|
|
if (key)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
*key = this->current->key;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
2011-05-23 16:35:21 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
*value = this->current->value;
|
|
|
|
}
|
2011-05-23 16:45:49 +00:00
|
|
|
this->count--;
|
2011-05-23 16:35:21 +00:00
|
|
|
return TRUE;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
|
|
|
this->row++;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-06-07 13:50:41 +00:00
|
|
|
METHOD(hashtable_t, create_enumerator, enumerator_t*,
|
2013-08-27 14:37:41 +00:00
|
|
|
private_hashtable_t *this)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2010-06-07 13:50:41 +00:00
|
|
|
private_enumerator_t *enumerator;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-06-07 13:50:41 +00:00
|
|
|
INIT(enumerator,
|
|
|
|
.enumerator = {
|
2017-05-11 07:17:02 +00:00
|
|
|
.enumerate = enumerator_enumerate_default,
|
|
|
|
.venumerate = _enumerate,
|
2011-05-23 16:35:21 +00:00
|
|
|
.destroy = (void*)free,
|
2010-06-07 13:50:41 +00:00
|
|
|
},
|
|
|
|
.table = this,
|
2011-05-23 16:45:49 +00:00
|
|
|
.count = this->count,
|
2010-06-07 13:50:41 +00:00
|
|
|
);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
return &enumerator->enumerator;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2013-08-27 14:37:41 +00:00
|
|
|
static void destroy_internal(private_hashtable_t *this,
|
|
|
|
void (*fn)(void*,const void*))
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
pair_t *pair, *next;
|
2008-12-03 09:32:16 +00:00
|
|
|
u_int row;
|
2009-10-26 15:08:14 +00:00
|
|
|
|
2020-04-24 08:56:50 +00:00
|
|
|
#ifdef HASHTABLE_PROFILER
|
|
|
|
if (this->success.count || this->failure.count)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%zu elements [max. %zu], %zu buckets [%zu], %zu "
|
|
|
|
"successful / %zu failed lookups, %.4f [%zu] / %.4f "
|
|
|
|
"[%zu] avg. probes in table created at:",
|
|
|
|
this->count, this->max.count, this->size, this->max.size,
|
|
|
|
this->success.count, this->failure.count,
|
|
|
|
(double)this->success.probes/this->success.count,
|
|
|
|
this->success.longest,
|
|
|
|
(double)this->failure.probes/this->failure.count,
|
|
|
|
this->failure.longest);
|
|
|
|
this->backtrace->log(this->backtrace, stderr, TRUE);
|
|
|
|
}
|
|
|
|
this->backtrace->destroy(this->backtrace);
|
|
|
|
#endif
|
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
for (row = 0; row < this->size; row++)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2011-05-23 16:35:21 +00:00
|
|
|
pair = this->table[row];
|
|
|
|
while (pair)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2013-08-27 14:37:41 +00:00
|
|
|
if (fn)
|
|
|
|
{
|
|
|
|
fn(pair->value, pair->key);
|
|
|
|
}
|
2011-05-23 16:35:21 +00:00
|
|
|
next = pair->next;
|
|
|
|
free(pair);
|
|
|
|
pair = next;
|
2008-12-03 09:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(this->table);
|
|
|
|
}
|
|
|
|
|
2013-08-27 14:37:41 +00:00
|
|
|
METHOD(hashtable_t, destroy, void,
|
|
|
|
private_hashtable_t *this)
|
|
|
|
{
|
|
|
|
destroy_internal(this, NULL);
|
2020-04-24 11:41:53 +00:00
|
|
|
free(this);
|
2013-08-27 14:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(hashtable_t, destroy_function, void,
|
|
|
|
private_hashtable_t *this, void (*fn)(void*,const void*))
|
|
|
|
{
|
|
|
|
destroy_internal(this, fn);
|
2020-04-24 11:41:53 +00:00
|
|
|
free(this);
|
2013-08-27 14:37:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 11:41:53 +00:00
|
|
|
METHOD(hashtable_t, create_enumerator_hashlist, enumerator_t*,
|
|
|
|
private_hashlist_t *this)
|
|
|
|
{
|
|
|
|
return create_enumerator(&this->super);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(hashtable_t, put_hashlist, void*,
|
|
|
|
private_hashlist_t *this, const void *key, void *value)
|
|
|
|
{
|
|
|
|
return put(&this->super, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(hashtable_t, get_hashlist, void*,
|
|
|
|
private_hashlist_t *this, const void *key)
|
|
|
|
{
|
|
|
|
return get(&this->super, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(hashlist_t, get_match, void*,
|
|
|
|
private_hashlist_t *this, const void *key, hashtable_equals_t match)
|
|
|
|
{
|
|
|
|
pair_t *pair = find_key(&this->super, key, match, NULL, NULL);
|
|
|
|
return pair ? pair->value : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(hashtable_t, remove_hashlist, void*,
|
|
|
|
private_hashlist_t *this, const void *key)
|
|
|
|
{
|
|
|
|
return remove_(&this->super, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(hashtable_t, remove_at_hashlist, void,
|
|
|
|
private_hashlist_t *this, private_enumerator_t *enumerator)
|
|
|
|
{
|
|
|
|
remove_at(&this->super, enumerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(hashtable_t, get_count_hashlist, u_int,
|
|
|
|
private_hashlist_t *this)
|
|
|
|
{
|
|
|
|
return get_count(&this->super);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD2(hashtable_t, hashlist_t, destroy_hashlist, void,
|
|
|
|
private_hashlist_t *this)
|
|
|
|
{
|
|
|
|
destroy_internal(&this->super, NULL);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(hashtable_t, destroy_function_hashlist, void,
|
|
|
|
private_hashlist_t *this, void (*fn)(void*,const void*))
|
|
|
|
{
|
|
|
|
destroy_internal(&this->super, fn);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header
|
2008-12-03 09:32:16 +00:00
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
hashtable_t *hashtable_create(hashtable_hash_t hash, hashtable_equals_t equals,
|
|
|
|
u_int size)
|
2008-12-03 09:32:16 +00:00
|
|
|
{
|
2010-06-07 13:50:41 +00:00
|
|
|
private_hashtable_t *this;
|
|
|
|
|
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.put = _put,
|
|
|
|
.get = _get,
|
|
|
|
.remove = _remove_,
|
2010-06-07 14:36:26 +00:00
|
|
|
.remove_at = (void*)_remove_at,
|
2010-06-07 13:50:41 +00:00
|
|
|
.get_count = _get_count,
|
|
|
|
.create_enumerator = _create_enumerator,
|
|
|
|
.destroy = _destroy,
|
2013-08-27 14:37:41 +00:00
|
|
|
.destroy_function = _destroy_function,
|
2010-06-07 13:50:41 +00:00
|
|
|
},
|
|
|
|
.hash = hash,
|
2020-04-24 11:41:53 +00:00
|
|
|
.equals = equals,
|
2010-06-07 13:50:41 +00:00
|
|
|
);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
init_hashtable(this, size);
|
|
|
|
|
2020-04-24 08:56:50 +00:00
|
|
|
#ifdef HASHTABLE_PROFILER
|
|
|
|
this->backtrace = backtrace_create(3);
|
|
|
|
#endif
|
|
|
|
|
2020-04-24 11:41:53 +00:00
|
|
|
return &this->public;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a hash table
|
|
|
|
*/
|
|
|
|
static private_hashlist_t *hashlist_create_internal(hashtable_hash_t hash,
|
|
|
|
u_int size)
|
|
|
|
{
|
|
|
|
private_hashlist_t *this;
|
|
|
|
|
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.ht = {
|
|
|
|
.put = _put_hashlist,
|
|
|
|
.get = _get_hashlist,
|
|
|
|
.remove = _remove_hashlist,
|
|
|
|
.remove_at = (void*)_remove_at_hashlist,
|
|
|
|
.get_count = _get_count_hashlist,
|
|
|
|
.create_enumerator = _create_enumerator_hashlist,
|
|
|
|
.destroy = _destroy_hashlist,
|
|
|
|
.destroy_function = _destroy_function_hashlist,
|
|
|
|
},
|
|
|
|
.get_match = _get_match,
|
|
|
|
.destroy = _destroy_hashlist,
|
|
|
|
},
|
|
|
|
.super = {
|
|
|
|
.hash = hash,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
init_hashtable(&this->super, size);
|
|
|
|
|
|
|
|
#ifdef HASHTABLE_PROFILER
|
|
|
|
this->super.backtrace = backtrace_create(3);
|
|
|
|
#endif
|
|
|
|
|
2020-04-24 06:50:24 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header
|
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
hashlist_t *hashlist_create(hashtable_hash_t hash, hashtable_equals_t equals,
|
|
|
|
u_int size)
|
2020-04-24 06:50:24 +00:00
|
|
|
{
|
2020-04-24 11:41:53 +00:00
|
|
|
private_hashlist_t *this = hashlist_create_internal(hash, size);
|
2020-04-24 06:50:24 +00:00
|
|
|
|
2020-04-24 11:41:53 +00:00
|
|
|
this->super.equals = equals;
|
2020-04-24 06:50:24 +00:00
|
|
|
|
|
|
|
return &this->public;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header
|
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
hashlist_t *hashlist_create_sorted(hashtable_hash_t hash,
|
|
|
|
hashtable_cmp_t cmp, u_int size)
|
2020-04-24 06:50:24 +00:00
|
|
|
{
|
2020-04-24 11:41:53 +00:00
|
|
|
private_hashlist_t *this = hashlist_create_internal(hash, size);
|
2020-04-24 06:50:24 +00:00
|
|
|
|
2020-04-24 11:41:53 +00:00
|
|
|
this->super.cmp = cmp;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-12-03 09:32:16 +00:00
|
|
|
return &this->public;
|
|
|
|
}
|