pthread_mutex_t _hashes_mtx = PTHREAD_MUTEX_INITIALIZER;
static struct list *_hashes;
-/* Allocate a new hash. */
struct hash *hash_create_size(unsigned int size,
unsigned int (*hash_key)(void *),
- int (*hash_cmp)(const void *, const void *),
+ bool (*hash_cmp)(const void *, const void *),
const char *name)
{
struct hash *hash;
return hash;
}
-/* Allocate a new hash with default hash size. */
struct hash *hash_create(unsigned int (*hash_key)(void *),
- int (*hash_cmp)(const void *, const void *),
+ bool (*hash_cmp)(const void *, const void *),
const char *name)
{
return hash_create_size(HASH_INITIAL_SIZE, hash_key, hash_cmp, name);
}
-/* Utility function for hash_get(). When this function is specified
- as alloc_func, return arugment as it is. This function is used for
- intern already allocated value. */
void *hash_alloc_intern(void *arg)
{
return arg;
hash->index = new_index;
}
-/* Lookup and return hash backet in hash. If there is no
- corresponding hash backet and alloc_func is specified, create new
- hash backet. */
void *hash_get(struct hash *hash, void *data, void *(*alloc_func)(void *))
{
unsigned int key;
void *newdata;
struct hash_backet *backet;
+ if (!alloc_func && !hash->count)
+ return NULL;
+
key = (*hash->hash_key)(data);
index = key & (hash->size - 1);
return NULL;
}
-/* Hash lookup. */
void *hash_lookup(struct hash *hash, void *data)
{
return hash_get(hash, data, NULL);
}
-/* Simple Bernstein hash which is simple and fast for common case */
unsigned int string_hash_make(const char *str)
{
unsigned int hash = 0;
return hash;
}
-/* This function release registered value from specified hash. When
- release is successfully finished, return the data pointer in the
- hash backet. */
void *hash_release(struct hash *hash, void *data)
{
void *ret;
return NULL;
}
-/* Iterator function for hash. */
void hash_iterate(struct hash *hash, void (*func)(struct hash_backet *, void *),
void *arg)
{
}
}
-/* Iterator function for hash. */
void hash_walk(struct hash *hash, int (*func)(struct hash_backet *, void *),
void *arg)
{
}
}
-/* Clean up hash. */
void hash_clean(struct hash *hash, void (*free_func)(void *))
{
unsigned int i;
hash->stats.empty = hash->size;
}
-/* Free hash memory. You may call hash_clean before call this
- function. */
+static void hash_to_list_iter(struct hash_backet *hb, void *arg)
+{
+ struct list *list = arg;
+
+ listnode_add(list, hb->data);
+}
+
+struct list *hash_to_list(struct hash *hash)
+{
+ struct list *list = list_new();
+
+ hash_iterate(hash, hash_to_list_iter, list);
+ return list;
+}
+
void hash_free(struct hash *hash)
{
pthread_mutex_lock(&_hashes_mtx);
if (_hashes) {
listnode_delete(_hashes, hash);
if (_hashes->count == 0) {
- list_delete_and_null(&_hashes);
+ list_delete(&_hashes);
}
}
}
pthread_mutex_lock(&_hashes_mtx);
if (!_hashes) {
pthread_mutex_unlock(&_hashes_mtx);
+ ttable_del(tt);
vty_out(vty, "No hash tables in use.\n");
return CMD_SUCCESS;
}