2 * astobj2 - replacement containers for asterisk data structures.
4 * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy
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.
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.
18 * Function implementing astobj2 objects.
22 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
24 #include "asterisk/_private.h"
25 #include "asterisk/astobj2.h"
26 #include "asterisk/utils.h"
27 #include "asterisk/cli.h"
28 #define REF_FILE "/tmp/refs"
31 * astobj2 objects are always preceded by this data structure,
32 * which contains a lock, a reference counter,
33 * the flags and a pointer to a destructor.
34 * The refcount is used to decide when it is time to
35 * invoke the destructor.
36 * The magic number is used for consistency check.
37 * XXX the lock is not always needed, and its initialization may be
38 * expensive. Consider making it external.
43 ao2_destructor_fn destructor_fn;
46 /*! magic number. This is used to verify that a pointer passed in is a
51 #define AO2_MAGIC 0xa570b123
54 * What an astobj2 object looks like: fixed-size private data
55 * followed by variable-size user data.
58 struct __priv_data priv_data;
68 volatile int total_objects;
69 volatile int total_mem;
70 volatile int total_containers;
71 volatile int total_refs;
72 volatile int total_locked;
75 static struct ao2_stats ao2;
78 #ifndef HAVE_BKTR /* backtrace support */
81 #include <execinfo.h> /* for backtrace */
90 c = backtrace(addresses, N1);
91 strings = backtrace_symbols(addresses,c);
92 ast_verbose("backtrace returned: %d\n", c);
93 for(i = 0; i < c; i++) {
94 ast_verbose("%d: %p %s\n", i, addresses[i], strings[i]);
101 * \brief convert from a pointer _p to a user-defined object
103 * \return the pointer to the astobj2 structure
105 static inline struct astobj2 *INTERNAL_OBJ(void *user_data)
110 ast_log(LOG_ERROR, "user_data is NULL\n");
114 p = (struct astobj2 *) ((char *) user_data - sizeof(*p));
115 if (AO2_MAGIC != (p->priv_data.magic) ) {
116 ast_log(LOG_ERROR, "bad magic number 0x%x for %p\n", p->priv_data.magic, p);
123 enum ao2_callback_type {
129 * \brief convert from a pointer _p to an astobj2 object
131 * \return the pointer to the user-defined portion.
133 #define EXTERNAL_OBJ(_p) ((_p) == NULL ? NULL : (_p)->user_data)
135 /* the underlying functions common to debug and non-debug versions */
137 static int internal_ao2_ref(void *user_data, const int delta);
138 static struct ao2_container *internal_ao2_container_alloc(struct ao2_container *c, const uint n_buckets, ao2_hash_fn *hash_fn,
139 ao2_callback_fn *cmp_fn);
140 static struct bucket_list *internal_ao2_link(struct ao2_container *c, void *user_data, const char *file, int line, const char *func);
141 static void *internal_ao2_callback(struct ao2_container *c,
142 const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type,
143 char *tag, char *file, int line, const char *funcname);
144 static void *internal_ao2_iterator_next(struct ao2_iterator *a, struct bucket_list **q);
146 #ifndef DEBUG_THREADS
147 int ao2_lock(void *user_data)
149 int __ao2_lock(void *user_data, const char *file, const char *func, int line, const char *var)
152 struct astobj2 *p = INTERNAL_OBJ(user_data);
158 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
161 #ifndef DEBUG_THREADS
162 return ast_mutex_lock(&p->priv_data.lock);
164 return __ast_pthread_mutex_lock(file, line, func, var, &p->priv_data.lock);
168 #ifndef DEBUG_THREADS
169 int ao2_unlock(void *user_data)
171 int __ao2_unlock(void *user_data, const char *file, const char *func, int line, const char *var)
174 struct astobj2 *p = INTERNAL_OBJ(user_data);
180 ast_atomic_fetchadd_int(&ao2.total_locked, -1);
183 #ifndef DEBUG_THREADS
184 return ast_mutex_unlock(&p->priv_data.lock);
186 return __ast_pthread_mutex_unlock(file, line, func, var, &p->priv_data.lock);
190 #ifndef DEBUG_THREADS
191 int ao2_trylock(void *user_data)
193 int __ao2_trylock(void *user_data, const char *file, const char *func, int line, const char *var)
196 struct astobj2 *p = INTERNAL_OBJ(user_data);
201 #ifndef DEBUG_THREADS
202 ret = ast_mutex_trylock(&p->priv_data.lock);
204 ret = __ast_pthread_mutex_trylock(file, line, func, var, &p->priv_data.lock);
209 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
214 void *ao2_object_get_lockaddr(void *obj)
216 struct astobj2 *p = INTERNAL_OBJ(obj);
221 return &p->priv_data.lock;
225 * The argument is a pointer to the user portion.
229 int __ao2_ref_debug(void *user_data, const int delta, char *tag, char *file, int line, const char *funcname)
231 struct astobj2 *obj = INTERNAL_OBJ(user_data);
237 FILE *refo = fopen(REF_FILE,"a");
238 fprintf(refo, "%p %s%d %s:%d:%s (%s) [@%d]\n", user_data, (delta<0? "":"+"), delta, file, line, funcname, tag, obj ? obj->priv_data.ref_counter : -1);
241 if (obj->priv_data.ref_counter + delta == 0 && obj->priv_data.destructor_fn != NULL) { /* this isn't protected with lock; just for o/p */
242 FILE *refo = fopen(REF_FILE,"a");
243 fprintf(refo, "%p **call destructor** %s:%d:%s (%s)\n", user_data, file, line, funcname, tag);
246 return internal_ao2_ref(user_data, delta);
249 int __ao2_ref(void *user_data, const int delta)
251 struct astobj2 *obj = INTERNAL_OBJ(user_data);
256 return internal_ao2_ref(user_data, delta);
259 static int internal_ao2_ref(void *user_data, const int delta)
261 struct astobj2 *obj = INTERNAL_OBJ(user_data);
265 /* if delta is 0, just return the refcount */
267 return (obj->priv_data.ref_counter);
269 /* we modify with an atomic operation the reference counter */
270 ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
271 current_value = ret + delta;
274 ast_atomic_fetchadd_int(&ao2.total_refs, delta);
277 /* this case must never happen */
278 if (current_value < 0)
279 ast_log(LOG_ERROR, "refcount %d on object %p\n", current_value, user_data);
281 if (current_value <= 0) { /* last reference, destroy the object */
282 if (obj->priv_data.destructor_fn != NULL) {
283 obj->priv_data.destructor_fn(user_data);
286 ast_mutex_destroy(&obj->priv_data.lock);
288 ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
289 ast_atomic_fetchadd_int(&ao2.total_objects, -1);
291 /* for safety, zero-out the astobj2 header and also the
292 * first word of the user-data, which we make sure is always
294 memset(obj, '\0', sizeof(struct astobj2 *) + sizeof(void *) );
302 * We always alloc at least the size of a void *,
303 * for debugging purposes.
305 static void *internal_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, const char *file, int line, const char *funcname)
310 if (data_size < sizeof(void *))
311 data_size = sizeof(void *);
313 #if defined(__AST_DEBUG_MALLOC)
314 obj = __ast_calloc(1, sizeof(*obj) + data_size, file, line, funcname);
316 obj = ast_calloc(1, sizeof(*obj) + data_size);
322 ast_mutex_init(&obj->priv_data.lock);
323 obj->priv_data.magic = AO2_MAGIC;
324 obj->priv_data.data_size = data_size;
325 obj->priv_data.ref_counter = 1;
326 obj->priv_data.destructor_fn = destructor_fn; /* can be NULL */
329 ast_atomic_fetchadd_int(&ao2.total_objects, 1);
330 ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
331 ast_atomic_fetchadd_int(&ao2.total_refs, 1);
334 /* return a pointer to the user data */
335 return EXTERNAL_OBJ(obj);
338 void *__ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, char *tag,
339 const char *file, int line, const char *funcname, int ref_debug)
343 FILE *refo = ref_debug ? fopen(REF_FILE,"a") : NULL;
345 if ((obj = internal_ao2_alloc(data_size, destructor_fn, file, line, funcname)) == NULL) {
351 fprintf(refo, "%p =1 %s:%d:%s (%s)\n", obj, file, line, funcname, tag);
355 /* return a pointer to the user data */
359 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
361 return internal_ao2_alloc(data_size, destructor_fn, __FILE__, __LINE__, __FUNCTION__);
365 /* internal callback to destroy a container. */
366 static void container_destruct(void *c);
368 /* internal callback to destroy a container. */
369 static void container_destruct_debug(void *c);
371 /* each bucket in the container is a tailq. */
372 AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
375 * A container; stores the hash and callback functions, information on
376 * the size, the hash bucket heads, and a version number, starting at 0
377 * (for a newly created, empty container)
378 * and incremented every time an object is inserted or deleted.
379 * The assumption is that an object is never moved in a container,
380 * but removed and readded with the new number.
381 * The version number is especially useful when implementing iterators.
382 * In fact, we can associate a unique, monotonically increasing number to
383 * each object, which means that, within an iterator, we can store the
384 * version number of the current object, and easily look for the next one,
385 * which is the next one in the list with a higher number.
386 * Since all objects have a version >0, we can use 0 as a marker for
387 * 'we need the first object in the bucket'.
389 * \todo Linking and unlink objects is typically expensive, as it
390 * involves a malloc() of a small object which is very inefficient.
391 * To optimize this, we allocate larger arrays of bucket_list's
392 * when we run out of them, and then manage our own freelist.
393 * This will be more efficient as we can do the freelist management while
394 * we hold the lock (that we need anyways).
396 struct ao2_container {
397 ao2_hash_fn *hash_fn;
398 ao2_callback_fn *cmp_fn;
400 /*! Number of elements in the container */
402 /*! described above */
405 struct bucket buckets[0];
409 * \brief always zero hash function
411 * it is convenient to have a hash function that always returns 0.
412 * This is basically used when we want to have a container that is
413 * a simple linked list.
417 static int hash_zero(const void *user_obj, const int flags)
423 * A container is just an object, after all!
425 static struct ao2_container *internal_ao2_container_alloc(struct ao2_container *c, const unsigned int n_buckets, ao2_hash_fn *hash_fn,
426 ao2_callback_fn *cmp_fn)
428 /* XXX maybe consistency check on arguments ? */
429 /* compute the container size */
434 c->version = 1; /* 0 is a reserved value here */
435 c->n_buckets = hash_fn ? n_buckets : 1;
436 c->hash_fn = hash_fn ? hash_fn : hash_zero;
440 ast_atomic_fetchadd_int(&ao2.total_containers, 1);
446 struct ao2_container *__ao2_container_alloc_debug(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
447 ao2_callback_fn *cmp_fn, char *tag, char *file, int line,
448 const char *funcname, int ref_debug)
450 /* XXX maybe consistency check on arguments ? */
451 /* compute the container size */
452 const unsigned int num_buckets = hash_fn ? n_buckets : 1;
453 size_t container_size = sizeof(struct ao2_container) + num_buckets * sizeof(struct bucket);
454 struct ao2_container *c = __ao2_alloc_debug(container_size, container_destruct_debug, tag, file, line, funcname, ref_debug);
456 return internal_ao2_container_alloc(c, num_buckets, hash_fn, cmp_fn);
459 struct ao2_container *__ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
460 ao2_callback_fn *cmp_fn)
462 /* XXX maybe consistency check on arguments ? */
463 /* compute the container size */
465 const unsigned int num_buckets = hash_fn ? n_buckets : 1;
466 size_t container_size = sizeof(struct ao2_container) + num_buckets * sizeof(struct bucket);
467 struct ao2_container *c = __ao2_alloc(container_size, container_destruct);
469 return internal_ao2_container_alloc(c, num_buckets, hash_fn, cmp_fn);
473 * return the number of elements in the container
475 int ao2_container_count(struct ao2_container *c)
481 * A structure to create a linked list of entries,
482 * used within a bucket.
483 * XXX \todo this should be private to the container code
486 AST_LIST_ENTRY(bucket_list) entry;
488 struct astobj2 *astobj; /* pointer to internal data */
492 * link an object to a container
495 static struct bucket_list *internal_ao2_link(struct ao2_container *c, void *user_data, const char *file, int line, const char *func)
498 /* create a new list entry */
499 struct bucket_list *p;
500 struct astobj2 *obj = INTERNAL_OBJ(user_data);
505 if (INTERNAL_OBJ(c) == NULL)
508 p = ast_calloc(1, sizeof(*p));
512 i = c->hash_fn(user_data, OBJ_POINTER);
517 p->version = ast_atomic_fetchadd_int(&c->version, 1);
518 AST_LIST_INSERT_TAIL(&c->buckets[i], p, entry);
519 ast_atomic_fetchadd_int(&c->elements, 1);
521 /* the last two operations (ao2_ref, ao2_unlock) must be done by the calling func */
525 void *__ao2_link_debug(struct ao2_container *c, void *user_data, char *tag, char *file, int line, const char *funcname)
527 struct bucket_list *p = internal_ao2_link(c, user_data, file, line, funcname);
530 __ao2_ref_debug(user_data, +1, tag, file, line, funcname);
536 void *__ao2_link(struct ao2_container *c, void *user_data)
538 struct bucket_list *p = internal_ao2_link(c, user_data, __FILE__, __LINE__, __PRETTY_FUNCTION__);
541 __ao2_ref(user_data, +1);
548 * \brief another convenience function is a callback that matches on address
550 int ao2_match_by_addr(void *user_data, void *arg, int flags)
552 return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
556 * Unlink an object from the container
557 * and destroy the associated * ao2_bucket_list structure.
559 void *__ao2_unlink_debug(struct ao2_container *c, void *user_data, char *tag,
560 char *file, int line, const char *funcname)
562 if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */
565 __ao2_callback_debug(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data, tag, file, line, funcname);
570 void *__ao2_unlink(struct ao2_container *c, void *user_data)
572 if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */
575 __ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data);
581 * \brief special callback that matches all
583 static int cb_true(void *user_data, void *arg, int flags)
589 * \brief similar to cb_true, but is an ao2_callback_data_fn instead
591 static int cb_true_data(void *user_data, void *arg, void *data, int flags)
597 * Browse the container using different stategies accoding the flags.
598 * \return Is a pointer to an object or to a list of object if OBJ_MULTIPLE is
600 * Luckily, for debug purposes, the added args (tag, file, line, funcname)
601 * aren't an excessive load to the system, as the callback should not be
602 * called as often as, say, the ao2_ref func is called.
604 static void *internal_ao2_callback(struct ao2_container *c,
605 const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type,
606 char *tag, char *file, int line, const char *funcname)
608 int i, start, last; /* search boundaries */
610 ao2_callback_fn *cb_default = NULL;
611 ao2_callback_data_fn *cb_withdata = NULL;
613 if (INTERNAL_OBJ(c) == NULL) /* safety check on the argument */
616 if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
617 ast_log(LOG_WARNING, "multiple data return not implemented yet (flags %x)\n", flags);
621 /* override the match function if necessary */
622 if (cb_fn == NULL) { /* if NULL, match everything */
623 if (type == WITH_DATA) {
624 cb_withdata = cb_true_data;
626 cb_default = cb_true;
629 /* We do this here to avoid the per object casting penalty (even though
630 that is probably optimized away anyway. */
631 if (type == WITH_DATA) {
639 * XXX this can be optimized.
640 * If we have a hash function and lookup by pointer,
641 * run the hash function. Otherwise, scan the whole container
642 * (this only for the time being. We need to optimize this.)
644 if ((flags & OBJ_POINTER)) /* we know hash can handle this case */
645 start = i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
646 else /* don't know, let's scan all buckets */
647 i = -1; /* XXX this must be fixed later. */
649 /* determine the search boundaries: i..last-1 */
653 } else if ((flags & OBJ_CONTINUE)) {
659 ao2_lock(c); /* avoid modifications to the content */
661 for (; i < last ; i++) {
662 /* scan the list with prev-cur pointers */
663 struct bucket_list *cur;
665 AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
666 int match = (CMP_MATCH | CMP_STOP);
668 if (type == WITH_DATA) {
669 match &= cb_withdata(EXTERNAL_OBJ(cur->astobj), arg, data, flags);
671 match &= cb_default(EXTERNAL_OBJ(cur->astobj), arg, flags);
674 /* we found the object, performing operations according flags */
675 if (match == 0) { /* no match, no stop, continue */
677 } else if (match == CMP_STOP) { /* no match but stop, we are done */
681 /* we have a match (CMP_MATCH) here */
682 if (!(flags & OBJ_NODATA)) { /* if must return the object, record the value */
683 /* it is important to handle this case before the unlink */
684 ret = EXTERNAL_OBJ(cur->astobj);
686 __ao2_ref_debug(ret, 1, tag, file, line, funcname);
691 if (flags & OBJ_UNLINK) { /* must unlink */
692 struct bucket_list *x = cur;
694 /* we are going to modify the container, so update version */
695 ast_atomic_fetchadd_int(&c->version, 1);
696 AST_LIST_REMOVE_CURRENT(entry);
697 /* update number of elements and version */
698 ast_atomic_fetchadd_int(&c->elements, -1);
700 __ao2_ref_debug(EXTERNAL_OBJ(x->astobj), -1, tag, file, line, funcname);
702 __ao2_ref(EXTERNAL_OBJ(x->astobj), -1);
703 free(x); /* free the link record */
706 if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
707 /* We found the only match we need */
708 i = last; /* force exit from outer loop */
711 if (!(flags & OBJ_NODATA)) {
712 #if 0 /* XXX to be completed */
714 * This is the multiple-return case. We need to link
715 * the object in a list. The refcount is already increased.
720 AST_LIST_TRAVERSE_SAFE_END;
723 /* This assumes OBJ_MULTIPLE with !OBJ_NODATA is still not implemented */
727 if (i == c->n_buckets - 1 && (flags & OBJ_POINTER) && (flags & OBJ_CONTINUE)) {
728 /* Move to the beginning to ensure we check every bucket */
737 void *__ao2_callback_debug(struct ao2_container *c,
738 const enum search_flags flags,
739 ao2_callback_fn *cb_fn, void *arg,
740 char *tag, char *file, int line, const char *funcname)
742 return internal_ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, tag, file, line, funcname);
745 void *__ao2_callback(struct ao2_container *c, const enum search_flags flags,
746 ao2_callback_fn *cb_fn, void *arg)
748 return internal_ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, NULL, NULL, 0, NULL);
751 void *__ao2_callback_data_debug(struct ao2_container *c,
752 const enum search_flags flags,
753 ao2_callback_data_fn *cb_fn, void *arg, void *data,
754 char *tag, char *file, int line, const char *funcname)
756 return internal_ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, tag, file, line, funcname);
759 void *__ao2_callback_data(struct ao2_container *c, const enum search_flags flags,
760 ao2_callback_data_fn *cb_fn, void *arg, void *data)
762 return internal_ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, NULL, NULL, 0, NULL);
766 * the find function just invokes the default callback with some reasonable flags.
768 void *__ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
770 return __ao2_callback_debug(c, flags, c->cmp_fn, arg, tag, file, line, funcname);
773 void *__ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
775 return __ao2_callback(c, flags, c->cmp_fn, arg);
779 * initialize an iterator so we start from the first object
781 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
783 struct ao2_iterator a = {
792 * move to the next element in the container.
794 static void *internal_ao2_iterator_next(struct ao2_iterator *a, struct bucket_list **q)
797 struct bucket_list *p = NULL;
802 if (INTERNAL_OBJ(a->c) == NULL)
805 if (!(a->flags & F_AO2I_DONTLOCK))
808 /* optimization. If the container is unchanged and
809 * we have a pointer, try follow it
811 if (a->c->version == a->c_version && (p = a->obj) ) {
812 if ( (p = AST_LIST_NEXT(p, entry)) )
814 /* nope, start from the next bucket */
820 lim = a->c->n_buckets;
822 /* Browse the buckets array, moving to the next
823 * buckets if we don't find the entry in the current one.
824 * Stop when we find an element with version number greater
825 * than the current one (we reset the version to 0 when we
828 for (; a->bucket < lim; a->bucket++, a->version = 0) {
829 /* scan the current bucket */
830 AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
831 if (p->version > a->version)
838 a->version = p->version;
840 a->c_version = a->c->version;
841 ret = EXTERNAL_OBJ(p->astobj);
842 /* inc refcount of returned object */
849 void *__ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname)
851 struct bucket_list *p;
854 ret = internal_ao2_iterator_next(a, &p);
857 /* inc refcount of returned object */
858 __ao2_ref_debug(ret, 1, tag, file, line, funcname);
861 if (!(a->flags & F_AO2I_DONTLOCK))
867 void *__ao2_iterator_next(struct ao2_iterator *a)
869 struct bucket_list *p = NULL;
872 ret = internal_ao2_iterator_next(a, &p);
875 /* inc refcount of returned object */
879 if (!(a->flags & F_AO2I_DONTLOCK))
885 /* callback for destroying container.
886 * we can make it simple as we know what it does
888 static int cd_cb(void *obj, void *arg, int flag)
894 static int cd_cb_debug(void *obj, void *arg, int flag)
896 __ao2_ref_debug(obj, -1, "deref object via container destroy", __FILE__, __LINE__, __PRETTY_FUNCTION__);
900 static void container_destruct(void *_c)
902 struct ao2_container *c = _c;
905 __ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
907 for (i = 0; i < c->n_buckets; i++) {
908 struct bucket_list *current;
910 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
916 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
920 static void container_destruct_debug(void *_c)
922 struct ao2_container *c = _c;
925 __ao2_callback_debug(c, OBJ_UNLINK, cd_cb_debug, NULL, "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__);
927 for (i = 0; i < c->n_buckets; i++) {
928 struct bucket_list *current;
930 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
936 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
941 static int print_cb(void *obj, void *arg, int flag)
943 struct ast_cli_args *a = (struct ast_cli_args *) arg;
944 char *s = (char *)obj;
946 ast_cli(a->fd, "string <%s>\n", s);
953 static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
957 e->command = "astobj2 show stats";
958 e->usage = "Usage: astobj2 show stats\n"
959 " Show astobj2 show stats\n";
964 ast_cli(a->fd, "Objects : %d\n", ao2.total_objects);
965 ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
966 ast_cli(a->fd, "Memory : %d\n", ao2.total_mem);
967 ast_cli(a->fd, "Locked : %d\n", ao2.total_locked);
968 ast_cli(a->fd, "Refs : %d\n", ao2.total_refs);
973 * This is testing code for astobj
975 static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
977 struct ao2_container *c1;
980 static int prof_id = -1;
981 struct ast_cli_args fake_args = { a->fd, 0, NULL };
985 e->command = "astobj2 test";
986 e->usage = "Usage: astobj2 test <num>\n"
987 " Runs astobj2 test. Creates 'num' objects,\n"
988 " and test iterators, callbacks and may be other stuff\n";
995 return CLI_SHOWUSAGE;
999 prof_id = ast_add_profile("ao2_alloc", 0);
1001 ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
1002 lim = atoi(a->argv[2]);
1003 ast_cli(a->fd, "called astobj_test\n");
1005 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
1007 * allocate a container with no default callback, and no hash function.
1008 * No hash means everything goes in the same bucket.
1010 c1 = ao2_t_container_alloc(100, NULL /* no callback */, NULL /* no hash */,"test");
1011 ast_cli(a->fd, "container allocated as %p\n", c1);
1014 * fill the container with objects.
1015 * ao2_alloc() gives us a reference which we pass to the
1016 * container when we do the insert.
1018 for (i = 0; i < lim; i++) {
1019 ast_mark(prof_id, 1 /* start */);
1020 obj = ao2_t_alloc(80, NULL,"test");
1021 ast_mark(prof_id, 0 /* stop */);
1022 ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
1023 sprintf(obj, "-- this is obj %d --", i);
1025 /* At this point, the refcount on obj is 2 due to the allocation
1026 * and linking. We can go ahead and reduce the refcount by 1
1027 * right here so that when the container is unreffed later, the
1028 * objects will be freed
1030 ao2_t_ref(obj, -1, "test");
1032 ast_cli(a->fd, "testing callbacks\n");
1033 ao2_t_callback(c1, 0, print_cb, a, "test callback");
1034 ast_cli(a->fd, "testing iterators, remove every second object\n");
1036 struct ao2_iterator ai;
1039 ai = ao2_iterator_init(c1, 0);
1040 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
1041 ast_cli(a->fd, "iterator on <%s>\n", obj);
1043 ao2_t_unlink(c1, obj,"test");
1044 ao2_t_ref(obj, -1,"test");
1046 ast_cli(a->fd, "testing iterators again\n");
1047 ai = ao2_iterator_init(c1, 0);
1048 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
1049 ast_cli(a->fd, "iterator on <%s>\n", obj);
1050 ao2_t_ref(obj, -1,"test");
1053 ast_cli(a->fd, "testing callbacks again\n");
1054 ao2_t_callback(c1, 0, print_cb, a, "test callback");
1056 ast_verbose("now you should see an error message:\n");
1057 ao2_t_ref(&i, -1, ""); /* i is not a valid object so we print an error here */
1059 ast_cli(a->fd, "destroy container\n");
1060 ao2_t_ref(c1, -1, ""); /* destroy container */
1061 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
1065 static struct ast_cli_entry cli_astobj2[] = {
1066 AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
1067 AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
1069 #endif /* AO2_DEBUG */
1071 int astobj2_init(void)
1074 ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));