astobj2_rbtree.c 52 KB

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