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);
124 * \brief convert from a pointer _p to an astobj2 object
126 * \return the pointer to the user-defined portion.
128 #define EXTERNAL_OBJ(_p) ((_p) == NULL ? NULL : (_p)->user_data)
130 /* the underlying functions common to debug and non-debug versions */
132 static int __ao2_ref(void *user_data, const int delta);
133 static void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn);
134 static struct ao2_container *__ao2_container_alloc(struct ao2_container *c, const uint n_buckets, ao2_hash_fn *hash_fn,
135 ao2_callback_fn *cmp_fn);
136 static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data);
137 static void *__ao2_callback(struct ao2_container *c,
138 const enum search_flags flags, ao2_callback_fn *cb_fn, void *arg, void *data,
139 char *tag, char *file, int line, const char *funcname);
140 static void * __ao2_iterator_next(struct ao2_iterator *a, struct bucket_list **q);
142 #ifndef DEBUG_THREADS
143 int ao2_lock(void *user_data)
145 int _ao2_lock(void *user_data, const char *file, const char *func, int line, const char *var)
148 struct astobj2 *p = INTERNAL_OBJ(user_data);
154 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
157 #ifndef DEBUG_THREADS
158 return ast_mutex_lock(&p->priv_data.lock);
160 return __ast_pthread_mutex_lock(file, line, func, var, &p->priv_data.lock);
164 #ifndef DEBUG_THREADS
165 int ao2_unlock(void *user_data)
167 int _ao2_unlock(void *user_data, const char *file, const char *func, int line, const char *var)
170 struct astobj2 *p = INTERNAL_OBJ(user_data);
176 ast_atomic_fetchadd_int(&ao2.total_locked, -1);
179 #ifndef DEBUG_THREADS
180 return ast_mutex_unlock(&p->priv_data.lock);
182 return __ast_pthread_mutex_unlock(file, line, func, var, &p->priv_data.lock);
186 int ao2_trylock(void *user_data)
188 struct astobj2 *p = INTERNAL_OBJ(user_data);
193 ret = ast_mutex_trylock(&p->priv_data.lock);
196 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
201 void *ao2_object_get_lockaddr(void *obj)
203 struct astobj2 *p = INTERNAL_OBJ(obj);
208 return &p->priv_data.lock;
212 * The argument is a pointer to the user portion.
216 int _ao2_ref_debug(void *user_data, const int delta, char *tag, char *file, int line, const char *funcname)
218 struct astobj2 *obj = INTERNAL_OBJ(user_data);
224 FILE *refo = fopen(REF_FILE,"a");
225 fprintf(refo, "%p %s%d %s:%d:%s (%s) [@%d]\n", user_data, (delta<0? "":"+"), delta, file, line, funcname, tag, obj->priv_data.ref_counter);
228 if (obj->priv_data.ref_counter + delta == 0 && obj->priv_data.destructor_fn != NULL) { /* this isn't protected with lock; just for o/p */
229 FILE *refo = fopen(REF_FILE,"a");
230 fprintf(refo, "%p **call destructor** %s:%d:%s (%s)\n", user_data, file, line, funcname, tag);
233 return __ao2_ref(user_data, delta);
236 int _ao2_ref(void *user_data, const int delta)
238 struct astobj2 *obj = INTERNAL_OBJ(user_data);
243 return __ao2_ref(user_data, delta);
246 static int __ao2_ref(void *user_data, const int delta)
248 struct astobj2 *obj = INTERNAL_OBJ(user_data);
252 /* if delta is 0, just return the refcount */
254 return (obj->priv_data.ref_counter);
256 /* we modify with an atomic operation the reference counter */
257 ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
258 current_value = ret + delta;
261 ast_atomic_fetchadd_int(&ao2.total_refs, delta);
264 /* this case must never happen */
265 if (current_value < 0)
266 ast_log(LOG_ERROR, "refcount %d on object %p\n", current_value, user_data);
268 if (current_value <= 0) { /* last reference, destroy the object */
269 if (obj->priv_data.destructor_fn != NULL) {
270 obj->priv_data.destructor_fn(user_data);
273 ast_mutex_destroy(&obj->priv_data.lock);
275 ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
276 ast_atomic_fetchadd_int(&ao2.total_objects, -1);
278 /* for safety, zero-out the astobj2 header and also the
279 * first word of the user-data, which we make sure is always
281 memset(obj, '\0', sizeof(struct astobj2 *) + sizeof(void *) );
289 * We always alloc at least the size of a void *,
290 * for debugging purposes.
292 static void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
297 if (data_size < sizeof(void *))
298 data_size = sizeof(void *);
300 obj = ast_calloc(1, sizeof(*obj) + data_size);
305 ast_mutex_init(&obj->priv_data.lock);
306 obj->priv_data.magic = AO2_MAGIC;
307 obj->priv_data.data_size = data_size;
308 obj->priv_data.ref_counter = 1;
309 obj->priv_data.destructor_fn = destructor_fn; /* can be NULL */
312 ast_atomic_fetchadd_int(&ao2.total_objects, 1);
313 ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
314 ast_atomic_fetchadd_int(&ao2.total_refs, 1);
317 /* return a pointer to the user data */
318 return EXTERNAL_OBJ(obj);
321 void *_ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, char *tag, char *file, int line, const char *funcname)
325 FILE *refo = fopen(REF_FILE,"a");
327 obj = __ao2_alloc(data_size, destructor_fn);
333 fprintf(refo, "%p =1 %s:%d:%s (%s)\n", obj, file, line, funcname, tag);
337 /* return a pointer to the user data */
341 void *_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
343 return __ao2_alloc(data_size, destructor_fn);
347 /* internal callback to destroy a container. */
348 static void container_destruct(void *c);
350 /* internal callback to destroy a container. */
351 static void container_destruct_debug(void *c);
353 /* each bucket in the container is a tailq. */
354 AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
357 * A container; stores the hash and callback functions, information on
358 * the size, the hash bucket heads, and a version number, starting at 0
359 * (for a newly created, empty container)
360 * and incremented every time an object is inserted or deleted.
361 * The assumption is that an object is never moved in a container,
362 * but removed and readded with the new number.
363 * The version number is especially useful when implementing iterators.
364 * In fact, we can associate a unique, monotonically increasing number to
365 * each object, which means that, within an iterator, we can store the
366 * version number of the current object, and easily look for the next one,
367 * which is the next one in the list with a higher number.
368 * Since all objects have a version >0, we can use 0 as a marker for
369 * 'we need the first object in the bucket'.
371 * \todo Linking and unlink objects is typically expensive, as it
372 * involves a malloc() of a small object which is very inefficient.
373 * To optimize this, we allocate larger arrays of bucket_list's
374 * when we run out of them, and then manage our own freelist.
375 * This will be more efficient as we can do the freelist management while
376 * we hold the lock (that we need anyways).
378 struct ao2_container {
379 ao2_hash_fn *hash_fn;
380 ao2_callback_fn *cmp_fn;
382 /*! Number of elements in the container */
384 /*! described above */
387 struct bucket buckets[0];
391 * \brief always zero hash function
393 * it is convenient to have a hash function that always returns 0.
394 * This is basically used when we want to have a container that is
395 * a simple linked list.
399 static int hash_zero(const void *user_obj, const int flags)
405 * A container is just an object, after all!
407 static struct ao2_container *__ao2_container_alloc(struct ao2_container *c, const uint n_buckets, ao2_hash_fn *hash_fn,
408 ao2_callback_fn *cmp_fn)
410 /* XXX maybe consistency check on arguments ? */
411 /* compute the container size */
416 c->version = 1; /* 0 is a reserved value here */
417 c->n_buckets = n_buckets;
418 c->hash_fn = hash_fn ? hash_fn : hash_zero;
422 ast_atomic_fetchadd_int(&ao2.total_containers, 1);
428 struct ao2_container *_ao2_container_alloc_debug(const uint n_buckets, ao2_hash_fn *hash_fn,
429 ao2_callback_fn *cmp_fn, char *tag, char *file, int line, const char *funcname)
431 /* XXX maybe consistency check on arguments ? */
432 /* compute the container size */
433 size_t container_size = sizeof(struct ao2_container) + n_buckets * sizeof(struct bucket);
434 struct ao2_container *c = _ao2_alloc_debug(container_size, container_destruct_debug, tag, file, line, funcname);
436 return __ao2_container_alloc(c, n_buckets, hash_fn, cmp_fn);
439 struct ao2_container *
440 _ao2_container_alloc(const uint n_buckets, ao2_hash_fn *hash_fn,
441 ao2_callback_fn *cmp_fn)
443 /* XXX maybe consistency check on arguments ? */
444 /* compute the container size */
446 size_t container_size = sizeof(struct ao2_container) + n_buckets * sizeof(struct bucket);
447 struct ao2_container *c = _ao2_alloc(container_size, container_destruct);
449 return __ao2_container_alloc(c, n_buckets, hash_fn, cmp_fn);
453 * return the number of elements in the container
455 int ao2_container_count(struct ao2_container *c)
461 * A structure to create a linked list of entries,
462 * used within a bucket.
463 * XXX \todo this should be private to the container code
466 AST_LIST_ENTRY(bucket_list) entry;
468 struct astobj2 *astobj; /* pointer to internal data */
472 * link an object to a container
475 static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data)
478 /* create a new list entry */
479 struct bucket_list *p;
480 struct astobj2 *obj = INTERNAL_OBJ(user_data);
485 if (INTERNAL_OBJ(c) == NULL)
488 p = ast_calloc(1, sizeof(*p));
492 i = c->hash_fn(user_data, OBJ_POINTER);
497 p->version = ast_atomic_fetchadd_int(&c->version, 1);
498 AST_LIST_INSERT_TAIL(&c->buckets[i], p, entry);
499 ast_atomic_fetchadd_int(&c->elements, 1);
501 /* the last two operations (ao2_ref, ao2_unlock) must be done by the calling func */
505 void *_ao2_link_debug(struct ao2_container *c, void *user_data, char *tag, char *file, int line, const char *funcname)
507 struct bucket_list *p = __ao2_link(c, user_data);
510 _ao2_ref_debug(user_data, +1, tag, file, line, funcname);
516 void *_ao2_link(struct ao2_container *c, void *user_data)
518 struct bucket_list *p = __ao2_link(c, user_data);
521 _ao2_ref(user_data, +1);
528 * \brief another convenience function is a callback that matches on address
530 int ao2_match_by_addr(void *user_data, void *arg, void *data, int flags)
532 return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
536 * Unlink an object from the container
537 * and destroy the associated * ao2_bucket_list structure.
539 void *_ao2_unlink_debug(struct ao2_container *c, void *user_data, char *tag,
540 char *file, int line, const char *funcname)
542 if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */
545 _ao2_callback_debug(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data, NULL, tag, file, line, funcname);
550 void *_ao2_unlink(struct ao2_container *c, void *user_data)
552 if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */
555 _ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data, NULL);
561 * \brief special callback that matches all
563 static int cb_true(void *user_data, void *arg, void *data, int flags)
565 ast_log(LOG_ERROR,"If you see this, something is strange!\n");
570 * Browse the container using different stategies accoding the flags.
571 * \return Is a pointer to an object or to a list of object if OBJ_MULTIPLE is
573 * Luckily, for debug purposes, the added args (tag, file, line, funcname)
574 * aren't an excessive load to the system, as the callback should not be
575 * called as often as, say, the ao2_ref func is called.
577 static void *__ao2_callback(struct ao2_container *c,
578 const enum search_flags flags, ao2_callback_fn *cb_fn, void *arg, void *data,
579 char *tag, char *file, int line, const char *funcname)
581 int i, last; /* search boundaries */
584 if (INTERNAL_OBJ(c) == NULL) /* safety check on the argument */
587 if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
588 ast_log(LOG_WARNING, "multiple data return not implemented yet (flags %x)\n", flags);
592 /* override the match function if necessary */
593 if (cb_fn == NULL) /* if NULL, match everything */
596 * XXX this can be optimized.
597 * If we have a hash function and lookup by pointer,
598 * run the hash function. Otherwise, scan the whole container
599 * (this only for the time being. We need to optimize this.)
601 if ((flags & OBJ_POINTER)) /* we know hash can handle this case */
602 i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
603 else /* don't know, let's scan all buckets */
604 i = -1; /* XXX this must be fixed later. */
606 /* determine the search boundaries: i..last-1 */
614 ao2_lock(c); /* avoid modifications to the content */
616 for (; i < last ; i++) {
617 /* scan the list with prev-cur pointers */
618 struct bucket_list *cur;
620 AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
621 int match = cb_fn(EXTERNAL_OBJ(cur->astobj), arg, data, flags) & (CMP_MATCH | CMP_STOP);
623 /* we found the object, performing operations according flags */
624 if (match == 0) { /* no match, no stop, continue */
626 } else if (match == CMP_STOP) { /* no match but stop, we are done */
630 /* we have a match (CMP_MATCH) here */
631 if (!(flags & OBJ_NODATA)) { /* if must return the object, record the value */
632 /* it is important to handle this case before the unlink */
633 ret = EXTERNAL_OBJ(cur->astobj);
635 _ao2_ref_debug(ret, 1, tag, file, line, funcname);
640 if (flags & OBJ_UNLINK) { /* must unlink */
641 struct bucket_list *x = cur;
643 /* we are going to modify the container, so update version */
644 ast_atomic_fetchadd_int(&c->version, 1);
645 AST_LIST_REMOVE_CURRENT(entry);
646 /* update number of elements and version */
647 ast_atomic_fetchadd_int(&c->elements, -1);
649 _ao2_ref_debug(EXTERNAL_OBJ(x->astobj), -1, tag, file, line, funcname);
651 _ao2_ref(EXTERNAL_OBJ(x->astobj), -1);
652 free(x); /* free the link record */
655 if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
656 /* We found the only match we need */
657 i = last; /* force exit from outer loop */
660 if (!(flags & OBJ_NODATA)) {
661 #if 0 /* XXX to be completed */
663 * This is the multiple-return case. We need to link
664 * the object in a list. The refcount is already increased.
669 AST_LIST_TRAVERSE_SAFE_END;
675 void *_ao2_callback_debug(struct ao2_container *c,
676 const enum search_flags flags,
677 ao2_callback_fn *cb_fn, void *arg, void *data,
678 char *tag, char *file, int line, const char *funcname)
680 return __ao2_callback(c,flags, cb_fn, arg, data, tag, file, line, funcname);
683 void *_ao2_callback(struct ao2_container *c,const enum search_flags flags,
684 ao2_callback_fn *cb_fn, void *arg, void *data)
686 return __ao2_callback(c,flags, cb_fn, arg, data, NULL, NULL, 0, NULL);
690 * the find function just invokes the default callback with some reasonable flags.
692 void *_ao2_find_debug(struct ao2_container *c, void *arg, void *data, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
694 return _ao2_callback_debug(c, flags, c->cmp_fn, arg, data, tag, file, line, funcname);
697 void *_ao2_find(struct ao2_container *c, void *arg, void *data, enum search_flags flags)
699 return _ao2_callback(c, flags, c->cmp_fn, arg, data);
703 * initialize an iterator so we start from the first object
705 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
707 struct ao2_iterator a = {
716 * move to the next element in the container.
718 static void * __ao2_iterator_next(struct ao2_iterator *a, struct bucket_list **q)
721 struct bucket_list *p = NULL;
726 if (INTERNAL_OBJ(a->c) == NULL)
729 if (!(a->flags & F_AO2I_DONTLOCK))
732 /* optimization. If the container is unchanged and
733 * we have a pointer, try follow it
735 if (a->c->version == a->c_version && (p = a->obj) ) {
736 if ( (p = AST_LIST_NEXT(p, entry)) )
738 /* nope, start from the next bucket */
744 lim = a->c->n_buckets;
746 /* Browse the buckets array, moving to the next
747 * buckets if we don't find the entry in the current one.
748 * Stop when we find an element with version number greater
749 * than the current one (we reset the version to 0 when we
752 for (; a->bucket < lim; a->bucket++, a->version = 0) {
753 /* scan the current bucket */
754 AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
755 if (p->version > a->version)
762 a->version = p->version;
764 a->c_version = a->c->version;
765 ret = EXTERNAL_OBJ(p->astobj);
766 /* inc refcount of returned object */
773 void * _ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname)
775 struct bucket_list *p;
778 ret = __ao2_iterator_next(a, &p);
781 /* inc refcount of returned object */
782 _ao2_ref_debug(ret, 1, tag, file, line, funcname);
785 if (!(a->flags & F_AO2I_DONTLOCK))
791 void * _ao2_iterator_next(struct ao2_iterator *a)
793 struct bucket_list *p = NULL;
796 ret = __ao2_iterator_next(a, &p);
799 /* inc refcount of returned object */
803 if (!(a->flags & F_AO2I_DONTLOCK))
809 /* callback for destroying container.
810 * we can make it simple as we know what it does
812 static int cd_cb(void *obj, void *arg, void *data, int flag)
818 static int cd_cb_debug(void *obj, void *arg, void *data, int flag)
820 _ao2_ref_debug(obj, -1, "deref object via container destroy", __FILE__, __LINE__, __PRETTY_FUNCTION__);
824 static void container_destruct(void *_c)
826 struct ao2_container *c = _c;
829 _ao2_callback(c, OBJ_UNLINK, cd_cb, NULL, NULL);
831 for (i = 0; i < c->n_buckets; i++) {
832 struct bucket_list *current;
834 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
840 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
844 static void container_destruct_debug(void *_c)
846 struct ao2_container *c = _c;
849 _ao2_callback_debug(c, OBJ_UNLINK, cd_cb_debug, NULL, NULL, "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__);
851 for (i = 0; i < c->n_buckets; i++) {
852 struct bucket_list *current;
854 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
860 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
865 static int print_cb(void *obj, void *arg, void *data, int flag)
868 char *s = (char *)obj;
870 ast_cli(*fd, "string <%s>\n", s);
877 static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
881 e->command = "astobj2 show stats";
882 e->usage = "Usage: astobj2 show stats\n"
883 " Show astobj2 show stats\n";
888 ast_cli(a->fd, "Objects : %d\n", ao2.total_objects);
889 ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
890 ast_cli(a->fd, "Memory : %d\n", ao2.total_mem);
891 ast_cli(a->fd, "Locked : %d\n", ao2.total_locked);
892 ast_cli(a->fd, "Refs : %d\n", ao2.total_refs);
897 * This is testing code for astobj
899 static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
901 struct ao2_container *c1;
904 static int prof_id = -1;
905 struct ast_cli_args fake_args = { a->fd, 0, NULL };
909 e->command = "astobj2 test";
910 e->usage = "Usage: astobj2 test <num>\n"
911 " Runs astobj2 test. Creates 'num' objects,\n"
912 " and test iterators, callbacks and may be other stuff\n";
919 return CLI_SHOWUSAGE;
923 prof_id = ast_add_profile("ao2_alloc", 0);
925 ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
926 lim = atoi(a->argv[2]);
927 ast_cli(a->fd, "called astobj_test\n");
929 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
931 * allocate a container with no default callback, and no hash function.
932 * No hash means everything goes in the same bucket.
934 c1 = ao2_t_container_alloc(100, NULL /* no callback */, NULL /* no hash */,"test");
935 ast_cli(a->fd, "container allocated as %p\n", c1);
938 * fill the container with objects.
939 * ao2_alloc() gives us a reference which we pass to the
940 * container when we do the insert.
942 for (i = 0; i < lim; i++) {
943 ast_mark(prof_id, 1 /* start */);
944 obj = ao2_t_alloc(80, NULL,"test");
945 ast_mark(prof_id, 0 /* stop */);
946 ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
947 sprintf(obj, "-- this is obj %d --", i);
949 /* At this point, the refcount on obj is 2 due to the allocation
950 * and linking. We can go ahead and reduce the refcount by 1
951 * right here so that when the container is unreffed later, the
952 * objects will be freed
954 ao2_t_ref(obj, -1, "test");
956 ast_cli(a->fd, "testing callbacks\n");
957 ao2_t_callback(c1, 0, print_cb, &a->fd, NULL, "test callback");
958 ast_cli(a->fd, "testing iterators, remove every second object\n");
960 struct ao2_iterator ai;
963 ai = ao2_iterator_init(c1, 0);
964 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
965 ast_cli(a->fd, "iterator on <%s>\n", obj);
967 ao2_t_unlink(c1, obj,"test");
968 ao2_t_ref(obj, -1,"test");
970 ast_cli(a->fd, "testing iterators again\n");
971 ai = ao2_iterator_init(c1, 0);
972 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
973 ast_cli(a->fd, "iterator on <%s>\n", obj);
974 ao2_t_ref(obj, -1,"test");
977 ast_cli(a->fd, "testing callbacks again\n");
978 ao2_t_callback(c1, 0, print_cb, &a->fd, NULL, "test callback");
980 ast_verbose("now you should see an error message:\n");
981 ao2_t_ref(&i, -1, ""); /* i is not a valid object so we print an error here */
983 ast_cli(a->fd, "destroy container\n");
984 ao2_t_ref(c1, -1, ""); /* destroy container */
985 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
989 static struct ast_cli_entry cli_astobj2[] = {
990 AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
991 AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
993 #endif /* AO2_DEBUG */
995 int astobj2_init(void)
998 ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));