astobj2_rbtree.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  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. __adjust_lock(my_container, AO2_LOCK_REQ_WRLOCK, 1);
  822. #if defined(AO2_DEBUG)
  823. if (!my_container->common.destroying
  824. && ao2_container_check(doomed->common.my_container, OBJ_NOLOCK)) {
  825. ast_log(LOG_ERROR, "Container integrity failed before node deletion.\n");
  826. }
  827. #endif /* defined(AO2_DEBUG) */
  828. rb_delete_node(my_container, doomed);
  829. #if defined(AO2_DEBUG)
  830. if (!my_container->common.destroying
  831. && ao2_container_check(doomed->common.my_container, OBJ_NOLOCK)) {
  832. ast_log(LOG_ERROR, "Container integrity failed after node deletion.\n");
  833. }
  834. #endif /* defined(AO2_DEBUG) */
  835. }
  836. /*
  837. * We could have an object in the node if the container is being
  838. * destroyed or the node had not been linked in yet.
  839. */
  840. if (doomed->common.obj) {
  841. __container_unlink_node(&doomed->common, AO2_UNLINK_NODE_UNLINK_OBJECT);
  842. }
  843. }
  844. /*!
  845. * \internal
  846. * \brief Create a new container node.
  847. * \since 12.0.0
  848. *
  849. * \param self Container to operate upon.
  850. * \param obj_new Object to put into the node.
  851. * \param tag used for debugging.
  852. * \param file Debug file name invoked from
  853. * \param line Debug line invoked from
  854. * \param func Debug function name invoked from
  855. *
  856. * \retval initialized-node on success.
  857. * \retval NULL on error.
  858. */
  859. 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)
  860. {
  861. struct rbtree_node *node;
  862. node = __ao2_alloc(sizeof(*node), rb_ao2_node_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  863. if (!node) {
  864. return NULL;
  865. }
  866. if (tag) {
  867. __ao2_ref_debug(obj_new, +1, tag, file, line, func);
  868. } else {
  869. ao2_t_ref(obj_new, +1, "Container node creation");
  870. }
  871. node->common.obj = obj_new;
  872. node->common.my_container = (struct ao2_container *) self;
  873. return node;
  874. }
  875. /*!
  876. * \internal
  877. * \brief Fixup the rbtree after inserting a node.
  878. * \since 12.0.0
  879. *
  880. * \param self Container to operate upon.
  881. * \param node Container node just inserted into the container.
  882. *
  883. * \note The just inserted node is red.
  884. *
  885. * \return Nothing
  886. */
  887. static void rb_insert_fixup(struct ao2_container_rbtree *self, struct rbtree_node *node)
  888. {
  889. struct rbtree_node *g_parent; /* Grand parent node. */
  890. while (node->parent && node->parent->is_red) {
  891. g_parent = node->parent->parent;
  892. /* The grand parent must exist if the parent is red. */
  893. ast_assert(g_parent != NULL);
  894. if (node->parent == g_parent->left) {
  895. /* The parent is a left child. */
  896. if (g_parent->right && g_parent->right->is_red) {
  897. /* Case 1: Push the black down from the grand parent node. */
  898. AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[0]);
  899. g_parent->right->is_red = 0;
  900. g_parent->left->is_red = 0;
  901. g_parent->is_red = 1;
  902. node = g_parent;
  903. } else {
  904. /* The uncle node is black. */
  905. if (node->parent->right == node) {
  906. /*
  907. * Case 2: The node is a right child.
  908. *
  909. * Which node is the grand parent does not change.
  910. */
  911. AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[1]);
  912. node = node->parent;
  913. rb_rotate_left(self, node);
  914. }
  915. /* Case 3: The node is a left child. */
  916. AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[2]);
  917. node->parent->is_red = 0;
  918. g_parent->is_red = 1;
  919. rb_rotate_right(self, g_parent);
  920. }
  921. } else {
  922. /* The parent is a right child. */
  923. if (g_parent->left && g_parent->left->is_red) {
  924. /* Case 1: Push the black down from the grand parent node. */
  925. AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[0]);
  926. g_parent->left->is_red = 0;
  927. g_parent->right->is_red = 0;
  928. g_parent->is_red = 1;
  929. node = g_parent;
  930. } else {
  931. /* The uncle node is black. */
  932. if (node->parent->left == node) {
  933. /*
  934. * Case 2: The node is a left child.
  935. *
  936. * Which node is the grand parent does not change.
  937. */
  938. AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[1]);
  939. node = node->parent;
  940. rb_rotate_right(self, node);
  941. }
  942. /* Case 3: The node is a right child. */
  943. AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[2]);
  944. node->parent->is_red = 0;
  945. g_parent->is_red = 1;
  946. rb_rotate_left(self, g_parent);
  947. }
  948. }
  949. }
  950. /*
  951. * The root could be red here because:
  952. * 1) We just inserted the root node in an empty tree.
  953. *
  954. * 2) Case 1 could leave the root red if the grand parent were
  955. * the root.
  956. */
  957. self->root->is_red = 0;
  958. }
  959. /*!
  960. * \internal
  961. * \brief Insert a node into this container.
  962. * \since 12.0.0
  963. *
  964. * \param self Container to operate upon.
  965. * \param node Container node to insert into the container.
  966. *
  967. * \return enum ao2_container_insert value.
  968. */
  969. static enum ao2_container_insert rb_ao2_insert_node(struct ao2_container_rbtree *self, struct rbtree_node *node)
  970. {
  971. int cmp;
  972. struct rbtree_node *cur;
  973. struct rbtree_node *next;
  974. ao2_sort_fn *sort_fn;
  975. uint32_t options;
  976. enum equal_node_bias bias;
  977. if (!self->root) {
  978. /* The tree is empty. */
  979. self->root = node;
  980. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  981. }
  982. sort_fn = self->common.sort_fn;
  983. options = self->common.options;
  984. switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  985. default:
  986. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  987. if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) {
  988. bias = BIAS_FIRST;
  989. } else {
  990. bias = BIAS_LAST;
  991. }
  992. break;
  993. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  994. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  995. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  996. bias = BIAS_EQUAL;
  997. break;
  998. }
  999. /*
  1000. * New nodes are always colored red when initially inserted into
  1001. * the tree. (Except for the root which is always black.)
  1002. */
  1003. node->is_red = 1;
  1004. /* Find node where normal insert would put a new node. */
  1005. cur = self->root;
  1006. for (;;) {
  1007. if (!cur->common.obj) {
  1008. /* Which direction do we go to insert this node? */
  1009. if (rb_find_empty_direction(cur, sort_fn, node->common.obj, OBJ_SEARCH_OBJECT, bias)
  1010. == GO_LEFT) {
  1011. if (cur->left) {
  1012. cur = cur->left;
  1013. continue;
  1014. }
  1015. /* Node becomes a left child */
  1016. cur->left = node;
  1017. node->parent = cur;
  1018. rb_insert_fixup(self, node);
  1019. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1020. }
  1021. if (cur->right) {
  1022. cur = cur->right;
  1023. continue;
  1024. }
  1025. /* Node becomes a right child */
  1026. cur->right = node;
  1027. node->parent = cur;
  1028. rb_insert_fixup(self, node);
  1029. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1030. }
  1031. cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1032. if (cmp > 0) {
  1033. if (cur->left) {
  1034. cur = cur->left;
  1035. continue;
  1036. }
  1037. /* Node becomes a left child */
  1038. cur->left = node;
  1039. node->parent = cur;
  1040. rb_insert_fixup(self, node);
  1041. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1042. } else if (cmp < 0) {
  1043. if (cur->right) {
  1044. cur = cur->right;
  1045. continue;
  1046. }
  1047. /* Node becomes a right child */
  1048. cur->right = node;
  1049. node->parent = cur;
  1050. rb_insert_fixup(self, node);
  1051. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1052. }
  1053. switch (bias) {
  1054. case BIAS_FIRST:
  1055. /* Duplicate nodes unconditionally accepted. */
  1056. if (cur->left) {
  1057. cur = cur->left;
  1058. continue;
  1059. }
  1060. /* Node becomes a left child */
  1061. cur->left = node;
  1062. node->parent = cur;
  1063. rb_insert_fixup(self, node);
  1064. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1065. case BIAS_EQUAL:
  1066. break;
  1067. case BIAS_LAST:
  1068. /* Duplicate nodes unconditionally accepted. */
  1069. if (cur->right) {
  1070. cur = cur->right;
  1071. continue;
  1072. }
  1073. /* Node becomes a right child */
  1074. cur->right = node;
  1075. node->parent = cur;
  1076. rb_insert_fixup(self, node);
  1077. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1078. }
  1079. break;
  1080. }
  1081. /* Node is a dupliate */
  1082. switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  1083. default:
  1084. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  1085. ast_assert(0);/* Case already handled by BIAS_FIRST/BIAS_LAST. */
  1086. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1087. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  1088. /* Reject all objects with the same key. */
  1089. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1090. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  1091. if (cur->common.obj == node->common.obj) {
  1092. /* Reject inserting the same object */
  1093. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1094. }
  1095. next = cur;
  1096. if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) {
  1097. /* Search to end of duplicates for the same object. */
  1098. for (;;) {
  1099. next = rb_node_next_full(next);
  1100. if (!next) {
  1101. break;
  1102. }
  1103. if (next->common.obj == node->common.obj) {
  1104. /* Reject inserting the same object */
  1105. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1106. }
  1107. cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1108. if (cmp) {
  1109. break;
  1110. }
  1111. }
  1112. /* Find first duplicate node. */
  1113. for (;;) {
  1114. next = rb_node_prev_full(cur);
  1115. if (!next) {
  1116. break;
  1117. }
  1118. if (next->common.obj == node->common.obj) {
  1119. /* Reject inserting the same object */
  1120. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1121. }
  1122. cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1123. if (cmp) {
  1124. break;
  1125. }
  1126. cur = next;
  1127. }
  1128. if (!cur->left) {
  1129. /* Node becomes a left child */
  1130. cur->left = node;
  1131. } else {
  1132. /* Node becomes a right child */
  1133. cur = rb_node_most_right(cur->left);
  1134. cur->right = node;
  1135. }
  1136. } else {
  1137. /* Search to beginning of duplicates for the same object. */
  1138. for (;;) {
  1139. next = rb_node_prev_full(next);
  1140. if (!next) {
  1141. break;
  1142. }
  1143. if (next->common.obj == node->common.obj) {
  1144. /* Reject inserting the same object */
  1145. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1146. }
  1147. cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1148. if (cmp) {
  1149. break;
  1150. }
  1151. }
  1152. /* Find last duplicate node. */
  1153. for (;;) {
  1154. next = rb_node_next_full(cur);
  1155. if (!next) {
  1156. break;
  1157. }
  1158. if (next->common.obj == node->common.obj) {
  1159. /* Reject inserting the same object */
  1160. return AO2_CONTAINER_INSERT_NODE_REJECTED;
  1161. }
  1162. cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
  1163. if (cmp) {
  1164. break;
  1165. }
  1166. cur = next;
  1167. }
  1168. if (!cur->right) {
  1169. /* Node becomes a right child */
  1170. cur->right = node;
  1171. } else {
  1172. /* Node becomes a left child */
  1173. cur = rb_node_most_left(cur->right);
  1174. cur->left = node;
  1175. }
  1176. }
  1177. break;
  1178. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  1179. SWAP(cur->common.obj, node->common.obj);
  1180. ao2_t_ref(node, -1, "Don't need the new node.");
  1181. return AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED;
  1182. }
  1183. /* Complete inserting duplicate node. */
  1184. node->parent = cur;
  1185. rb_insert_fixup(self, node);
  1186. return AO2_CONTAINER_INSERT_NODE_INSERTED;
  1187. }
  1188. /*!
  1189. * \internal
  1190. * \brief Find the next rbtree container node in a traversal.
  1191. * \since 12.0.0
  1192. *
  1193. * \param self Container to operate upon.
  1194. * \param state Traversal state to restart rbtree container traversal.
  1195. * \param prev Previous node returned by the traversal search functions.
  1196. * The ref ownership is passed back to this function.
  1197. *
  1198. * \retval node-ptr of found node (Reffed).
  1199. * \retval NULL when no node found.
  1200. */
  1201. static struct rbtree_node *rb_ao2_find_next(struct ao2_container_rbtree *self, struct rbtree_traversal_state *state, struct rbtree_node *prev)
  1202. {
  1203. struct rbtree_node *node;
  1204. void *arg;
  1205. enum search_flags flags;
  1206. int cmp;
  1207. arg = state->arg;
  1208. flags = state->flags;
  1209. node = prev;
  1210. for (;;) {
  1211. /* Find next node in traversal order. */
  1212. switch (flags & OBJ_ORDER_MASK) {
  1213. default:
  1214. case OBJ_ORDER_ASCENDING:
  1215. node = rb_node_next(node);
  1216. break;
  1217. case OBJ_ORDER_DESCENDING:
  1218. node = rb_node_prev(node);
  1219. break;
  1220. case OBJ_ORDER_PRE:
  1221. node = rb_node_pre(node);
  1222. break;
  1223. case OBJ_ORDER_POST:
  1224. node = rb_node_post(node);
  1225. break;
  1226. }
  1227. if (!node) {
  1228. /* No more nodes left to traverse. */
  1229. break;
  1230. }
  1231. if (!node->common.obj) {
  1232. /* Node is empty */
  1233. continue;
  1234. }
  1235. if (state->sort_fn) {
  1236. /* Filter node through the sort_fn */
  1237. cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
  1238. if (cmp) {
  1239. /* No more nodes in this container are possible to match. */
  1240. break;
  1241. }
  1242. }
  1243. /* We have the next traversal node */
  1244. __ao2_ref(node, +1);
  1245. /*
  1246. * Dereferencing the prev node may result in our next node
  1247. * object being removed by another thread. This could happen if
  1248. * the container uses RW locks and the container was read
  1249. * locked.
  1250. */
  1251. __ao2_ref(prev, -1);
  1252. if (node->common.obj) {
  1253. return node;
  1254. }
  1255. prev = node;
  1256. }
  1257. /* No more nodes in the container left to traverse. */
  1258. __ao2_ref(prev, -1);
  1259. return NULL;
  1260. }
  1261. /*!
  1262. * \internal
  1263. * \brief Find an initial matching node.
  1264. * \since 12.0.0
  1265. *
  1266. * \param self Container to operate upon.
  1267. * \param obj_right pointer to the (user-defined part) of an object.
  1268. * \param flags flags from ao2_callback()
  1269. * OBJ_SEARCH_OBJECT - if set, 'obj_right', is an object.
  1270. * OBJ_SEARCH_KEY - if set, 'obj_right', is a search key item that is not an object.
  1271. * OBJ_SEARCH_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
  1272. * \param bias How to bias search direction for duplicates
  1273. *
  1274. * \retval node on success.
  1275. * \retval NULL if not found.
  1276. */
  1277. static struct rbtree_node *rb_find_initial(struct ao2_container_rbtree *self, void *obj_right, enum search_flags flags, enum equal_node_bias bias)
  1278. {
  1279. int cmp;
  1280. enum search_flags sort_flags;
  1281. struct rbtree_node *node;
  1282. struct rbtree_node *next = NULL;
  1283. ao2_sort_fn *sort_fn;
  1284. sort_flags = flags & OBJ_SEARCH_MASK;
  1285. sort_fn = self->common.sort_fn;
  1286. /* Find node where normal search would find it. */
  1287. node = self->root;
  1288. if (!node) {
  1289. return NULL;
  1290. }
  1291. for (;;) {
  1292. if (!node->common.obj) {
  1293. /* Which direction do we go to find the node? */
  1294. if (rb_find_empty_direction(node, sort_fn, obj_right, sort_flags, bias)
  1295. == GO_LEFT) {
  1296. next = node->left;
  1297. } else {
  1298. next = node->right;
  1299. }
  1300. if (!next) {
  1301. switch (bias) {
  1302. case BIAS_FIRST:
  1303. /* Check successor node for match. */
  1304. next = rb_node_next_full(node);
  1305. break;
  1306. case BIAS_EQUAL:
  1307. break;
  1308. case BIAS_LAST:
  1309. /* Check previous node for match. */
  1310. next = rb_node_prev_full(node);
  1311. break;
  1312. }
  1313. if (next) {
  1314. cmp = sort_fn(next->common.obj, obj_right, sort_flags);
  1315. if (cmp == 0) {
  1316. /* Found the first/last matching node. */
  1317. return next;
  1318. }
  1319. next = NULL;
  1320. }
  1321. /* No match found. */
  1322. return next;
  1323. }
  1324. } else {
  1325. cmp = sort_fn(node->common.obj, obj_right, sort_flags);
  1326. if (cmp > 0) {
  1327. next = node->left;
  1328. } else if (cmp < 0) {
  1329. next = node->right;
  1330. } else {
  1331. switch (bias) {
  1332. case BIAS_FIRST:
  1333. next = node->left;
  1334. break;
  1335. case BIAS_EQUAL:
  1336. return node;
  1337. case BIAS_LAST:
  1338. next = node->right;
  1339. break;
  1340. }
  1341. if (!next) {
  1342. /* Found the first/last matching node. */
  1343. return node;
  1344. }
  1345. }
  1346. if (!next) {
  1347. switch (bias) {
  1348. case BIAS_FIRST:
  1349. if (cmp < 0) {
  1350. /* Check successor node for match. */
  1351. next = rb_node_next_full(node);
  1352. }
  1353. break;
  1354. case BIAS_EQUAL:
  1355. break;
  1356. case BIAS_LAST:
  1357. if (cmp > 0) {
  1358. /* Check previous node for match. */
  1359. next = rb_node_prev_full(node);
  1360. }
  1361. break;
  1362. }
  1363. if (next) {
  1364. cmp = sort_fn(next->common.obj, obj_right, sort_flags);
  1365. if (cmp == 0) {
  1366. /* Found the first/last matching node. */
  1367. return next;
  1368. }
  1369. }
  1370. /* No match found. */
  1371. return NULL;
  1372. }
  1373. }
  1374. node = next;
  1375. }
  1376. }
  1377. /*!
  1378. * \internal
  1379. * \brief Find the first rbtree container node in a traversal.
  1380. * \since 12.0.0
  1381. *
  1382. * \param self Container to operate upon.
  1383. * \param flags search_flags to control traversing the container
  1384. * \param arg Comparison callback arg parameter.
  1385. * \param state Traversal state to restart rbtree container traversal.
  1386. *
  1387. * \retval node-ptr of found node (Reffed).
  1388. * \retval NULL when no node found.
  1389. */
  1390. static struct rbtree_node *rb_ao2_find_first(struct ao2_container_rbtree *self, enum search_flags flags, void *arg, struct rbtree_traversal_state *state)
  1391. {
  1392. struct rbtree_node *node;
  1393. enum equal_node_bias bias;
  1394. if (self->common.destroying) {
  1395. /* Force traversal to be post order for tree destruction. */
  1396. flags = OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_ORDER_POST;
  1397. }
  1398. memset(state, 0, sizeof(*state));
  1399. state->arg = arg;
  1400. state->flags = flags;
  1401. switch (flags & OBJ_SEARCH_MASK) {
  1402. case OBJ_SEARCH_OBJECT:
  1403. case OBJ_SEARCH_KEY:
  1404. case OBJ_SEARCH_PARTIAL_KEY:
  1405. /* We are asked to do a directed search. */
  1406. state->sort_fn = self->common.sort_fn;
  1407. break;
  1408. default:
  1409. /* Don't know, let's visit all nodes */
  1410. state->sort_fn = NULL;
  1411. break;
  1412. }
  1413. if (!self->root) {
  1414. /* Tree is empty. */
  1415. return NULL;
  1416. }
  1417. /* Find first traversal node. */
  1418. switch (flags & OBJ_ORDER_MASK) {
  1419. default:
  1420. case OBJ_ORDER_ASCENDING:
  1421. if (!state->sort_fn) {
  1422. /* Find left most child. */
  1423. node = rb_node_most_left(self->root);
  1424. if (!node->common.obj) {
  1425. node = rb_node_next_full(node);
  1426. if (!node) {
  1427. return NULL;
  1428. }
  1429. }
  1430. break;
  1431. }
  1432. /* Search for initial node. */
  1433. switch (self->common.options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  1434. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  1435. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  1436. if ((flags & OBJ_SEARCH_MASK) != OBJ_SEARCH_PARTIAL_KEY) {
  1437. /* There are no duplicates allowed. */
  1438. bias = BIAS_EQUAL;
  1439. break;
  1440. }
  1441. /* Fall through */
  1442. default:
  1443. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  1444. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  1445. /* Find first duplicate node. */
  1446. bias = BIAS_FIRST;
  1447. break;
  1448. }
  1449. node = rb_find_initial(self, arg, flags, bias);
  1450. if (!node) {
  1451. return NULL;
  1452. }
  1453. break;
  1454. case OBJ_ORDER_DESCENDING:
  1455. if (!state->sort_fn) {
  1456. /* Find right most child. */
  1457. node = rb_node_most_right(self->root);
  1458. if (!node->common.obj) {
  1459. node = rb_node_prev_full(node);
  1460. if (!node) {
  1461. return NULL;
  1462. }
  1463. }
  1464. break;
  1465. }
  1466. /* Search for initial node. */
  1467. switch (self->common.options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
  1468. case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
  1469. case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
  1470. if ((flags & OBJ_SEARCH_MASK) != OBJ_SEARCH_PARTIAL_KEY) {
  1471. /* There are no duplicates allowed. */
  1472. bias = BIAS_EQUAL;
  1473. break;
  1474. }
  1475. /* Fall through */
  1476. default:
  1477. case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
  1478. case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
  1479. /* Find last duplicate node. */
  1480. bias = BIAS_LAST;
  1481. break;
  1482. }
  1483. node = rb_find_initial(self, arg, flags, bias);
  1484. if (!node) {
  1485. return NULL;
  1486. }
  1487. break;
  1488. case OBJ_ORDER_PRE:
  1489. /* This is a tree structure traversal so we must visit all nodes. */
  1490. state->sort_fn = NULL;
  1491. node = self->root;
  1492. /* Find a non-empty node. */
  1493. while (!node->common.obj) {
  1494. node = rb_node_pre(node);
  1495. if (!node) {
  1496. return NULL;
  1497. }
  1498. }
  1499. break;
  1500. case OBJ_ORDER_POST:
  1501. /* This is a tree structure traversal so we must visit all nodes. */
  1502. state->sort_fn = NULL;
  1503. /* Find the left most childless node. */
  1504. node = self->root;
  1505. for (;;) {
  1506. node = rb_node_most_left(node);
  1507. if (!node->right) {
  1508. /* This node has no children. */
  1509. break;
  1510. }
  1511. node = node->right;
  1512. }
  1513. /* Find a non-empty node. */
  1514. while (!node->common.obj) {
  1515. node = rb_node_post(node);
  1516. if (!node) {
  1517. return NULL;
  1518. }
  1519. }
  1520. break;
  1521. }
  1522. /* We have the first traversal node */
  1523. __ao2_ref(node, +1);
  1524. return node;
  1525. }
  1526. /*!
  1527. * \internal
  1528. * \brief Find the next non-empty iteration node in the container.
  1529. * \since 12.0.0
  1530. *
  1531. * \param self Container to operate upon.
  1532. * \param node Previous node returned by the iterator.
  1533. * \param flags search_flags to control iterating the container.
  1534. * Only AO2_ITERATOR_DESCENDING is useful by the method.
  1535. *
  1536. * \note The container is already locked.
  1537. *
  1538. * \retval node on success.
  1539. * \retval NULL on error or no more nodes in the container.
  1540. */
  1541. static struct rbtree_node *rb_ao2_iterator_next(struct ao2_container_rbtree *self, struct rbtree_node *node, enum ao2_iterator_flags flags)
  1542. {
  1543. if (flags & AO2_ITERATOR_DESCENDING) {
  1544. if (!node) {
  1545. /* Find right most node. */
  1546. if (!self->root) {
  1547. return NULL;
  1548. }
  1549. node = rb_node_most_right(self->root);
  1550. if (node->common.obj) {
  1551. /* Found a non-empty node. */
  1552. return node;
  1553. }
  1554. }
  1555. /* Find next non-empty node. */
  1556. node = rb_node_prev_full(node);
  1557. } else {
  1558. if (!node) {
  1559. /* Find left most node. */
  1560. if (!self->root) {
  1561. return NULL;
  1562. }
  1563. node = rb_node_most_left(self->root);
  1564. if (node->common.obj) {
  1565. /* Found a non-empty node. */
  1566. return node;
  1567. }
  1568. }
  1569. /* Find next non-empty node. */
  1570. node = rb_node_next_full(node);
  1571. }
  1572. return node;
  1573. }
  1574. /*!
  1575. * \internal
  1576. *
  1577. * \brief Destroy this container.
  1578. * \since 12.0.0
  1579. *
  1580. * \param self Container to operate upon.
  1581. *
  1582. * \return Nothing
  1583. */
  1584. static void rb_ao2_destroy(struct ao2_container_rbtree *self)
  1585. {
  1586. /* Check that the container no longer has any nodes */
  1587. if (self->root) {
  1588. ast_log(LOG_ERROR, "Node ref leak. Red-Black tree container still has nodes!\n");
  1589. ast_assert(0);
  1590. }
  1591. }
  1592. #if defined(AO2_DEBUG)
  1593. /*!
  1594. * \internal
  1595. * \brief Display contents of the specified container.
  1596. * \since 12.0.0
  1597. *
  1598. * \param self Container to dump.
  1599. * \param where User data needed by prnt to determine where to put output.
  1600. * \param prnt Print output callback function to use.
  1601. * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
  1602. *
  1603. * \return Nothing
  1604. */
  1605. static void rb_ao2_dump(struct ao2_container_rbtree *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj)
  1606. {
  1607. #define FORMAT "%16s, %16s, %16s, %16s, %5s, %16s, %s\n"
  1608. #define FORMAT2 "%16p, %16p, %16p, %16p, %5s, %16p, "
  1609. struct rbtree_node *node;
  1610. prnt(where, FORMAT, "Node", "Parent", "Left", "Right", "Color", "Obj", "Key");
  1611. for (node = self->root; node; node = rb_node_pre(node)) {
  1612. prnt(where, FORMAT2,
  1613. node,
  1614. node->parent,
  1615. node->left,
  1616. node->right,
  1617. node->is_red ? "Red" : "Black",
  1618. node->common.obj);
  1619. if (node->common.obj && prnt_obj) {
  1620. prnt_obj(node->common.obj, where, prnt);
  1621. }
  1622. prnt(where, "\n");
  1623. }
  1624. #undef FORMAT
  1625. #undef FORMAT2
  1626. }
  1627. #endif /* defined(AO2_DEBUG) */
  1628. #if defined(AO2_DEBUG)
  1629. /*!
  1630. * \internal
  1631. * \brief Display statistics of the specified container.
  1632. * \since 12.0.0
  1633. *
  1634. * \param self Container to display statistics.
  1635. * \param where User data needed by prnt to determine where to put output.
  1636. * \param prnt Print output callback function to use.
  1637. *
  1638. * \note The container is already locked for reading.
  1639. *
  1640. * \return Nothing
  1641. */
  1642. static void rb_ao2_stats(struct ao2_container_rbtree *self, void *where, ao2_prnt_fn *prnt)
  1643. {
  1644. int idx;
  1645. for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_insert_left); ++idx) {
  1646. prnt(where, "Number of left insert fixups case %d: %d\n", idx + 1,
  1647. self->stats.fixup_insert_left[idx]);
  1648. }
  1649. for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_insert_right); ++idx) {
  1650. prnt(where, "Number of right insert fixups case %d: %d\n", idx + 1,
  1651. self->stats.fixup_insert_right[idx]);
  1652. }
  1653. for (idx = 0; idx < ARRAY_LEN(self->stats.delete_children); ++idx) {
  1654. prnt(where, "Number of nodes deleted with %d children: %d\n", idx,
  1655. self->stats.delete_children[idx]);
  1656. }
  1657. for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_delete_left); ++idx) {
  1658. prnt(where, "Number of left delete fixups case %d: %d\n", idx + 1,
  1659. self->stats.fixup_delete_left[idx]);
  1660. }
  1661. for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_delete_right); ++idx) {
  1662. prnt(where, "Number of right delete fixups case %d: %d\n", idx + 1,
  1663. self->stats.fixup_delete_right[idx]);
  1664. }
  1665. }
  1666. #endif /* defined(AO2_DEBUG) */
  1667. #if defined(AO2_DEBUG)
  1668. /*!
  1669. * \internal
  1670. * \brief Check the black height of the given node.
  1671. * \since 12.0.0
  1672. *
  1673. * \param node Node to check black height.
  1674. *
  1675. * \retval black-height of node on success.
  1676. * \retval -1 on error. Node black height did not balance.
  1677. */
  1678. static int rb_check_black_height(struct rbtree_node *node)
  1679. {
  1680. int height_left;
  1681. int height_right;
  1682. if (!node) {
  1683. /* A NULL child is a black node. */
  1684. return 0;
  1685. }
  1686. height_left = rb_check_black_height(node->left);
  1687. if (height_left < 0) {
  1688. return -1;
  1689. }
  1690. height_right = rb_check_black_height(node->right);
  1691. if (height_right < 0) {
  1692. return -1;
  1693. }
  1694. if (height_left != height_right) {
  1695. ast_log(LOG_ERROR,
  1696. "Tree node black height of children does not match! L:%d != R:%d\n",
  1697. height_left, height_right);
  1698. return -1;
  1699. }
  1700. if (!node->is_red) {
  1701. /* The node itself is black. */
  1702. ++height_left;
  1703. }
  1704. return height_left;
  1705. }
  1706. #endif /* defined(AO2_DEBUG) */
  1707. #if defined(AO2_DEBUG)
  1708. /*!
  1709. * \internal
  1710. * \brief Perform an integrity check on the specified container.
  1711. * \since 12.0.0
  1712. *
  1713. * \param self Container to check integrity.
  1714. *
  1715. * \note The container is already locked for reading.
  1716. *
  1717. * \retval 0 on success.
  1718. * \retval -1 on error.
  1719. */
  1720. static int rb_ao2_integrity(struct ao2_container_rbtree *self)
  1721. {
  1722. int res;
  1723. int count_node;
  1724. int count_obj;
  1725. void *obj_last;
  1726. struct rbtree_node *node;
  1727. res = 0;
  1728. count_node = 0;
  1729. count_obj = 0;
  1730. /*
  1731. * See the properties listed at struct rbtree_node definition.
  1732. *
  1733. * The rbtree properties 1 and 3 are not testable.
  1734. *
  1735. * Property 1 is not testable because we are not rebalancing at
  1736. * this time so all nodes are either red or black.
  1737. *
  1738. * Property 3 is not testable because it is the definition of a
  1739. * NULL child.
  1740. */
  1741. if (self->root) {
  1742. /* Check tree links. */
  1743. if (self->root->parent) {
  1744. if (self->root->parent == self->root) {
  1745. ast_log(LOG_ERROR, "Tree root parent pointer points to itself!\n");
  1746. } else {
  1747. ast_log(LOG_ERROR, "Tree root is not a root node!\n");
  1748. }
  1749. return -1;
  1750. }
  1751. if (self->root->is_red) {
  1752. /* Violation rbtree property 2. */
  1753. ast_log(LOG_ERROR, "Tree root is red!\n");
  1754. res = -1;
  1755. }
  1756. node = self->root;
  1757. do {
  1758. if (node->left) {
  1759. if (node->left == node) {
  1760. ast_log(LOG_ERROR, "Tree node's left pointer points to itself!\n");
  1761. return -1;
  1762. }
  1763. if (node->left->parent != node) {
  1764. ast_log(LOG_ERROR, "Tree node's left child does not link back!\n");
  1765. return -1;
  1766. }
  1767. }
  1768. if (node->right) {
  1769. if (node->right == node) {
  1770. ast_log(LOG_ERROR, "Tree node's right pointer points to itself!\n");
  1771. return -1;
  1772. }
  1773. if (node->right->parent != node) {
  1774. ast_log(LOG_ERROR, "Tree node's right child does not link back!\n");
  1775. return -1;
  1776. }
  1777. }
  1778. /* Check red/black node flags. */
  1779. if (node->is_red) {
  1780. /* A red node must have two black children or no children. */
  1781. if (node->left && node->right) {
  1782. /* Node has two children. */
  1783. if (node->left->is_red) {
  1784. /* Violation rbtree property 4. */
  1785. ast_log(LOG_ERROR, "Tree node is red and its left child is red!\n");
  1786. res = -1;
  1787. }
  1788. if (node->right->is_red) {
  1789. /* Violation rbtree property 4. */
  1790. ast_log(LOG_ERROR, "Tree node is red and its right child is red!\n");
  1791. res = -1;
  1792. }
  1793. } else if (node->left || node->right) {
  1794. /*
  1795. * Violation rbtree property 4 if the child is red.
  1796. * Violation rbtree property 5 if the child is black.
  1797. */
  1798. ast_log(LOG_ERROR, "Tree node is red and it only has one child!\n");
  1799. res = -1;
  1800. }
  1801. } else {
  1802. /*
  1803. * A black node must have two children, or one red child, or no
  1804. * children. If the black node has two children and only one of
  1805. * them is red, that red child must have two children.
  1806. */
  1807. if (node->left && node->right) {
  1808. /* Node has two children. */
  1809. if (node->left->is_red != node->right->is_red) {
  1810. /* The children are not the same color. */
  1811. struct rbtree_node *red;
  1812. if (node->left->is_red) {
  1813. red = node->left;
  1814. } else {
  1815. red = node->right;
  1816. }
  1817. if (!red->left || !red->right) {
  1818. /* Violation rbtree property 5. */
  1819. ast_log(LOG_ERROR,
  1820. "Tree node is black and the red child does not have two children!\n");
  1821. res = -1;
  1822. }
  1823. }
  1824. } else if ((node->left && !node->left->is_red)
  1825. || (node->right && !node->right->is_red)) {
  1826. /* Violation rbtree property 5. */
  1827. ast_log(LOG_ERROR, "Tree node is black and its only child is black!\n");
  1828. res = -1;
  1829. }
  1830. }
  1831. /* Count nodes and objects. */
  1832. ++count_node;
  1833. if (node->common.obj) {
  1834. ++count_obj;
  1835. }
  1836. node = rb_node_pre(node);
  1837. } while (node);
  1838. /* Check node key sort order. */
  1839. obj_last = NULL;
  1840. for (node = rb_node_most_left(self->root); node; node = rb_node_next(node)) {
  1841. if (!node->common.obj) {
  1842. /* Node is empty. */
  1843. continue;
  1844. }
  1845. if (obj_last) {
  1846. if (self->common.sort_fn(obj_last, node->common.obj, OBJ_SEARCH_OBJECT) > 0) {
  1847. ast_log(LOG_ERROR, "Tree nodes are out of sorted order!\n");
  1848. return -1;
  1849. }
  1850. }
  1851. obj_last = node->common.obj;
  1852. }
  1853. /* Completely check property 5 */
  1854. if (!res && rb_check_black_height(self->root) < 0) {
  1855. /* Violation rbtree property 5. */
  1856. res = -1;
  1857. }
  1858. }
  1859. /* Check total obj count. */
  1860. if (count_obj != ao2_container_count(&self->common)) {
  1861. ast_log(LOG_ERROR, "Total object count does not match ao2_container_count()!\n");
  1862. return -1;
  1863. }
  1864. /* Check total node count. */
  1865. if (count_node != self->common.nodes) {
  1866. ast_log(LOG_ERROR, "Total node count of %d does not match stat of %d!\n",
  1867. count_node, self->common.nodes);
  1868. return -1;
  1869. }
  1870. return res;
  1871. }
  1872. #endif /* defined(AO2_DEBUG) */
  1873. /*! rbtree container virtual method table. */
  1874. static const struct ao2_container_methods v_table_rbtree = {
  1875. .alloc_empty_clone = (ao2_container_alloc_empty_clone_fn) rb_ao2_alloc_empty_clone,
  1876. .alloc_empty_clone_debug =
  1877. (ao2_container_alloc_empty_clone_debug_fn) rb_ao2_alloc_empty_clone_debug,
  1878. .new_node = (ao2_container_new_node_fn) rb_ao2_new_node,
  1879. .insert = (ao2_container_insert_fn) rb_ao2_insert_node,
  1880. .traverse_first = (ao2_container_find_first_fn) rb_ao2_find_first,
  1881. .traverse_next = (ao2_container_find_next_fn) rb_ao2_find_next,
  1882. .iterator_next = (ao2_iterator_next_fn) rb_ao2_iterator_next,
  1883. .destroy = (ao2_container_destroy_fn) rb_ao2_destroy,
  1884. #if defined(AO2_DEBUG)
  1885. .dump = (ao2_container_display) rb_ao2_dump,
  1886. .stats = (ao2_container_statistics) rb_ao2_stats,
  1887. .integrity = (ao2_container_integrity) rb_ao2_integrity,
  1888. #endif /* defined(AO2_DEBUG) */
  1889. };
  1890. /*!
  1891. * \brief Initialize a rbtree container.
  1892. *
  1893. * \param self Container to initialize.
  1894. * \param options Container behaviour options (See enum ao2_container_opts)
  1895. * \param sort_fn Pointer to a sort function.
  1896. * \param cmp_fn Pointer to a compare function used by ao2_find.
  1897. *
  1898. * \return A pointer to a struct container.
  1899. */
  1900. static struct ao2_container *rb_ao2_container_init(struct ao2_container_rbtree *self,
  1901. unsigned int options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
  1902. {
  1903. if (!self) {
  1904. return NULL;
  1905. }
  1906. self->common.v_table = &v_table_rbtree;
  1907. self->common.sort_fn = sort_fn;
  1908. self->common.cmp_fn = cmp_fn;
  1909. self->common.options = options;
  1910. #ifdef AO2_DEBUG
  1911. ast_atomic_fetchadd_int(&ao2.total_containers, 1);
  1912. #endif /* defined(AO2_DEBUG) */
  1913. return (struct ao2_container *) self;
  1914. }
  1915. struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options,
  1916. ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
  1917. {
  1918. struct ao2_container_rbtree *self;
  1919. if (!sort_fn) {
  1920. /* Sanity checks. */
  1921. ast_log(LOG_ERROR, "Missing sort_fn()!\n");
  1922. return NULL;
  1923. }
  1924. self = ao2_t_alloc_options(sizeof(*self), container_destruct, ao2_options,
  1925. "New rbtree container");
  1926. return rb_ao2_container_init(self, container_options, sort_fn, cmp_fn);
  1927. }
  1928. struct ao2_container *__ao2_container_alloc_rbtree_debug(unsigned int ao2_options, unsigned int container_options,
  1929. ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
  1930. const char *tag, const char *file, int line, const char *func, int ref_debug)
  1931. {
  1932. struct ao2_container_rbtree *self;
  1933. if (!sort_fn) {
  1934. /* Sanity checks. */
  1935. ast_log(__LOG_ERROR, file, line, func, "Missing sort_fn()!\n");
  1936. return NULL;
  1937. }
  1938. self = __ao2_alloc_debug(sizeof(*self),
  1939. ref_debug ? container_destruct_debug : container_destruct, ao2_options,
  1940. tag, file, line, func, ref_debug);
  1941. return rb_ao2_container_init(self, container_options, sort_fn, cmp_fn);
  1942. }