astobj2_hash.c 32 KB

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