astobj2_rbtree.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /*
  2. * astobj2_hash - RBTree 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 RBTree 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 "asterisk/utils.h"
  27. #include "astobj2_private.h"
  28. #include "astobj2_container_private.h"
  29. /*!
  30. * A structure to hold the object held by the container and
  31. * where it is located in it.
  32. *
  33. * A red-black tree has the following properties:
  34. *
  35. * 1) Every node is either black or red.
  36. *
  37. * 2) The root is black.
  38. *
  39. * 3) If a node has a NULL child, that "child" is considered
  40. * black.
  41. *
  42. * 4) If a node is red, then both of its children are black.
  43. *
  44. * 5) Every path from a node to a descendant NULL child has the
  45. * same number of black nodes. (Including the black NULL
  46. * child.)
  47. */
  48. struct rbtree_node {
  49. /*!
  50. * \brief Items common to all container nodes.
  51. * \note Must be first in the specific node struct.
  52. */
  53. struct ao2_container_node common;
  54. /*! Parent node of this node. NULL if this is the root node. */
  55. struct rbtree_node *parent;
  56. /*! Left child node of this node. NULL if does not have this child. */
  57. struct rbtree_node *left;
  58. /*! Right child node of this node. NULL if does not have this child. */
  59. struct rbtree_node *right;
  60. /*! TRUE if the node is red. */
  61. unsigned int is_red:1;
  62. };
  63. /*!
  64. * A rbtree container in addition to values common to all
  65. * container types, stores the pointer to the root node of the
  66. * tree.
  67. */
  68. struct ao2_container_rbtree {
  69. /*!
  70. * \brief Items common to all containers.
  71. * \note Must be first in the specific container struct.
  72. */
  73. struct ao2_container common;
  74. /*! Root node of the tree. NULL if the tree is empty. */
  75. struct rbtree_node *root;
  76. #if defined(AO2_DEBUG)
  77. struct {
  78. /*! Fixup insert left cases 1-3 */
  79. int fixup_insert_left[3];
  80. /*! Fixup insert right cases 1-3 */
  81. int fixup_insert_right[3];
  82. /*! Fixup delete left cases 1-4 */
  83. int fixup_delete_left[4];
  84. /*! Fixup delete right cases 1-4 */
  85. int fixup_delete_right[4];
  86. /*! Deletion of node with number of children (0-2). */
  87. int delete_children[3];
  88. } stats;
  89. #endif /* defined(AO2_DEBUG) */
  90. };
  91. enum equal_node_bias {
  92. /*! Bias search toward first matching node in the container. */
  93. BIAS_FIRST,
  94. /*! Bias search toward any matching node. */
  95. BIAS_EQUAL,
  96. /*! Bias search toward last matching node in the container. */
  97. BIAS_LAST,
  98. };
  99. enum empty_node_direction {
  100. GO_LEFT,
  101. GO_RIGHT,
  102. };
  103. /*! Traversal state to restart a rbtree container traversal. */
  104. struct rbtree_traversal_state {
  105. /*! Active sort function in the traversal if not NULL. */
  106. ao2_sort_fn *sort_fn;
  107. /*! Saved comparison callback arg pointer. */
  108. void *arg;
  109. /*! Saved search flags to control traversing the container. */
  110. enum search_flags flags;
  111. };
  112. struct rbtree_traversal_state_check {
  113. /*
  114. * If we have a division by zero compile error here then there
  115. * is not enough room for the state. Increase AO2_TRAVERSAL_STATE_SIZE.
  116. */
  117. char check[1 / (AO2_TRAVERSAL_STATE_SIZE / sizeof(struct rbtree_traversal_state))];
  118. };
  119. /*!
  120. * \internal
  121. * \brief Get the most left node in the tree.
  122. * \since 12.0.0
  123. *
  124. * \param node Starting node to find the most left node.
  125. *
  126. * \return Left most node. Never NULL.
  127. */
  128. static struct rbtree_node *rb_node_most_left(struct rbtree_node *node)
  129. {
  130. while (node->left) {
  131. node = node->left;
  132. }
  133. return node;
  134. }
  135. /*!
  136. * \internal
  137. * \brief Get the most right node in the tree.
  138. * \since 12.0.0
  139. *
  140. * \param node Starting node to find the most right node.
  141. *
  142. * \return Right most node. Never NULL.
  143. */
  144. static struct rbtree_node *rb_node_most_right(struct rbtree_node *node)
  145. {
  146. while (node->right) {
  147. node = node->right;
  148. }
  149. return node;
  150. }
  151. /*!
  152. * \internal
  153. * \brief Get the next node in ascending sequence.
  154. * \since 12.0.0
  155. *
  156. * \param node Starting node to find the next node.
  157. *
  158. * \retval node on success.
  159. * \retval NULL if no node.
  160. */
  161. static struct rbtree_node *rb_node_next(struct rbtree_node *node)
  162. {
  163. if (node->right) {
  164. return rb_node_most_left(node->right);
  165. }
  166. /* Find the parent that the node is a left child of. */
  167. while (node->parent) {
  168. if (node->parent->left == node) {
  169. /* We are the left child. The parent is the next node. */
  170. return node->parent;
  171. }
  172. node = node->parent;
  173. }
  174. return NULL;
  175. }
  176. /*!
  177. * \internal
  178. * \brief Get the next node in descending sequence.
  179. * \since 12.0.0
  180. *
  181. * \param node Starting node to find the previous node.
  182. *
  183. * \retval node on success.
  184. * \retval NULL if no node.
  185. */
  186. static struct rbtree_node *rb_node_prev(struct rbtree_node *node)
  187. {
  188. if (node->left) {
  189. return rb_node_most_right(node->left);
  190. }
  191. /* Find the parent that the node is a right child of. */
  192. while (node->parent) {
  193. if (node->parent->right == node) {
  194. /* We are the right child. The parent is the previous node. */
  195. return node->parent;
  196. }
  197. node = node->parent;
  198. }
  199. return NULL;
  200. }
  201. /*!
  202. * \internal
  203. * \brief Get the next node in pre-order sequence.
  204. * \since 12.0.0
  205. *
  206. * \param node Starting node to find the next node.
  207. *
  208. * \retval node on success.
  209. * \retval NULL if no node.
  210. */
  211. static struct rbtree_node *rb_node_pre(struct rbtree_node *node)
  212. {
  213. /* Visit the children if the node has any. */
  214. if (node->left) {
  215. return node->left;
  216. }
  217. if (node->right) {
  218. return node->right;
  219. }
  220. /* Time to go back up. */
  221. for (;;) {
  222. if (!node->parent) {
  223. return NULL;
  224. }
  225. if (node->parent->left == node && node->parent->right) {
  226. /*
  227. * We came up the left child and there's a right child. Visit
  228. * it.
  229. */
  230. return node->parent->right;
  231. }
  232. node = node->parent;
  233. }
  234. }
  235. /*!
  236. * \internal
  237. * \brief Get the next node in post-order sequence.
  238. * \since 12.0.0
  239. *
  240. * \param node Starting node to find the next node.
  241. *
  242. * \retval node on success.
  243. * \retval NULL if no node.
  244. */
  245. static struct rbtree_node *rb_node_post(struct rbtree_node *node)
  246. {
  247. /* This node's children have already been visited. */
  248. for (;;) {
  249. if (!node->parent) {
  250. return NULL;
  251. }
  252. if (node->parent->left == node) {
  253. /* We came up the left child. */
  254. node = node->parent;
  255. /*
  256. * Find the right child's left most childless node.
  257. */
  258. while (node->right) {
  259. node = rb_node_most_left(node->right);
  260. }
  261. /*
  262. * This node's left child has already been visited or it doesn't
  263. * have any children.
  264. */
  265. return node;
  266. }
  267. /*
  268. * We came up the right child.
  269. *
  270. * This node's children have already been visited. Time to
  271. * visit the parent.
  272. */
  273. return node->parent;
  274. }
  275. }
  276. /*!
  277. * \internal
  278. * \brief Get the next non-empty node in ascending sequence.
  279. * \since 12.0.0
  280. *
  281. * \param node Starting node to find the next node.
  282. *
  283. * \retval node on success.
  284. * \retval NULL if no node.
  285. */
  286. static struct rbtree_node *rb_node_next_full(struct rbtree_node *node)
  287. {
  288. for (;;) {
  289. node = rb_node_next(node);
  290. if (!node || node->common.obj) {
  291. return node;
  292. }
  293. }
  294. }
  295. /*!
  296. * \internal
  297. * \brief Get the next non-empty node in descending sequence.
  298. * \since 12.0.0
  299. *
  300. * \param node Starting node to find the previous node.
  301. *
  302. * \retval node on success.
  303. * \retval NULL if no node.
  304. */
  305. static struct rbtree_node *rb_node_prev_full(struct rbtree_node *node)
  306. {
  307. for (;;) {
  308. node = rb_node_prev(node);
  309. if (!node || node->common.obj) {
  310. return node;
  311. }
  312. }
  313. }
  314. /*!
  315. * \internal
  316. * \brief Determine which way to go from an empty node.
  317. * \since 12.0.0
  318. *
  319. * \param empty Empty node to determine which side obj_right goes on.
  320. * \param sort_fn Sort comparison function for non-empty nodes.
  321. * \param obj_right pointer to the (user-defined part) of an object.
  322. * \param flags flags from ao2_callback()
  323. * OBJ_SEARCH_OBJECT - if set, 'obj_right', is an object.
  324. * OBJ_SEARCH_KEY - if set, 'obj_right', is a search key item that is not an object.
  325. * OBJ_SEARCH_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
  326. * \param bias How to bias search direction for duplicates
  327. *
  328. * \return enum empty_node_direction to proceed.
  329. */
  330. static enum empty_node_direction rb_find_empty_direction(struct rbtree_node *empty, ao2_sort_fn *sort_fn, void *obj_right, enum search_flags flags, enum equal_node_bias bias)
  331. {
  332. int cmp;
  333. struct rbtree_node *cur;
  334. struct rbtree_node *right_most;
  335. /* Try for a quick definite go left. */
  336. if (!empty->left) {
  337. /* The empty node has no left child. */
  338. return GO_RIGHT;
  339. }
  340. right_most = rb_node_most_right(empty->left);
  341. if (right_most->common.obj) {
  342. cmp = sort_fn(right_most->common.obj, obj_right, flags);
  343. if (cmp < 0) {
  344. return GO_RIGHT;
  345. }
  346. if (cmp == 0 && bias == BIAS_LAST) {
  347. return GO_RIGHT;
  348. }
  349. return GO_LEFT;
  350. }
  351. /* Try for a quick definite go right. */
  352. if (!empty->right) {
  353. /* The empty node has no right child. */
  354. return GO_LEFT;
  355. }
  356. cur = rb_node_most_left(empty->right);
  357. if (cur->common.obj) {
  358. cmp = sort_fn(cur->common.obj, obj_right, flags);
  359. if (cmp > 0) {
  360. return GO_LEFT;
  361. }
  362. if (cmp == 0 && bias == BIAS_FIRST) {
  363. return GO_LEFT;
  364. }
  365. return GO_RIGHT;
  366. }
  367. /*
  368. * Have to scan the previous nodes from the right_most node of
  369. * the left subtree for the first non-empty node to determine
  370. * direction.
  371. */
  372. cur = right_most;
  373. for (;;) {
  374. /* Find previous node. */
  375. if (cur->left) {
  376. cur = rb_node_most_right(cur->left);
  377. } else {
  378. /* Find the parent that the node is a right child of. */
  379. for (;;) {
  380. if (cur->parent == empty) {
  381. /* The left side of the empty node is all empty nodes. */
  382. return GO_RIGHT;
  383. }
  384. if (cur->parent->right == cur) {
  385. /* We are the right child. The parent is the previous node. */
  386. cur = cur->parent;
  387. break;
  388. }
  389. cur = cur->parent;
  390. }
  391. }
  392. if (cur->common.obj) {
  393. cmp = sort_fn(cur->common.obj, obj_right, flags);
  394. if (cmp < 0) {
  395. return GO_RIGHT;
  396. }
  397. if (cmp == 0 && bias == BIAS_LAST) {
  398. return GO_RIGHT;
  399. }
  400. return GO_LEFT;
  401. }
  402. }
  403. }
  404. /*!
  405. * \internal
  406. * \brief Tree node rotation left.
  407. * \since 12.0.0
  408. *
  409. * \param self Container holding node.
  410. * \param node Node to perform a left rotation with.
  411. *
  412. * p p
  413. * | Left rotation |
  414. * N ---> Ch
  415. * / \ / \
  416. * a Ch N c
  417. * / \ / \
  418. * b c a b
  419. *
  420. * N = node
  421. * Ch = child
  422. * p = parent
  423. * a,b,c = other nodes that are unaffected by the rotation.
  424. *
  425. * \note It is assumed that the node's right child exists.
  426. *
  427. * \return Nothing
  428. */
  429. static void rb_rotate_left(struct ao2_container_rbtree *self, struct rbtree_node *node)
  430. {
  431. struct rbtree_node *child; /*!< Node's right child. */
  432. child = node->right;
  433. /* Link the node's parent to the child. */
  434. if (!node->parent) {
  435. /* Node is the root so we get a new root node. */
  436. self->root = child;
  437. } else if (node->parent->left == node) {
  438. /* Node is a left child. */
  439. node->parent->left = child;
  440. } else {
  441. /* Node is a right child. */
  442. node->parent->right = child;
  443. }
  444. child->parent = node->parent;
  445. /* Link node's right subtree to the child's left subtree. */
  446. node->right = child->left;
  447. if (node->right) {
  448. node->right->parent = node;
  449. }
  450. /* Link the node to the child's left. */
  451. node->parent = child;
  452. child->left = node;
  453. }
  454. /*!
  455. * \internal
  456. * \brief Tree node rotation right.
  457. * \since 12.0.0
  458. *
  459. * \param self Container holding node.
  460. * \param node Node to perform a right rotation with.
  461. *
  462. * p p
  463. * | Right rotation |
  464. * Ch N
  465. * / \ <--- / \
  466. * a N Ch c
  467. * / \ / \
  468. * b c a b
  469. *
  470. * N = node
  471. * Ch = child
  472. * p = parent
  473. * a,b,c = other nodes that are unaffected by the rotation.
  474. *
  475. * \note It is assumed that the node's left child exists.
  476. *
  477. * \return Nothing
  478. */
  479. static void rb_rotate_right(struct ao2_container_rbtree *self, struct rbtree_node *node)
  480. {
  481. struct rbtree_node *child; /*!< Node's left child. */
  482. child = node->left;
  483. /* Link the node's parent to the child. */
  484. if (!node->parent) {
  485. /* Node is the root so we get a new root node. */
  486. self->root = child;
  487. } else if (node->parent->right == node) {
  488. /* Node is a right child. */
  489. node->parent->right = child;
  490. } else {
  491. /* Node is a left child. */
  492. node->parent->left = child;
  493. }
  494. child->parent = node->parent;
  495. /* Link node's left subtree to the child's right subtree. */
  496. node->left = child->right;
  497. if (node->left) {
  498. node->left->parent = node;
  499. }
  500. /* Link the node to the child's right. */
  501. node->parent = child;
  502. child->right = node;
  503. }
  504. /*!
  505. * \internal
  506. * \brief Create an empty copy of this container.
  507. * \since 12.0.0
  508. *
  509. * \param self Container to operate upon.
  510. *
  511. * \retval empty-clone-container on success.
  512. * \retval NULL on error.
  513. */
  514. static struct ao2_container *rb_ao2_alloc_empty_clone(struct ao2_container_rbtree *self)
  515. {
  516. if (!is_ao2_object(self)) {
  517. return NULL;
  518. }
  519. return ao2_t_container_alloc_rbtree(ao2_options_get(self), self->common.options,
  520. self->common.sort_fn, self->common.cmp_fn, "Clone rbtree container");
  521. }
  522. /*!
  523. * \internal
  524. * \brief Create an empty copy of this container. (Debug version)
  525. * \since 12.0.0
  526. *
  527. * \param self Container to operate upon.
  528. * \param tag used for debugging.
  529. * \param file Debug file name invoked from
  530. * \param line Debug line invoked from
  531. * \param func Debug function name invoked from
  532. * \param ref_debug TRUE if to output a debug reference message.
  533. *
  534. * \retval empty-clone-container on success.
  535. * \retval NULL on error.
  536. */
  537. static struct ao2_container *rb_ao2_alloc_empty_clone_debug(struct ao2_container_rbtree *self, const char *tag, const char *file, int line, const char *func, int ref_debug)
  538. {
  539. if (!is_ao2_object(self)) {
  540. return NULL;
  541. }
  542. return __ao2_container_alloc_rbtree_debug(ao2_options_get(self), self->common.options,
  543. self->common.sort_fn, self->common.cmp_fn, tag, file, line, func, ref_debug);
  544. }
  545. /*!
  546. * \internal
  547. * \brief Fixup the rbtree after deleting a node.
  548. * \since 12.0.0
  549. *
  550. * \param self Container to operate upon.
  551. * \param child Child of the node just deleted from the container.
  552. *
  553. * \note The child must be a dummy black node if there really
  554. * was no child of the deleted node. Otherwise, the caller must
  555. * pass in the parent node and which child was deleted. In
  556. * addition, the fixup routine would be more complicated.
  557. *
  558. * \return Nothing
  559. */
  560. static void rb_delete_fixup(struct ao2_container_rbtree *self, struct rbtree_node *child)
  561. {
  562. struct rbtree_node *sibling;
  563. while (self->root != child && !child->is_red) {
  564. if (child->parent->left == child) {
  565. /* Child is a left child. */
  566. sibling = child->parent->right;
  567. ast_assert(sibling != NULL);
  568. if (sibling->is_red) {
  569. /* Case 1: The child's sibling is red. */
  570. AO2_DEVMODE_STAT(++self->stats.fixup_delete_left[0]);
  571. sibling->is_red = 0;
  572. child->parent->is_red = 1;
  573. rb_rotate_left(self, child->parent);
  574. sibling = child->parent->right;
  575. ast_assert(sibling != NULL);
  576. }
  577. /*
  578. * The sibling is black. A black node must have two children,
  579. * or one red child, or no children.
  580. */
  581. if ((!sibling->left || !sibling->left->is_red)
  582. && (!sibling->right || !sibling->right->is_red)) {
  583. /*
  584. * Case 2: The sibling is black and both of its children are black.
  585. *
  586. * This case handles the two black children or no children
  587. * possibilities of a black node.
  588. */
  589. AO2_DEVMODE_STAT(++self->stats.fixup_delete_left[1]);
  590. sibling->is_red = 1;
  591. child = child->parent;
  592. } else {
  593. /* At this point the sibling has at least one red child. */
  594. if (!sibling->right || !sibling->right->is_red) {
  595. /*
  596. * Case 3: The sibling is black, its left child is red, and its
  597. * right child is black.
  598. */
  599. AO2_DEVMODE_STAT(++self->stats.fixup_delete_left[2]);
  600. ast_assert(sibling->left != NULL);
  601. ast_assert(sibling->left->is_red);
  602. sibling->left->is_red = 0;
  603. sibling->is_red = 1;
  604. rb_rotate_right(self, sibling);
  605. sibling = child->parent->right;
  606. ast_assert(sibling != NULL);
  607. }
  608. /* Case 4: The sibling is black and its right child is red. */
  609. AO2_DEVMODE_STAT(++self->stats.fixup_delete_left[3]);
  610. sibling->is_red = child->parent->is_red;
  611. child->parent->is_red = 0;
  612. if (sibling->right) {
  613. sibling->right->is_red = 0;
  614. }
  615. rb_rotate_left(self, child->parent);
  616. child = self->root;
  617. }
  618. } else {
  619. /* Child is a right child. */
  620. sibling = child->parent->left;
  621. ast_assert(sibling != NULL);
  622. if (sibling->is_red) {
  623. /* Case 1: The child's sibling is red. */
  624. AO2_DEVMODE_STAT(++self->stats.fixup_delete_right[0]);
  625. sibling->is_red = 0;
  626. child->parent->is_red = 1;
  627. rb_rotate_right(self, child->parent);
  628. sibling = child->parent->left;
  629. ast_assert(sibling != NULL);
  630. }
  631. /*
  632. * The sibling is black. A black node must have two children,
  633. * or one red child, or no children.
  634. */
  635. if ((!sibling->right || !sibling->right->is_red)
  636. && (!sibling->left || !sibling->left->is_red)) {
  637. /*
  638. * Case 2: The sibling is black and both of its children are black.
  639. *
  640. * This case handles the two black children or no children
  641. * possibilities of a black node.
  642. */
  643. AO2_DEVMODE_STAT(++self->stats.fixup_delete_right[1]);
  644. sibling->is_red = 1;
  645. child = child->parent;
  646. } else {
  647. /* At this point the sibling has at least one red child. */
  648. if (!sibling->left || !sibling->left->is_red) {
  649. /*
  650. * Case 3: The sibling is black, its right child is red, and its
  651. * left child is black.
  652. */
  653. AO2_DEVMODE_STAT(++self->stats.fixup_delete_right[2]);
  654. ast_assert(sibling->right != NULL);
  655. ast_assert(sibling->right->is_red);
  656. sibling->right->is_red = 0;
  657. sibling->is_red = 1;
  658. rb_rotate_left(self, sibling);
  659. sibling = child->parent->left;
  660. ast_assert(sibling != NULL);
  661. }
  662. /* Case 4: The sibling is black and its left child is red. */
  663. AO2_DEVMODE_STAT(++self->stats.fixup_delete_right[3]);
  664. sibling->is_red = child->parent->is_red;
  665. child->parent->is_red = 0;
  666. if (sibling->left) {
  667. sibling->left->is_red = 0;
  668. }
  669. rb_rotate_right(self, child->parent);
  670. child = self->root;
  671. }
  672. }
  673. }
  674. /*
  675. * Case 2 could leave the child node red and it needs to leave
  676. * with it black.
  677. *
  678. * Case 4 sets the child node to the root which of course must
  679. * be black.
  680. */
  681. child->is_red = 0;
  682. }
  683. /*!
  684. * \internal
  685. * \brief Delete the doomed node from this container.
  686. * \since 12.0.0
  687. *
  688. * \param self Container to operate upon.
  689. * \param doomed Container node to delete from the container.
  690. *
  691. * \return Nothing
  692. */
  693. static void rb_delete_node(struct ao2_container_rbtree *self, struct rbtree_node *doomed)
  694. {
  695. struct rbtree_node *child;
  696. int need_fixup;
  697. if (doomed->left && doomed->right) {
  698. struct rbtree_node *next;
  699. int is_red;
  700. /*
  701. * The doomed node has two children.
  702. *
  703. * Find the next child node and swap it with the doomed node in
  704. * the tree.
  705. */
  706. AO2_DEVMODE_STAT(++self->stats.delete_children[2]);
  707. next = rb_node_most_left(doomed->right);
  708. SWAP(doomed->parent, next->parent);
  709. SWAP(doomed->left, next->left);
  710. SWAP(doomed->right, next->right);
  711. is_red = doomed->is_red;
  712. doomed->is_red = next->is_red;
  713. next->is_red = is_red;
  714. /* Link back in the next node. */
  715. if (!next->parent) {
  716. /* Doomed was the root so we get a new root node. */
  717. self->root = next;
  718. } else if (next->parent->left == doomed) {
  719. /* Doomed was the left child. */
  720. next->parent->left = next;
  721. } else {
  722. /* Doomed was the right child. */
  723. next->parent->right = next;
  724. }
  725. next->left->parent = next;
  726. if (next->right == next) {
  727. /* The next node was the right child of doomed. */
  728. next->right = doomed;
  729. doomed->parent = next;
  730. } else {
  731. next->right->parent = next;
  732. doomed->parent->left = doomed;
  733. }
  734. /* The doomed node has no left child now. */
  735. ast_assert(doomed->left == NULL);
  736. /*
  737. * We don't have to link the right child back in with doomed
  738. * since we are going to link it with doomed's parent anyway.
  739. */
  740. child = doomed->right;
  741. } else {
  742. /* Doomed has at most one child. */
  743. child = doomed->left;
  744. if (!child) {
  745. child = doomed->right;
  746. }
  747. }
  748. if (child) {
  749. AO2_DEVMODE_STAT(++self->stats.delete_children[1]);
  750. } else {
  751. AO2_DEVMODE_STAT(++self->stats.delete_children[0]);
  752. }
  753. need_fixup = (!doomed->is_red && !self->common.destroying);
  754. if (need_fixup && !child) {
  755. /*
  756. * Use the doomed node as a place holder node for the
  757. * nonexistent child so we also don't have to pass to the fixup
  758. * routine the parent and which child the deleted node came
  759. * from.
  760. */
  761. rb_delete_fixup(self, doomed);
  762. ast_assert(doomed->left == NULL);
  763. ast_assert(doomed->right == NULL);
  764. ast_assert(!doomed->is_red);
  765. }
  766. /* Link the child in place of doomed. */
  767. if (!doomed->parent) {
  768. /* Doomed was the root so we get a new root node. */
  769. self->root = child;
  770. } else if (doomed->parent->left == doomed) {
  771. /* Doomed was the left child. */
  772. doomed->parent->left = child;
  773. } else {
  774. /* Doomed was the right child. */
  775. doomed->parent->right = child;
  776. }
  777. if (child) {
  778. child->parent = doomed->parent;
  779. if (need_fixup) {
  780. rb_delete_fixup(self, child);
  781. }
  782. }
  783. AO2_DEVMODE_STAT(--self->common.nodes);
  784. }
  785. /*!
  786. * \internal
  787. * \brief Destroy a rbtree container node.
  788. * \since 12.0.0
  789. *
  790. * \param v_doomed Container node to destroy.
  791. *
  792. * \details
  793. * The container node unlinks itself from the container as part
  794. * of its destruction. The node must be destroyed while the
  795. * container is already locked.
  796. *
  797. * \note The container must be locked when the node is
  798. * unreferenced.
  799. *
  800. * \return Nothing
  801. */
  802. static void rb_ao2_node_destructor(void *v_doomed)
  803. {
  804. struct rbtree_node *doomed = v_doomed;
  805. if (doomed->common.is_linked) {
  806. struct ao2_container_rbtree *my_container;
  807. /*
  808. * Promote to write lock if not already there. Since
  809. * adjust_lock() can potentially release and block waiting for a
  810. * write lock, care must be taken to ensure that node references
  811. * are released before releasing the container references.
  812. *
  813. * Node references held by an iterator can only be held while
  814. * the iterator also holds a reference to the container. These
  815. * node references must be unreferenced before the container can
  816. * be unreferenced to ensure that the node will not get a
  817. * negative reference and the destructor called twice for the
  818. * same node.
  819. */
  820. my_container = (struct ao2_container_rbtree *) doomed->common.my_container;
  821. #if defined(AST_DEVMODE)
  822. is_ao2_object(my_container);
  823. #endif
  824. __adjust_lock(my_container, AO2_LOCK_REQ_WRLOCK, 1);
  825. #if defined(AO2_DEBUG)
  826. if (!my_container->common.destroying
  827. && ao2_container_check(doomed->common.my_container, OBJ_NOLOCK)) {
  828. ast_log(LOG_ERROR, "Container integrity failed before node deletion.\n");
  829. }
  830. #endif /* defined(AO2_DEBUG) */
  831. rb_delete_node(my_container, doomed);
  832. #if defined(AO2_DEBUG)
  833. if (!my_container->common.destroying
  834. && ao2_container_check(doomed->common.my_container, OBJ_NOLOCK)) {
  835. ast_log(LOG_ERROR, "Container integrity failed after node deletion.\n");
  836. }
  837. #endif /* defined(AO2_DEBUG) */
  838. }
  839. /*
  840. * We could have an object in the node if the container is being
  841. * destroyed or the node had not been linked in yet.
  842. */
  843. if (doomed->common.obj) {
  844. __container_unlink_node(&doomed->common, AO2_UNLINK_NODE_UNLINK_OBJECT);
  845. }
  846. }
  847. /*!
  848. * \internal
  849. * \brief Create a new container node.
  850. * \since 12.0.0
  851. *
  852. * \param self Container to operate upon.
  853. * \param obj_new Object to put into the node.
  854. * \param tag used for debugging.
  855. * \param file Debug file name invoked from
  856. * \param line Debug line invoked from
  857. * \param func Debug function name invoked from
  858. *
  859. * \retval initialized-node on success.
  860. * \retval NULL on error.
  861. */
  862. static struct rbtree_node *rb_ao2_new_node(struct ao2_container_rbtree *self, void *obj_new, const char *tag, const char *file, int line, const char *func)
  863. {
  864. struct rbtree_node *node;
  865. node = __ao2_alloc(sizeof(*node), rb_ao2_node_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  866. if (!node) {
  867. return NULL;
  868. }
  869. if (tag) {
  870. __ao2_ref_debug(obj_new, +1, tag, file, line, func);
  871. } else {
  872. ao2_t_ref(obj_new, +1, "Container node creation");
  873. }
  874. node->common.obj = obj_new;
  875. node->common.my_container = (struct ao2_container *) self;
  876. return node;
  877. }
  878. /*!
  879. * \internal
  880. * \brief Fixup the rbtree after inserting a node.
  881. * \since 12.0.0
  882. *
  883. * \param self Container to operate upon.
  884. * \param node Container node just inserted into the container.
  885. *
  886. * \note The just inserted node is red.
  887. *
  888. * \return Nothing
  889. */
  890. static void rb_insert_fixup(struct ao2_container_rbtree *self, struct rbtree_node *node)
  891. {
  892. struct rbtree_node *g_parent; /* Grand parent node. */
  893. while (node->parent && node->parent->is_red) {
  894. g_parent = node->parent->parent;
  895. /* The grand parent must exist if the parent is red. */
  896. ast_assert(g_parent != NULL);
  897. if (node->parent == g_parent->left) {
  898. /* The parent is a left child. */
  899. if (g_parent->right && g_parent->right->is_red) {
  900. /* Case 1: Push the black down from the grand parent node. */
  901. AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[0]);
  902. g_parent->right->is_red = 0;
  903. g_parent->left->is_red = 0;
  904. g_parent->is_red = 1;
  905. node = g_parent;
  906. } else {
  907. /* The uncle node is black. */
  908. if (node->parent->right == node) {
  909. /*
  910. * Case 2: The node is a right child.
  911. *
  912. * Which node is the grand parent does not change.
  913. */
  914. AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[1]);
  915. node = node->parent;
  916. rb_rotate_left(self, node);
  917. }
  918. /* Case 3: The node is a left child. */
  919. AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[2]);
  920. node->parent->is_red = 0;
  921. g_parent->is_red = 1;
  922. rb_rotate_right(self, g_parent);
  923. }
  924. } else {
  925. /* The parent is a right child. */
  926. if (g_parent->left && g_parent->left->is_red) {
  927. /* Case 1: Push the black down from the grand parent node. */
  928. AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[0]);
  929. g_parent->left->is_red = 0;
  930. g_parent->right->is_red = 0;
  931. g_parent->is_red = 1;
  932. node = g_parent;
  933. } else {
  934. /* The uncle node is black. */
  935. if (node->parent->left == node) {
  936. /*
  937. * Case 2: The node is a left child.
  938. *
  939. * Which node is the grand parent does not change.
  940. */
  941. AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[1]);
  942. node = node->parent;
  943. rb_rotate_right(self, node);
  944. }
  945. /* Case 3: The node is a right child. */
  946. AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[2]);
  947. node->parent->is_red = 0;
  948. g_parent->is_red = 1;
  949. rb_rotate_left(self, g_parent);
  950. }
  951. }
  952. }
  953. /*
  954. * The root could be red here because:
  955. * 1) We just inserted the root node in an empty tree.
  956. *
  957. * 2) Case 1 could leave the root red if the grand parent were
  958. * the root.
  959. */
  960. self->root->is_red = 0;
  961. }
  962. /*!
  963. * \internal
  964. * \brief Insert a node into this container.
  965. * \since 12.0.0
  966. *
  967. * \param self Container to operate upon.
  968. * \param node Container node to insert into the container.
  969. *
  970. * \return enum ao2_container_insert value.
  971. */
  972. static enum ao2_container_insert rb_ao2_insert_node(struct ao2_container_rbtree *self, struct rbtree_node *node)
  973. {
  974. int cmp;
  975. struct rbtree_node *cur;
  976. struct rbtree_node *next;
  977. ao2_sort_fn *sort_fn;
  978. uint32_t options;
  979. enum equal_node_bias bias;
  980. if (!self->root) {
  981. /* The tree is empty. */
  982. self->root = node;
  983. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  984. }
  985. sort_fn = self->common.sort_fn;
  986. options = self->common.options;
  987. switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  988. default:
  989. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  990. if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) {
  991. bias = BIAS_FIRST;
  992. } else {
  993. bias = BIAS_LAST;
  994. }
  995. break;
  996. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  997. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  998. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  999. bias = BIAS_EQUAL;
  1000. break;
  1001. }
  1002. /*
  1003. * New nodes are always colored red when initially inserted into
  1004. * the tree. (Except for the root which is always black.)
  1005. */
  1006. node->is_red = 1;
  1007. /* Find node where normal insert would put a new node. */
  1008. cur = self->root;
  1009. for (;;) {
  1010. if (!cur->common.obj) {
  1011. /* Which direction do we go to insert this node? */
  1012. if (rb_find_empty_direction(cur, sort_fn, node->common.obj, OBJ_SEARCH_OBJECT, bias)
  1013. == GO_LEFT) {
  1014. if (cur->left) {
  1015. cur = cur->left;
  1016. continue;
  1017. }
  1018. /* Node becomes a left child */
  1019. cur->left = node;
  1020. node->parent = cur;
  1021. rb_insert_fixup(self, node);
  1022. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1023. }
  1024. if (cur->right) {
  1025. cur = cur->right;
  1026. continue;
  1027. }
  1028. /* Node becomes a right child */
  1029. cur->right = node;
  1030. node->parent = cur;
  1031. rb_insert_fixup(self, node);
  1032. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1033. }
  1034. cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1035. if (cmp > 0) {
  1036. if (cur->left) {
  1037. cur = cur->left;
  1038. continue;
  1039. }
  1040. /* Node becomes a left child */
  1041. cur->left = node;
  1042. node->parent = cur;
  1043. rb_insert_fixup(self, node);
  1044. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1045. } else if (cmp < 0) {
  1046. if (cur->right) {
  1047. cur = cur->right;
  1048. continue;
  1049. }
  1050. /* Node becomes a right child */
  1051. cur->right = node;
  1052. node->parent = cur;
  1053. rb_insert_fixup(self, node);
  1054. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1055. }
  1056. switch (bias) {
  1057. case BIAS_FIRST:
  1058. /* Duplicate nodes unconditionally accepted. */
  1059. if (cur->left) {
  1060. cur = cur->left;
  1061. continue;
  1062. }
  1063. /* Node becomes a left child */
  1064. cur->left = node;
  1065. node->parent = cur;
  1066. rb_insert_fixup(self, node);
  1067. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1068. case BIAS_EQUAL:
  1069. break;
  1070. case BIAS_LAST:
  1071. /* Duplicate nodes unconditionally accepted. */
  1072. if (cur->right) {
  1073. cur = cur->right;
  1074. continue;
  1075. }
  1076. /* Node becomes a right child */
  1077. cur->right = node;
  1078. node->parent = cur;
  1079. rb_insert_fixup(self, node);
  1080. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1081. }
  1082. break;
  1083. }
  1084. /* Node is a dupliate */
  1085. switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  1086. default:
  1087. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  1088. ast_assert(0);/* Case already handled by BIAS_FIRST/BIAS_LAST. */
  1089. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1090. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  1091. /* Reject all objects with the same key. */
  1092. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1093. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  1094. if (cur->common.obj == node->common.obj) {
  1095. /* Reject inserting the same object */
  1096. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1097. }
  1098. next = cur;
  1099. if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) {
  1100. /* Search to end of duplicates for the same object. */
  1101. for (;;) {
  1102. next = rb_node_next_full(next);
  1103. if (!next) {
  1104. break;
  1105. }
  1106. if (next->common.obj == node->common.obj) {
  1107. /* Reject inserting the same object */
  1108. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1109. }
  1110. cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1111. if (cmp) {
  1112. break;
  1113. }
  1114. }
  1115. /* Find first duplicate node. */
  1116. for (;;) {
  1117. next = rb_node_prev_full(cur);
  1118. if (!next) {
  1119. break;
  1120. }
  1121. if (next->common.obj == node->common.obj) {
  1122. /* Reject inserting the same object */
  1123. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1124. }
  1125. cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1126. if (cmp) {
  1127. break;
  1128. }
  1129. cur = next;
  1130. }
  1131. if (!cur->left) {
  1132. /* Node becomes a left child */
  1133. cur->left = node;
  1134. } else {
  1135. /* Node becomes a right child */
  1136. cur = rb_node_most_right(cur->left);
  1137. cur->right = node;
  1138. }
  1139. } else {
  1140. /* Search to beginning of duplicates for the same object. */
  1141. for (;;) {
  1142. next = rb_node_prev_full(next);
  1143. if (!next) {
  1144. break;
  1145. }
  1146. if (next->common.obj == node->common.obj) {
  1147. /* Reject inserting the same object */
  1148. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1149. }
  1150. cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1151. if (cmp) {
  1152. break;
  1153. }
  1154. }
  1155. /* Find last duplicate node. */
  1156. for (;;) {
  1157. next = rb_node_next_full(cur);
  1158. if (!next) {
  1159. break;
  1160. }
  1161. if (next->common.obj == node->common.obj) {
  1162. /* Reject inserting the same object */
  1163. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1164. }
  1165. cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1166. if (cmp) {
  1167. break;
  1168. }
  1169. cur = next;
  1170. }
  1171. if (!cur->right) {
  1172. /* Node becomes a right child */
  1173. cur->right = node;
  1174. } else {
  1175. /* Node becomes a left child */
  1176. cur = rb_node_most_left(cur->right);
  1177. cur->left = node;
  1178. }
  1179. }
  1180. break;
  1181. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  1182. SWAP(cur->common.obj, node->common.obj);
  1183. __ao2_ref(node, -1);
  1184. return AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED;
  1185. }
  1186. /* Complete inserting duplicate node. */
  1187. node->parent = cur;
  1188. rb_insert_fixup(self, node);
  1189. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1190. }
  1191. /*!
  1192. * \internal
  1193. * \brief Find the next rbtree container node in a traversal.
  1194. * \since 12.0.0
  1195. *
  1196. * \param self Container to operate upon.
  1197. * \param state Traversal state to restart rbtree container traversal.
  1198. * \param prev Previous node returned by the traversal search functions.
  1199. * The ref ownership is passed back to this function.
  1200. *
  1201. * \retval node-ptr of found node (Reffed).
  1202. * \retval NULL when no node found.
  1203. */
  1204. static struct rbtree_node *rb_ao2_find_next(struct ao2_container_rbtree *self, struct rbtree_traversal_state *state, struct rbtree_node *prev)
  1205. {
  1206. struct rbtree_node *node;
  1207. void *arg;
  1208. enum search_flags flags;
  1209. int cmp;
  1210. arg = state->arg;
  1211. flags = state->flags;
  1212. node = prev;
  1213. for (;;) {
  1214. /* Find next node in traversal order. */
  1215. switch (flags & OBJ_ORDER_MASK) {
  1216. default:
  1217. case OBJ_ORDER_ASCENDING:
  1218. node = rb_node_next(node);
  1219. break;
  1220. case OBJ_ORDER_DESCENDING:
  1221. node = rb_node_prev(node);
  1222. break;
  1223. case OBJ_ORDER_PRE:
  1224. node = rb_node_pre(node);
  1225. break;
  1226. case OBJ_ORDER_POST:
  1227. node = rb_node_post(node);
  1228. break;
  1229. }
  1230. if (!node) {
  1231. /* No more nodes left to traverse. */
  1232. break;
  1233. }
  1234. if (!node->common.obj) {
  1235. /* Node is empty */
  1236. continue;
  1237. }
  1238. if (state->sort_fn) {
  1239. /* Filter node through the sort_fn */
  1240. cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
  1241. if (cmp) {
  1242. /* No more nodes in this container are possible to match. */
  1243. break;
  1244. }
  1245. }
  1246. /* We have the next traversal node */
  1247. __ao2_ref(node, +1);
  1248. /*
  1249. * Dereferencing the prev node may result in our next node
  1250. * object being removed by another thread. This could happen if
  1251. * the container uses RW locks and the container was read
  1252. * locked.
  1253. */
  1254. __ao2_ref(prev, -1);
  1255. if (node->common.obj) {
  1256. return node;
  1257. }
  1258. prev = node;
  1259. }
  1260. /* No more nodes in the container left to traverse. */
  1261. __ao2_ref(prev, -1);
  1262. return NULL;
  1263. }
  1264. /*!
  1265. * \internal
  1266. * \brief Find an initial matching node.
  1267. * \since 12.0.0
  1268. *
  1269. * \param self Container to operate upon.
  1270. * \param obj_right pointer to the (user-defined part) of an object.
  1271. * \param flags flags from ao2_callback()
  1272. * OBJ_SEARCH_OBJECT - if set, 'obj_right', is an object.
  1273. * OBJ_SEARCH_KEY - if set, 'obj_right', is a search key item that is not an object.
  1274. * OBJ_SEARCH_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
  1275. * \param bias How to bias search direction for duplicates
  1276. *
  1277. * \retval node on success.
  1278. * \retval NULL if not found.
  1279. */
  1280. static struct rbtree_node *rb_find_initial(struct ao2_container_rbtree *self, void *obj_right, enum search_flags flags, enum equal_node_bias bias)
  1281. {
  1282. int cmp;
  1283. enum search_flags sort_flags;
  1284. struct rbtree_node *node;
  1285. struct rbtree_node *next = NULL;
  1286. ao2_sort_fn *sort_fn;
  1287. sort_flags = flags & OBJ_SEARCH_MASK;
  1288. sort_fn = self->common.sort_fn;
  1289. /* Find node where normal search would find it. */
  1290. node = self->root;
  1291. if (!node) {
  1292. return NULL;
  1293. }
  1294. for (;;) {
  1295. if (!node->common.obj) {
  1296. /* Which direction do we go to find the node? */
  1297. if (rb_find_empty_direction(node, sort_fn, obj_right, sort_flags, bias)
  1298. == GO_LEFT) {
  1299. next = node->left;
  1300. } else {
  1301. next = node->right;
  1302. }
  1303. if (!next) {
  1304. switch (bias) {
  1305. case BIAS_FIRST:
  1306. /* Check successor node for match. */
  1307. next = rb_node_next_full(node);
  1308. break;
  1309. case BIAS_EQUAL:
  1310. break;
  1311. case BIAS_LAST:
  1312. /* Check previous node for match. */
  1313. next = rb_node_prev_full(node);
  1314. break;
  1315. }
  1316. if (next) {
  1317. cmp = sort_fn(next->common.obj, obj_right, sort_flags);
  1318. if (cmp == 0) {
  1319. /* Found the first/last matching node. */
  1320. return next;
  1321. }
  1322. next = NULL;
  1323. }
  1324. /* No match found. */
  1325. return next;
  1326. }
  1327. } else {
  1328. cmp = sort_fn(node->common.obj, obj_right, sort_flags);
  1329. if (cmp > 0) {
  1330. next = node->left;
  1331. } else if (cmp < 0) {
  1332. next = node->right;
  1333. } else {
  1334. switch (bias) {
  1335. case BIAS_FIRST:
  1336. next = node->left;
  1337. break;
  1338. case BIAS_EQUAL:
  1339. return node;
  1340. case BIAS_LAST:
  1341. next = node->right;
  1342. break;
  1343. }
  1344. if (!next) {
  1345. /* Found the first/last matching node. */
  1346. return node;
  1347. }
  1348. }
  1349. if (!next) {
  1350. switch (bias) {
  1351. case BIAS_FIRST:
  1352. if (cmp < 0) {
  1353. /* Check successor node for match. */
  1354. next = rb_node_next_full(node);
  1355. }
  1356. break;
  1357. case BIAS_EQUAL:
  1358. break;
  1359. case BIAS_LAST:
  1360. if (cmp > 0) {
  1361. /* Check previous node for match. */
  1362. next = rb_node_prev_full(node);
  1363. }
  1364. break;
  1365. }
  1366. if (next) {
  1367. cmp = sort_fn(next->common.obj, obj_right, sort_flags);
  1368. if (cmp == 0) {
  1369. /* Found the first/last matching node. */
  1370. return next;
  1371. }
  1372. }
  1373. /* No match found. */
  1374. return NULL;
  1375. }
  1376. }
  1377. node = next;
  1378. }
  1379. }
  1380. /*!
  1381. * \internal
  1382. * \brief Find the first rbtree container node in a traversal.
  1383. * \since 12.0.0
  1384. *
  1385. * \param self Container to operate upon.
  1386. * \param flags search_flags to control traversing the container
  1387. * \param arg Comparison callback arg parameter.
  1388. * \param state Traversal state to restart rbtree container traversal.
  1389. *
  1390. * \retval node-ptr of found node (Reffed).
  1391. * \retval NULL when no node found.
  1392. */
  1393. static struct rbtree_node *rb_ao2_find_first(struct ao2_container_rbtree *self, enum search_flags flags, void *arg, struct rbtree_traversal_state *state)
  1394. {
  1395. struct rbtree_node *node;
  1396. enum equal_node_bias bias;
  1397. if (self->common.destroying) {
  1398. /* Force traversal to be post order for tree destruction. */
  1399. flags = OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_ORDER_POST;
  1400. }
  1401. memset(state, 0, sizeof(*state));
  1402. state->arg = arg;
  1403. state->flags = flags;
  1404. switch (flags & OBJ_SEARCH_MASK) {
  1405. case OBJ_SEARCH_OBJECT:
  1406. case OBJ_SEARCH_KEY:
  1407. case OBJ_SEARCH_PARTIAL_KEY:
  1408. /* We are asked to do a directed search. */
  1409. state->sort_fn = self->common.sort_fn;
  1410. break;
  1411. default:
  1412. /* Don't know, let's visit all nodes */
  1413. state->sort_fn = NULL;
  1414. break;
  1415. }
  1416. if (!self->root) {
  1417. /* Tree is empty. */
  1418. return NULL;
  1419. }
  1420. /* Find first traversal node. */
  1421. switch (flags & OBJ_ORDER_MASK) {
  1422. default:
  1423. case OBJ_ORDER_ASCENDING:
  1424. if (!state->sort_fn) {
  1425. /* Find left most child. */
  1426. node = rb_node_most_left(self->root);
  1427. if (!node->common.obj) {
  1428. node = rb_node_next_full(node);
  1429. if (!node) {
  1430. return NULL;
  1431. }
  1432. }
  1433. break;
  1434. }
  1435. /* Search for initial node. */
  1436. switch (self->common.options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  1437. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  1438. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  1439. if ((flags & OBJ_SEARCH_MASK) != OBJ_SEARCH_PARTIAL_KEY) {
  1440. /* There are no duplicates allowed. */
  1441. bias = BIAS_EQUAL;
  1442. break;
  1443. }
  1444. /* Fall through */
  1445. default:
  1446. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  1447. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  1448. /* Find first duplicate node. */
  1449. bias = BIAS_FIRST;
  1450. break;
  1451. }
  1452. node = rb_find_initial(self, arg, flags, bias);
  1453. if (!node) {
  1454. return NULL;
  1455. }
  1456. break;
  1457. case OBJ_ORDER_DESCENDING:
  1458. if (!state->sort_fn) {
  1459. /* Find right most child. */
  1460. node = rb_node_most_right(self->root);
  1461. if (!node->common.obj) {
  1462. node = rb_node_prev_full(node);
  1463. if (!node) {
  1464. return NULL;
  1465. }
  1466. }
  1467. break;
  1468. }
  1469. /* Search for initial node. */
  1470. switch (self->common.options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  1471. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  1472. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  1473. if ((flags & OBJ_SEARCH_MASK) != OBJ_SEARCH_PARTIAL_KEY) {
  1474. /* There are no duplicates allowed. */
  1475. bias = BIAS_EQUAL;
  1476. break;
  1477. }
  1478. /* Fall through */
  1479. default:
  1480. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  1481. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  1482. /* Find last duplicate node. */
  1483. bias = BIAS_LAST;
  1484. break;
  1485. }
  1486. node = rb_find_initial(self, arg, flags, bias);
  1487. if (!node) {
  1488. return NULL;
  1489. }
  1490. break;
  1491. case OBJ_ORDER_PRE:
  1492. /* This is a tree structure traversal so we must visit all nodes. */
  1493. state->sort_fn = NULL;
  1494. node = self->root;
  1495. /* Find a non-empty node. */
  1496. while (!node->common.obj) {
  1497. node = rb_node_pre(node);
  1498. if (!node) {
  1499. return NULL;
  1500. }
  1501. }
  1502. break;
  1503. case OBJ_ORDER_POST:
  1504. /* This is a tree structure traversal so we must visit all nodes. */
  1505. state->sort_fn = NULL;
  1506. /* Find the left most childless node. */
  1507. node = self->root;
  1508. for (;;) {
  1509. node = rb_node_most_left(node);
  1510. if (!node->right) {
  1511. /* This node has no children. */
  1512. break;
  1513. }
  1514. node = node->right;
  1515. }
  1516. /* Find a non-empty node. */
  1517. while (!node->common.obj) {
  1518. node = rb_node_post(node);
  1519. if (!node) {
  1520. return NULL;
  1521. }
  1522. }
  1523. break;
  1524. }
  1525. /* We have the first traversal node */
  1526. __ao2_ref(node, +1);
  1527. return node;
  1528. }
  1529. /*!
  1530. * \internal
  1531. * \brief Find the next non-empty iteration node in the container.
  1532. * \since 12.0.0
  1533. *
  1534. * \param self Container to operate upon.
  1535. * \param node Previous node returned by the iterator.
  1536. * \param flags search_flags to control iterating the container.
  1537. * Only AO2_ITERATOR_DESCENDING is useful by the method.
  1538. *
  1539. * \note The container is already locked.
  1540. *
  1541. * \retval node on success.
  1542. * \retval NULL on error or no more nodes in the container.
  1543. */
  1544. static struct rbtree_node *rb_ao2_iterator_next(struct ao2_container_rbtree *self, struct rbtree_node *node, enum ao2_iterator_flags flags)
  1545. {
  1546. if (flags & AO2_ITERATOR_DESCENDING) {
  1547. if (!node) {
  1548. /* Find right most node. */
  1549. if (!self->root) {
  1550. return NULL;
  1551. }
  1552. node = rb_node_most_right(self->root);
  1553. if (node->common.obj) {
  1554. /* Found a non-empty node. */
  1555. return node;
  1556. }
  1557. }
  1558. /* Find next non-empty node. */
  1559. node = rb_node_prev_full(node);
  1560. } else {
  1561. if (!node) {
  1562. /* Find left most node. */
  1563. if (!self->root) {
  1564. return NULL;
  1565. }
  1566. node = rb_node_most_left(self->root);
  1567. if (node->common.obj) {
  1568. /* Found a non-empty node. */
  1569. return node;
  1570. }
  1571. }
  1572. /* Find next non-empty node. */
  1573. node = rb_node_next_full(node);
  1574. }
  1575. return node;
  1576. }
  1577. /*!
  1578. * \internal
  1579. *
  1580. * \brief Destroy this container.
  1581. * \since 12.0.0
  1582. *
  1583. * \param self Container to operate upon.
  1584. *
  1585. * \return Nothing
  1586. */
  1587. static void rb_ao2_destroy(struct ao2_container_rbtree *self)
  1588. {
  1589. /* Check that the container no longer has any nodes */
  1590. if (self->root) {
  1591. ast_log(LOG_ERROR, "Node ref leak. Red-Black tree container still has nodes!\n");
  1592. ast_assert(0);
  1593. }
  1594. }
  1595. #if defined(AO2_DEBUG)
  1596. /*!
  1597. * \internal
  1598. * \brief Display contents of the specified container.
  1599. * \since 12.0.0
  1600. *
  1601. * \param self Container to dump.
  1602. * \param where User data needed by prnt to determine where to put output.
  1603. * \param prnt Print output callback function to use.
  1604. * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
  1605. *
  1606. * \return Nothing
  1607. */
  1608. static void rb_ao2_dump(struct ao2_container_rbtree *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj)
  1609. {
  1610. #define FORMAT "%16s, %16s, %16s, %16s, %5s, %16s, %s\n"
  1611. #define FORMAT2 "%16p, %16p, %16p, %16p, %5s, %16p, "
  1612. struct rbtree_node *node;
  1613. prnt(where, FORMAT, "Node", "Parent", "Left", "Right", "Color", "Obj", "Key");
  1614. for (node = self->root; node; node = rb_node_pre(node)) {
  1615. prnt(where, FORMAT2,
  1616. node,
  1617. node->parent,
  1618. node->left,
  1619. node->right,
  1620. node->is_red ? "Red" : "Black",
  1621. node->common.obj);
  1622. if (node->common.obj && prnt_obj) {
  1623. prnt_obj(node->common.obj, where, prnt);
  1624. }
  1625. prnt(where, "\n");
  1626. }
  1627. #undef FORMAT
  1628. #undef FORMAT2
  1629. }
  1630. #endif /* defined(AO2_DEBUG) */
  1631. #if defined(AO2_DEBUG)
  1632. /*!
  1633. * \internal
  1634. * \brief Display statistics of the specified container.
  1635. * \since 12.0.0
  1636. *
  1637. * \param self Container to display statistics.
  1638. * \param where User data needed by prnt to determine where to put output.
  1639. * \param prnt Print output callback function to use.
  1640. *
  1641. * \note The container is already locked for reading.
  1642. *
  1643. * \return Nothing
  1644. */
  1645. static void rb_ao2_stats(struct ao2_container_rbtree *self, void *where, ao2_prnt_fn *prnt)
  1646. {
  1647. int idx;
  1648. for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_insert_left); ++idx) {
  1649. prnt(where, "Number of left insert fixups case %d: %d\n", idx + 1,
  1650. self->stats.fixup_insert_left[idx]);
  1651. }
  1652. for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_insert_right); ++idx) {
  1653. prnt(where, "Number of right insert fixups case %d: %d\n", idx + 1,
  1654. self->stats.fixup_insert_right[idx]);
  1655. }
  1656. for (idx = 0; idx < ARRAY_LEN(self->stats.delete_children); ++idx) {
  1657. prnt(where, "Number of nodes deleted with %d children: %d\n", idx,
  1658. self->stats.delete_children[idx]);
  1659. }
  1660. for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_delete_left); ++idx) {
  1661. prnt(where, "Number of left delete fixups case %d: %d\n", idx + 1,
  1662. self->stats.fixup_delete_left[idx]);
  1663. }
  1664. for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_delete_right); ++idx) {
  1665. prnt(where, "Number of right delete fixups case %d: %d\n", idx + 1,
  1666. self->stats.fixup_delete_right[idx]);
  1667. }
  1668. }
  1669. #endif /* defined(AO2_DEBUG) */
  1670. #if defined(AO2_DEBUG)
  1671. /*!
  1672. * \internal
  1673. * \brief Check the black height of the given node.
  1674. * \since 12.0.0
  1675. *
  1676. * \param node Node to check black height.
  1677. *
  1678. * \retval black-height of node on success.
  1679. * \retval -1 on error. Node black height did not balance.
  1680. */
  1681. static int rb_check_black_height(struct rbtree_node *node)
  1682. {
  1683. int height_left;
  1684. int height_right;
  1685. if (!node) {
  1686. /* A NULL child is a black node. */
  1687. return 0;
  1688. }
  1689. height_left = rb_check_black_height(node->left);
  1690. if (height_left < 0) {
  1691. return -1;
  1692. }
  1693. height_right = rb_check_black_height(node->right);
  1694. if (height_right < 0) {
  1695. return -1;
  1696. }
  1697. if (height_left != height_right) {
  1698. ast_log(LOG_ERROR,
  1699. "Tree node black height of children does not match! L:%d != R:%d\n",
  1700. height_left, height_right);
  1701. return -1;
  1702. }
  1703. if (!node->is_red) {
  1704. /* The node itself is black. */
  1705. ++height_left;
  1706. }
  1707. return height_left;
  1708. }
  1709. #endif /* defined(AO2_DEBUG) */
  1710. #if defined(AO2_DEBUG)
  1711. /*!
  1712. * \internal
  1713. * \brief Perform an integrity check on the specified container.
  1714. * \since 12.0.0
  1715. *
  1716. * \param self Container to check integrity.
  1717. *
  1718. * \note The container is already locked for reading.
  1719. *
  1720. * \retval 0 on success.
  1721. * \retval -1 on error.
  1722. */
  1723. static int rb_ao2_integrity(struct ao2_container_rbtree *self)
  1724. {
  1725. int res;
  1726. int count_node;
  1727. int count_obj;
  1728. void *obj_last;
  1729. struct rbtree_node *node;
  1730. res = 0;
  1731. count_node = 0;
  1732. count_obj = 0;
  1733. /*
  1734. * See the properties listed at struct rbtree_node definition.
  1735. *
  1736. * The rbtree properties 1 and 3 are not testable.
  1737. *
  1738. * Property 1 is not testable because we are not rebalancing at
  1739. * this time so all nodes are either red or black.
  1740. *
  1741. * Property 3 is not testable because it is the definition of a
  1742. * NULL child.
  1743. */
  1744. if (self->root) {
  1745. /* Check tree links. */
  1746. if (self->root->parent) {
  1747. if (self->root->parent == self->root) {
  1748. ast_log(LOG_ERROR, "Tree root parent pointer points to itself!\n");
  1749. } else {
  1750. ast_log(LOG_ERROR, "Tree root is not a root node!\n");
  1751. }
  1752. return -1;
  1753. }
  1754. if (self->root->is_red) {
  1755. /* Violation rbtree property 2. */
  1756. ast_log(LOG_ERROR, "Tree root is red!\n");
  1757. res = -1;
  1758. }
  1759. node = self->root;
  1760. do {
  1761. if (node->left) {
  1762. if (node->left == node) {
  1763. ast_log(LOG_ERROR, "Tree node's left pointer points to itself!\n");
  1764. return -1;
  1765. }
  1766. if (node->left->parent != node) {
  1767. ast_log(LOG_ERROR, "Tree node's left child does not link back!\n");
  1768. return -1;
  1769. }
  1770. }
  1771. if (node->right) {
  1772. if (node->right == node) {
  1773. ast_log(LOG_ERROR, "Tree node's right pointer points to itself!\n");
  1774. return -1;
  1775. }
  1776. if (node->right->parent != node) {
  1777. ast_log(LOG_ERROR, "Tree node's right child does not link back!\n");
  1778. return -1;
  1779. }
  1780. }
  1781. /* Check red/black node flags. */
  1782. if (node->is_red) {
  1783. /* A red node must have two black children or no children. */
  1784. if (node->left && node->right) {
  1785. /* Node has two children. */
  1786. if (node->left->is_red) {
  1787. /* Violation rbtree property 4. */
  1788. ast_log(LOG_ERROR, "Tree node is red and its left child is red!\n");
  1789. res = -1;
  1790. }
  1791. if (node->right->is_red) {
  1792. /* Violation rbtree property 4. */
  1793. ast_log(LOG_ERROR, "Tree node is red and its right child is red!\n");
  1794. res = -1;
  1795. }
  1796. } else if (node->left || node->right) {
  1797. /*
  1798. * Violation rbtree property 4 if the child is red.
  1799. * Violation rbtree property 5 if the child is black.
  1800. */
  1801. ast_log(LOG_ERROR, "Tree node is red and it only has one child!\n");
  1802. res = -1;
  1803. }
  1804. } else {
  1805. /*
  1806. * A black node must have two children, or one red child, or no
  1807. * children. If the black node has two children and only one of
  1808. * them is red, that red child must have two children.
  1809. */
  1810. if (node->left && node->right) {
  1811. /* Node has two children. */
  1812. if (node->left->is_red != node->right->is_red) {
  1813. /* The children are not the same color. */
  1814. struct rbtree_node *red;
  1815. if (node->left->is_red) {
  1816. red = node->left;
  1817. } else {
  1818. red = node->right;
  1819. }
  1820. if (!red->left || !red->right) {
  1821. /* Violation rbtree property 5. */
  1822. ast_log(LOG_ERROR,
  1823. "Tree node is black and the red child does not have two children!\n");
  1824. res = -1;
  1825. }
  1826. }
  1827. } else if ((node->left && !node->left->is_red)
  1828. || (node->right && !node->right->is_red)) {
  1829. /* Violation rbtree property 5. */
  1830. ast_log(LOG_ERROR, "Tree node is black and its only child is black!\n");
  1831. res = -1;
  1832. }
  1833. }
  1834. /* Count nodes and objects. */
  1835. ++count_node;
  1836. if (node->common.obj) {
  1837. ++count_obj;
  1838. }
  1839. node = rb_node_pre(node);
  1840. } while (node);
  1841. /* Check node key sort order. */
  1842. obj_last = NULL;
  1843. for (node = rb_node_most_left(self->root); node; node = rb_node_next(node)) {
  1844. if (!node->common.obj) {
  1845. /* Node is empty. */
  1846. continue;
  1847. }
  1848. if (obj_last) {
  1849. if (self->common.sort_fn(obj_last, node->common.obj, OBJ_SEARCH_OBJECT) > 0) {
  1850. ast_log(LOG_ERROR, "Tree nodes are out of sorted order!\n");
  1851. return -1;
  1852. }
  1853. }
  1854. obj_last = node->common.obj;
  1855. }
  1856. /* Completely check property 5 */
  1857. if (!res && rb_check_black_height(self->root) < 0) {
  1858. /* Violation rbtree property 5. */
  1859. res = -1;
  1860. }
  1861. }
  1862. /* Check total obj count. */
  1863. if (count_obj != ao2_container_count(&self->common)) {
  1864. ast_log(LOG_ERROR, "Total object count does not match ao2_container_count()!\n");
  1865. return -1;
  1866. }
  1867. /* Check total node count. */
  1868. if (count_node != self->common.nodes) {
  1869. ast_log(LOG_ERROR, "Total node count of %d does not match stat of %d!\n",
  1870. count_node, self->common.nodes);
  1871. return -1;
  1872. }
  1873. return res;
  1874. }
  1875. #endif /* defined(AO2_DEBUG) */
  1876. /*! rbtree container virtual method table. */
  1877. static const struct ao2_container_methods v_table_rbtree = {
  1878. .alloc_empty_clone = (ao2_container_alloc_empty_clone_fn) rb_ao2_alloc_empty_clone,
  1879. .alloc_empty_clone_debug =
  1880. (ao2_container_alloc_empty_clone_debug_fn) rb_ao2_alloc_empty_clone_debug,
  1881. .new_node = (ao2_container_new_node_fn) rb_ao2_new_node,
  1882. .insert = (ao2_container_insert_fn) rb_ao2_insert_node,
  1883. .traverse_first = (ao2_container_find_first_fn) rb_ao2_find_first,
  1884. .traverse_next = (ao2_container_find_next_fn) rb_ao2_find_next,
  1885. .iterator_next = (ao2_iterator_next_fn) rb_ao2_iterator_next,
  1886. .destroy = (ao2_container_destroy_fn) rb_ao2_destroy,
  1887. #if defined(AO2_DEBUG)
  1888. .dump = (ao2_container_display) rb_ao2_dump,
  1889. .stats = (ao2_container_statistics) rb_ao2_stats,
  1890. .integrity = (ao2_container_integrity) rb_ao2_integrity,
  1891. #endif /* defined(AO2_DEBUG) */
  1892. };
  1893. /*!
  1894. * \brief Initialize a rbtree container.
  1895. *
  1896. * \param self Container to initialize.
  1897. * \param options Container behaviour options (See enum ao2_container_opts)
  1898. * \param sort_fn Pointer to a sort function.
  1899. * \param cmp_fn Pointer to a compare function used by ao2_find.
  1900. *
  1901. * \return A pointer to a struct container.
  1902. */
  1903. static struct ao2_container *rb_ao2_container_init(struct ao2_container_rbtree *self,
  1904. unsigned int options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
  1905. {
  1906. if (!self) {
  1907. return NULL;
  1908. }
  1909. self->common.v_table = &v_table_rbtree;
  1910. self->common.sort_fn = sort_fn;
  1911. self->common.cmp_fn = cmp_fn;
  1912. self->common.options = options;
  1913. #ifdef AO2_DEBUG
  1914. ast_atomic_fetchadd_int(&ao2.total_containers, 1);
  1915. #endif /* defined(AO2_DEBUG) */
  1916. return (struct ao2_container *) self;
  1917. }
  1918. struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options,
  1919. ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
  1920. {
  1921. struct ao2_container_rbtree *self;
  1922. if (!sort_fn) {
  1923. /* Sanity checks. */
  1924. ast_log(LOG_ERROR, "Missing sort_fn()!\n");
  1925. return NULL;
  1926. }
  1927. self = ao2_t_alloc_options(sizeof(*self), container_destruct, ao2_options,
  1928. "New rbtree container");
  1929. return rb_ao2_container_init(self, container_options, sort_fn, cmp_fn);
  1930. }
  1931. struct ao2_container *__ao2_container_alloc_rbtree_debug(unsigned int ao2_options, unsigned int container_options,
  1932. ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
  1933. const char *tag, const char *file, int line, const char *func, int ref_debug)
  1934. {
  1935. struct ao2_container_rbtree *self;
  1936. if (!sort_fn) {
  1937. /* Sanity checks. */
  1938. ast_log(__LOG_ERROR, file, line, func, "Missing sort_fn()!\n");
  1939. return NULL;
  1940. }
  1941. self = __ao2_alloc_debug(sizeof(*self),
  1942. ref_debug ? container_destruct_debug : container_destruct, ao2_options,
  1943. tag, file, line, func, ref_debug);
  1944. return rb_ao2_container_init(self, container_options, sort_fn, cmp_fn);
  1945. }