Asterisk - The Open Source Telephony Project GIT-master-a358458
hashtab.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2006, Digium, Inc.
5 *
6 * Steve Murphy <murf@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18#ifndef _ASTERISK_HASHTAB_H_
19#define _ASTERISK_HASHTAB_H_
20#define __USE_UNIX98 1 /* to get the MUTEX_RECURSIVE stuff */
21
22/*! \file
23 * \brief Generic (perhaps overly so) hashtable implementation
24 * \ref AstHash
25 */
26
27#include "asterisk/lock.h"
28
29/*! \page AstHash Hash Table support in Asterisk
30
31A hash table is a structure that allows for an exact-match search
32in O(1) (or close to that) time.
33
34The method: given: a set of {key,val} pairs. (at a minimum).
35 given: a hash function, which, given a key,
36 will return an integer. Ideally, each key in the
37 set will have its own unique associated hash value.
38 This hash number will index into an array. "buckets"
39 are what the elements of this array are called. To
40 handle possible collisions in hash values, buckets can form a list.
41
42The key for a value must be contained in the value, or we won't
43be able to find it in the bucket list.
44
45This implementation is pretty generic, because:
46
47 1. The value and key are expected to be in a structure
48 (along with other data, perhaps) and it's address is a "void *".
49 2. The pointer to a compare function must be passed in at the
50 time of creation, and is stored in the hashtable.
51 3. The pointer to a resize function, which returns 1 if the
52 hash table is to be grown. A default routine is provided
53 if the pointer is NULL, and uses the java hashtable metric
54 of a 75% load factor.
55 4. The pointer to a "new size" function, which returns a preferable
56 new size for the hash table bucket array. By default, a function
57 is supplied which roughly doubles the size of the array, is provided.
58 This size should ideally be a prime number.
59 5. The hashing function pointer must also be supplied. This function
60 must be written by the user to access the keys in the objects being
61 stored. Some helper functions that use a simple "mult by prime, add
62 the next char", sort of string hash, or a simple modulus of the hash
63 table size for ints, is provided; the user can use these simple
64 algorithms to generate a hash, or implement any other algorithms they
65 wish.
66 6. Recently updated the hash routines to use Doubly-linked lists for buckets,
67 and added a doubly-linked list that threads thru every bucket in the table.
68 The list of all buckets is on the HashTab struct. The Traversal was modified
69 to go thru this list instead of searching the bucket array for buckets.
70 This also should make it safe to remove a bucket during the traversal.
71 Removal and destruction routines will work faster.
72*/
73
75{
76 const void *object; /*!< whatever it is we are storing in this table */
77 struct ast_hashtab_bucket *next; /*!< a DLL of buckets in hash collision */
78 struct ast_hashtab_bucket *prev; /*!< a DLL of buckets in hash collision */
79 struct ast_hashtab_bucket *tnext; /*!< a DLL of all the hash buckets for traversal */
80 struct ast_hashtab_bucket *tprev; /*!< a DLL of all the hash buckets for traversal */
81};
82
84{
86 struct ast_hashtab_bucket *tlist; /*!< the head of a DLList of all the hashbuckets in the table (for traversal). */
87
88 int (*compare) (const void *a, const void *b); /*!< a ptr to func that returns int, and take two void* ptrs, compares them,
89 rets -1 if a < b; rets 0 if a==b; rets 1 if a>b */
90 int (*newsize) (struct ast_hashtab *tab); /*!< a ptr to func that returns int, a new size for hash tab, based on curr_size */
91 int (*resize) (struct ast_hashtab *tab); /*!< a function to decide whether this hashtable should be resized now */
92 unsigned int (*hash) (const void *obj); /*!< a hash func ptr for this table. Given a raw ptr to an obj,
93 it calcs a hash.*/
94 int hash_tab_size; /*!< the size of the bucket array */
95 int hash_tab_elements; /*!< the number of objects currently stored in the table */
96 int largest_bucket_size; /*!< a stat on the health of the table */
97 int resize_count; /*!< a count of the number of times this table has been
98 resized */
99 int do_locking; /*!< if 1 use locks to guarantee safety of insertions/deletions */
100 /* this spot reserved for the proper lock storage */
101 ast_rwlock_t lock; /* is this as good as it sounds? */
102};
103
104/*! \brief an iterator for traversing the buckets */
106{
109};
110
111
112/* some standard, default routines for general use */
113
114/*!
115 * \brief Determines if the specified number is prime.
116 *
117 * \param num the number to test
118 * \retval 0 if the number is not prime
119 * \retval 1 if the number is prime
120 */
121int ast_is_prime(int num);
122
123/*!
124 * \brief Compares two strings for equality.
125 *
126 * \param a a character string
127 * \param b a character string
128 * \retval 0 if the strings match
129 * \retval negative if string a is less than string b
130 * \retval positive if string a is greater than string b
131 */
132int ast_hashtab_compare_strings(const void *a, const void *b);
133
134/*!
135 * \brief Compares two strings for equality, ignoring case.
136 *
137 * \param a a character string
138 * \param b a character string
139 * \retval 0 if the strings match
140 * \retval negative if string a is less than string b
141 * \retval positive if string a is greater than string b
142 */
143int ast_hashtab_compare_strings_nocase(const void *a, const void *b);
144
145/*!
146 * \brief Compares two integers for equality.
147 *
148 * \param a an integer pointer (int *)
149 * \param b an integer pointer (int *)
150 * \retval 0 if the integers pointed to are equal
151 * \retval 1 if a is greater than b
152 * \retval -1 if a is less than b
153 */
154int ast_hashtab_compare_ints(const void *a, const void *b);
155
156/*!
157 * \brief Compares two shorts for equality.
158 *
159 * \param a a short pointer (short *)
160 * \param b a short pointer (short *)
161 * \retval 0 if the shorts pointed to are equal
162 * \retval 1 if a is greater than b
163 * \retval -1 if a is less than b
164 */
165int ast_hashtab_compare_shorts(const void *a, const void *b);
166
167/*!
168 * \brief Determines if a table resize should occur using the Java algorithm
169 * (if the table load factor is 75% or higher).
170 *
171 * \param tab the hash table to operate on
172 * \retval 0 if the table load factor is less than or equal to 75%
173 * \retval 1 if the table load factor is greater than 75%
174 */
175int ast_hashtab_resize_java(struct ast_hashtab *tab);
176
177/*! \brief Causes a resize whenever the number of elements stored in the table
178 * exceeds the number of buckets in the table.
179 *
180 * \param tab the hash table to operate on
181 * \retval 0 if the number of elements in the table is less than or equal to
182 * the number of buckets
183 * \retval 1 if the number of elements in the table exceeds the number of
184 * buckets
185 */
187
188/*!
189 * \brief Effectively disables resizing by always returning 0, regardless of
190 * of load factor.
191 *
192 * \param tab the hash table to operate on
193 * \retval 0 is always returned
194 */
195int ast_hashtab_resize_none(struct ast_hashtab *tab);
196
197/*! \brief Create a prime number roughly 2x the current table size */
199
200/* not yet specified, probably will return 1.5x the current table size */
202
203/*! \brief always return current size -- no resizing */
205
206/*!
207 * \brief Hashes a string to a number
208 *
209 * \param obj the string to hash
210 * \return Integer hash of the specified string
211 * \sa ast_hashtable_hash_string_nocase
212 * \sa ast_hashtab_hash_string_sax
213 * \note A modulus will be applied to the return value of this function
214 */
215unsigned int ast_hashtab_hash_string(const void *obj);
216
217/*!
218 * \brief Hashes a string to a number ignoring case
219 *
220 * \param obj the string to hash
221 * \return Integer hash of the specified string
222 * \sa ast_hashtable_hash_string
223 * \sa ast_hashtab_hash_string_sax
224 * \note A modulus will be applied to the return value of this function
225 */
226unsigned int ast_hashtab_hash_string_nocase(const void *obj);
227
228/*!
229 * \brief Hashes a string to a number using a modified Shift-And-XOR algorithm
230 *
231 * \param obj the string to hash
232 * \return Integer has of the specified string
233 * \sa ast_hastable_hash_string
234 * \sa ast_hastable_hash_string_nocase
235 */
236unsigned int ast_hashtab_hash_string_sax(const void *obj);
237
238
239unsigned int ast_hashtab_hash_int(const int num); /* right now, both these funcs are just result = num%modulus; */
240
241
242unsigned int ast_hashtab_hash_short(const short num);
243
244
245/*!
246 * \brief Create the hashtable list
247 * \param initial_buckets starting number of buckets
248 * \param compare a func ptr to compare two elements in the hash -- cannot be null
249 * \param resize a func ptr to decide if the table needs to be resized, a NULL ptr here will cause a default to be used
250 * \param newsize a func ptr that returns a new size of the array. A NULL will cause a default to be used
251 * \param hash a func ptr to do the hashing
252 * \param do_locking use locks to guarantee safety of iterators/insertion/deletion -- real simpleminded right now
253*/
254#define ast_hashtab_create(initial_buckets, compare, resize, newsize, hash, do_locking) \
255 _ast_hashtab_create(initial_buckets, compare, resize, newsize, hash, do_locking, __FILE__, __LINE__, __PRETTY_FUNCTION__)
256struct ast_hashtab *_ast_hashtab_create(int initial_buckets,
257 int (*compare)(const void *a, const void *b),
258 int (*resize)(struct ast_hashtab *),
259 int (*newsize)(struct ast_hashtab *tab),
260 unsigned int (*hash)(const void *obj),
261 int do_locking,
262 const char *file, int lineno, const char *function);
263
264/*!
265 * \brief This func will free the hash table and all its memory.
266 * \note It doesn't touch the objects stored in it, unless you
267 * specify a destroy func; it will call that func for each
268 * object in the hashtab, remove all the objects, and then
269 * free the hashtab itself. If no destroyfunc is specified
270 * then the routine will assume you will free it yourself.
271 * \param tab
272 * \param objdestroyfunc
273*/
274void ast_hashtab_destroy( struct ast_hashtab *tab, void (*objdestroyfunc)(void *obj));
275
276
277/*!
278 * \brief Insert without checking
279 * \param tab
280 * \param obj
281 *
282 * Normally, you'd insert "safely" by checking to see if the element is
283 * already there; in this case, you must already have checked. If an element
284 * is already in the hashtable, that matches this one, most likely this one
285 * will be found first.
286 * \note will force a resize if the resize func returns 1
287 * \retval 1 on success
288 * \retval 0 if there's a problem
289*/
290#define ast_hashtab_insert_immediate(tab, obj) \
291 _ast_hashtab_insert_immediate(tab, obj, __FILE__, __LINE__, __PRETTY_FUNCTION__)
292int _ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func);
293
294/*!
295 * \brief Insert without checking, hashing or locking
296 * \param tab
297 * \param obj
298 * \param h hashed index value
299 *
300 * \note Will force a resize if the resize func returns 1
301 * \retval 1 on success
302 * \retval 0 if there's a problem
303*/
304#define ast_hashtab_insert_immediate_bucket(tab, obj, h) \
305 _ast_hashtab_insert_immediate_bucket(tab, obj, h, __FILE__, __LINE__, __PRETTY_FUNCTION__)
306
307int _ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h, const char *file, int lineno, const char *func);
308
309/*!
310 * \brief Check and insert new object only if it is not there.
311 * \note Will force a resize if the resize func returns 1
312 * \retval 1 on success
313 * \retval 0 if there's a problem, or it's already there.
314*/
315int _ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func);
316#define ast_hashtab_insert_safe(tab, obj) \
317 _ast_hashtab_insert_safe(tab, obj, __FILE__, __LINE__, __PRETTY_FUNCTION__)
318
319/*!
320 * \brief Lookup this object in the hash table.
321 * \param tab
322 * \param obj
323 * \return a ptr if found
324 * \retval NULL if not found
325*/
326void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj);
327
328/*!
329 * \brief Use this if have the hash val for the object
330 * \note This and avoid the recalc of the hash (the modulus (table_size) is not applied)
331*/
332void * ast_hashtab_lookup_with_hash(struct ast_hashtab *tab, const void *obj, unsigned int hashval);
333
334/*!
335 * \brief Similar to ast_hashtab_lookup but sets h to the key hash value if the lookup fails.
336 * \note This has the modulus applied, and will not be useful for long term storage if the table is resizable.
337*/
338void * ast_hashtab_lookup_bucket(struct ast_hashtab *tab, const void *obj, unsigned int *h);
339
340/*! \brief Returns key stats for the table */
341void ast_hashtab_get_stats( struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets);
342
343/*! \brief Returns the number of elements stored in the hashtab */
344int ast_hashtab_size( struct ast_hashtab *tab);
345
346/*! \brief Returns the size of the bucket array in the hashtab */
347int ast_hashtab_capacity( struct ast_hashtab *tab);
348
349/*! \brief Return a copy of the hash table */
350struct ast_hashtab *_ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_func)(const void *obj), const char *file, int lineno, const char *func);
351#define ast_hashtab_dup(tab, obj_dup_func) \
352 _ast_hashtab_dup(tab, obj_dup_func, __FILE__, __LINE__, __PRETTY_FUNCTION__)
353
354/*! \brief Gives an iterator to hastable */
355struct ast_hashtab_iter *_ast_hashtab_start_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func);
356#define ast_hashtab_start_traversal(tab) \
357 _ast_hashtab_start_traversal(tab, __FILE__, __LINE__, __PRETTY_FUNCTION__)
358
359/*! \brief end the traversal, free the iterator, unlock if necc. */
361
362/*! \brief Gets the next object in the list, advances iter one step returns null on end of traversal */
363void *ast_hashtab_next(struct ast_hashtab_iter *it);
364
365/*! \brief Looks up the object, removes the corresponding bucket */
367
368/*! \brief Hash the object and then compare ptrs in bucket list instead of
369 calling the compare routine, will remove the bucket */
370void *ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj);
371
372/* ------------------ */
373/* for lock-enabled traversals with ability to remove an object during the traversal*/
374/* ------------------ */
375
376/*! \brief Gives an iterator to hastable */
377struct ast_hashtab_iter *_ast_hashtab_start_write_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func);
378#define ast_hashtab_start_write_traversal(tab) \
379 _ast_hashtab_start_write_traversal(tab, __FILE__, __LINE__, __PRETTY_FUNCTION__)
380
381/*! \brief Looks up the object, removes the corresponding bucket */
383
384/*! \brief Hash the object and then compare ptrs in bucket list instead of
385 calling the compare routine, will remove the bucket */
387
388/* ------------------ */
389/* ------------------ */
390
391/* user-controlled hashtab locking. Create a hashtab without locking, then call the
392 following locking routines yourself to lock the table between threads. */
393
394/*! \brief Call this after you create the table to init the lock */
396/*! \brief Request a write-lock on the table. */
398/*! \brief Request a read-lock on the table -- don't change anything! */
400/*! \brief release a read- or write- lock. */
402/*! \brief Call this before you destroy the table. */
404
405
406#endif
static int compare(const char *text, const char *template)
void ast_hashtab_unlock(struct ast_hashtab *tab)
release a read- or write- lock.
Definition: hashtab.c:358
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), int do_locking, const char *file, int lineno, const char *function)
Definition: hashtab.c:216
unsigned int ast_hashtab_hash_string_nocase(const void *obj)
Hashes a string to a number ignoring case.
Definition: hashtab.c:181
int _ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h, const char *file, int lineno, const char *func)
Definition: hashtab.c:425
void * ast_hashtab_remove_object_via_lookup_nolock(struct ast_hashtab *tab, void *obj)
Looks up the object, removes the corresponding bucket.
Definition: hashtab.c:765
struct ast_hashtab_iter * _ast_hashtab_start_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func)
Gives an iterator to hastable.
Definition: hashtab.c:637
void ast_hashtab_wrlock(struct ast_hashtab *tab)
Request a write-lock on the table.
Definition: hashtab.c:338
void ast_hashtab_destroylock(struct ast_hashtab *tab)
Call this before you destroy the table.
Definition: hashtab.c:353
unsigned int ast_hashtab_hash_int(const int num)
Definition: hashtab.c:205
void * ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj)
Hash the object and then compare ptrs in bucket list instead of calling the compare routine,...
Definition: hashtab.c:789
void ast_hashtab_rdlock(struct ast_hashtab *tab)
Request a read-lock on the table – don't change anything!
Definition: hashtab.c:343
unsigned int ast_hashtab_hash_short(const short num)
Definition: hashtab.c:210
int ast_hashtab_compare_ints(const void *a, const void *b)
Compares two integers for equality.
Definition: hashtab.c:62
void ast_hashtab_destroy(struct ast_hashtab *tab, void(*objdestroyfunc)(void *obj))
This func will free the hash table and all its memory.
Definition: hashtab.c:363
void * ast_hashtab_lookup_bucket(struct ast_hashtab *tab, const void *obj, unsigned int *h)
Similar to ast_hashtab_lookup but sets h to the key hash value if the lookup fails.
Definition: hashtab.c:531
int ast_hashtab_newsize_java(struct ast_hashtab *tab)
Create a prime number roughly 2x the current table size.
Definition: hashtab.c:127
int ast_is_prime(int num)
Determines if the specified number is prime.
Definition: hashtab.c:101
int _ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func)
Check and insert new object only if it is not there.
Definition: hashtab.c:460
int ast_hashtab_compare_strings(const void *a, const void *b)
Compares two strings for equality.
Definition: hashtab.c:52
void * ast_hashtab_remove_this_object_nolock(struct ast_hashtab *tab, void *obj)
Hash the object and then compare ptrs in bucket list instead of calling the compare routine,...
Definition: hashtab.c:810
int ast_hashtab_compare_strings_nocase(const void *a, const void *b)
Compares two strings for equality, ignoring case.
Definition: hashtab.c:57
void ast_hashtab_end_traversal(struct ast_hashtab_iter *it)
end the traversal, free the iterator, unlock if necc.
Definition: hashtab.c:674
int ast_hashtab_newsize_none(struct ast_hashtab *tab)
always return current size – no resizing
Definition: hashtab.c:148
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)
Return a copy of the hash table.
Definition: hashtab.c:261
unsigned int ast_hashtab_hash_string_sax(const void *obj)
Hashes a string to a number using a modified Shift-And-XOR algorithm.
Definition: hashtab.c:170
int ast_hashtab_size(struct ast_hashtab *tab)
Returns the number of elements stored in the hashtab.
Definition: hashtab.c:577
void * ast_hashtab_next(struct ast_hashtab_iter *it)
Gets the next object in the list, advances iter one step returns null on end of traversal.
Definition: hashtab.c:683
int ast_hashtab_newsize_tight(struct ast_hashtab *tab)
Definition: hashtab.c:137
int ast_hashtab_resize_tight(struct ast_hashtab *tab)
Causes a resize whenever the number of elements stored in the table exceeds the number of buckets in ...
Definition: hashtab.c:91
void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
Lookup this object in the hash table.
Definition: hashtab.c:486
int _ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func)
Definition: hashtab.c:404
struct ast_hashtab_iter * _ast_hashtab_start_write_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func)
Gives an iterator to hastable.
Definition: hashtab.c:656
int ast_hashtab_compare_shorts(const void *a, const void *b)
Compares two shorts for equality.
Definition: hashtab.c:73
void ast_hashtab_get_stats(struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets)
Returns key stats for the table.
Definition: hashtab.c:563
void * ast_hashtab_lookup_with_hash(struct ast_hashtab *tab, const void *obj, unsigned int hashval)
Use this if have the hash val for the object.
Definition: hashtab.c:509
int ast_hashtab_resize_java(struct ast_hashtab *tab)
Determines if a table resize should occur using the Java algorithm (if the table load factor is 75% o...
Definition: hashtab.c:84
int ast_hashtab_capacity(struct ast_hashtab *tab)
Returns the size of the bucket array in the hashtab.
Definition: hashtab.c:583
void * ast_hashtab_remove_object_via_lookup(struct ast_hashtab *tab, void *obj)
Looks up the object, removes the corresponding bucket.
Definition: hashtab.c:746
unsigned int ast_hashtab_hash_string(const void *obj)
Hashes a string to a number.
Definition: hashtab.c:153
void ast_hashtab_initlock(struct ast_hashtab *tab)
Call this after you create the table to init the lock.
Definition: hashtab.c:348
int ast_hashtab_resize_none(struct ast_hashtab *tab)
Effectively disables resizing by always returning 0, regardless of of load factor.
Definition: hashtab.c:96
Asterisk locking-related definitions:
struct ast_hashtab_bucket * next
Definition: hashtab.h:77
struct ast_hashtab_bucket * tprev
Definition: hashtab.h:80
const void * object
Definition: hashtab.h:76
struct ast_hashtab_bucket * tnext
Definition: hashtab.h:79
struct ast_hashtab_bucket * prev
Definition: hashtab.h:78
an iterator for traversing the buckets
Definition: hashtab.h:106
struct ast_hashtab_bucket * next
Definition: hashtab.h:108
struct ast_hashtab * tab
Definition: hashtab.h:107
int(* newsize)(struct ast_hashtab *tab)
Definition: hashtab.h:90
int hash_tab_elements
Definition: hashtab.h:95
int(* compare)(const void *a, const void *b)
Definition: hashtab.h:88
int(* resize)(struct ast_hashtab *tab)
Definition: hashtab.h:91
int hash_tab_size
Definition: hashtab.h:94
unsigned int(* hash)(const void *obj)
Definition: hashtab.h:92
struct ast_hashtab_bucket * tlist
Definition: hashtab.h:86
int do_locking
Definition: hashtab.h:99
int resize_count
Definition: hashtab.h:97
ast_rwlock_t lock
Definition: hashtab.h:101
struct ast_hashtab_bucket ** array
Definition: hashtab.h:85
int largest_bucket_size
Definition: hashtab.h:96
Structure for rwlock and tracking information.
Definition: lock.h:157
static struct test_val b
static struct test_val a