astobj2_container.c 31 KB

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