Files
asterisk/main/hashtab.c
T

834 lines
20 KiB
C
Raw Normal View History

2007-11-09 16:00:22 +00:00
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2007, Digium, Inc.
*
* Steve Murphy <murf@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief code to implement generic hash tables
*
* \author Steve Murphy <murf@digium.com>
*/
2012-06-15 16:20:16 +00:00
/*** MODULEINFO
<support_level>core</support_level>
***/
2007-11-09 16:00:22 +00:00
#include "asterisk.h"
#include <ctype.h>
#include "asterisk/lock.h"
#include "asterisk/frame.h"
#include "asterisk/channel.h"
#include "asterisk/cli.h"
#include "asterisk/term.h"
#include "asterisk/utils.h"
#include "asterisk/threadstorage.h"
#include "asterisk/linkedlists.h"
#include "asterisk/hashtab.h"
static void _ast_hashtab_resize(struct ast_hashtab *tab, const char *file, int lineno, const char *func);
2018-02-19 19:55:50 -06:00
#define ast_hashtab_resize(tab) \
_ast_hashtab_resize(tab, __FILE__, __LINE__, __PRETTY_FUNCTION__)
static void *ast_hashtab_lookup_internal(struct ast_hashtab *tab, const void *obj, unsigned int h);
2007-11-09 16:00:22 +00:00
/* some standard, default routines for general use */
int ast_hashtab_compare_strings(const void *a, const void *b)
{
2008-03-04 04:44:28 +00:00
return strcmp(a, b);
2007-11-09 16:00:22 +00:00
}
int ast_hashtab_compare_strings_nocase(const void *a, const void *b)
{
2008-03-04 04:44:28 +00:00
return strcasecmp(a, b);
2007-11-09 16:00:22 +00:00
}
int ast_hashtab_compare_ints(const void *a, const void *b)
{
2007-11-14 03:22:09 +00:00
int ai = *((int *) a);
int bi = *((int *) b);
2007-11-09 16:00:22 +00:00
if (ai < bi)
return -1;
2007-11-14 03:22:09 +00:00
return !(ai == bi);
2007-11-09 16:00:22 +00:00
}
int ast_hashtab_compare_shorts(const void *a, const void *b)
{
2007-11-14 03:22:09 +00:00
short as = *((short *) a);
short bs = *((short *) b);
2007-11-09 16:00:22 +00:00
if (as < bs)
return -1;
2007-11-14 03:22:09 +00:00
return !(as == bs);
2007-11-09 16:00:22 +00:00
}
int ast_hashtab_resize_java(struct ast_hashtab *tab)
{
2007-11-14 03:22:09 +00:00
double loadfactor = (double) tab->hash_tab_elements / (double) tab->hash_tab_size;
return (loadfactor > 0.75);
2007-11-09 16:00:22 +00:00
}
int ast_hashtab_resize_tight(struct ast_hashtab *tab)
{
2007-11-14 03:22:09 +00:00
return (tab->hash_tab_elements > tab->hash_tab_size); /* this is quicker than division */
2007-11-09 16:00:22 +00:00
}
int ast_hashtab_resize_none(struct ast_hashtab *tab) /* always return 0 -- no resizing */
{
return 0;
}
2007-11-14 03:22:09 +00:00
int ast_is_prime(int num)
2007-11-09 16:00:22 +00:00
{
2007-11-14 03:22:09 +00:00
int tnum, limit;
2007-11-14 03:22:09 +00:00
if (!(num & 0x1)) /* even number -- not prime */
2007-11-09 16:00:22 +00:00
return 0;
2007-11-14 03:22:09 +00:00
/* Loop through ODD numbers starting with 3 */
2007-11-09 16:00:22 +00:00
tnum = 3;
limit = num;
2007-11-14 03:22:09 +00:00
while (tnum < limit) {
if (!(num % tnum))
2007-11-09 16:00:22 +00:00
return 0;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
/* really, we only need to check sqrt(num) numbers */
limit = num / tnum;
2007-11-09 16:00:22 +00:00
/* we only check odd numbers */
2007-11-14 03:22:09 +00:00
tnum = tnum + 2;
2007-11-09 16:00:22 +00:00
}
2007-11-14 03:22:09 +00:00
/* if we made it through the loop, the number is a prime */
2007-11-09 16:00:22 +00:00
return 1;
}
int ast_hashtab_newsize_java(struct ast_hashtab *tab)
{
2007-11-14 03:22:09 +00:00
int i = (tab->hash_tab_size << 1); /* multiply by two */
2007-11-14 03:22:09 +00:00
while (!ast_is_prime(i))
2007-11-09 16:00:22 +00:00
i++;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return i;
}
int ast_hashtab_newsize_tight(struct ast_hashtab *tab)
{
2007-11-14 03:22:09 +00:00
int x = (tab->hash_tab_size << 1);
int i = (tab->hash_tab_size + x);
2007-11-14 03:22:09 +00:00
while (!ast_is_prime(i))
2007-11-09 16:00:22 +00:00
i++;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return i;
}
int ast_hashtab_newsize_none(struct ast_hashtab *tab) /* always return current size -- no resizing */
{
return tab->hash_tab_size;
}
unsigned int ast_hashtab_hash_string(const void *obj)
{
2007-11-14 03:22:09 +00:00
unsigned char *str = (unsigned char *) obj;
2007-11-09 16:00:22 +00:00
unsigned int total;
for (total = 0; *str; str++) {
2007-11-09 16:00:22 +00:00
unsigned int tmp = total;
total <<= 1; /* multiply by 2 */
total += tmp; /* multiply by 3 */
total <<= 2; /* multiply by 12 */
total += tmp; /* multiply by 13 */
2007-11-09 16:00:22 +00:00
total += ((unsigned int)(*str));
}
return total;
}
unsigned int ast_hashtab_hash_string_sax(const void *obj) /* from Josh */
{
const unsigned char *str = obj;
2007-11-09 16:00:22 +00:00
unsigned int total = 0, c = 0;
while ((c = *str++))
2007-11-14 03:22:09 +00:00
total ^= (total << 5) + (total >> 2) + (total << 10) + c;
2007-11-09 16:00:22 +00:00
return total;
}
unsigned int ast_hashtab_hash_string_nocase(const void *obj)
{
const unsigned char *str = obj;
2007-11-09 16:00:22 +00:00
unsigned int total;
2007-11-14 03:22:09 +00:00
for (total = 0; *str; str++) {
2007-11-09 16:00:22 +00:00
unsigned int tmp = total;
unsigned int charval = toupper(*str);
/* hopefully, the following is faster than multiplication by 7 */
/* why do I go to this bother? A good compiler will do this
2007-11-09 16:00:22 +00:00
anyway, if I say total *= 13 */
/* BTW, tried *= 7, and it doesn't do as well in spreading things around! */
total <<= 1; /* multiply by 2 */
total += tmp; /* multiply by 3 */
total <<= 2; /* multiply by 12 */
total += tmp; /* multiply by 13 */
2007-11-09 16:00:22 +00:00
total += (charval);
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return total;
}
unsigned int ast_hashtab_hash_int(const int x)
{
return x;
}
unsigned int ast_hashtab_hash_short(const short x)
{
/* hmmmm.... modulus is best < 65535 !! */
return x;
}
2018-02-19 19:55:50 -06:00
struct ast_hashtab *_ast_hashtab_create(int initial_buckets,
int (*compare)(const void *a, const void *b),
int (*resize)(struct ast_hashtab *),
int (*newsize)(struct ast_hashtab *tab),
unsigned int (*hash)(const void *obj),
2018-02-19 19:55:50 -06:00
int do_locking,
const char *file, int lineno, const char *function
)
2007-11-09 16:00:22 +00:00
{
2007-11-14 03:22:09 +00:00
struct ast_hashtab *ht;
2018-02-19 19:55:50 -06:00
ht = __ast_calloc(1, sizeof(*ht), file, lineno, function);
if (!ht) {
2007-11-14 03:22:09 +00:00
return NULL;
2018-02-19 19:55:50 -06:00
}
2007-11-14 03:22:09 +00:00
while (!ast_is_prime(initial_buckets)) /* make sure this is prime */
2007-11-09 16:00:22 +00:00
initial_buckets++;
2007-11-14 03:22:09 +00:00
2018-02-19 19:55:50 -06:00
ht->array = __ast_calloc(initial_buckets, sizeof(*(ht->array)),
file, lineno, function);
if (!ht->array) {
ast_free(ht);
2007-11-14 03:22:09 +00:00
return NULL;
}
2007-11-09 16:00:22 +00:00
ht->hash_tab_size = initial_buckets;
ht->compare = compare;
ht->resize = resize;
ht->newsize = newsize;
ht->hash = hash;
ht->do_locking = do_locking;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (do_locking)
ast_rwlock_init(&ht->lock);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (!ht->resize)
ht->resize = ast_hashtab_resize_java;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (!ht->newsize)
ht->newsize = ast_hashtab_newsize_java;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return ht;
}
struct ast_hashtab *_ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_func)(const void *obj), const char *file, int lineno, const char *func)
2007-11-09 16:00:22 +00:00
{
2007-11-14 03:22:09 +00:00
struct ast_hashtab *ht;
unsigned int i;
2007-11-14 03:22:09 +00:00
2018-02-19 19:55:50 -06:00
ht = __ast_calloc(1, sizeof(*ht), file, lineno, func);
if (!ht) {
2007-11-14 03:22:09 +00:00
return NULL;
2018-02-19 19:55:50 -06:00
}
2007-11-14 03:22:09 +00:00
2018-02-19 19:55:50 -06:00
ht->array = __ast_calloc(tab->hash_tab_size, sizeof(*(ht->array)),
file, lineno, func);
if (!ht->array) {
2017-10-07 13:14:08 -04:00
ast_free(ht);
2007-11-14 03:22:09 +00:00
return NULL;
}
2007-11-09 16:00:22 +00:00
ht->hash_tab_size = tab->hash_tab_size;
ht->compare = tab->compare;
ht->resize = tab->resize;
ht->newsize = tab->newsize;
ht->hash = tab->hash;
ht->do_locking = tab->do_locking;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (ht->do_locking)
ast_rwlock_init(&ht->lock);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
/* now, dup the objects in the buckets and get them into the table */
/* the fast way is to use the existing array index, and not have to hash
the objects again */
2007-11-14 03:22:09 +00:00
for (i = 0; i < ht->hash_tab_size; i++) {
2007-11-09 16:00:22 +00:00
struct ast_hashtab_bucket *b = tab->array[i];
2007-11-14 03:22:09 +00:00
while (b) {
2007-11-09 16:00:22 +00:00
void *newobj = (*obj_dup_func)(b->object);
2018-02-19 19:55:50 -06:00
if (newobj) {
_ast_hashtab_insert_immediate_bucket(ht, newobj, i, file, lineno, func);
2018-02-19 19:55:50 -06:00
}
2007-11-09 16:00:22 +00:00
b = b->next;
}
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return ht;
}
static void tlist_del_item(struct ast_hashtab_bucket **head, struct ast_hashtab_bucket *item)
{
/* item had better be in the list! or suffer the weirdness that occurs, later! */
if (*head == item) { /* first item in the list */
*head = item->tnext;
if (item->tnext)
item->tnext->tprev = NULL;
} else {
/* short circuit stuff */
item->tprev->tnext = item->tnext;
if (item->tnext)
item->tnext->tprev = item->tprev;
}
}
static void tlist_add_head(struct ast_hashtab_bucket **head, struct ast_hashtab_bucket *item)
{
if (*head) {
item->tnext = *head;
item->tprev = NULL;
(*head)->tprev = item;
*head = item;
} else {
/* the list is empty */
*head = item;
item->tprev = NULL;
item->tnext = NULL;
}
}
/* user-controlled hashtab locking. Create a hashtab without locking, then call the
following locking routines yourself to lock the table between threads. */
void ast_hashtab_wrlock(struct ast_hashtab *tab)
{
ast_rwlock_wrlock(&tab->lock);
}
void ast_hashtab_rdlock(struct ast_hashtab *tab)
{
ast_rwlock_rdlock(&tab->lock);
}
void ast_hashtab_initlock(struct ast_hashtab *tab)
{
ast_rwlock_init(&tab->lock);
}
void ast_hashtab_destroylock(struct ast_hashtab *tab)
{
ast_rwlock_destroy(&tab->lock);
}
void ast_hashtab_unlock(struct ast_hashtab *tab)
{
ast_rwlock_unlock(&tab->lock);
}
void ast_hashtab_destroy(struct ast_hashtab *tab, void (*objdestroyfunc)(void *obj))
2007-11-09 16:00:22 +00:00
{
/* this func will free the hash table and all its memory. It
doesn't touch the objects stored in it */
if (tab) {
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_wrlock(&tab->lock);
if (tab->array) {
/* go thru and destroy the buckets */
struct ast_hashtab_bucket *t;
int i;
2007-11-09 16:00:22 +00:00
while (tab->tlist) {
t = tab->tlist;
if (t->object && objdestroyfunc) {
/* I cast this because I'm not going to MOD it, I'm going to DESTROY
* it.
*/
(*objdestroyfunc)((void *) t->object);
}
2007-11-09 16:00:22 +00:00
tlist_del_item(&(tab->tlist), tab->tlist);
2017-10-07 13:14:08 -04:00
ast_free(t);
2007-11-09 16:00:22 +00:00
}
for (i = 0; i < tab->hash_tab_size; i++) {
/* Not totally necessary, but best to destroy old pointers */
tab->array[i] = NULL;
}
2017-10-07 13:14:08 -04:00
ast_free(tab->array);
2007-11-09 16:00:22 +00:00
}
if (tab->do_locking) {
ast_rwlock_unlock(&tab->lock);
ast_rwlock_destroy(&tab->lock);
}
2017-10-07 13:14:08 -04:00
ast_free(tab);
2007-11-09 16:00:22 +00:00
}
}
int _ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func)
2007-11-09 16:00:22 +00:00
{
unsigned int h;
int res=0;
if (!tab || !obj)
return res;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_wrlock(&tab->lock);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
h = (*tab->hash)(obj) % tab->hash_tab_size;
2007-11-14 03:22:09 +00:00
res = _ast_hashtab_insert_immediate_bucket(tab, obj, h, file, lineno, func);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_unlock(&tab->lock);
2007-11-14 03:22:09 +00:00
return res;
2007-11-09 16:00:22 +00:00
}
int _ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h, const char *file, int lineno, const char *func)
2007-11-09 16:00:22 +00:00
{
int c;
struct ast_hashtab_bucket *b;
2007-11-09 16:00:22 +00:00
if (!tab || !obj)
return 0;
2007-11-14 03:22:09 +00:00
for (c = 0, b = tab->array[h]; b; b= b->next)
2007-11-09 16:00:22 +00:00
c++;
2007-11-14 03:22:09 +00:00
if (c + 1 > tab->largest_bucket_size)
tab->largest_bucket_size = c + 1;
2018-02-19 19:55:50 -06:00
b = __ast_calloc(1, sizeof(*b), file, lineno, func);
if (!b) {
return 0;
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
b->object = obj;
b->next = tab->array[h];
tab->array[h] = b;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (b->next)
b->next->prev = b;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
tlist_add_head(&(tab->tlist), b);
tab->hash_tab_elements++;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if ((*tab->resize)(tab))
ast_hashtab_resize(tab);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return 1;
}
int _ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func)
2007-11-09 16:00:22 +00:00
{
/* check to see if the element is already there; insert only if
it is not there. */
/* will force a resize if the resize func returns 1 */
/* returns 1 on success, 0 if there's a problem, or it's already there. */
unsigned int bucket = 0;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_wrlock(&tab->lock);
2007-11-14 03:22:09 +00:00
if (!ast_hashtab_lookup_bucket(tab, obj, &bucket)) {
int ret2 = _ast_hashtab_insert_immediate_bucket(tab, obj, bucket, file, lineno, func);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_unlock(&tab->lock);
2007-11-09 16:00:22 +00:00
return ret2;
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_unlock(&tab->lock);
2007-11-09 16:00:22 +00:00
return 0;
}
void *ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
2007-11-09 16:00:22 +00:00
{
/* lookup this object in the hash table. return a ptr if found, or NULL if not */
unsigned int h;
void *ret;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (!tab || !obj)
return 0;
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_rdlock(&tab->lock);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
h = (*tab->hash)(obj) % tab->hash_tab_size;
ret = ast_hashtab_lookup_internal(tab,obj,h);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_unlock(&tab->lock);
return ret;
2007-11-09 16:00:22 +00:00
}
2007-11-14 03:22:09 +00:00
void *ast_hashtab_lookup_with_hash(struct ast_hashtab *tab, const void *obj, unsigned int hashval)
2007-11-09 16:00:22 +00:00
{
/* lookup this object in the hash table. return a ptr if found, or NULL if not */
unsigned int h;
void *ret;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (!tab || !obj)
return 0;
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_rdlock(&tab->lock);
2007-11-09 16:00:22 +00:00
h = hashval % tab->hash_tab_size;
2007-11-14 03:22:09 +00:00
ret = ast_hashtab_lookup_internal(tab,obj,h);
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_unlock(&tab->lock);
return ret;
2007-11-09 16:00:22 +00:00
}
void *ast_hashtab_lookup_bucket(struct ast_hashtab *tab, const void *obj, unsigned int *bucket)
2007-11-09 16:00:22 +00:00
{
/* lookup this object in the hash table. return a ptr if found, or NULL if not */
unsigned int h;
void *ret;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (!tab || !obj)
return 0;
2007-11-09 16:00:22 +00:00
h = (*tab->hash)(obj) % tab->hash_tab_size;
ret = ast_hashtab_lookup_internal(tab,obj,h);
*bucket = h;
return ret;
}
static void *ast_hashtab_lookup_internal(struct ast_hashtab *tab, const void *obj, unsigned int h)
{
struct ast_hashtab_bucket *b;
2007-11-14 03:22:09 +00:00
for (b = tab->array[h]; b; b = b->next) {
if (!(*tab->compare)(obj,b->object)) {
/* I can't touch obj in this func, but the outside world is welcome to */
return (void*) b->object;
}
2007-11-09 16:00:22 +00:00
}
2007-11-14 03:22:09 +00:00
2007-12-03 23:49:40 +00:00
return NULL;
2007-11-09 16:00:22 +00:00
}
void ast_hashtab_get_stats(struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets)
2007-11-09 16:00:22 +00:00
{
/* returns key stats for the table */
if (tab->do_locking)
ast_rwlock_rdlock(&tab->lock);
*biggest_bucket_size = tab->largest_bucket_size;
*resize_count = tab->resize_count;
*num_objects = tab->hash_tab_elements;
*num_buckets = tab->hash_tab_size;
if (tab->do_locking)
ast_rwlock_unlock(&tab->lock);
}
/* this function returns the number of elements stored in the hashtab */
int ast_hashtab_size(struct ast_hashtab *tab)
2007-11-09 16:00:22 +00:00
{
return tab->hash_tab_elements;
}
/* this function returns the size of the bucket array in the hashtab */
int ast_hashtab_capacity( struct ast_hashtab *tab)
2007-11-09 16:00:22 +00:00
{
return tab->hash_tab_size;
}
/* the insert operation calls this, and is wrlock'd when it does. */
/* if you want to call it, you should set the wrlock yourself */
static void _ast_hashtab_resize(struct ast_hashtab *tab, const char *file, int lineno, const char *func)
2007-11-09 16:00:22 +00:00
{
/* this function is called either internally, when the resize func returns 1, or
externally by the user to force a resize of the hash table */
int newsize = (*tab->newsize)(tab), i, c;
unsigned int h;
struct ast_hashtab_bucket *b,*bn;
2007-11-09 16:00:22 +00:00
/* Since we keep a DLL of all the buckets in tlist,
all we have to do is free the array, malloc a new one,
and then go thru the tlist array and reassign them into
2007-11-09 16:00:22 +00:00
the bucket arrayj.
*/
2007-11-14 03:22:09 +00:00
for (i = 0; i < tab->hash_tab_size; i++) { /* don't absolutely have to do this, but
2007-11-09 16:00:22 +00:00
why leave ptrs laying around */
tab->array[i] = 0; /* erase old ptrs */
}
2017-10-07 13:14:08 -04:00
ast_free(tab->array);
2018-02-19 19:55:50 -06:00
tab->array = __ast_calloc(newsize, sizeof(*(tab->array)), file, lineno, func);
if (!tab->array) {
2007-11-14 03:22:09 +00:00
return;
2018-02-19 19:55:50 -06:00
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
/* now sort the buckets into their rightful new slots */
tab->resize_count++;
tab->hash_tab_size = newsize;
tab->largest_bucket_size = 0;
2007-11-14 03:22:09 +00:00
for (b = tab->tlist; b; b = bn) {
2007-11-09 16:00:22 +00:00
b->prev = 0;
bn = b->tnext;
h = (*tab->hash)(b->object) % tab->hash_tab_size;
b->next = tab->array[h];
if (b->next)
b->next->prev = b;
tab->array[h] = b;
}
/* recalc the largest bucket size */
2007-11-14 03:22:09 +00:00
for (i = 0; i < tab->hash_tab_size; i++) {
for (c = 0, b = tab->array[i]; b; b = b->next)
2007-11-09 16:00:22 +00:00
c++;
if (c > tab->largest_bucket_size)
tab->largest_bucket_size = c;
}
}
struct ast_hashtab_iter *_ast_hashtab_start_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func)
2007-11-09 16:00:22 +00:00
{
/* returns an iterator */
2007-11-14 03:22:09 +00:00
struct ast_hashtab_iter *it;
2018-02-19 19:55:50 -06:00
it = __ast_calloc(1, sizeof(*it), file, lineno, func);
if (!it) {
2007-11-14 03:22:09 +00:00
return NULL;
2018-02-19 19:55:50 -06:00
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
it->next = tab->tlist;
it->tab = tab;
if (tab->do_locking)
ast_rwlock_rdlock(&tab->lock);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return it;
}
/* use this function to get a write lock */
struct ast_hashtab_iter *_ast_hashtab_start_write_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func)
2007-11-09 16:00:22 +00:00
{
/* returns an iterator */
2007-11-14 03:22:09 +00:00
struct ast_hashtab_iter *it;
2018-02-19 19:55:50 -06:00
it = __ast_calloc(1, sizeof(*it), file, lineno, func);
if (!it) {
2007-11-14 03:22:09 +00:00
return NULL;
2018-02-19 19:55:50 -06:00
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
it->next = tab->tlist;
it->tab = tab;
if (tab->do_locking)
ast_rwlock_wrlock(&tab->lock);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return it;
}
void ast_hashtab_end_traversal(struct ast_hashtab_iter *it)
{
if (!it)
return;
2007-11-09 16:00:22 +00:00
if (it->tab->do_locking)
ast_rwlock_unlock(&it->tab->lock);
2017-10-07 13:14:08 -04:00
ast_free(it);
2007-11-09 16:00:22 +00:00
}
void *ast_hashtab_next(struct ast_hashtab_iter *it)
{
/* returns the next object in the list, advances iter one step */
struct ast_hashtab_bucket *retval;
2007-11-09 16:00:22 +00:00
if (it && it->next) { /* there's a next in the bucket list */
retval = it->next;
it->next = retval->tnext;
2007-11-14 03:22:09 +00:00
return (void *) retval->object;
2007-11-09 16:00:22 +00:00
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return NULL;
}
static void *ast_hashtab_remove_object_internal(struct ast_hashtab *tab, struct ast_hashtab_bucket *b, int h)
{
const void *obj2;
2007-11-14 03:22:09 +00:00
if (b->prev)
2007-11-09 16:00:22 +00:00
b->prev->next = b->next;
2007-11-14 03:22:09 +00:00
else
2007-11-09 16:00:22 +00:00
tab->array[h] = b->next;
2007-11-14 03:22:09 +00:00
if (b->next)
2007-11-09 16:00:22 +00:00
b->next->prev = b->prev;
2007-11-09 16:00:22 +00:00
tlist_del_item(&(tab->tlist), b);
2007-11-09 16:00:22 +00:00
obj2 = b->object;
b->object = b->next = (void*)2;
2017-10-07 13:14:08 -04:00
ast_free(b); /* free up the hashbucket */
2007-11-09 16:00:22 +00:00
tab->hash_tab_elements--;
#ifdef DEBUG
{
int c2;
struct ast_hashtab_bucket *b2;
/* do a little checking */
2007-11-14 03:22:09 +00:00
for (c2 = 0, b2 = tab->tlist; b2; b2 = b2->tnext) {
2007-11-09 16:00:22 +00:00
c2++;
}
if (c2 != tab->hash_tab_elements) {
printf("Hey! we didn't delete right! there are %d elements in the list, and we expected %d\n",
c2, tab->hash_tab_elements);
}
2007-11-14 03:22:09 +00:00
for (c2 = 0, b2 = tab->tlist; b2; b2 = b2->tnext) {
unsigned int obj3 = (unsigned long) obj2;
unsigned int b3 = (unsigned long) b;
2007-11-09 16:00:22 +00:00
if (b2->object == obj2)
printf("Hey-- you've still got a bucket pointing at ht_element %x\n", obj3);
if (b2->next == b)
printf("Hey-- you've still got a bucket with next ptr pointing to deleted bucket %x\n", b3);
if (b2->prev == b)
printf("Hey-- you've still got a bucket with prev ptr pointing to deleted bucket %x\n", b3);
if (b2->tprev == b)
printf("Hey-- you've still got a bucket with tprev ptr pointing to deleted bucket %x\n", b3);
if (b2->tnext == b)
printf("Hey-- you've still got a bucket with tnext ptr pointing to deleted bucket %x\n", b3);
}
}
#endif
2007-11-14 03:22:09 +00:00
return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
2007-11-09 16:00:22 +00:00
}
void *ast_hashtab_remove_object_via_lookup(struct ast_hashtab *tab, void *obj)
{
/* looks up the object; removes the corresponding bucket */
const void *obj2;
2007-11-09 16:00:22 +00:00
if (!tab || !obj)
return 0;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_wrlock(&tab->lock);
2007-11-14 03:22:09 +00:00
obj2 = ast_hashtab_remove_object_via_lookup_nolock(tab,obj);
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_unlock(&tab->lock);
2007-11-14 03:22:09 +00:00
return (void *)obj2;
2007-11-09 16:00:22 +00:00
}
void *ast_hashtab_remove_object_via_lookup_nolock(struct ast_hashtab *tab, void *obj)
{
/* looks up the object; removes the corresponding bucket */
unsigned int h;
struct ast_hashtab_bucket *b;
if (!tab || !obj)
return 0;
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
h = (*tab->hash)(obj) % tab->hash_tab_size;
2007-11-14 03:22:09 +00:00
for (b = tab->array[h]; b; b = b->next) {
2007-11-14 03:22:09 +00:00
if (!(*tab->compare)(obj, b->object)) {
const void *obj2;
2007-11-09 16:00:22 +00:00
2007-11-14 03:22:09 +00:00
obj2 = ast_hashtab_remove_object_internal(tab, b, h);
2007-11-14 03:22:09 +00:00
return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
2007-11-09 16:00:22 +00:00
}
}
2007-11-14 03:22:09 +00:00
2007-11-09 16:00:22 +00:00
return 0;
}
void *ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj)
{
/* looks up the object by hash and then comparing pts in bucket list instead of
calling the compare routine; removes the bucket -- a slightly cheaper operation */
/* looks up the object; removes the corresponding bucket */
const void *obj2;
2007-11-09 16:00:22 +00:00
if (!tab || !obj)
return 0;
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_wrlock(&tab->lock);
obj2 = ast_hashtab_remove_this_object_nolock(tab,obj);
2007-11-09 16:00:22 +00:00
if (tab->do_locking)
ast_rwlock_unlock(&tab->lock);
2007-11-14 03:22:09 +00:00
return (void *)obj2;
2007-11-09 16:00:22 +00:00
}
void *ast_hashtab_remove_this_object_nolock(struct ast_hashtab *tab, void *obj)
{
/* looks up the object by hash and then comparing pts in bucket list instead of
calling the compare routine; removes the bucket -- a slightly cheaper operation */
/* looks up the object; removes the corresponding bucket */
unsigned int h;
struct ast_hashtab_bucket *b;
if (!tab || !obj)
return 0;
2007-11-09 16:00:22 +00:00
h = (*tab->hash)(obj) % tab->hash_tab_size;
2007-11-14 03:22:09 +00:00
for (b = tab->array[h]; b; b = b->next) {
2007-11-09 16:00:22 +00:00
if (obj == b->object) {
const void *obj2;
2007-11-14 03:22:09 +00:00
obj2 = ast_hashtab_remove_object_internal(tab, b, h);
2007-11-14 03:22:09 +00:00
return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
2007-11-09 16:00:22 +00:00
}
}
return 0;
}