astobj2_container.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /* astobj2 - replacement containers for asterisk data structures.
  2. *
  3. * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy
  4. *
  5. * See http://www.asterisk.org for more information about
  6. * the Asterisk project. Please do not directly contact
  7. * any of the maintainers of this project for assistance;
  8. * the project provides a web site, mailing lists and IRC
  9. * channels for your use.
  10. *
  11. * This program is free software, distributed under the terms of
  12. * the GNU General Public License Version 2. See the LICENSE file
  13. * at the top of the source tree.
  14. */
  15. /*! \file
  16. *
  17. * \brief Functions implementing astobj2 objects.
  18. *
  19. * \author Richard Mudgett <rmudgett@digium.com>
  20. */
  21. #include "asterisk.h"
  22. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  23. #include "asterisk/_private.h"
  24. #include "asterisk/astobj2.h"
  25. #include "astobj2_private.h"
  26. #include "astobj2_container_private.h"
  27. #include "asterisk/cli.h"
  28. /*!
  29. * return the number of elements in the container
  30. */
  31. int ao2_container_count(struct ao2_container *c)
  32. {
  33. return ast_atomic_fetchadd_int(&c->elements, 0);
  34. }
  35. int __container_unlink_node_debug(struct ao2_container_node *node, uint32_t flags,
  36. const char *tag, const char *file, int line, const char *func)
  37. {
  38. struct ao2_container *container = node->my_container;
  39. if (container == NULL && (flags & AO2_UNLINK_NODE_DEC_COUNT)) {
  40. return 0;
  41. }
  42. if ((flags & AO2_UNLINK_NODE_UNLINK_OBJECT)
  43. && !(flags & AO2_UNLINK_NODE_NOUNREF_OBJECT)) {
  44. if (tag) {
  45. __ao2_ref_debug(node->obj, -1, tag, file, line, func);
  46. } else {
  47. ao2_t_ref(node->obj, -1, "Remove obj from container");
  48. }
  49. }
  50. node->obj = NULL;
  51. if (flags & AO2_UNLINK_NODE_DEC_COUNT) {
  52. ast_atomic_fetchadd_int(&container->elements, -1);
  53. #if defined(AO2_DEBUG)
  54. {
  55. int empty = container->nodes - container->elements;
  56. if (container->max_empty_nodes < empty) {
  57. container->max_empty_nodes = empty;
  58. }
  59. if (container->v_table->unlink_stat) {
  60. container->v_table->unlink_stat(container, node);
  61. }
  62. }
  63. #endif /* defined(AO2_DEBUG) */
  64. }
  65. if (flags & AO2_UNLINK_NODE_UNREF_NODE) {
  66. /* Remove node from container */
  67. __ao2_ref(node, -1);
  68. }
  69. return 1;
  70. }
  71. /*!
  72. * \internal
  73. * \brief Link an object into this container. (internal)
  74. *
  75. * \param self Container to operate upon.
  76. * \param obj_new Object to insert into the container.
  77. * \param flags search_flags to control linking the object. (OBJ_NOLOCK)
  78. * \param tag used for debugging.
  79. * \param file Debug file name invoked from
  80. * \param line Debug line invoked from
  81. * \param func Debug function name invoked from
  82. *
  83. * \retval 0 on errors.
  84. * \retval 1 on success.
  85. */
  86. static int internal_ao2_link(struct ao2_container *self, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func)
  87. {
  88. int res;
  89. enum ao2_lock_req orig_lock;
  90. struct ao2_container_node *node;
  91. if (!is_ao2_object(obj_new) || !is_ao2_object(self)) {
  92. return 0;
  93. }
  94. if (!self->v_table || !self->v_table->new_node || !self->v_table->insert) {
  95. /* Sanity checks. */
  96. ast_assert(0);
  97. return 0;
  98. }
  99. if (flags & OBJ_NOLOCK) {
  100. orig_lock = __adjust_lock(self, AO2_LOCK_REQ_WRLOCK, 1);
  101. } else {
  102. ao2_wrlock(self);
  103. orig_lock = AO2_LOCK_REQ_MUTEX;
  104. }
  105. res = 0;
  106. node = self->v_table->new_node(self, obj_new, tag, file, line, func);
  107. if (node) {
  108. #if defined(AO2_DEBUG)
  109. if (ao2_container_check(self, OBJ_NOLOCK)) {
  110. ast_log(LOG_ERROR, "Container integrity failed before insert.\n");
  111. }
  112. #endif /* defined(AO2_DEBUG) */
  113. /* Insert the new node. */
  114. switch (self->v_table->insert(self, node)) {
  115. case AO2_CONTAINER_INSERT_NODE_INSERTED:
  116. node->is_linked = 1;
  117. ast_atomic_fetchadd_int(&self->elements, 1);
  118. #if defined(AO2_DEBUG)
  119. AO2_DEVMODE_STAT(++self->nodes);
  120. if (self->v_table->link_stat) {
  121. self->v_table->link_stat(self, node);
  122. }
  123. #endif /* defined(AO2_DEBUG) */
  124. /* Fall through */
  125. case AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED:
  126. #if defined(AO2_DEBUG)
  127. if (ao2_container_check(self, OBJ_NOLOCK)) {
  128. ast_log(LOG_ERROR, "Container integrity failed after insert or replace.\n");
  129. }
  130. #endif /* defined(AO2_DEBUG) */
  131. res = 1;
  132. break;
  133. case AO2_CONTAINER_INSERT_NODE_REJECTED:
  134. __ao2_ref(node, -1);
  135. break;
  136. }
  137. }
  138. if (flags & OBJ_NOLOCK) {
  139. __adjust_lock(self, orig_lock, 0);
  140. } else {
  141. ao2_unlock(self);
  142. }
  143. return res;
  144. }
  145. int __ao2_link_debug(struct ao2_container *c, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func)
  146. {
  147. return internal_ao2_link(c, obj_new, flags, tag, file, line, func);
  148. }
  149. int __ao2_link(struct ao2_container *c, void *obj_new, int flags)
  150. {
  151. return internal_ao2_link(c, obj_new, flags, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__);
  152. }
  153. /*!
  154. * \brief another convenience function is a callback that matches on address
  155. */
  156. int ao2_match_by_addr(void *user_data, void *arg, int flags)
  157. {
  158. return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
  159. }
  160. /*
  161. * Unlink an object from the container
  162. * and destroy the associated * bucket_entry structure.
  163. */
  164. void *__ao2_unlink_debug(struct ao2_container *c, void *user_data, int flags,
  165. const char *tag, const char *file, int line, const char *func)
  166. {
  167. if (!is_ao2_object(user_data)) {
  168. return NULL;
  169. }
  170. flags &= ~OBJ_SEARCH_MASK;
  171. flags |= (OBJ_UNLINK | OBJ_SEARCH_OBJECT | OBJ_NODATA);
  172. __ao2_callback_debug(c, flags, ao2_match_by_addr, user_data, tag, file, line, func);
  173. return NULL;
  174. }
  175. void *__ao2_unlink(struct ao2_container *c, void *user_data, int flags)
  176. {
  177. if (!is_ao2_object(user_data)) {
  178. return NULL;
  179. }
  180. flags &= ~OBJ_SEARCH_MASK;
  181. flags |= (OBJ_UNLINK | OBJ_SEARCH_OBJECT | OBJ_NODATA);
  182. __ao2_callback(c, flags, ao2_match_by_addr, user_data);
  183. return NULL;
  184. }
  185. /*!
  186. * \brief special callback that matches all
  187. */
  188. static int cb_true(void *user_data, void *arg, int flags)
  189. {
  190. return CMP_MATCH;
  191. }
  192. /*!
  193. * \brief similar to cb_true, but is an ao2_callback_data_fn instead
  194. */
  195. static int cb_true_data(void *user_data, void *arg, void *data, int flags)
  196. {
  197. return CMP_MATCH;
  198. }
  199. /*!
  200. * \internal
  201. * \brief Traverse the container. (internal)
  202. *
  203. * \param self Container to operate upon.
  204. * \param flags search_flags to control traversing the container
  205. * \param cb_fn Comparison callback function.
  206. * \param arg Comparison callback arg parameter.
  207. * \param data Data comparison callback data parameter.
  208. * \param type Type of comparison callback cb_fn.
  209. * \param tag used for debugging.
  210. * \param file Debug file name invoked from
  211. * \param line Debug line invoked from
  212. * \param func Debug function name invoked from
  213. *
  214. * \retval NULL on failure or no matching object found.
  215. *
  216. * \retval object found if OBJ_MULTIPLE is not set in the flags
  217. * parameter.
  218. *
  219. * \retval ao2_iterator pointer if OBJ_MULTIPLE is set in the
  220. * flags parameter. The iterator must be destroyed with
  221. * ao2_iterator_destroy() when the caller no longer needs it.
  222. */
  223. static void *internal_ao2_traverse(struct ao2_container *self, enum search_flags flags,
  224. void *cb_fn, void *arg, void *data, enum ao2_callback_type type,
  225. const char *tag, const char *file, int line, const char *func)
  226. {
  227. void *ret;
  228. ao2_callback_fn *cb_default = NULL;
  229. ao2_callback_data_fn *cb_withdata = NULL;
  230. struct ao2_container_node *node;
  231. void *traversal_state;
  232. enum ao2_lock_req orig_lock;
  233. struct ao2_container *multi_container = NULL;
  234. struct ao2_iterator *multi_iterator = NULL;
  235. if (!is_ao2_object(self)) {
  236. return NULL;
  237. }
  238. if (!self->v_table || !self->v_table->traverse_first
  239. || !self->v_table->traverse_next) {
  240. /* Sanity checks. */
  241. ast_assert(0);
  242. return NULL;
  243. }
  244. /*
  245. * This logic is used so we can support OBJ_MULTIPLE with OBJ_NODATA
  246. * turned off. This if statement checks for the special condition
  247. * where multiple items may need to be returned.
  248. */
  249. if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
  250. /* we need to return an ao2_iterator with the results,
  251. * as there could be more than one. the iterator will
  252. * hold the only reference to a container that has all the
  253. * matching objects linked into it, so when the iterator
  254. * is destroyed, the container will be automatically
  255. * destroyed as well.
  256. */
  257. multi_container = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL,
  258. NULL, "OBJ_MULTIPLE return container creation");
  259. if (!multi_container) {
  260. return NULL;
  261. }
  262. if (!(multi_iterator = ast_calloc(1, sizeof(*multi_iterator)))) {
  263. ao2_t_ref(multi_container, -1, "OBJ_MULTIPLE interator creation failed.");
  264. return NULL;
  265. }
  266. }
  267. if (!cb_fn) {
  268. /* Match everything if no callback match function provided. */
  269. if (type == AO2_CALLBACK_WITH_DATA) {
  270. cb_withdata = cb_true_data;
  271. } else {
  272. cb_default = cb_true;
  273. }
  274. } else {
  275. /*
  276. * We do this here to avoid the per object casting penalty (even
  277. * though that is probably optimized away anyway).
  278. */
  279. if (type == AO2_CALLBACK_WITH_DATA) {
  280. cb_withdata = cb_fn;
  281. } else {
  282. cb_default = cb_fn;
  283. }
  284. }
  285. /* avoid modifications to the content */
  286. if (flags & OBJ_NOLOCK) {
  287. if (flags & OBJ_UNLINK) {
  288. orig_lock = __adjust_lock(self, AO2_LOCK_REQ_WRLOCK, 1);
  289. } else {
  290. orig_lock = __adjust_lock(self, AO2_LOCK_REQ_RDLOCK, 1);
  291. }
  292. } else {
  293. orig_lock = AO2_LOCK_REQ_MUTEX;
  294. if (flags & OBJ_UNLINK) {
  295. ao2_wrlock(self);
  296. } else {
  297. ao2_rdlock(self);
  298. }
  299. }
  300. /* Create a buffer for the traversal state. */
  301. traversal_state = alloca(AO2_TRAVERSAL_STATE_SIZE);
  302. ret = NULL;
  303. for (node = self->v_table->traverse_first(self, flags, arg, traversal_state);
  304. node;
  305. node = self->v_table->traverse_next(self, traversal_state, node)) {
  306. int match;
  307. /* Visit the current node. */
  308. match = (CMP_MATCH | CMP_STOP);
  309. if (type == AO2_CALLBACK_WITH_DATA) {
  310. match &= cb_withdata(node->obj, arg, data, flags);
  311. } else {
  312. match &= cb_default(node->obj, arg, flags);
  313. }
  314. if (match == 0) {
  315. /* no match, no stop, continue */
  316. continue;
  317. }
  318. if (match == CMP_STOP) {
  319. /* no match but stop, we are done */
  320. break;
  321. }
  322. /*
  323. * CMP_MATCH is set here
  324. *
  325. * we found the object, performing operations according to flags
  326. */
  327. if (node->obj) {
  328. /* The object is still in the container. */
  329. if (!(flags & OBJ_NODATA)) {
  330. /*
  331. * We are returning the object, record the value. It is
  332. * important to handle this case before the unlink.
  333. */
  334. if (multi_container) {
  335. /*
  336. * Link the object into the container that will hold the
  337. * results.
  338. */
  339. if (tag) {
  340. __ao2_link_debug(multi_container, node->obj, flags,
  341. tag, file, line, func);
  342. } else {
  343. __ao2_link(multi_container, node->obj, flags);
  344. }
  345. } else {
  346. ret = node->obj;
  347. /* Returning a single object. */
  348. if (!(flags & OBJ_UNLINK)) {
  349. /*
  350. * Bump the ref count since we are not going to unlink and
  351. * transfer the container's object ref to the returned object.
  352. */
  353. if (tag) {
  354. __ao2_ref_debug(ret, 1, tag, file, line, func);
  355. } else {
  356. ao2_t_ref(ret, 1, "Traversal found object");
  357. }
  358. }
  359. }
  360. }
  361. if (flags & OBJ_UNLINK) {
  362. int ulflag = AO2_UNLINK_NODE_UNREF_NODE | AO2_UNLINK_NODE_DEC_COUNT;
  363. if (multi_container || (flags & OBJ_NODATA)) {
  364. ulflag |= AO2_UNLINK_NODE_UNLINK_OBJECT;
  365. }
  366. __container_unlink_node_debug(node, ulflag, tag, file, line, func);
  367. }
  368. }
  369. if ((match & CMP_STOP) || !(flags & OBJ_MULTIPLE)) {
  370. /* We found our only (or last) match, so we are done */
  371. break;
  372. }
  373. }
  374. if (self->v_table->traverse_cleanup) {
  375. self->v_table->traverse_cleanup(traversal_state);
  376. }
  377. if (node) {
  378. /* Unref the node from self->v_table->traverse_first/traverse_next() */
  379. __ao2_ref(node, -1);
  380. }
  381. if (flags & OBJ_NOLOCK) {
  382. __adjust_lock(self, orig_lock, 0);
  383. } else {
  384. ao2_unlock(self);
  385. }
  386. /* if multi_container was created, we are returning multiple objects */
  387. if (multi_container) {
  388. *multi_iterator = ao2_iterator_init(multi_container,
  389. AO2_ITERATOR_UNLINK | AO2_ITERATOR_MALLOCD);
  390. ao2_t_ref(multi_container, -1,
  391. "OBJ_MULTIPLE for multiple objects traversal complete.");
  392. return multi_iterator;
  393. } else {
  394. return ret;
  395. }
  396. }
  397. void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags,
  398. ao2_callback_fn *cb_fn, void *arg, const char *tag, const char *file, int line,
  399. const char *func)
  400. {
  401. return internal_ao2_traverse(c, flags, cb_fn, arg, NULL, AO2_CALLBACK_DEFAULT, tag, file, line, func);
  402. }
  403. void *__ao2_callback(struct ao2_container *c, enum search_flags flags,
  404. ao2_callback_fn *cb_fn, void *arg)
  405. {
  406. return internal_ao2_traverse(c, flags, cb_fn, arg, NULL, AO2_CALLBACK_DEFAULT, NULL, NULL, 0, NULL);
  407. }
  408. void *__ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags,
  409. ao2_callback_data_fn *cb_fn, void *arg, void *data, const char *tag, const char *file,
  410. int line, const char *func)
  411. {
  412. return internal_ao2_traverse(c, flags, cb_fn, arg, data, AO2_CALLBACK_WITH_DATA, tag, file, line, func);
  413. }
  414. void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags,
  415. ao2_callback_data_fn *cb_fn, void *arg, void *data)
  416. {
  417. return internal_ao2_traverse(c, flags, cb_fn, arg, data, AO2_CALLBACK_WITH_DATA, NULL, NULL, 0, NULL);
  418. }
  419. /*!
  420. * the find function just invokes the default callback with some reasonable flags.
  421. */
  422. void *__ao2_find_debug(struct ao2_container *c, const void *arg, enum search_flags flags,
  423. const char *tag, const char *file, int line, const char *func)
  424. {
  425. void *arged = (void *) arg;/* Done to avoid compiler const warning */
  426. if (!c) {
  427. /* Sanity checks. */
  428. ast_assert(0);
  429. return NULL;
  430. }
  431. return __ao2_callback_debug(c, flags, c->cmp_fn, arged, tag, file, line, func);
  432. }
  433. void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags)
  434. {
  435. void *arged = (void *) arg;/* Done to avoid compiler const warning */
  436. if (!c) {
  437. /* Sanity checks. */
  438. ast_assert(0);
  439. return NULL;
  440. }
  441. return __ao2_callback(c, flags, c->cmp_fn, arged);
  442. }
  443. /*!
  444. * initialize an iterator so we start from the first object
  445. */
  446. struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
  447. {
  448. struct ao2_iterator a = {
  449. .c = c,
  450. .flags = flags
  451. };
  452. ao2_t_ref(c, +1, "Init iterator with container.");
  453. return a;
  454. }
  455. void ao2_iterator_restart(struct ao2_iterator *iter)
  456. {
  457. if (!is_ao2_object(iter->c)) {
  458. ast_log(LOG_ERROR, "Iterator container is not valid.\n");
  459. return;
  460. }
  461. /* Release the last container node reference if we have one. */
  462. if (iter->last_node) {
  463. enum ao2_lock_req orig_lock;
  464. /*
  465. * Do a read lock in case the container node unref does not
  466. * destroy the node. If the container node is destroyed then
  467. * the lock will be upgraded to a write lock.
  468. */
  469. if (iter->flags & AO2_ITERATOR_DONTLOCK) {
  470. orig_lock = __adjust_lock(iter->c, AO2_LOCK_REQ_RDLOCK, 1);
  471. } else {
  472. orig_lock = AO2_LOCK_REQ_MUTEX;
  473. ao2_rdlock(iter->c);
  474. }
  475. __ao2_ref(iter->last_node, -1);
  476. iter->last_node = NULL;
  477. if (iter->flags & AO2_ITERATOR_DONTLOCK) {
  478. __adjust_lock(iter->c, orig_lock, 0);
  479. } else {
  480. ao2_unlock(iter->c);
  481. }
  482. }
  483. /* The iteration is no longer complete. */
  484. iter->complete = 0;
  485. }
  486. void ao2_iterator_destroy(struct ao2_iterator *iter)
  487. {
  488. /* Release any last container node reference. */
  489. ao2_iterator_restart(iter);
  490. /* Release the iterated container reference. */
  491. ao2_t_ref(iter->c, -1, "Unref iterator in ao2_iterator_destroy");
  492. iter->c = NULL;
  493. /* Free the malloced iterator. */
  494. if (iter->flags & AO2_ITERATOR_MALLOCD) {
  495. ast_free(iter);
  496. }
  497. }
  498. void ao2_iterator_cleanup(struct ao2_iterator *iter)
  499. {
  500. if (iter) {
  501. ao2_iterator_destroy(iter);
  502. }
  503. }
  504. /*
  505. * move to the next element in the container.
  506. */
  507. static void *internal_ao2_iterator_next(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func)
  508. {
  509. enum ao2_lock_req orig_lock;
  510. struct ao2_container_node *node;
  511. void *ret;
  512. if (!is_ao2_object(iter->c)) {
  513. return NULL;
  514. }
  515. if (!iter->c->v_table || !iter->c->v_table->iterator_next) {
  516. /* Sanity checks. */
  517. ast_assert(0);
  518. return NULL;
  519. }
  520. if (iter->complete) {
  521. /* Don't return any more objects. */
  522. return NULL;
  523. }
  524. if (iter->flags & AO2_ITERATOR_DONTLOCK) {
  525. if (iter->flags & AO2_ITERATOR_UNLINK) {
  526. orig_lock = __adjust_lock(iter->c, AO2_LOCK_REQ_WRLOCK, 1);
  527. } else {
  528. orig_lock = __adjust_lock(iter->c, AO2_LOCK_REQ_RDLOCK, 1);
  529. }
  530. } else {
  531. orig_lock = AO2_LOCK_REQ_MUTEX;
  532. if (iter->flags & AO2_ITERATOR_UNLINK) {
  533. ao2_wrlock(iter->c);
  534. } else {
  535. ao2_rdlock(iter->c);
  536. }
  537. }
  538. node = iter->c->v_table->iterator_next(iter->c, iter->last_node, iter->flags);
  539. if (node) {
  540. ret = node->obj;
  541. if (iter->flags & AO2_ITERATOR_UNLINK) {
  542. /* Transfer the object ref from the container to the returned object. */
  543. __container_unlink_node_debug(node, AO2_UNLINK_NODE_DEC_COUNT, tag, file, line, func);
  544. /* Transfer the container's node ref to the iterator. */
  545. } else {
  546. /* Bump ref of returned object */
  547. if (tag) {
  548. __ao2_ref_debug(ret, +1, tag, file, line, func);
  549. } else {
  550. ao2_t_ref(ret, +1, "Next iterator object.");
  551. }
  552. /* Bump the container's node ref for the iterator. */
  553. __ao2_ref(node, +1);
  554. }
  555. } else {
  556. /* The iteration has completed. */
  557. iter->complete = 1;
  558. ret = NULL;
  559. }
  560. /* Replace the iterator's node */
  561. if (iter->last_node) {
  562. __ao2_ref(iter->last_node, -1);
  563. }
  564. iter->last_node = node;
  565. if (iter->flags & AO2_ITERATOR_DONTLOCK) {
  566. __adjust_lock(iter->c, orig_lock, 0);
  567. } else {
  568. ao2_unlock(iter->c);
  569. }
  570. return ret;
  571. }
  572. void *__ao2_iterator_next_debug(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func)
  573. {
  574. return internal_ao2_iterator_next(iter, tag, file, line, func);
  575. }
  576. void *__ao2_iterator_next(struct ao2_iterator *iter)
  577. {
  578. return internal_ao2_iterator_next(iter, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__);
  579. }
  580. int ao2_iterator_count(struct ao2_iterator *iter)
  581. {
  582. return ao2_container_count(iter->c);
  583. }
  584. void container_destruct(void *_c)
  585. {
  586. struct ao2_container *c = _c;
  587. /* Unlink any stored objects in the container. */
  588. c->destroying = 1;
  589. __ao2_callback(c, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  590. /* Perform any extra container cleanup. */
  591. if (c->v_table && c->v_table->destroy) {
  592. c->v_table->destroy(c);
  593. }
  594. #if defined(AO2_DEBUG)
  595. ast_atomic_fetchadd_int(&ao2.total_containers, -1);
  596. #endif
  597. }
  598. void container_destruct_debug(void *_c)
  599. {
  600. struct ao2_container *c = _c;
  601. /* Unlink any stored objects in the container. */
  602. c->destroying = 1;
  603. __ao2_callback_debug(c, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL,
  604. "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__);
  605. /* Perform any extra container cleanup. */
  606. if (c->v_table && c->v_table->destroy) {
  607. c->v_table->destroy(c);
  608. }
  609. #if defined(AO2_DEBUG)
  610. ast_atomic_fetchadd_int(&ao2.total_containers, -1);
  611. #endif
  612. }
  613. /*!
  614. * \internal
  615. * \brief Put obj into the arg container.
  616. * \since 11.0
  617. *
  618. * \param obj pointer to the (user-defined part) of an object.
  619. * \param arg callback argument from ao2_callback()
  620. * \param flags flags from ao2_callback()
  621. *
  622. * \retval 0 on success.
  623. * \retval CMP_STOP|CMP_MATCH on error.
  624. */
  625. static int dup_obj_cb(void *obj, void *arg, int flags)
  626. {
  627. struct ao2_container *dest = arg;
  628. return __ao2_link(dest, obj, OBJ_NOLOCK) ? 0 : (CMP_MATCH | CMP_STOP);
  629. }
  630. int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags)
  631. {
  632. void *obj;
  633. int res = 0;
  634. if (!(flags & OBJ_NOLOCK)) {
  635. ao2_rdlock(src);
  636. ao2_wrlock(dest);
  637. }
  638. obj = __ao2_callback(src, OBJ_NOLOCK, dup_obj_cb, dest);
  639. if (obj) {
  640. /* Failed to put this obj into the dest container. */
  641. ao2_t_ref(obj, -1, "Failed to put this object into the dest container.");
  642. /* Remove all items from the dest container. */
  643. __ao2_callback(dest, OBJ_NOLOCK | OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL,
  644. NULL);
  645. res = -1;
  646. }
  647. if (!(flags & OBJ_NOLOCK)) {
  648. ao2_unlock(dest);
  649. ao2_unlock(src);
  650. }
  651. return res;
  652. }
  653. struct ao2_container *__ao2_container_clone(struct ao2_container *orig, enum search_flags flags)
  654. {
  655. struct ao2_container *clone;
  656. int failed;
  657. /* Create the clone container with the same properties as the original. */
  658. if (!is_ao2_object(orig)) {
  659. return NULL;
  660. }
  661. if (!orig->v_table || !orig->v_table->alloc_empty_clone) {
  662. /* Sanity checks. */
  663. ast_assert(0);
  664. return NULL;
  665. }
  666. clone = orig->v_table->alloc_empty_clone(orig);
  667. if (!clone) {
  668. return NULL;
  669. }
  670. /* This test is correct. clone must be locked before calling
  671. * ao2_container_dup when the OBJ_NOLOCK flag is set, otherwise
  672. * we could have errors in __adjust_lock. */
  673. if (flags & OBJ_NOLOCK) {
  674. ao2_wrlock(clone);
  675. }
  676. failed = ao2_container_dup(clone, orig, flags);
  677. if (flags & OBJ_NOLOCK) {
  678. ao2_unlock(clone);
  679. }
  680. if (failed) {
  681. /* Object copy into the clone container failed. */
  682. ao2_t_ref(clone, -1, "Clone creation failed.");
  683. clone = NULL;
  684. }
  685. return clone;
  686. }
  687. struct ao2_container *__ao2_container_clone_debug(struct ao2_container *orig, enum search_flags flags, const char *tag, const char *file, int line, const char *func, int ref_debug)
  688. {
  689. struct ao2_container *clone;
  690. int failed;
  691. /* Create the clone container with the same properties as the original. */
  692. if (!is_ao2_object(orig)) {
  693. return NULL;
  694. }
  695. if (!orig->v_table || !orig->v_table->alloc_empty_clone_debug) {
  696. /* Sanity checks. */
  697. ast_assert(0);
  698. return NULL;
  699. }
  700. clone = orig->v_table->alloc_empty_clone_debug(orig, tag, file, line, func, ref_debug);
  701. if (!clone) {
  702. return NULL;
  703. }
  704. if (flags & OBJ_NOLOCK) {
  705. ao2_wrlock(clone);
  706. }
  707. failed = ao2_container_dup(clone, orig, flags);
  708. if (flags & OBJ_NOLOCK) {
  709. ao2_unlock(clone);
  710. }
  711. if (failed) {
  712. /* Object copy into the clone container failed. */
  713. if (ref_debug) {
  714. __ao2_ref_debug(clone, -1, tag, file, line, func);
  715. } else {
  716. ao2_t_ref(clone, -1, "Clone creation failed.");
  717. }
  718. clone = NULL;
  719. }
  720. return clone;
  721. }
  722. void ao2_container_dump(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj)
  723. {
  724. if (!is_ao2_object(self) || !self->v_table) {
  725. prnt(where, "Invalid container\n");
  726. ast_assert(0);
  727. return;
  728. }
  729. if (!(flags & OBJ_NOLOCK)) {
  730. ao2_rdlock(self);
  731. }
  732. if (name) {
  733. prnt(where, "Container name: %s\n", name);
  734. }
  735. #if defined(AO2_DEBUG)
  736. if (self->v_table->dump) {
  737. self->v_table->dump(self, where, prnt, prnt_obj);
  738. } else
  739. #endif /* defined(AO2_DEBUG) */
  740. {
  741. prnt(where, "Container dump not available.\n");
  742. }
  743. if (!(flags & OBJ_NOLOCK)) {
  744. ao2_unlock(self);
  745. }
  746. }
  747. void ao2_container_stats(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt)
  748. {
  749. if (!is_ao2_object(self) || !self->v_table) {
  750. prnt(where, "Invalid container\n");
  751. ast_assert(0);
  752. return;
  753. }
  754. if (!(flags & OBJ_NOLOCK)) {
  755. ao2_rdlock(self);
  756. }
  757. if (name) {
  758. prnt(where, "Container name: %s\n", name);
  759. }
  760. prnt(where, "Number of objects: %d\n", self->elements);
  761. #if defined(AO2_DEBUG)
  762. prnt(where, "Number of nodes: %d\n", self->nodes);
  763. prnt(where, "Number of empty nodes: %d\n", self->nodes - self->elements);
  764. /*
  765. * XXX
  766. * If the max_empty_nodes count gets out of single digits you
  767. * likely have a code path where ao2_iterator_destroy() is not
  768. * called.
  769. *
  770. * Empty nodes do not harm the container but they do make
  771. * container operations less efficient.
  772. */
  773. prnt(where, "Maximum empty nodes: %d\n", self->max_empty_nodes);
  774. if (self->v_table->stats) {
  775. self->v_table->stats(self, where, prnt);
  776. }
  777. #endif /* defined(AO2_DEBUG) */
  778. if (!(flags & OBJ_NOLOCK)) {
  779. ao2_unlock(self);
  780. }
  781. }
  782. int ao2_container_check(struct ao2_container *self, enum search_flags flags)
  783. {
  784. int res = 0;
  785. if (!is_ao2_object(self) || !self->v_table) {
  786. /* Sanity checks. */
  787. ast_assert(0);
  788. return -1;
  789. }
  790. #if defined(AO2_DEBUG)
  791. if (!self->v_table->integrity) {
  792. /* No ingetrigy check available. Assume container is ok. */
  793. return 0;
  794. }
  795. if (!(flags & OBJ_NOLOCK)) {
  796. ao2_rdlock(self);
  797. }
  798. res = self->v_table->integrity(self);
  799. if (!(flags & OBJ_NOLOCK)) {
  800. ao2_unlock(self);
  801. }
  802. #endif /* defined(AO2_DEBUG) */
  803. return res;
  804. }
  805. #if defined(AO2_DEBUG)
  806. static struct ao2_container *reg_containers;
  807. struct ao2_reg_container {
  808. /*! Registered container pointer. */
  809. struct ao2_container *registered;
  810. /*! Callback function to print the given object's key. (NULL if not available) */
  811. ao2_prnt_obj_fn *prnt_obj;
  812. /*! Name container registered under. */
  813. char name[1];
  814. };
  815. struct ao2_reg_partial_key {
  816. /*! Length of partial key match. */
  817. int len;
  818. /*! Registration partial key name. */
  819. const char *name;
  820. };
  821. struct ao2_reg_match {
  822. /*! The nth match to find. */
  823. int find_nth;
  824. /*! Count of the matches already found. */
  825. int count;
  826. };
  827. #endif /* defined(AO2_DEBUG) */
  828. #if defined(AO2_DEBUG)
  829. static int ao2_reg_sort_cb(const void *obj_left, const void *obj_right, int flags)
  830. {
  831. const struct ao2_reg_container *reg_left = obj_left;
  832. int cmp;
  833. switch (flags & OBJ_SEARCH_MASK) {
  834. case OBJ_SEARCH_OBJECT:
  835. {
  836. const struct ao2_reg_container *reg_right = obj_right;
  837. cmp = strcasecmp(reg_left->name, reg_right->name);
  838. }
  839. break;
  840. case OBJ_SEARCH_KEY:
  841. {
  842. const char *name = obj_right;
  843. cmp = strcasecmp(reg_left->name, name);
  844. }
  845. break;
  846. case OBJ_SEARCH_PARTIAL_KEY:
  847. {
  848. const struct ao2_reg_partial_key *partial_key = obj_right;
  849. cmp = strncasecmp(reg_left->name, partial_key->name, partial_key->len);
  850. }
  851. break;
  852. default:
  853. /* Sort can only work on something with a full or partial key. */
  854. ast_assert(0);
  855. cmp = 0;
  856. break;
  857. }
  858. return cmp;
  859. }
  860. #endif /* defined(AO2_DEBUG) */
  861. #if defined(AO2_DEBUG)
  862. static void ao2_reg_destructor(void *v_doomed)
  863. {
  864. struct ao2_reg_container *doomed = v_doomed;
  865. if (doomed->registered) {
  866. ao2_t_ref(doomed->registered, -1, "Releasing registered container.");
  867. }
  868. }
  869. #endif /* defined(AO2_DEBUG) */
  870. int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj)
  871. {
  872. int res = 0;
  873. #if defined(AO2_DEBUG)
  874. struct ao2_reg_container *reg;
  875. reg = ao2_t_alloc_options(sizeof(*reg) + strlen(name), ao2_reg_destructor,
  876. AO2_ALLOC_OPT_LOCK_NOLOCK, "Container registration object.");
  877. if (!reg) {
  878. return -1;
  879. }
  880. /* Fill in registered entry */
  881. ao2_t_ref(self, +1, "Registering container.");
  882. reg->registered = self;
  883. reg->prnt_obj = prnt_obj;
  884. strcpy(reg->name, name);/* safe */
  885. if (!ao2_t_link(reg_containers, reg, "Save registration object.")) {
  886. res = -1;
  887. }
  888. ao2_t_ref(reg, -1, "Done registering container.");
  889. #endif /* defined(AO2_DEBUG) */
  890. return res;
  891. }
  892. void ao2_container_unregister(const char *name)
  893. {
  894. #if defined(AO2_DEBUG)
  895. ao2_t_find(reg_containers, name, OBJ_UNLINK | OBJ_NODATA | OBJ_SEARCH_KEY,
  896. "Unregister container");
  897. #endif /* defined(AO2_DEBUG) */
  898. }
  899. #if defined(AO2_DEBUG)
  900. static int ao2_complete_reg_cb(void *obj, void *arg, int flags)
  901. {
  902. struct ao2_reg_container *reg = obj;
  903. if (ast_cli_completion_add(ast_strdup(reg->name))) {
  904. return CMP_STOP;
  905. }
  906. return 0;
  907. }
  908. #endif /* defined(AO2_DEBUG) */
  909. #if defined(AO2_DEBUG)
  910. static char *complete_container_names(struct ast_cli_args *a)
  911. {
  912. struct ao2_reg_partial_key partial_key;
  913. if (a->pos != 3) {
  914. return NULL;
  915. }
  916. partial_key.len = strlen(a->word);
  917. partial_key.name = a->word;
  918. ao2_callback(reg_containers, partial_key.len ? OBJ_SEARCH_PARTIAL_KEY : 0,
  919. ao2_complete_reg_cb, &partial_key);
  920. return NULL;
  921. }
  922. #endif /* defined(AO2_DEBUG) */
  923. #if defined(AO2_DEBUG)
  924. AST_THREADSTORAGE(ao2_out_buf);
  925. /*!
  926. * \brief Print CLI output.
  927. * \since 12.0.0
  928. *
  929. * \param where User data pointer needed to determine where to put output.
  930. * \param fmt printf type format string.
  931. *
  932. * \return Nothing
  933. */
  934. static void cli_output(void *where, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
  935. static void cli_output(void *where, const char *fmt, ...)
  936. {
  937. int res;
  938. struct ast_str *buf;
  939. va_list ap;
  940. buf = ast_str_thread_get(&ao2_out_buf, 256);
  941. if (!buf) {
  942. return;
  943. }
  944. va_start(ap, fmt);
  945. res = ast_str_set_va(&buf, 0, fmt, ap);
  946. va_end(ap);
  947. if (res != AST_DYNSTR_BUILD_FAILED) {
  948. ast_cli(*(int *) where, "%s", ast_str_buffer(buf));
  949. }
  950. }
  951. #endif /* defined(AO2_DEBUG) */
  952. #if defined(AO2_DEBUG)
  953. /*! \brief Show container contents - CLI command */
  954. static char *handle_cli_astobj2_container_dump(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  955. {
  956. const char *name;
  957. struct ao2_reg_container *reg;
  958. switch (cmd) {
  959. case CLI_INIT:
  960. e->command = "astobj2 container dump";
  961. e->usage =
  962. "Usage: astobj2 container dump <name>\n"
  963. " Show contents of the container <name>.\n";
  964. return NULL;
  965. case CLI_GENERATE:
  966. return complete_container_names(a);
  967. }
  968. if (a->argc != 4) {
  969. return CLI_SHOWUSAGE;
  970. }
  971. name = a->argv[3];
  972. reg = ao2_t_find(reg_containers, name, OBJ_SEARCH_KEY, "Find registered container");
  973. if (reg) {
  974. ao2_container_dump(reg->registered, 0, name, (void *) &a->fd, cli_output,
  975. reg->prnt_obj);
  976. ao2_t_ref(reg, -1, "Done with registered container object.");
  977. } else {
  978. ast_cli(a->fd, "Container '%s' not found.\n", name);
  979. }
  980. return CLI_SUCCESS;
  981. }
  982. #endif /* defined(AO2_DEBUG) */
  983. #if defined(AO2_DEBUG)
  984. /*! \brief Show container statistics - CLI command */
  985. static char *handle_cli_astobj2_container_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  986. {
  987. const char *name;
  988. struct ao2_reg_container *reg;
  989. switch (cmd) {
  990. case CLI_INIT:
  991. e->command = "astobj2 container stats";
  992. e->usage =
  993. "Usage: astobj2 container stats <name>\n"
  994. " Show statistics about the specified container <name>.\n";
  995. return NULL;
  996. case CLI_GENERATE:
  997. return complete_container_names(a);
  998. }
  999. if (a->argc != 4) {
  1000. return CLI_SHOWUSAGE;
  1001. }
  1002. name = a->argv[3];
  1003. reg = ao2_t_find(reg_containers, name, OBJ_SEARCH_KEY, "Find registered container");
  1004. if (reg) {
  1005. ao2_container_stats(reg->registered, 0, name, (void *) &a->fd, cli_output);
  1006. ao2_t_ref(reg, -1, "Done with registered container object.");
  1007. } else {
  1008. ast_cli(a->fd, "Container '%s' not found.\n", name);
  1009. }
  1010. return CLI_SUCCESS;
  1011. }
  1012. #endif /* defined(AO2_DEBUG) */
  1013. #if defined(AO2_DEBUG)
  1014. /*! \brief Show container check results - CLI command */
  1015. static char *handle_cli_astobj2_container_check(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1016. {
  1017. const char *name;
  1018. struct ao2_reg_container *reg;
  1019. switch (cmd) {
  1020. case CLI_INIT:
  1021. e->command = "astobj2 container check";
  1022. e->usage =
  1023. "Usage: astobj2 container check <name>\n"
  1024. " Perform a container integrity check on <name>.\n";
  1025. return NULL;
  1026. case CLI_GENERATE:
  1027. return complete_container_names(a);
  1028. }
  1029. if (a->argc != 4) {
  1030. return CLI_SHOWUSAGE;
  1031. }
  1032. name = a->argv[3];
  1033. reg = ao2_t_find(reg_containers, name, OBJ_SEARCH_KEY, "Find registered container");
  1034. if (reg) {
  1035. ast_cli(a->fd, "Container check of '%s': %s.\n", name,
  1036. ao2_container_check(reg->registered, 0) ? "failed" : "OK");
  1037. ao2_t_ref(reg, -1, "Done with registered container object.");
  1038. } else {
  1039. ast_cli(a->fd, "Container '%s' not found.\n", name);
  1040. }
  1041. return CLI_SUCCESS;
  1042. }
  1043. #endif /* defined(AO2_DEBUG) */
  1044. #if defined(AO2_DEBUG)
  1045. static struct ast_cli_entry cli_astobj2[] = {
  1046. AST_CLI_DEFINE(handle_cli_astobj2_container_dump, "Show container contents"),
  1047. AST_CLI_DEFINE(handle_cli_astobj2_container_stats, "Show container statistics"),
  1048. AST_CLI_DEFINE(handle_cli_astobj2_container_check, "Perform a container integrity check"),
  1049. };
  1050. #endif /* defined(AO2_DEBUG) */
  1051. #if defined(AO2_DEBUG)
  1052. static void container_cleanup(void)
  1053. {
  1054. ao2_t_ref(reg_containers, -1, "Releasing container registration container");
  1055. reg_containers = NULL;
  1056. ast_cli_unregister_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
  1057. }
  1058. #endif /* defined(AO2_DEBUG) */
  1059. int container_init(void)
  1060. {
  1061. #if defined(AO2_DEBUG)
  1062. reg_containers = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK,
  1063. AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, ao2_reg_sort_cb, NULL,
  1064. "Container registration container.");
  1065. if (!reg_containers) {
  1066. return -1;
  1067. }
  1068. ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
  1069. ast_register_cleanup(container_cleanup);
  1070. #endif /* defined(AO2_DEBUG) */
  1071. return 0;
  1072. }