astobj2_hash.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * astobj2_hash - Hash table implementation for astobj2.
  3. *
  4. * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Hash table functions implementing astobj2 containers.
  19. *
  20. * \author Richard Mudgett <rmudgett@digium.com>
  21. */
  22. #include "asterisk.h"
  23. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  24. #include "asterisk/_private.h"
  25. #include "asterisk/astobj2.h"
  26. #include "astobj2_private.h"
  27. #include "astobj2_container_private.h"
  28. #include "asterisk/dlinkedlists.h"
  29. #include "asterisk/utils.h"
  30. /*!
  31. * A structure to create a linked list of entries,
  32. * used within a bucket.
  33. */
  34. struct hash_bucket_node {
  35. /*!
  36. * \brief Items common to all container nodes.
  37. * \note Must be first in the specific node struct.
  38. */
  39. struct ao2_container_node common;
  40. /*! Next node links in the list. */
  41. AST_DLLIST_ENTRY(hash_bucket_node) links;
  42. /*! Hash bucket holding the node. */
  43. int my_bucket;
  44. };
  45. struct hash_bucket {
  46. /*! List of objects held in the bucket. */
  47. AST_DLLIST_HEAD_NOLOCK(, hash_bucket_node) list;
  48. #if defined(AO2_DEBUG)
  49. /*! Number of elements currently in the bucket. */
  50. int elements;
  51. /*! Maximum number of elements in the bucket. */
  52. int max_elements;
  53. #endif /* defined(AO2_DEBUG) */
  54. };
  55. /*!
  56. * A hash container in addition to values common to all
  57. * container types, stores the hash callback function, the
  58. * number of hash buckets, and the hash bucket heads.
  59. */
  60. struct ao2_container_hash {
  61. /*!
  62. * \brief Items common to all containers.
  63. * \note Must be first in the specific container struct.
  64. */
  65. struct ao2_container common;
  66. ao2_hash_fn *hash_fn;
  67. /*! Number of hash buckets in this container. */
  68. int n_buckets;
  69. /*! Hash bucket array of n_buckets. Variable size. */
  70. struct hash_bucket buckets[0];
  71. };
  72. /*! Traversal state to restart a hash container traversal. */
  73. struct hash_traversal_state {
  74. /*! Active sort function in the traversal if not NULL. */
  75. ao2_sort_fn *sort_fn;
  76. /*! Saved comparison callback arg pointer. */
  77. void *arg;
  78. /*! Starting hash bucket */
  79. int bucket_start;
  80. /*! Stopping hash bucket */
  81. int bucket_last;
  82. /*! Saved search flags to control traversing the container. */
  83. enum search_flags flags;
  84. /*! TRUE if it is a descending search */
  85. unsigned int descending:1;
  86. };
  87. struct hash_traversal_state_check {
  88. /*
  89. * If we have a division by zero compile error here then there
  90. * is not enough room for the state. Increase AO2_TRAVERSAL_STATE_SIZE.
  91. */
  92. char check[1 / (AO2_TRAVERSAL_STATE_SIZE / sizeof(struct hash_traversal_state))];
  93. };
  94. /*!
  95. * \internal
  96. * \brief Create an empty copy of this container.
  97. * \since 12.0.0
  98. *
  99. * \param self Container to operate upon.
  100. *
  101. * \retval empty-clone-container on success.
  102. * \retval NULL on error.
  103. */
  104. static struct ao2_container *hash_ao2_alloc_empty_clone(struct ao2_container_hash *self)
  105. {
  106. if (!is_ao2_object(self)) {
  107. return NULL;
  108. }
  109. return ao2_t_container_alloc_hash(ao2_options_get(self), self->common.options, self->n_buckets,
  110. self->hash_fn, self->common.sort_fn, self->common.cmp_fn, "Clone hash container");
  111. }
  112. /*!
  113. * \internal
  114. * \brief Create an empty copy of this container. (Debug version)
  115. * \since 12.0.0
  116. *
  117. * \param self Container to operate upon.
  118. * \param tag used for debugging.
  119. * \param file Debug file name invoked from
  120. * \param line Debug line invoked from
  121. * \param func Debug function name invoked from
  122. * \param ref_debug TRUE if to output a debug reference message.
  123. *
  124. * \retval empty-clone-container on success.
  125. * \retval NULL on error.
  126. */
  127. static struct ao2_container *hash_ao2_alloc_empty_clone_debug(struct ao2_container_hash *self, const char *tag, const char *file, int line, const char *func, int ref_debug)
  128. {
  129. if (!is_ao2_object(self)) {
  130. return NULL;
  131. }
  132. return __ao2_container_alloc_hash_debug(ao2_options_get(self), self->common.options,
  133. self->n_buckets, self->hash_fn, self->common.sort_fn, self->common.cmp_fn,
  134. tag, file, line, func, ref_debug);
  135. }
  136. /*!
  137. * \internal
  138. * \brief Destroy a hash container list node.
  139. * \since 12.0.0
  140. *
  141. * \param v_doomed Container node to destroy.
  142. *
  143. * \details
  144. * The container node unlinks itself from the container as part
  145. * of its destruction. The node must be destroyed while the
  146. * container is already locked.
  147. *
  148. * \note The container must be locked when the node is
  149. * unreferenced.
  150. *
  151. * \return Nothing
  152. */
  153. static void hash_ao2_node_destructor(void *v_doomed)
  154. {
  155. struct hash_bucket_node *doomed = v_doomed;
  156. if (doomed->common.is_linked) {
  157. struct ao2_container_hash *my_container;
  158. struct hash_bucket *bucket;
  159. /*
  160. * Promote to write lock if not already there. Since
  161. * adjust_lock() can potentially release and block waiting for a
  162. * write lock, care must be taken to ensure that node references
  163. * are released before releasing the container references.
  164. *
  165. * Node references held by an iterator can only be held while
  166. * the iterator also holds a reference to the container. These
  167. * node references must be unreferenced before the container can
  168. * be unreferenced to ensure that the node will not get a
  169. * negative reference and the destructor called twice for the
  170. * same node.
  171. */
  172. my_container = (struct ao2_container_hash *) doomed->common.my_container;
  173. #if defined(AST_DEVMODE)
  174. is_ao2_object(my_container);
  175. #endif
  176. __adjust_lock(my_container, AO2_LOCK_REQ_WRLOCK, 1);
  177. #if defined(AO2_DEBUG)
  178. if (!my_container->common.destroying
  179. && ao2_container_check(doomed->common.my_container, OBJ_NOLOCK)) {
  180. ast_log(LOG_ERROR, "Container integrity failed before node deletion.\n");
  181. }
  182. #endif /* defined(AO2_DEBUG) */
  183. bucket = &my_container->buckets[doomed->my_bucket];
  184. AST_DLLIST_REMOVE(&bucket->list, doomed, links);
  185. AO2_DEVMODE_STAT(--my_container->common.nodes);
  186. }
  187. /*
  188. * We could have an object in the node if the container is being
  189. * destroyed or the node had not been linked in yet.
  190. */
  191. if (doomed->common.obj) {
  192. __container_unlink_node(&doomed->common, AO2_UNLINK_NODE_UNLINK_OBJECT);
  193. }
  194. }
  195. /*!
  196. * \internal
  197. * \brief Create a new container node.
  198. * \since 12.0.0
  199. *
  200. * \param self Container to operate upon.
  201. * \param obj_new Object to put into the node.
  202. * \param tag used for debugging.
  203. * \param file Debug file name invoked from
  204. * \param line Debug line invoked from
  205. * \param func Debug function name invoked from
  206. *
  207. * \retval initialized-node on success.
  208. * \retval NULL on error.
  209. */
  210. static struct hash_bucket_node *hash_ao2_new_node(struct ao2_container_hash *self, void *obj_new, const char *tag, const char *file, int line, const char *func)
  211. {
  212. struct hash_bucket_node *node;
  213. int i;
  214. node = __ao2_alloc(sizeof(*node), hash_ao2_node_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  215. if (!node) {
  216. return NULL;
  217. }
  218. i = abs(self->hash_fn(obj_new, OBJ_SEARCH_OBJECT) % self->n_buckets);
  219. if (tag) {
  220. __ao2_ref_debug(obj_new, +1, tag, file, line, func);
  221. } else {
  222. ao2_t_ref(obj_new, +1, "Container node creation");
  223. }
  224. node->common.obj = obj_new;
  225. node->common.my_container = (struct ao2_container *) self;
  226. node->my_bucket = i;
  227. return node;
  228. }
  229. /*!
  230. * \internal
  231. * \brief Insert a node into this container.
  232. * \since 12.0.0
  233. *
  234. * \param self Container to operate upon.
  235. * \param node Container node to insert into the container.
  236. *
  237. * \return enum ao2_container_insert value.
  238. */
  239. static enum ao2_container_insert hash_ao2_insert_node(struct ao2_container_hash *self,
  240. struct hash_bucket_node *node)
  241. {
  242. int cmp;
  243. struct hash_bucket *bucket;
  244. struct hash_bucket_node *cur;
  245. ao2_sort_fn *sort_fn;
  246. uint32_t options;
  247. bucket = &self->buckets[node->my_bucket];
  248. sort_fn = self->common.sort_fn;
  249. options = self->common.options;
  250. if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) {
  251. if (sort_fn) {
  252. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&bucket->list, cur, links) {
  253. cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  254. if (cmp > 0) {
  255. continue;
  256. }
  257. if (cmp < 0) {
  258. AST_DLLIST_INSERT_AFTER_CURRENT(node, links);
  259. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  260. }
  261. switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  262. default:
  263. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  264. break;
  265. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  266. /* Reject all objects with the same key. */
  267. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  268. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  269. if (cur->common.obj == node->common.obj) {
  270. /* Reject inserting the same object */
  271. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  272. }
  273. break;
  274. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  275. SWAP(cur->common.obj, node->common.obj);
  276. __ao2_ref(node, -1);
  277. return AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED;
  278. }
  279. }
  280. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
  281. }
  282. AST_DLLIST_INSERT_HEAD(&bucket->list, node, links);
  283. } else {
  284. if (sort_fn) {
  285. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&bucket->list, cur, links) {
  286. cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  287. if (cmp < 0) {
  288. continue;
  289. }
  290. if (cmp > 0) {
  291. AST_DLLIST_INSERT_BEFORE_CURRENT(node, links);
  292. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  293. }
  294. switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  295. default:
  296. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  297. break;
  298. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  299. /* Reject all objects with the same key. */
  300. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  301. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  302. if (cur->common.obj == node->common.obj) {
  303. /* Reject inserting the same object */
  304. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  305. }
  306. break;
  307. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  308. SWAP(cur->common.obj, node->common.obj);
  309. __ao2_ref(node, -1);
  310. return AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED;
  311. }
  312. }
  313. AST_DLLIST_TRAVERSE_SAFE_END;
  314. }
  315. AST_DLLIST_INSERT_TAIL(&bucket->list, node, links);
  316. }
  317. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  318. }
  319. /*!
  320. * \internal
  321. * \brief Find the first hash container node in a traversal.
  322. * \since 12.0.0
  323. *
  324. * \param self Container to operate upon.
  325. * \param flags search_flags to control traversing the container
  326. * \param arg Comparison callback arg parameter.
  327. * \param state Traversal state to restart hash container traversal.
  328. *
  329. * \retval node-ptr of found node (Reffed).
  330. * \retval NULL when no node found.
  331. */
  332. static struct hash_bucket_node *hash_ao2_find_first(struct ao2_container_hash *self, enum search_flags flags, void *arg, struct hash_traversal_state *state)
  333. {
  334. struct hash_bucket_node *node;
  335. int bucket_cur;
  336. int cmp;
  337. memset(state, 0, sizeof(*state));
  338. state->arg = arg;
  339. state->flags = flags;
  340. /* Determine traversal order. */
  341. switch (flags & OBJ_ORDER_MASK) {
  342. case OBJ_ORDER_POST:
  343. case OBJ_ORDER_DESCENDING:
  344. state->descending = 1;
  345. break;
  346. case OBJ_ORDER_PRE:
  347. case OBJ_ORDER_ASCENDING:
  348. default:
  349. break;
  350. }
  351. /*
  352. * If lookup by pointer or search key, run the hash and optional
  353. * sort functions. Otherwise, traverse the whole container.
  354. */
  355. switch (flags & OBJ_SEARCH_MASK) {
  356. case OBJ_SEARCH_OBJECT:
  357. case OBJ_SEARCH_KEY:
  358. /* we know hash can handle this case */
  359. bucket_cur = abs(self->hash_fn(arg, flags & OBJ_SEARCH_MASK)
  360. % self->n_buckets);
  361. state->sort_fn = self->common.sort_fn;
  362. break;
  363. case OBJ_SEARCH_PARTIAL_KEY:
  364. /* scan all buckets for partial key matches */
  365. bucket_cur = -1;
  366. state->sort_fn = self->common.sort_fn;
  367. break;
  368. default:
  369. /* don't know, let's scan all buckets */
  370. bucket_cur = -1;
  371. state->sort_fn = NULL;
  372. break;
  373. }
  374. if (state->descending) {
  375. /*
  376. * Determine the search boundaries of a descending traversal.
  377. *
  378. * bucket_cur downto state->bucket_last
  379. */
  380. if (bucket_cur < 0) {
  381. bucket_cur = self->n_buckets - 1;
  382. state->bucket_last = 0;
  383. } else {
  384. state->bucket_last = bucket_cur;
  385. }
  386. state->bucket_start = bucket_cur;
  387. /* For each bucket */
  388. for (; state->bucket_last <= bucket_cur; --bucket_cur) {
  389. /* For each node in the bucket. */
  390. for (node = AST_DLLIST_LAST(&self->buckets[bucket_cur].list);
  391. node;
  392. node = AST_DLLIST_PREV(node, links)) {
  393. if (!node->common.obj) {
  394. /* Node is empty */
  395. continue;
  396. }
  397. if (state->sort_fn) {
  398. /* Filter node through the sort_fn */
  399. cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
  400. if (cmp > 0) {
  401. continue;
  402. }
  403. if (cmp < 0) {
  404. /* No more nodes in this bucket are possible to match. */
  405. break;
  406. }
  407. }
  408. /* We have the first traversal node */
  409. __ao2_ref(node, +1);
  410. return node;
  411. }
  412. }
  413. } else {
  414. /*
  415. * Determine the search boundaries of an ascending traversal.
  416. *
  417. * bucket_cur to state->bucket_last-1
  418. */
  419. if (bucket_cur < 0) {
  420. bucket_cur = 0;
  421. state->bucket_last = self->n_buckets;
  422. } else {
  423. state->bucket_last = bucket_cur + 1;
  424. }
  425. state->bucket_start = bucket_cur;
  426. /* For each bucket */
  427. for (; bucket_cur < state->bucket_last; ++bucket_cur) {
  428. /* For each node in the bucket. */
  429. for (node = AST_DLLIST_FIRST(&self->buckets[bucket_cur].list);
  430. node;
  431. node = AST_DLLIST_NEXT(node, links)) {
  432. if (!node->common.obj) {
  433. /* Node is empty */
  434. continue;
  435. }
  436. if (state->sort_fn) {
  437. /* Filter node through the sort_fn */
  438. cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
  439. if (cmp < 0) {
  440. continue;
  441. }
  442. if (cmp > 0) {
  443. /* No more nodes in this bucket are possible to match. */
  444. break;
  445. }
  446. }
  447. /* We have the first traversal node */
  448. __ao2_ref(node, +1);
  449. return node;
  450. }
  451. }
  452. }
  453. return NULL;
  454. }
  455. /*!
  456. * \internal
  457. * \brief Find the next hash container node in a traversal.
  458. * \since 12.0.0
  459. *
  460. * \param self Container to operate upon.
  461. * \param state Traversal state to restart hash container traversal.
  462. * \param prev Previous node returned by the traversal search functions.
  463. * The ref ownership is passed back to this function.
  464. *
  465. * \retval node-ptr of found node (Reffed).
  466. * \retval NULL when no node found.
  467. */
  468. static struct hash_bucket_node *hash_ao2_find_next(struct ao2_container_hash *self, struct hash_traversal_state *state, struct hash_bucket_node *prev)
  469. {
  470. struct hash_bucket_node *node;
  471. void *arg;
  472. enum search_flags flags;
  473. int bucket_cur;
  474. int cmp;
  475. arg = state->arg;
  476. flags = state->flags;
  477. bucket_cur = prev->my_bucket;
  478. node = prev;
  479. /*
  480. * This function is structured the same as hash_ao2_find_first()
  481. * intentionally. We are resuming the search loops from
  482. * hash_ao2_find_first() in order to find the next node. The
  483. * search loops must be resumed where hash_ao2_find_first()
  484. * returned with the first node.
  485. */
  486. if (state->descending) {
  487. goto hash_descending_resume;
  488. /* For each bucket */
  489. for (; state->bucket_last <= bucket_cur; --bucket_cur) {
  490. /* For each node in the bucket. */
  491. for (node = AST_DLLIST_LAST(&self->buckets[bucket_cur].list);
  492. node;
  493. node = AST_DLLIST_PREV(node, links)) {
  494. if (!node->common.obj) {
  495. /* Node is empty */
  496. continue;
  497. }
  498. if (state->sort_fn) {
  499. /* Filter node through the sort_fn */
  500. cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
  501. if (cmp > 0) {
  502. continue;
  503. }
  504. if (cmp < 0) {
  505. /* No more nodes in this bucket are possible to match. */
  506. break;
  507. }
  508. }
  509. /* We have the next traversal node */
  510. __ao2_ref(node, +1);
  511. /*
  512. * Dereferencing the prev node may result in our next node
  513. * object being removed by another thread. This could happen if
  514. * the container uses RW locks and the container was read
  515. * locked.
  516. */
  517. __ao2_ref(prev, -1);
  518. if (node->common.obj) {
  519. return node;
  520. }
  521. prev = node;
  522. hash_descending_resume:;
  523. }
  524. }
  525. } else {
  526. goto hash_ascending_resume;
  527. /* For each bucket */
  528. for (; bucket_cur < state->bucket_last; ++bucket_cur) {
  529. /* For each node in the bucket. */
  530. for (node = AST_DLLIST_FIRST(&self->buckets[bucket_cur].list);
  531. node;
  532. node = AST_DLLIST_NEXT(node, links)) {
  533. if (!node->common.obj) {
  534. /* Node is empty */
  535. continue;
  536. }
  537. if (state->sort_fn) {
  538. /* Filter node through the sort_fn */
  539. cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
  540. if (cmp < 0) {
  541. continue;
  542. }
  543. if (cmp > 0) {
  544. /* No more nodes in this bucket are possible to match. */
  545. break;
  546. }
  547. }
  548. /* We have the next traversal node */
  549. __ao2_ref(node, +1);
  550. /*
  551. * Dereferencing the prev node may result in our next node
  552. * object being removed by another thread. This could happen if
  553. * the container uses RW locks and the container was read
  554. * locked.
  555. */
  556. __ao2_ref(prev, -1);
  557. if (node->common.obj) {
  558. return node;
  559. }
  560. prev = node;
  561. hash_ascending_resume:;
  562. }
  563. }
  564. }
  565. /* No more nodes in the container left to traverse. */
  566. __ao2_ref(prev, -1);
  567. return NULL;
  568. }
  569. /*!
  570. * \internal
  571. * \brief Find the next non-empty iteration node in the container.
  572. * \since 12.0.0
  573. *
  574. * \param self Container to operate upon.
  575. * \param node Previous node returned by the iterator.
  576. * \param flags search_flags to control iterating the container.
  577. * Only AO2_ITERATOR_DESCENDING is useful by the method.
  578. *
  579. * \note The container is already locked.
  580. *
  581. * \retval node on success.
  582. * \retval NULL on error or no more nodes in the container.
  583. */
  584. static struct hash_bucket_node *hash_ao2_iterator_next(struct ao2_container_hash *self, struct hash_bucket_node *node, enum ao2_iterator_flags flags)
  585. {
  586. int cur_bucket;
  587. if (flags & AO2_ITERATOR_DESCENDING) {
  588. if (node) {
  589. cur_bucket = node->my_bucket;
  590. /* Find next non-empty node. */
  591. for (;;) {
  592. node = AST_DLLIST_PREV(node, links);
  593. if (!node) {
  594. break;
  595. }
  596. if (node->common.obj) {
  597. /* Found a non-empty node. */
  598. return node;
  599. }
  600. }
  601. } else {
  602. /* Find first non-empty node. */
  603. cur_bucket = self->n_buckets;
  604. }
  605. /* Find a non-empty node in the remaining buckets */
  606. while (0 <= --cur_bucket) {
  607. node = AST_DLLIST_LAST(&self->buckets[cur_bucket].list);
  608. while (node) {
  609. if (node->common.obj) {
  610. /* Found a non-empty node. */
  611. return node;
  612. }
  613. node = AST_DLLIST_PREV(node, links);
  614. }
  615. }
  616. } else {
  617. if (node) {
  618. cur_bucket = node->my_bucket;
  619. /* Find next non-empty node. */
  620. for (;;) {
  621. node = AST_DLLIST_NEXT(node, links);
  622. if (!node) {
  623. break;
  624. }
  625. if (node->common.obj) {
  626. /* Found a non-empty node. */
  627. return node;
  628. }
  629. }
  630. } else {
  631. /* Find first non-empty node. */
  632. cur_bucket = -1;
  633. }
  634. /* Find a non-empty node in the remaining buckets */
  635. while (++cur_bucket < self->n_buckets) {
  636. node = AST_DLLIST_FIRST(&self->buckets[cur_bucket].list);
  637. while (node) {
  638. if (node->common.obj) {
  639. /* Found a non-empty node. */
  640. return node;
  641. }
  642. node = AST_DLLIST_NEXT(node, links);
  643. }
  644. }
  645. }
  646. /* No more nodes to visit in the container. */
  647. return NULL;
  648. }
  649. #if defined(AO2_DEBUG)
  650. /*!
  651. * \internal
  652. * \brief Increment the hash container linked object statistic.
  653. * \since 12.0.0
  654. *
  655. * \param hash Container to operate upon.
  656. * \param hash_node Container node linking object to.
  657. *
  658. * \return Nothing
  659. */
  660. static void hash_ao2_link_node_stat(struct ao2_container *hash, struct ao2_container_node *hash_node)
  661. {
  662. struct ao2_container_hash *self = (struct ao2_container_hash *) hash;
  663. struct hash_bucket_node *node = (struct hash_bucket_node *) hash_node;
  664. int i = node->my_bucket;
  665. ++self->buckets[i].elements;
  666. if (self->buckets[i].max_elements < self->buckets[i].elements) {
  667. self->buckets[i].max_elements = self->buckets[i].elements;
  668. }
  669. }
  670. #endif /* defined(AO2_DEBUG) */
  671. #if defined(AO2_DEBUG)
  672. /*!
  673. * \internal
  674. * \brief Decrement the hash container linked object statistic.
  675. * \since 12.0.0
  676. *
  677. * \param hash Container to operate upon.
  678. * \param hash_node Container node unlinking object from.
  679. *
  680. * \return Nothing
  681. */
  682. static void hash_ao2_unlink_node_stat(struct ao2_container *hash, struct ao2_container_node *hash_node)
  683. {
  684. struct ao2_container_hash *self = (struct ao2_container_hash *) hash;
  685. struct hash_bucket_node *node = (struct hash_bucket_node *) hash_node;
  686. --self->buckets[node->my_bucket].elements;
  687. }
  688. #endif /* defined(AO2_DEBUG) */
  689. /*!
  690. * \internal
  691. *
  692. * \brief Destroy this container.
  693. * \since 12.0.0
  694. *
  695. * \param self Container to operate upon.
  696. *
  697. * \return Nothing
  698. */
  699. static void hash_ao2_destroy(struct ao2_container_hash *self)
  700. {
  701. int idx;
  702. /* Check that the container no longer has any nodes */
  703. for (idx = self->n_buckets; idx--;) {
  704. if (!AST_DLLIST_EMPTY(&self->buckets[idx].list)) {
  705. ast_log(LOG_ERROR, "Node ref leak. Hash container still has nodes!\n");
  706. ast_assert(0);
  707. break;
  708. }
  709. }
  710. }
  711. #if defined(AO2_DEBUG)
  712. /*!
  713. * \internal
  714. * \brief Display contents of the specified container.
  715. * \since 12.0.0
  716. *
  717. * \param self Container to dump.
  718. * \param where User data needed by prnt to determine where to put output.
  719. * \param prnt Print output callback function to use.
  720. * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
  721. *
  722. * \return Nothing
  723. */
  724. static void hash_ao2_dump(struct ao2_container_hash *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj)
  725. {
  726. #define FORMAT "%6s, %16s, %16s, %16s, %16s, %s\n"
  727. #define FORMAT2 "%6d, %16p, %16p, %16p, %16p, "
  728. int bucket;
  729. int suppressed_buckets = 0;
  730. struct hash_bucket_node *node;
  731. prnt(where, "Number of buckets: %d\n\n", self->n_buckets);
  732. prnt(where, FORMAT, "Bucket", "Node", "Prev", "Next", "Obj", "Key");
  733. for (bucket = 0; bucket < self->n_buckets; ++bucket) {
  734. node = AST_DLLIST_FIRST(&self->buckets[bucket].list);
  735. if (node) {
  736. suppressed_buckets = 0;
  737. do {
  738. prnt(where, FORMAT2,
  739. bucket,
  740. node,
  741. AST_DLLIST_PREV(node, links),
  742. AST_DLLIST_NEXT(node, links),
  743. node->common.obj);
  744. if (node->common.obj && prnt_obj) {
  745. prnt_obj(node->common.obj, where, prnt);
  746. }
  747. prnt(where, "\n");
  748. node = AST_DLLIST_NEXT(node, links);
  749. } while (node);
  750. } else if (!suppressed_buckets) {
  751. suppressed_buckets = 1;
  752. prnt(where, "...\n");
  753. }
  754. }
  755. #undef FORMAT
  756. #undef FORMAT2
  757. }
  758. #endif /* defined(AO2_DEBUG) */
  759. #if defined(AO2_DEBUG)
  760. /*!
  761. * \internal
  762. * \brief Display statistics of the specified container.
  763. * \since 12.0.0
  764. *
  765. * \param self Container to display statistics.
  766. * \param where User data needed by prnt to determine where to put output.
  767. * \param prnt Print output callback function to use.
  768. *
  769. * \note The container is already locked for reading.
  770. *
  771. * \return Nothing
  772. */
  773. static void hash_ao2_stats(struct ao2_container_hash *self, void *where, ao2_prnt_fn *prnt)
  774. {
  775. #define FORMAT "%10.10s %10.10s %10.10s\n"
  776. #define FORMAT2 "%10d %10d %10d\n"
  777. int bucket;
  778. int suppressed_buckets = 0;
  779. prnt(where, "Number of buckets: %d\n\n", self->n_buckets);
  780. prnt(where, FORMAT, "Bucket", "Objects", "Max");
  781. for (bucket = 0; bucket < self->n_buckets; ++bucket) {
  782. if (self->buckets[bucket].max_elements) {
  783. suppressed_buckets = 0;
  784. prnt(where, FORMAT2, bucket, self->buckets[bucket].elements,
  785. self->buckets[bucket].max_elements);
  786. } else if (!suppressed_buckets) {
  787. suppressed_buckets = 1;
  788. prnt(where, "...\n");
  789. }
  790. }
  791. #undef FORMAT
  792. #undef FORMAT2
  793. }
  794. #endif /* defined(AO2_DEBUG) */
  795. #if defined(AO2_DEBUG)
  796. /*!
  797. * \internal
  798. * \brief Perform an integrity check on the specified container.
  799. * \since 12.0.0
  800. *
  801. * \param self Container to check integrity.
  802. *
  803. * \note The container is already locked for reading.
  804. *
  805. * \retval 0 on success.
  806. * \retval -1 on error.
  807. */
  808. static int hash_ao2_integrity(struct ao2_container_hash *self)
  809. {
  810. int bucket_exp;
  811. int bucket;
  812. int count_obj;
  813. int count_total_obj;
  814. int count_total_node;
  815. void *obj_last;
  816. struct hash_bucket_node *node;
  817. struct hash_bucket_node *prev;
  818. struct hash_bucket_node *next;
  819. count_total_obj = 0;
  820. count_total_node = 0;
  821. /* For each bucket in the container. */
  822. for (bucket = 0; bucket < self->n_buckets; ++bucket) {
  823. if (!AST_DLLIST_FIRST(&self->buckets[bucket].list)
  824. && !AST_DLLIST_LAST(&self->buckets[bucket].list)) {
  825. /* The bucket list is empty. */
  826. continue;
  827. }
  828. count_obj = 0;
  829. obj_last = NULL;
  830. /* Check bucket list links and nodes. */
  831. node = AST_DLLIST_LAST(&self->buckets[bucket].list);
  832. if (!node) {
  833. ast_log(LOG_ERROR, "Bucket %d list tail is NULL when it should not be!\n",
  834. bucket);
  835. return -1;
  836. }
  837. if (AST_DLLIST_NEXT(node, links)) {
  838. ast_log(LOG_ERROR, "Bucket %d list tail node is not the last node!\n",
  839. bucket);
  840. return -1;
  841. }
  842. node = AST_DLLIST_FIRST(&self->buckets[bucket].list);
  843. if (!node) {
  844. ast_log(LOG_ERROR, "Bucket %d list head is NULL when it should not be!\n",
  845. bucket);
  846. return -1;
  847. }
  848. if (AST_DLLIST_PREV(node, links)) {
  849. ast_log(LOG_ERROR, "Bucket %d list head node is not the first node!\n",
  850. bucket);
  851. return -1;
  852. }
  853. for (; node; node = next) {
  854. /* Check backward link. */
  855. prev = AST_DLLIST_PREV(node, links);
  856. if (prev) {
  857. if (prev == node) {
  858. ast_log(LOG_ERROR, "Bucket %d list node's prev pointer points to itself!\n",
  859. bucket);
  860. return -1;
  861. }
  862. if (node != AST_DLLIST_NEXT(prev, links)) {
  863. ast_log(LOG_ERROR, "Bucket %d list node's prev node does not link back!\n",
  864. bucket);
  865. return -1;
  866. }
  867. } else if (node != AST_DLLIST_FIRST(&self->buckets[bucket].list)) {
  868. ast_log(LOG_ERROR, "Bucket %d backward list chain is broken!\n",
  869. bucket);
  870. return -1;
  871. }
  872. /* Check forward link. */
  873. next = AST_DLLIST_NEXT(node, links);
  874. if (next) {
  875. if (next == node) {
  876. ast_log(LOG_ERROR, "Bucket %d list node's next pointer points to itself!\n",
  877. bucket);
  878. return -1;
  879. }
  880. if (node != AST_DLLIST_PREV(next, links)) {
  881. ast_log(LOG_ERROR, "Bucket %d list node's next node does not link back!\n",
  882. bucket);
  883. return -1;
  884. }
  885. } else if (node != AST_DLLIST_LAST(&self->buckets[bucket].list)) {
  886. ast_log(LOG_ERROR, "Bucket %d forward list chain is broken!\n",
  887. bucket);
  888. return -1;
  889. }
  890. if (bucket != node->my_bucket) {
  891. ast_log(LOG_ERROR, "Bucket %d node claims to be in bucket %d!\n",
  892. bucket, node->my_bucket);
  893. return -1;
  894. }
  895. ++count_total_node;
  896. if (!node->common.obj) {
  897. /* Node is empty. */
  898. continue;
  899. }
  900. ++count_obj;
  901. /* Check container hash key for expected bucket. */
  902. bucket_exp = abs(self->hash_fn(node->common.obj, OBJ_SEARCH_OBJECT)
  903. % self->n_buckets);
  904. if (bucket != bucket_exp) {
  905. ast_log(LOG_ERROR, "Bucket %d node hashes to bucket %d!\n",
  906. bucket, bucket_exp);
  907. return -1;
  908. }
  909. /* Check sort if configured. */
  910. if (self->common.sort_fn) {
  911. if (obj_last
  912. && self->common.sort_fn(obj_last, node->common.obj, OBJ_SEARCH_OBJECT) > 0) {
  913. ast_log(LOG_ERROR, "Bucket %d nodes out of sorted order!\n",
  914. bucket);
  915. return -1;
  916. }
  917. obj_last = node->common.obj;
  918. }
  919. }
  920. /* Check bucket obj count statistic. */
  921. if (count_obj != self->buckets[bucket].elements) {
  922. ast_log(LOG_ERROR, "Bucket %d object count of %d does not match stat of %d!\n",
  923. bucket, count_obj, self->buckets[bucket].elements);
  924. return -1;
  925. }
  926. /* Accumulate found object counts. */
  927. count_total_obj += count_obj;
  928. }
  929. /* Check total obj count. */
  930. if (count_total_obj != ao2_container_count(&self->common)) {
  931. ast_log(LOG_ERROR,
  932. "Total object count of %d does not match ao2_container_count() of %d!\n",
  933. count_total_obj, ao2_container_count(&self->common));
  934. return -1;
  935. }
  936. /* Check total node count. */
  937. if (count_total_node != self->common.nodes) {
  938. ast_log(LOG_ERROR, "Total node count of %d does not match stat of %d!\n",
  939. count_total_node, self->common.nodes);
  940. return -1;
  941. }
  942. return 0;
  943. }
  944. #endif /* defined(AO2_DEBUG) */
  945. /*! Hash container virtual method table. */
  946. static const struct ao2_container_methods v_table_hash = {
  947. .alloc_empty_clone = (ao2_container_alloc_empty_clone_fn) hash_ao2_alloc_empty_clone,
  948. .alloc_empty_clone_debug =
  949. (ao2_container_alloc_empty_clone_debug_fn) hash_ao2_alloc_empty_clone_debug,
  950. .new_node = (ao2_container_new_node_fn) hash_ao2_new_node,
  951. .insert = (ao2_container_insert_fn) hash_ao2_insert_node,
  952. .traverse_first = (ao2_container_find_first_fn) hash_ao2_find_first,
  953. .traverse_next = (ao2_container_find_next_fn) hash_ao2_find_next,
  954. .iterator_next = (ao2_iterator_next_fn) hash_ao2_iterator_next,
  955. .destroy = (ao2_container_destroy_fn) hash_ao2_destroy,
  956. #if defined(AO2_DEBUG)
  957. .link_stat = hash_ao2_link_node_stat,
  958. .unlink_stat = hash_ao2_unlink_node_stat,
  959. .dump = (ao2_container_display) hash_ao2_dump,
  960. .stats = (ao2_container_statistics) hash_ao2_stats,
  961. .integrity = (ao2_container_integrity) hash_ao2_integrity,
  962. #endif /* defined(AO2_DEBUG) */
  963. };
  964. /*!
  965. * \brief always zero hash function
  966. *
  967. * it is convenient to have a hash function that always returns 0.
  968. * This is basically used when we want to have a container that is
  969. * a simple linked list.
  970. *
  971. * \returns 0
  972. */
  973. static int hash_zero(const void *user_obj, const int flags)
  974. {
  975. return 0;
  976. }
  977. /*!
  978. * \brief Initialize a hash container with the desired number of buckets.
  979. *
  980. * \param self Container to initialize.
  981. * \param options Container behaviour options (See enum ao2_container_opts)
  982. * \param n_buckets Number of buckets for hash
  983. * \param hash_fn Pointer to a function computing a hash value.
  984. * \param sort_fn Pointer to a sort function.
  985. * \param cmp_fn Pointer to a compare function used by ao2_find.
  986. *
  987. * \return A pointer to a struct container.
  988. */
  989. static struct ao2_container *hash_ao2_container_init(
  990. struct ao2_container_hash *self, unsigned int options, unsigned int n_buckets,
  991. ao2_hash_fn *hash_fn, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
  992. {
  993. if (!self) {
  994. return NULL;
  995. }
  996. self->common.v_table = &v_table_hash;
  997. self->common.sort_fn = sort_fn;
  998. self->common.cmp_fn = cmp_fn;
  999. self->common.options = options;
  1000. self->hash_fn = hash_fn ? hash_fn : hash_zero;
  1001. self->n_buckets = n_buckets;
  1002. #ifdef AO2_DEBUG
  1003. ast_atomic_fetchadd_int(&ao2.total_containers, 1);
  1004. #endif /* defined(AO2_DEBUG) */
  1005. return (struct ao2_container *) self;
  1006. }
  1007. struct ao2_container *__ao2_container_alloc_hash(unsigned int ao2_options,
  1008. unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
  1009. ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
  1010. {
  1011. unsigned int num_buckets;
  1012. size_t container_size;
  1013. struct ao2_container_hash *self;
  1014. num_buckets = hash_fn ? n_buckets : 1;
  1015. container_size = sizeof(struct ao2_container_hash) + num_buckets * sizeof(struct hash_bucket);
  1016. self = ao2_t_alloc_options(container_size, container_destruct, ao2_options,
  1017. "New hash container");
  1018. return hash_ao2_container_init(self, container_options, num_buckets,
  1019. hash_fn, sort_fn, cmp_fn);
  1020. }
  1021. struct ao2_container *__ao2_container_alloc_hash_debug(unsigned int ao2_options,
  1022. unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
  1023. ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
  1024. const char *tag, const char *file, int line, const char *func, int ref_debug)
  1025. {
  1026. unsigned int num_buckets;
  1027. size_t container_size;
  1028. struct ao2_container_hash *self;
  1029. num_buckets = hash_fn ? n_buckets : 1;
  1030. container_size = sizeof(struct ao2_container_hash) + num_buckets * sizeof(struct hash_bucket);
  1031. self = __ao2_alloc_debug(container_size,
  1032. ref_debug ? container_destruct_debug : container_destruct, ao2_options,
  1033. tag, file, line, func, ref_debug);
  1034. return hash_ao2_container_init(self, container_options, num_buckets, hash_fn,
  1035. sort_fn, cmp_fn);
  1036. }
  1037. struct ao2_container *__ao2_container_alloc_list(unsigned int ao2_options,
  1038. unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
  1039. {
  1040. return __ao2_container_alloc_hash(ao2_options, container_options, 1, NULL, sort_fn,
  1041. cmp_fn);
  1042. }
  1043. struct ao2_container *__ao2_container_alloc_list_debug(unsigned int ao2_options,
  1044. unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
  1045. const char *tag, const char *file, int line, const char *func, int ref_debug)
  1046. {
  1047. return __ao2_container_alloc_hash_debug(ao2_options, container_options, 1, NULL,
  1048. sort_fn, cmp_fn, tag, file, line, func, ref_debug);
  1049. }