astobj2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * astobj2 - replacement containers for asterisk data structures.
  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. /*
  17. * Function implementing astobj2 objects.
  18. */
  19. #include "asterisk.h"
  20. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  21. #include "asterisk/_private.h"
  22. #include "asterisk/astobj2.h"
  23. #include "asterisk/utils.h"
  24. #include "asterisk/cli.h"
  25. /*!
  26. * astobj2 objects are always preceded by this data structure,
  27. * which contains a lock, a reference counter,
  28. * the flags and a pointer to a destructor.
  29. * The refcount is used to decide when it is time to
  30. * invoke the destructor.
  31. * The magic number is used for consistency check.
  32. * XXX the lock is not always needed, and its initialization may be
  33. * expensive. Consider making it external.
  34. */
  35. struct __priv_data {
  36. ast_mutex_t lock;
  37. int ref_counter;
  38. ao2_destructor_fn destructor_fn;
  39. /*! for stats */
  40. size_t data_size;
  41. /*! magic number. This is used to verify that a pointer passed in is a
  42. * valid astobj2 */
  43. uint32_t magic;
  44. };
  45. #define AO2_MAGIC 0xa570b123
  46. /*!
  47. * What an astobj2 object looks like: fixed-size private data
  48. * followed by variable-size user data.
  49. */
  50. struct astobj2 {
  51. struct __priv_data priv_data;
  52. void *user_data[0];
  53. };
  54. #ifdef AST_DEVMODE
  55. #define AO2_DEBUG 1
  56. #endif
  57. #ifdef AO2_DEBUG
  58. struct ao2_stats {
  59. volatile int total_objects;
  60. volatile int total_mem;
  61. volatile int total_containers;
  62. volatile int total_refs;
  63. volatile int total_locked;
  64. };
  65. static struct ao2_stats ao2;
  66. #endif
  67. #ifndef HAVE_BKTR /* backtrace support */
  68. void ao2_bt(void) {}
  69. #else
  70. #include <execinfo.h> /* for backtrace */
  71. void ao2_bt(void)
  72. {
  73. int c, i;
  74. #define N1 20
  75. void *addresses[N1];
  76. char **strings;
  77. c = backtrace(addresses, N1);
  78. strings = backtrace_symbols(addresses,c);
  79. ast_verbose("backtrace returned: %d\n", c);
  80. for(i = 0; i < c; i++) {
  81. ast_verbose("%d: %p %s\n", i, addresses[i], strings[i]);
  82. }
  83. free(strings);
  84. }
  85. #endif
  86. /*!
  87. * \brief convert from a pointer _p to a user-defined object
  88. *
  89. * \return the pointer to the astobj2 structure
  90. */
  91. static inline struct astobj2 *INTERNAL_OBJ(void *user_data)
  92. {
  93. struct astobj2 *p;
  94. if (!user_data) {
  95. ast_log(LOG_ERROR, "user_data is NULL\n");
  96. return NULL;
  97. }
  98. p = (struct astobj2 *) ((char *) user_data - sizeof(*p));
  99. if (AO2_MAGIC != (p->priv_data.magic) ) {
  100. ast_log(LOG_ERROR, "bad magic number 0x%x for %p\n", p->priv_data.magic, p);
  101. p = NULL;
  102. }
  103. return p;
  104. }
  105. /*!
  106. * \brief convert from a pointer _p to an astobj2 object
  107. *
  108. * \return the pointer to the user-defined portion.
  109. */
  110. #define EXTERNAL_OBJ(_p) ((_p) == NULL ? NULL : (_p)->user_data)
  111. #ifdef DEBUG_THREADS
  112. /* Need to override the macros defined in astobj2.h */
  113. #undef ao2_lock
  114. #undef ao2_trylock
  115. #undef ao2_unlock
  116. #endif
  117. int ao2_lock(void *user_data)
  118. {
  119. struct astobj2 *p = INTERNAL_OBJ(user_data);
  120. if (p == NULL)
  121. return -1;
  122. #ifdef AO2_DEBUG
  123. ast_atomic_fetchadd_int(&ao2.total_locked, 1);
  124. #endif
  125. return ast_mutex_lock(&p->priv_data.lock);
  126. }
  127. int _ao2_lock(void *user_data, const char *file, const char *func, int line, const char *var)
  128. {
  129. struct astobj2 *p = INTERNAL_OBJ(user_data);
  130. if (p == NULL)
  131. return -1;
  132. #ifdef AO2_DEBUG
  133. ast_atomic_fetchadd_int(&ao2.total_locked, 1);
  134. #endif
  135. #ifndef DEBUG_THREADS
  136. return ast_mutex_lock(&p->priv_data.lock);
  137. #else
  138. return __ast_pthread_mutex_lock(file, line, func, var, &p->priv_data.lock);
  139. #endif
  140. }
  141. int ao2_unlock(void *user_data)
  142. {
  143. struct astobj2 *p = INTERNAL_OBJ(user_data);
  144. if (p == NULL)
  145. return -1;
  146. #ifdef AO2_DEBUG
  147. ast_atomic_fetchadd_int(&ao2.total_locked, -1);
  148. #endif
  149. return ast_mutex_unlock(&p->priv_data.lock);
  150. }
  151. int _ao2_unlock(void *user_data, const char *file, const char *func, int line, const char *var)
  152. {
  153. struct astobj2 *p = INTERNAL_OBJ(user_data);
  154. if (p == NULL)
  155. return -1;
  156. #ifdef AO2_DEBUG
  157. ast_atomic_fetchadd_int(&ao2.total_locked, -1);
  158. #endif
  159. #ifndef DEBUG_THREADS
  160. return ast_mutex_unlock(&p->priv_data.lock);
  161. #else
  162. return __ast_pthread_mutex_unlock(file, line, func, var, &p->priv_data.lock);
  163. #endif
  164. }
  165. int ao2_trylock(void *user_data)
  166. {
  167. struct astobj2 *p = INTERNAL_OBJ(user_data);
  168. int ret;
  169. if (p == NULL)
  170. return -1;
  171. ret = ast_mutex_trylock(&p->priv_data.lock);
  172. #ifdef AO2_DEBUG
  173. if (!ret)
  174. ast_atomic_fetchadd_int(&ao2.total_locked, 1);
  175. #endif
  176. return ret;
  177. }
  178. int _ao2_trylock(void *user_data, const char *file, const char *func, int line, const char *var)
  179. {
  180. struct astobj2 *p = INTERNAL_OBJ(user_data);
  181. int ret;
  182. if (p == NULL)
  183. return -1;
  184. #ifndef DEBUG_THREADS
  185. ret = ast_mutex_trylock(&p->priv_data.lock);
  186. #else
  187. ret = __ast_pthread_mutex_trylock(file, line, func, var, &p->priv_data.lock);
  188. #endif
  189. #ifdef AO2_DEBUG
  190. if (!ret)
  191. ast_atomic_fetchadd_int(&ao2.total_locked, 1);
  192. #endif
  193. return ret;
  194. }
  195. /*
  196. * The argument is a pointer to the user portion.
  197. */
  198. int ao2_ref(void *user_data, const int delta)
  199. {
  200. int current_value;
  201. int ret;
  202. struct astobj2 *obj = INTERNAL_OBJ(user_data);
  203. if (obj == NULL)
  204. return -1;
  205. /* if delta is 0, just return the refcount */
  206. if (delta == 0)
  207. return (obj->priv_data.ref_counter);
  208. /* we modify with an atomic operation the reference counter */
  209. ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
  210. current_value = ret + delta;
  211. #ifdef AO2_DEBUG
  212. ast_atomic_fetchadd_int(&ao2.total_refs, delta);
  213. #endif
  214. /* this case must never happen */
  215. if (current_value < 0)
  216. ast_log(LOG_ERROR, "refcount %d on object %p\n", current_value, user_data);
  217. if (current_value <= 0) { /* last reference, destroy the object */
  218. if (obj->priv_data.destructor_fn != NULL)
  219. obj->priv_data.destructor_fn(user_data);
  220. ast_mutex_destroy(&obj->priv_data.lock);
  221. #ifdef AO2_DEBUG
  222. ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
  223. ast_atomic_fetchadd_int(&ao2.total_objects, -1);
  224. #endif
  225. /* for safety, zero-out the astobj2 header and also the
  226. * first word of the user-data, which we make sure is always
  227. * allocated. */
  228. memset(obj, '\0', sizeof(struct astobj2 *) + sizeof(void *) );
  229. free(obj);
  230. }
  231. return ret;
  232. }
  233. /*
  234. * We always alloc at least the size of a void *,
  235. * for debugging purposes.
  236. */
  237. void *ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
  238. {
  239. /* allocation */
  240. struct astobj2 *obj;
  241. if (data_size < sizeof(void *))
  242. data_size = sizeof(void *);
  243. obj = ast_calloc(1, sizeof(*obj) + data_size);
  244. if (obj == NULL)
  245. return NULL;
  246. ast_mutex_init(&obj->priv_data.lock);
  247. obj->priv_data.magic = AO2_MAGIC;
  248. obj->priv_data.data_size = data_size;
  249. obj->priv_data.ref_counter = 1;
  250. obj->priv_data.destructor_fn = destructor_fn; /* can be NULL */
  251. #ifdef AO2_DEBUG
  252. ast_atomic_fetchadd_int(&ao2.total_objects, 1);
  253. ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
  254. ast_atomic_fetchadd_int(&ao2.total_refs, 1);
  255. #endif
  256. /* return a pointer to the user data */
  257. return EXTERNAL_OBJ(obj);
  258. }
  259. /* internal callback to destroy a container. */
  260. static void container_destruct(void *c);
  261. /* each bucket in the container is a tailq. */
  262. AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
  263. /*!
  264. * A container; stores the hash and callback functions, information on
  265. * the size, the hash bucket heads, and a version number, starting at 0
  266. * (for a newly created, empty container)
  267. * and incremented every time an object is inserted or deleted.
  268. * The assumption is that an object is never moved in a container,
  269. * but removed and readded with the new number.
  270. * The version number is especially useful when implementing iterators.
  271. * In fact, we can associate a unique, monotonically increasing number to
  272. * each object, which means that, within an iterator, we can store the
  273. * version number of the current object, and easily look for the next one,
  274. * which is the next one in the list with a higher number.
  275. * Since all objects have a version >0, we can use 0 as a marker for
  276. * 'we need the first object in the bucket'.
  277. *
  278. * \todo Linking and unlink objects is typically expensive, as it
  279. * involves a malloc() of a small object which is very inefficient.
  280. * To optimize this, we allocate larger arrays of bucket_list's
  281. * when we run out of them, and then manage our own freelist.
  282. * This will be more efficient as we can do the freelist management while
  283. * we hold the lock (that we need anyways).
  284. */
  285. struct ao2_container {
  286. ao2_hash_fn *hash_fn;
  287. ao2_callback_fn *cmp_fn;
  288. int n_buckets;
  289. /*! Number of elements in the container */
  290. int elements;
  291. /*! described above */
  292. int version;
  293. /*! variable size */
  294. struct bucket buckets[0];
  295. };
  296. /*!
  297. * \brief always zero hash function
  298. *
  299. * it is convenient to have a hash function that always returns 0.
  300. * This is basically used when we want to have a container that is
  301. * a simple linked list.
  302. *
  303. * \returns 0
  304. */
  305. static int hash_zero(const void *user_obj, const int flags)
  306. {
  307. return 0;
  308. }
  309. /*
  310. * A container is just an object, after all!
  311. */
  312. struct ao2_container *
  313. ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
  314. ao2_callback_fn *cmp_fn)
  315. {
  316. /* XXX maybe consistency check on arguments ? */
  317. /* compute the container size */
  318. size_t container_size = sizeof(struct ao2_container) + n_buckets * sizeof(struct bucket);
  319. struct ao2_container *c = ao2_alloc(container_size, container_destruct);
  320. if (!c)
  321. return NULL;
  322. c->version = 1; /* 0 is a reserved value here */
  323. c->n_buckets = n_buckets;
  324. c->hash_fn = hash_fn ? hash_fn : hash_zero;
  325. c->cmp_fn = cmp_fn;
  326. #ifdef AO2_DEBUG
  327. ast_atomic_fetchadd_int(&ao2.total_containers, 1);
  328. #endif
  329. return c;
  330. }
  331. /*!
  332. * return the number of elements in the container
  333. */
  334. int ao2_container_count(struct ao2_container *c)
  335. {
  336. return c->elements;
  337. }
  338. /*!
  339. * A structure to create a linked list of entries,
  340. * used within a bucket.
  341. * XXX \todo this should be private to the container code
  342. */
  343. struct bucket_list {
  344. AST_LIST_ENTRY(bucket_list) entry;
  345. int version;
  346. struct astobj2 *astobj; /* pointer to internal data */
  347. };
  348. /*
  349. * link an object to a container
  350. */
  351. void *ao2_link(struct ao2_container *c, void *user_data)
  352. {
  353. int i;
  354. /* create a new list entry */
  355. struct bucket_list *p;
  356. struct astobj2 *obj = INTERNAL_OBJ(user_data);
  357. if (!obj)
  358. return NULL;
  359. if (INTERNAL_OBJ(c) == NULL)
  360. return NULL;
  361. p = ast_calloc(1, sizeof(*p));
  362. if (!p)
  363. return NULL;
  364. i = abs(c->hash_fn(user_data, OBJ_POINTER));
  365. ao2_lock(c);
  366. i %= c->n_buckets;
  367. p->astobj = obj;
  368. p->version = ast_atomic_fetchadd_int(&c->version, 1);
  369. AST_LIST_INSERT_TAIL(&c->buckets[i], p, entry);
  370. ast_atomic_fetchadd_int(&c->elements, 1);
  371. ao2_ref(user_data, +1);
  372. ao2_unlock(c);
  373. return p;
  374. }
  375. /*!
  376. * \brief another convenience function is a callback that matches on address
  377. */
  378. int ao2_match_by_addr(void *user_data, void *arg, int flags)
  379. {
  380. return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
  381. }
  382. /*
  383. * Unlink an object from the container
  384. * and destroy the associated * ao2_bucket_list structure.
  385. */
  386. void *ao2_unlink(struct ao2_container *c, void *user_data)
  387. {
  388. if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */
  389. return NULL;
  390. ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data);
  391. return NULL;
  392. }
  393. /*!
  394. * \brief special callback that matches all
  395. */
  396. static int cb_true(void *user_data, void *arg, int flags)
  397. {
  398. return CMP_MATCH;
  399. }
  400. /*!
  401. * Browse the container using different stategies accoding the flags.
  402. * \return Is a pointer to an object or to a list of object if OBJ_MULTIPLE is
  403. * specified.
  404. */
  405. void *ao2_callback(struct ao2_container *c,
  406. const enum search_flags flags,
  407. ao2_callback_fn *cb_fn, void *arg)
  408. {
  409. int i, start, last; /* search boundaries */
  410. void *ret = NULL;
  411. if (INTERNAL_OBJ(c) == NULL) /* safety check on the argument */
  412. return NULL;
  413. if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
  414. ast_log(LOG_WARNING, "multiple data return not implemented yet (flags %x)\n", flags);
  415. return NULL;
  416. }
  417. /* override the match function if necessary */
  418. if (cb_fn == NULL) /* if NULL, match everything */
  419. cb_fn = cb_true;
  420. /*
  421. * XXX this can be optimized.
  422. * If we have a hash function and lookup by pointer,
  423. * run the hash function. Otherwise, scan the whole container
  424. * (this only for the time being. We need to optimize this.)
  425. */
  426. if ((flags & OBJ_POINTER)) /* we know hash can handle this case */
  427. start = i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
  428. else /* don't know, let's scan all buckets */
  429. start = i = -1; /* XXX this must be fixed later. */
  430. /* determine the search boundaries: i..last-1 */
  431. if (i < 0) {
  432. start = i = 0;
  433. last = c->n_buckets;
  434. } else if ((flags & OBJ_CONTINUE)) {
  435. last = c->n_buckets;
  436. } else {
  437. last = i + 1;
  438. }
  439. ao2_lock(c); /* avoid modifications to the content */
  440. for (; i < last ; i++) {
  441. /* scan the list with prev-cur pointers */
  442. struct bucket_list *cur;
  443. AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
  444. int match = cb_fn(EXTERNAL_OBJ(cur->astobj), arg, flags) & (CMP_MATCH | CMP_STOP);
  445. /* we found the object, performing operations according flags */
  446. if (match == 0) { /* no match, no stop, continue */
  447. continue;
  448. } else if (match == CMP_STOP) { /* no match but stop, we are done */
  449. i = last;
  450. break;
  451. }
  452. /* we have a match (CMP_MATCH) here */
  453. if (!(flags & OBJ_NODATA)) { /* if must return the object, record the value */
  454. /* it is important to handle this case before the unlink */
  455. ret = EXTERNAL_OBJ(cur->astobj);
  456. ao2_ref(ret, 1);
  457. }
  458. if (flags & OBJ_UNLINK) { /* must unlink */
  459. struct bucket_list *x = cur;
  460. /* we are going to modify the container, so update version */
  461. ast_atomic_fetchadd_int(&c->version, 1);
  462. AST_LIST_REMOVE_CURRENT(entry);
  463. /* update number of elements and version */
  464. ast_atomic_fetchadd_int(&c->elements, -1);
  465. ao2_ref(EXTERNAL_OBJ(x->astobj), -1);
  466. free(x); /* free the link record */
  467. }
  468. if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
  469. /* We found the only match we need */
  470. i = last; /* force exit from outer loop */
  471. break;
  472. }
  473. if (!(flags & OBJ_NODATA)) {
  474. #if 0 /* XXX to be completed */
  475. /*
  476. * This is the multiple-return case. We need to link
  477. * the object in a list. The refcount is already increased.
  478. */
  479. #endif
  480. }
  481. }
  482. AST_LIST_TRAVERSE_SAFE_END;
  483. if (ret) {
  484. /* This assumes OBJ_MULTIPLE with !OBJ_NODATA is still not implemented */
  485. break;
  486. }
  487. if (i == c->n_buckets - 1 && (flags & OBJ_POINTER) && (flags & OBJ_CONTINUE)) {
  488. /* Move to the beginning to ensure we check every bucket */
  489. i = -1;
  490. last = start;
  491. }
  492. }
  493. ao2_unlock(c);
  494. return ret;
  495. }
  496. /*!
  497. * the find function just invokes the default callback with some reasonable flags.
  498. */
  499. void *ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
  500. {
  501. return ao2_callback(c, flags, c->cmp_fn, arg);
  502. }
  503. /*!
  504. * initialize an iterator so we start from the first object
  505. */
  506. struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
  507. {
  508. struct ao2_iterator a = {
  509. .c = c,
  510. .flags = flags
  511. };
  512. ao2_ref(c, +1);
  513. return a;
  514. }
  515. /*!
  516. * destroy an iterator
  517. */
  518. void ao2_iterator_destroy(struct ao2_iterator *i)
  519. {
  520. ao2_ref(i->c, -1);
  521. i->c = NULL;
  522. }
  523. /*
  524. * move to the next element in the container.
  525. */
  526. void * ao2_iterator_next(struct ao2_iterator *a)
  527. {
  528. int lim;
  529. struct bucket_list *p = NULL;
  530. void *ret = NULL;
  531. if (INTERNAL_OBJ(a->c) == NULL)
  532. return NULL;
  533. if (!(a->flags & AO2_ITERATOR_DONTLOCK))
  534. ao2_lock(a->c);
  535. /* optimization. If the container is unchanged and
  536. * we have a pointer, try follow it
  537. */
  538. if (a->c->version == a->c_version && (p = a->obj) ) {
  539. if ( (p = AST_LIST_NEXT(p, entry)) )
  540. goto found;
  541. /* nope, start from the next bucket */
  542. a->bucket++;
  543. a->version = 0;
  544. a->obj = NULL;
  545. }
  546. lim = a->c->n_buckets;
  547. /* Browse the buckets array, moving to the next
  548. * buckets if we don't find the entry in the current one.
  549. * Stop when we find an element with version number greater
  550. * than the current one (we reset the version to 0 when we
  551. * switch buckets).
  552. */
  553. for (; a->bucket < lim; a->bucket++, a->version = 0) {
  554. /* scan the current bucket */
  555. AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
  556. if (p->version > a->version)
  557. goto found;
  558. }
  559. }
  560. found:
  561. if (p) {
  562. a->version = p->version;
  563. a->obj = p;
  564. a->c_version = a->c->version;
  565. ret = EXTERNAL_OBJ(p->astobj);
  566. /* inc refcount of returned object */
  567. ao2_ref(ret, 1);
  568. }
  569. if (!(a->flags & AO2_ITERATOR_DONTLOCK))
  570. ao2_unlock(a->c);
  571. return ret;
  572. }
  573. /* callback for destroying container.
  574. * we can make it simple as we know what it does
  575. */
  576. static int cd_cb(void *obj, void *arg, int flag)
  577. {
  578. ao2_ref(obj, -1);
  579. return 0;
  580. }
  581. static void container_destruct(void *_c)
  582. {
  583. struct ao2_container *c = _c;
  584. int i;
  585. ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
  586. for (i = 0; i < c->n_buckets; i++) {
  587. struct bucket_list *cur;
  588. while ((cur = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
  589. ast_free(cur);
  590. }
  591. }
  592. #ifdef AO2_DEBUG
  593. ast_atomic_fetchadd_int(&ao2.total_containers, -1);
  594. #endif
  595. }
  596. #ifdef AO2_DEBUG
  597. static int print_cb(void *obj, void *arg, int flag)
  598. {
  599. int *fd = arg;
  600. char *s = (char *)obj;
  601. ast_cli(*fd, "string <%s>\n", s);
  602. return 0;
  603. }
  604. /*
  605. * Print stats
  606. */
  607. static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  608. {
  609. switch (cmd) {
  610. case CLI_INIT:
  611. e->command = "astobj2 stats";
  612. e->usage = "Usage: astobj2 stats\n"
  613. " Show astobj2 stats\n";
  614. return NULL;
  615. case CLI_GENERATE:
  616. return NULL;
  617. }
  618. ast_cli(a->fd, "Objects : %d\n", ao2.total_objects);
  619. ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
  620. ast_cli(a->fd, "Memory : %d\n", ao2.total_mem);
  621. ast_cli(a->fd, "Locked : %d\n", ao2.total_locked);
  622. ast_cli(a->fd, "Refs : %d\n", ao2.total_refs);
  623. return CLI_SUCCESS;
  624. }
  625. /*
  626. * This is testing code for astobj
  627. */
  628. static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  629. {
  630. struct ao2_container *c1;
  631. int i, lim;
  632. char *obj;
  633. static int prof_id = -1;
  634. struct ast_cli_args fake_args = { a->fd, 0, NULL };
  635. switch (cmd) {
  636. case CLI_INIT:
  637. e->command = "astobj2 test";
  638. e->usage = "Usage: astobj2 test <num>\n"
  639. " Runs astobj2 test. Creates 'num' objects,\n"
  640. " and test iterators, callbacks and may be other stuff\n";
  641. return NULL;
  642. case CLI_GENERATE:
  643. return NULL;
  644. }
  645. if (a->argc != 3) {
  646. return CLI_SHOWUSAGE;
  647. }
  648. if (prof_id == -1)
  649. prof_id = ast_add_profile("ao2_alloc", 0);
  650. ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
  651. lim = atoi(a->argv[2]);
  652. ast_cli(a->fd, "called astobj_test\n");
  653. handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
  654. /*
  655. * allocate a container with no default callback, and no hash function.
  656. * No hash means everything goes in the same bucket.
  657. */
  658. c1 = ao2_container_alloc(100, NULL /* no callback */, NULL /* no hash */);
  659. ast_cli(a->fd, "container allocated as %p\n", c1);
  660. /*
  661. * fill the container with objects.
  662. * ao2_alloc() gives us a reference which we pass to the
  663. * container when we do the insert.
  664. */
  665. for (i = 0; i < lim; i++) {
  666. ast_mark(prof_id, 1 /* start */);
  667. obj = ao2_alloc(80, NULL);
  668. ast_mark(prof_id, 0 /* stop */);
  669. ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
  670. sprintf(obj, "-- this is obj %d --", i);
  671. ao2_link(c1, obj);
  672. }
  673. ast_cli(a->fd, "testing callbacks\n");
  674. ao2_callback(c1, 0, print_cb, &a->fd);
  675. ast_cli(a->fd, "testing iterators, remove every second object\n");
  676. {
  677. struct ao2_iterator ai;
  678. int x = 0;
  679. ai = ao2_iterator_init(c1, 0);
  680. while ( (obj = ao2_iterator_next(&ai)) ) {
  681. ast_cli(a->fd, "iterator on <%s>\n", obj);
  682. if (x++ & 1)
  683. ao2_unlink(c1, obj);
  684. ao2_ref(obj, -1);
  685. }
  686. ast_cli(a->fd, "testing iterators again\n");
  687. ai = ao2_iterator_init(c1, 0);
  688. while ( (obj = ao2_iterator_next(&ai)) ) {
  689. ast_cli(a->fd, "iterator on <%s>\n", obj);
  690. ao2_ref(obj, -1);
  691. }
  692. }
  693. ast_cli(a->fd, "testing callbacks again\n");
  694. ao2_callback(c1, 0, print_cb, &a->fd);
  695. ast_verbose("now you should see an error message:\n");
  696. ao2_ref(&i, -1); /* i is not a valid object so we print an error here */
  697. ast_cli(a->fd, "destroy container\n");
  698. ao2_ref(c1, -1); /* destroy container */
  699. handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
  700. return CLI_SUCCESS;
  701. }
  702. static struct ast_cli_entry cli_astobj2[] = {
  703. AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
  704. AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
  705. };
  706. #endif /* AO2_DEBUG */
  707. int astobj2_init(void)
  708. {
  709. #ifdef AO2_DEBUG
  710. ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
  711. #endif
  712. return 0;
  713. }