hashtab.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007, 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. /*! \file
  19. *
  20. * \brief code to implement generic hash tables
  21. *
  22. * \author Steve Murphy <murf@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #define WRAP_LIBC_MALLOC
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include <ctype.h>
  31. #include "asterisk/lock.h"
  32. #include "asterisk/frame.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/cli.h"
  35. #include "asterisk/term.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/threadstorage.h"
  38. #include "asterisk/linkedlists.h"
  39. #include "asterisk/hashtab.h"
  40. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  41. static void _ast_hashtab_resize(struct ast_hashtab *tab, const char *file, int lineno, const char *func);
  42. #define ast_hashtab_resize(a) _ast_hashtab_resize(a,__FILE__, __LINE__, __PRETTY_FUNCTION__)
  43. #else
  44. static void ast_hashtab_resize(struct ast_hashtab *tab);
  45. #endif
  46. static void *ast_hashtab_lookup_internal(struct ast_hashtab *tab, const void *obj, unsigned int h);
  47. /* some standard, default routines for general use */
  48. int ast_hashtab_compare_strings(const void *a, const void *b)
  49. {
  50. return strcmp(a, b);
  51. }
  52. int ast_hashtab_compare_strings_nocase(const void *a, const void *b)
  53. {
  54. return strcasecmp(a, b);
  55. }
  56. int ast_hashtab_compare_ints(const void *a, const void *b)
  57. {
  58. int ai = *((int *) a);
  59. int bi = *((int *) b);
  60. if (ai < bi)
  61. return -1;
  62. return !(ai == bi);
  63. }
  64. int ast_hashtab_compare_shorts(const void *a, const void *b)
  65. {
  66. short as = *((short *) a);
  67. short bs = *((short *) b);
  68. if (as < bs)
  69. return -1;
  70. return !(as == bs);
  71. }
  72. int ast_hashtab_resize_java(struct ast_hashtab *tab)
  73. {
  74. double loadfactor = (double) tab->hash_tab_elements / (double) tab->hash_tab_size;
  75. return (loadfactor > 0.75);
  76. }
  77. int ast_hashtab_resize_tight(struct ast_hashtab *tab)
  78. {
  79. return (tab->hash_tab_elements > tab->hash_tab_size); /* this is quicker than division */
  80. }
  81. int ast_hashtab_resize_none(struct ast_hashtab *tab) /* always return 0 -- no resizing */
  82. {
  83. return 0;
  84. }
  85. int ast_is_prime(int num)
  86. {
  87. int tnum, limit;
  88. if (!(num & 0x1)) /* even number -- not prime */
  89. return 0;
  90. /* Loop through ODD numbers starting with 3 */
  91. tnum = 3;
  92. limit = num;
  93. while (tnum < limit) {
  94. if (!(num % tnum))
  95. return 0;
  96. /* really, we only need to check sqrt(num) numbers */
  97. limit = num / tnum;
  98. /* we only check odd numbers */
  99. tnum = tnum + 2;
  100. }
  101. /* if we made it through the loop, the number is a prime */
  102. return 1;
  103. }
  104. int ast_hashtab_newsize_java(struct ast_hashtab *tab)
  105. {
  106. int i = (tab->hash_tab_size << 1); /* multiply by two */
  107. while (!ast_is_prime(i))
  108. i++;
  109. return i;
  110. }
  111. int ast_hashtab_newsize_tight(struct ast_hashtab *tab)
  112. {
  113. int x = (tab->hash_tab_size << 1);
  114. int i = (tab->hash_tab_size + x);
  115. while (!ast_is_prime(i))
  116. i++;
  117. return i;
  118. }
  119. int ast_hashtab_newsize_none(struct ast_hashtab *tab) /* always return current size -- no resizing */
  120. {
  121. return tab->hash_tab_size;
  122. }
  123. unsigned int ast_hashtab_hash_string(const void *obj)
  124. {
  125. unsigned char *str = (unsigned char *) obj;
  126. unsigned int total;
  127. for (total = 0; *str; str++) {
  128. unsigned int tmp = total;
  129. total <<= 1; /* multiply by 2 */
  130. total += tmp; /* multiply by 3 */
  131. total <<= 2; /* multiply by 12 */
  132. total += tmp; /* multiply by 13 */
  133. total += ((unsigned int)(*str));
  134. }
  135. return total;
  136. }
  137. unsigned int ast_hashtab_hash_string_sax(const void *obj) /* from Josh */
  138. {
  139. const unsigned char *str = obj;
  140. unsigned int total = 0, c = 0;
  141. while ((c = *str++))
  142. total ^= (total << 5) + (total >> 2) + (total << 10) + c;
  143. return total;
  144. }
  145. unsigned int ast_hashtab_hash_string_nocase(const void *obj)
  146. {
  147. const unsigned char *str = obj;
  148. unsigned int total;
  149. for (total = 0; *str; str++) {
  150. unsigned int tmp = total;
  151. unsigned int charval = toupper(*str);
  152. /* hopefully, the following is faster than multiplication by 7 */
  153. /* why do I go to this bother? A good compiler will do this
  154. anyway, if I say total *= 13 */
  155. /* BTW, tried *= 7, and it doesn't do as well in spreading things around! */
  156. total <<= 1; /* multiply by 2 */
  157. total += tmp; /* multiply by 3 */
  158. total <<= 2; /* multiply by 12 */
  159. total += tmp; /* multiply by 13 */
  160. total += (charval);
  161. }
  162. return total;
  163. }
  164. unsigned int ast_hashtab_hash_int(const int x)
  165. {
  166. return x;
  167. }
  168. unsigned int ast_hashtab_hash_short(const short x)
  169. {
  170. /* hmmmm.... modulus is best < 65535 !! */
  171. return x;
  172. }
  173. struct ast_hashtab *
  174. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  175. _ast_hashtab_create
  176. #else
  177. ast_hashtab_create
  178. #endif
  179. (int initial_buckets,
  180. int (*compare)(const void *a, const void *b),
  181. int (*resize)(struct ast_hashtab *),
  182. int (*newsize)(struct ast_hashtab *tab),
  183. unsigned int (*hash)(const void *obj),
  184. int do_locking
  185. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  186. , const char *file, int lineno, const char *function
  187. #endif
  188. )
  189. {
  190. struct ast_hashtab *ht;
  191. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  192. if (!(ht = __ast_calloc(1, sizeof(*ht), file, lineno, function)))
  193. #else
  194. if (!(ht = ast_calloc(1, sizeof(*ht))))
  195. #endif
  196. return NULL;
  197. while (!ast_is_prime(initial_buckets)) /* make sure this is prime */
  198. initial_buckets++;
  199. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  200. if (!(ht->array = __ast_calloc(initial_buckets, sizeof(*(ht->array)), file, lineno, function))) {
  201. #else
  202. if (!(ht->array = ast_calloc(initial_buckets, sizeof(*(ht->array))))) {
  203. #endif
  204. ast_free(ht);
  205. return NULL;
  206. }
  207. ht->hash_tab_size = initial_buckets;
  208. ht->compare = compare;
  209. ht->resize = resize;
  210. ht->newsize = newsize;
  211. ht->hash = hash;
  212. ht->do_locking = do_locking;
  213. if (do_locking)
  214. ast_rwlock_init(&ht->lock);
  215. if (!ht->resize)
  216. ht->resize = ast_hashtab_resize_java;
  217. if (!ht->newsize)
  218. ht->newsize = ast_hashtab_newsize_java;
  219. return ht;
  220. }
  221. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  222. 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)
  223. #else
  224. struct ast_hashtab *ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_func)(const void *obj))
  225. #endif
  226. {
  227. struct ast_hashtab *ht;
  228. unsigned int i;
  229. if (!(ht = ast_calloc(1, sizeof(*ht))))
  230. return NULL;
  231. if (!(ht->array =
  232. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  233. __ast_calloc(tab->hash_tab_size, sizeof(*(ht->array)), file, lineno, func)
  234. #else
  235. ast_calloc(tab->hash_tab_size, sizeof(*(ht->array)))
  236. #endif
  237. )) {
  238. free(ht);
  239. return NULL;
  240. }
  241. ht->hash_tab_size = tab->hash_tab_size;
  242. ht->compare = tab->compare;
  243. ht->resize = tab->resize;
  244. ht->newsize = tab->newsize;
  245. ht->hash = tab->hash;
  246. ht->do_locking = tab->do_locking;
  247. if (ht->do_locking)
  248. ast_rwlock_init(&ht->lock);
  249. /* now, dup the objects in the buckets and get them into the table */
  250. /* the fast way is to use the existing array index, and not have to hash
  251. the objects again */
  252. for (i = 0; i < ht->hash_tab_size; i++) {
  253. struct ast_hashtab_bucket *b = tab->array[i];
  254. while (b) {
  255. void *newobj = (*obj_dup_func)(b->object);
  256. if (newobj)
  257. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  258. _ast_hashtab_insert_immediate_bucket(ht, newobj, i, file, lineno, func);
  259. #else
  260. ast_hashtab_insert_immediate_bucket(ht, newobj, i);
  261. #endif
  262. b = b->next;
  263. }
  264. }
  265. return ht;
  266. }
  267. static void tlist_del_item(struct ast_hashtab_bucket **head, struct ast_hashtab_bucket *item)
  268. {
  269. /* item had better be in the list! or suffer the weirdness that occurs, later! */
  270. if (*head == item) { /* first item in the list */
  271. *head = item->tnext;
  272. if (item->tnext)
  273. item->tnext->tprev = NULL;
  274. } else {
  275. /* short circuit stuff */
  276. item->tprev->tnext = item->tnext;
  277. if (item->tnext)
  278. item->tnext->tprev = item->tprev;
  279. }
  280. }
  281. static void tlist_add_head(struct ast_hashtab_bucket **head, struct ast_hashtab_bucket *item)
  282. {
  283. if (*head) {
  284. item->tnext = *head;
  285. item->tprev = NULL;
  286. (*head)->tprev = item;
  287. *head = item;
  288. } else {
  289. /* the list is empty */
  290. *head = item;
  291. item->tprev = NULL;
  292. item->tnext = NULL;
  293. }
  294. }
  295. /* user-controlled hashtab locking. Create a hashtab without locking, then call the
  296. following locking routines yourself to lock the table between threads. */
  297. void ast_hashtab_wrlock(struct ast_hashtab *tab)
  298. {
  299. ast_rwlock_wrlock(&tab->lock);
  300. }
  301. void ast_hashtab_rdlock(struct ast_hashtab *tab)
  302. {
  303. ast_rwlock_rdlock(&tab->lock);
  304. }
  305. void ast_hashtab_initlock(struct ast_hashtab *tab)
  306. {
  307. ast_rwlock_init(&tab->lock);
  308. }
  309. void ast_hashtab_destroylock(struct ast_hashtab *tab)
  310. {
  311. ast_rwlock_destroy(&tab->lock);
  312. }
  313. void ast_hashtab_unlock(struct ast_hashtab *tab)
  314. {
  315. ast_rwlock_unlock(&tab->lock);
  316. }
  317. void ast_hashtab_destroy(struct ast_hashtab *tab, void (*objdestroyfunc)(void *obj))
  318. {
  319. /* this func will free the hash table and all its memory. It
  320. doesn't touch the objects stored in it */
  321. if (tab) {
  322. if (tab->do_locking)
  323. ast_rwlock_wrlock(&tab->lock);
  324. if (tab->array) {
  325. /* go thru and destroy the buckets */
  326. struct ast_hashtab_bucket *t;
  327. int i;
  328. while (tab->tlist) {
  329. t = tab->tlist;
  330. if (t->object && objdestroyfunc) {
  331. /* I cast this because I'm not going to MOD it, I'm going to DESTROY
  332. * it.
  333. */
  334. (*objdestroyfunc)((void *) t->object);
  335. }
  336. tlist_del_item(&(tab->tlist), tab->tlist);
  337. free(t);
  338. }
  339. for (i = 0; i < tab->hash_tab_size; i++) {
  340. /* Not totally necessary, but best to destroy old pointers */
  341. tab->array[i] = NULL;
  342. }
  343. free(tab->array);
  344. }
  345. if (tab->do_locking) {
  346. ast_rwlock_unlock(&tab->lock);
  347. ast_rwlock_destroy(&tab->lock);
  348. }
  349. free(tab);
  350. }
  351. }
  352. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  353. int _ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func)
  354. #else
  355. int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj)
  356. #endif
  357. {
  358. unsigned int h;
  359. int res=0;
  360. if (!tab || !obj)
  361. return res;
  362. if (tab->do_locking)
  363. ast_rwlock_wrlock(&tab->lock);
  364. h = (*tab->hash)(obj) % tab->hash_tab_size;
  365. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  366. res = _ast_hashtab_insert_immediate_bucket(tab, obj, h, file, lineno, func);
  367. #else
  368. res = ast_hashtab_insert_immediate_bucket(tab, obj, h);
  369. #endif
  370. if (tab->do_locking)
  371. ast_rwlock_unlock(&tab->lock);
  372. return res;
  373. }
  374. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  375. int _ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h, const char *file, int lineno, const char *func)
  376. #else
  377. int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h)
  378. #endif
  379. {
  380. int c;
  381. struct ast_hashtab_bucket *b;
  382. if (!tab || !obj)
  383. return 0;
  384. for (c = 0, b = tab->array[h]; b; b= b->next)
  385. c++;
  386. if (c + 1 > tab->largest_bucket_size)
  387. tab->largest_bucket_size = c + 1;
  388. if (!(b =
  389. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  390. __ast_calloc(1, sizeof(*b), file, lineno, func)
  391. #else
  392. ast_calloc(1, sizeof(*b))
  393. #endif
  394. )) return 0;
  395. b->object = obj;
  396. b->next = tab->array[h];
  397. tab->array[h] = b;
  398. if (b->next)
  399. b->next->prev = b;
  400. tlist_add_head(&(tab->tlist), b);
  401. tab->hash_tab_elements++;
  402. if ((*tab->resize)(tab))
  403. ast_hashtab_resize(tab);
  404. return 1;
  405. }
  406. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  407. int _ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func)
  408. #else
  409. int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj)
  410. #endif
  411. {
  412. /* check to see if the element is already there; insert only if
  413. it is not there. */
  414. /* will force a resize if the resize func returns 1 */
  415. /* returns 1 on success, 0 if there's a problem, or it's already there. */
  416. unsigned int bucket = 0;
  417. if (tab->do_locking)
  418. ast_rwlock_wrlock(&tab->lock);
  419. if (!ast_hashtab_lookup_bucket(tab, obj, &bucket)) {
  420. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  421. int ret2 = _ast_hashtab_insert_immediate_bucket(tab, obj, bucket, file, lineno, func);
  422. #else
  423. int ret2 = ast_hashtab_insert_immediate_bucket(tab, obj, bucket);
  424. #endif
  425. if (tab->do_locking)
  426. ast_rwlock_unlock(&tab->lock);
  427. return ret2;
  428. }
  429. if (tab->do_locking)
  430. ast_rwlock_unlock(&tab->lock);
  431. return 0;
  432. }
  433. void *ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
  434. {
  435. /* lookup this object in the hash table. return a ptr if found, or NULL if not */
  436. unsigned int h;
  437. void *ret;
  438. if (!tab || !obj)
  439. return 0;
  440. if (tab->do_locking)
  441. ast_rwlock_rdlock(&tab->lock);
  442. h = (*tab->hash)(obj) % tab->hash_tab_size;
  443. ret = ast_hashtab_lookup_internal(tab,obj,h);
  444. if (tab->do_locking)
  445. ast_rwlock_unlock(&tab->lock);
  446. return ret;
  447. }
  448. void *ast_hashtab_lookup_with_hash(struct ast_hashtab *tab, const void *obj, unsigned int hashval)
  449. {
  450. /* lookup this object in the hash table. return a ptr if found, or NULL if not */
  451. unsigned int h;
  452. void *ret;
  453. if (!tab || !obj)
  454. return 0;
  455. if (tab->do_locking)
  456. ast_rwlock_rdlock(&tab->lock);
  457. h = hashval % tab->hash_tab_size;
  458. ret = ast_hashtab_lookup_internal(tab,obj,h);
  459. if (tab->do_locking)
  460. ast_rwlock_unlock(&tab->lock);
  461. return ret;
  462. }
  463. void *ast_hashtab_lookup_bucket(struct ast_hashtab *tab, const void *obj, unsigned int *bucket)
  464. {
  465. /* lookup this object in the hash table. return a ptr if found, or NULL if not */
  466. unsigned int h;
  467. void *ret;
  468. if (!tab || !obj)
  469. return 0;
  470. h = (*tab->hash)(obj) % tab->hash_tab_size;
  471. ret = ast_hashtab_lookup_internal(tab,obj,h);
  472. *bucket = h;
  473. return ret;
  474. }
  475. static void *ast_hashtab_lookup_internal(struct ast_hashtab *tab, const void *obj, unsigned int h)
  476. {
  477. struct ast_hashtab_bucket *b;
  478. for (b = tab->array[h]; b; b = b->next) {
  479. if (!(*tab->compare)(obj,b->object)) {
  480. /* I can't touch obj in this func, but the outside world is welcome to */
  481. return (void*) b->object;
  482. }
  483. }
  484. return NULL;
  485. }
  486. void ast_hashtab_get_stats(struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets)
  487. {
  488. /* returns key stats for the table */
  489. if (tab->do_locking)
  490. ast_rwlock_rdlock(&tab->lock);
  491. *biggest_bucket_size = tab->largest_bucket_size;
  492. *resize_count = tab->resize_count;
  493. *num_objects = tab->hash_tab_elements;
  494. *num_buckets = tab->hash_tab_size;
  495. if (tab->do_locking)
  496. ast_rwlock_unlock(&tab->lock);
  497. }
  498. /* this function returns the number of elements stored in the hashtab */
  499. int ast_hashtab_size(struct ast_hashtab *tab)
  500. {
  501. return tab->hash_tab_elements;
  502. }
  503. /* this function returns the size of the bucket array in the hashtab */
  504. int ast_hashtab_capacity( struct ast_hashtab *tab)
  505. {
  506. return tab->hash_tab_size;
  507. }
  508. /* the insert operation calls this, and is wrlock'd when it does. */
  509. /* if you want to call it, you should set the wrlock yourself */
  510. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  511. static void _ast_hashtab_resize(struct ast_hashtab *tab, const char *file, int lineno, const char *func)
  512. #else
  513. static void ast_hashtab_resize(struct ast_hashtab *tab)
  514. #endif
  515. {
  516. /* this function is called either internally, when the resize func returns 1, or
  517. externally by the user to force a resize of the hash table */
  518. int newsize = (*tab->newsize)(tab), i, c;
  519. unsigned int h;
  520. struct ast_hashtab_bucket *b,*bn;
  521. /* Since we keep a DLL of all the buckets in tlist,
  522. all we have to do is free the array, malloc a new one,
  523. and then go thru the tlist array and reassign them into
  524. the bucket arrayj.
  525. */
  526. for (i = 0; i < tab->hash_tab_size; i++) { /* don't absolutely have to do this, but
  527. why leave ptrs laying around */
  528. tab->array[i] = 0; /* erase old ptrs */
  529. }
  530. free(tab->array);
  531. if (!(tab->array =
  532. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  533. __ast_calloc(newsize, sizeof(*(tab->array)), file, lineno, func)
  534. #else
  535. ast_calloc(newsize, sizeof(*(tab->array)))
  536. #endif
  537. ))
  538. return;
  539. /* now sort the buckets into their rightful new slots */
  540. tab->resize_count++;
  541. tab->hash_tab_size = newsize;
  542. tab->largest_bucket_size = 0;
  543. for (b = tab->tlist; b; b = bn) {
  544. b->prev = 0;
  545. bn = b->tnext;
  546. h = (*tab->hash)(b->object) % tab->hash_tab_size;
  547. b->next = tab->array[h];
  548. if (b->next)
  549. b->next->prev = b;
  550. tab->array[h] = b;
  551. }
  552. /* recalc the largest bucket size */
  553. for (i = 0; i < tab->hash_tab_size; i++) {
  554. for (c = 0, b = tab->array[i]; b; b = b->next)
  555. c++;
  556. if (c > tab->largest_bucket_size)
  557. tab->largest_bucket_size = c;
  558. }
  559. }
  560. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  561. struct ast_hashtab_iter *_ast_hashtab_start_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func)
  562. #else
  563. struct ast_hashtab_iter *ast_hashtab_start_traversal(struct ast_hashtab *tab)
  564. #endif
  565. {
  566. /* returns an iterator */
  567. struct ast_hashtab_iter *it;
  568. if (!(it =
  569. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  570. __ast_calloc(1, sizeof(*it), file, lineno, func)
  571. #else
  572. ast_calloc(1, sizeof(*it))
  573. #endif
  574. ))
  575. return NULL;
  576. it->next = tab->tlist;
  577. it->tab = tab;
  578. if (tab->do_locking)
  579. ast_rwlock_rdlock(&tab->lock);
  580. return it;
  581. }
  582. /* use this function to get a write lock */
  583. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  584. struct ast_hashtab_iter *_ast_hashtab_start_write_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func)
  585. #else
  586. struct ast_hashtab_iter *ast_hashtab_start_write_traversal(struct ast_hashtab *tab)
  587. #endif
  588. {
  589. /* returns an iterator */
  590. struct ast_hashtab_iter *it;
  591. if (!(it =
  592. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  593. __ast_calloc(1, sizeof(*it), file, lineno, func)
  594. #else
  595. ast_calloc(1, sizeof(*it))
  596. #endif
  597. ))
  598. return NULL;
  599. it->next = tab->tlist;
  600. it->tab = tab;
  601. if (tab->do_locking)
  602. ast_rwlock_wrlock(&tab->lock);
  603. return it;
  604. }
  605. void ast_hashtab_end_traversal(struct ast_hashtab_iter *it)
  606. {
  607. if (it->tab->do_locking)
  608. ast_rwlock_unlock(&it->tab->lock);
  609. free(it);
  610. }
  611. void *ast_hashtab_next(struct ast_hashtab_iter *it)
  612. {
  613. /* returns the next object in the list, advances iter one step */
  614. struct ast_hashtab_bucket *retval;
  615. if (it && it->next) { /* there's a next in the bucket list */
  616. retval = it->next;
  617. it->next = retval->tnext;
  618. return (void *) retval->object;
  619. }
  620. return NULL;
  621. }
  622. static void *ast_hashtab_remove_object_internal(struct ast_hashtab *tab, struct ast_hashtab_bucket *b, int h)
  623. {
  624. const void *obj2;
  625. if (b->prev)
  626. b->prev->next = b->next;
  627. else
  628. tab->array[h] = b->next;
  629. if (b->next)
  630. b->next->prev = b->prev;
  631. tlist_del_item(&(tab->tlist), b);
  632. obj2 = b->object;
  633. b->object = b->next = (void*)2;
  634. free(b); /* free up the hashbucket */
  635. tab->hash_tab_elements--;
  636. #ifdef DEBUG
  637. {
  638. int c2;
  639. struct ast_hashtab_bucket *b2;
  640. /* do a little checking */
  641. for (c2 = 0, b2 = tab->tlist; b2; b2 = b2->tnext) {
  642. c2++;
  643. }
  644. if (c2 != tab->hash_tab_elements) {
  645. printf("Hey! we didn't delete right! there are %d elements in the list, and we expected %d\n",
  646. c2, tab->hash_tab_elements);
  647. }
  648. for (c2 = 0, b2 = tab->tlist; b2; b2 = b2->tnext) {
  649. unsigned int obj3 = (unsigned long) obj2;
  650. unsigned int b3 = (unsigned long) b;
  651. if (b2->object == obj2)
  652. printf("Hey-- you've still got a bucket pointing at ht_element %x\n", obj3);
  653. if (b2->next == b)
  654. printf("Hey-- you've still got a bucket with next ptr pointing to deleted bucket %x\n", b3);
  655. if (b2->prev == b)
  656. printf("Hey-- you've still got a bucket with prev ptr pointing to deleted bucket %x\n", b3);
  657. if (b2->tprev == b)
  658. printf("Hey-- you've still got a bucket with tprev ptr pointing to deleted bucket %x\n", b3);
  659. if (b2->tnext == b)
  660. printf("Hey-- you've still got a bucket with tnext ptr pointing to deleted bucket %x\n", b3);
  661. }
  662. }
  663. #endif
  664. return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
  665. }
  666. void *ast_hashtab_remove_object_via_lookup(struct ast_hashtab *tab, void *obj)
  667. {
  668. /* looks up the object; removes the corresponding bucket */
  669. const void *obj2;
  670. if (!tab || !obj)
  671. return 0;
  672. if (tab->do_locking)
  673. ast_rwlock_wrlock(&tab->lock);
  674. obj2 = ast_hashtab_remove_object_via_lookup_nolock(tab,obj);
  675. if (tab->do_locking)
  676. ast_rwlock_unlock(&tab->lock);
  677. return (void *)obj2;
  678. }
  679. void *ast_hashtab_remove_object_via_lookup_nolock(struct ast_hashtab *tab, void *obj)
  680. {
  681. /* looks up the object; removes the corresponding bucket */
  682. unsigned int h;
  683. struct ast_hashtab_bucket *b;
  684. if (!tab || !obj)
  685. return 0;
  686. h = (*tab->hash)(obj) % tab->hash_tab_size;
  687. for (b = tab->array[h]; b; b = b->next) {
  688. if (!(*tab->compare)(obj, b->object)) {
  689. const void *obj2;
  690. obj2 = ast_hashtab_remove_object_internal(tab, b, h);
  691. return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
  692. }
  693. }
  694. return 0;
  695. }
  696. void *ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj)
  697. {
  698. /* looks up the object by hash and then comparing pts in bucket list instead of
  699. calling the compare routine; removes the bucket -- a slightly cheaper operation */
  700. /* looks up the object; removes the corresponding bucket */
  701. const void *obj2;
  702. if (!tab || !obj)
  703. return 0;
  704. if (tab->do_locking)
  705. ast_rwlock_wrlock(&tab->lock);
  706. obj2 = ast_hashtab_remove_this_object_nolock(tab,obj);
  707. if (tab->do_locking)
  708. ast_rwlock_unlock(&tab->lock);
  709. return (void *)obj2;
  710. }
  711. void *ast_hashtab_remove_this_object_nolock(struct ast_hashtab *tab, void *obj)
  712. {
  713. /* looks up the object by hash and then comparing pts in bucket list instead of
  714. calling the compare routine; removes the bucket -- a slightly cheaper operation */
  715. /* looks up the object; removes the corresponding bucket */
  716. unsigned int h;
  717. struct ast_hashtab_bucket *b;
  718. if (!tab || !obj)
  719. return 0;
  720. h = (*tab->hash)(obj) % tab->hash_tab_size;
  721. for (b = tab->array[h]; b; b = b->next) {
  722. if (obj == b->object) {
  723. const void *obj2;
  724. obj2 = ast_hashtab_remove_object_internal(tab, b, h);
  725. return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
  726. }
  727. }
  728. return 0;
  729. }