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;
63 /* #define AO2_DEBUG 1 */
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 = ast_bt_get_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_entry *internal_ao2_link(struct ao2_container *c, void *user_data, int flags, 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_entry **q);
146 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 return __ast_pthread_mutex_lock(file, line, func, var, &p->priv_data.lock);
160 int __ao2_unlock(void *user_data, const char *file, const char *func, int line, const char *var)
162 struct astobj2 *p = INTERNAL_OBJ(user_data);
168 ast_atomic_fetchadd_int(&ao2.total_locked, -1);
171 return __ast_pthread_mutex_unlock(file, line, func, var, &p->priv_data.lock);
174 int __ao2_trylock(void *user_data, const char *file, const char *func, int line, const char *var)
176 struct astobj2 *p = INTERNAL_OBJ(user_data);
181 ret = __ast_pthread_mutex_trylock(file, line, func, var, &p->priv_data.lock);
185 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
190 void *ao2_object_get_lockaddr(void *obj)
192 struct astobj2 *p = INTERNAL_OBJ(obj);
197 return &p->priv_data.lock;
201 * The argument is a pointer to the user portion.
205 int __ao2_ref_debug(void *user_data, const int delta, char *tag, char *file, int line, const char *funcname)
207 struct astobj2 *obj = INTERNAL_OBJ(user_data);
213 FILE *refo = fopen(REF_FILE,"a");
214 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);
217 if (obj->priv_data.ref_counter + delta == 0 && obj->priv_data.destructor_fn != NULL) { /* this isn't protected with lock; just for o/p */
218 FILE *refo = fopen(REF_FILE,"a");
219 fprintf(refo, "%p **call destructor** %s:%d:%s (%s)\n", user_data, file, line, funcname, tag);
222 return internal_ao2_ref(user_data, delta);
225 int __ao2_ref(void *user_data, const int delta)
227 struct astobj2 *obj = INTERNAL_OBJ(user_data);
232 return internal_ao2_ref(user_data, delta);
235 static int internal_ao2_ref(void *user_data, const int delta)
237 struct astobj2 *obj = INTERNAL_OBJ(user_data);
244 /* if delta is 0, just return the refcount */
246 return (obj->priv_data.ref_counter);
248 /* we modify with an atomic operation the reference counter */
249 ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
250 current_value = ret + delta;
253 ast_atomic_fetchadd_int(&ao2.total_refs, delta);
256 /* this case must never happen */
257 if (current_value < 0)
258 ast_log(LOG_ERROR, "refcount %d on object %p\n", current_value, user_data);
260 if (current_value <= 0) { /* last reference, destroy the object */
261 if (obj->priv_data.destructor_fn != NULL) {
262 obj->priv_data.destructor_fn(user_data);
265 ast_mutex_destroy(&obj->priv_data.lock);
267 ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
268 ast_atomic_fetchadd_int(&ao2.total_objects, -1);
270 /* for safety, zero-out the astobj2 header and also the
271 * first word of the user-data, which we make sure is always
273 memset(obj, '\0', sizeof(struct astobj2 *) + sizeof(void *) );
281 * We always alloc at least the size of a void *,
282 * for debugging purposes.
284 static void *internal_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, const char *file, int line, const char *funcname)
289 if (data_size < sizeof(void *))
290 data_size = sizeof(void *);
292 #if defined(__AST_DEBUG_MALLOC)
293 obj = __ast_calloc(1, sizeof(*obj) + data_size, file, line, funcname);
295 obj = ast_calloc(1, sizeof(*obj) + data_size);
301 ast_mutex_init(&obj->priv_data.lock);
302 obj->priv_data.magic = AO2_MAGIC;
303 obj->priv_data.data_size = data_size;
304 obj->priv_data.ref_counter = 1;
305 obj->priv_data.destructor_fn = destructor_fn; /* can be NULL */
308 ast_atomic_fetchadd_int(&ao2.total_objects, 1);
309 ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
310 ast_atomic_fetchadd_int(&ao2.total_refs, 1);
313 /* return a pointer to the user data */
314 return EXTERNAL_OBJ(obj);
317 void *__ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, char *tag,
318 const char *file, int line, const char *funcname, int ref_debug)
322 FILE *refo = ref_debug ? fopen(REF_FILE,"a") : NULL;
324 if ((obj = internal_ao2_alloc(data_size, destructor_fn, file, line, funcname)) == NULL) {
330 fprintf(refo, "%p =1 %s:%d:%s (%s)\n", obj, file, line, funcname, tag);
334 /* return a pointer to the user data */
338 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
340 return internal_ao2_alloc(data_size, destructor_fn, __FILE__, __LINE__, __FUNCTION__);
344 /* internal callback to destroy a container. */
345 static void container_destruct(void *c);
347 /* internal callback to destroy a container. */
348 static void container_destruct_debug(void *c);
350 /* each bucket in the container is a tailq. */
351 AST_LIST_HEAD_NOLOCK(bucket, bucket_entry);
354 * A container; stores the hash and callback functions, information on
355 * the size, the hash bucket heads, and a version number, starting at 0
356 * (for a newly created, empty container)
357 * and incremented every time an object is inserted or deleted.
358 * The assumption is that an object is never moved in a container,
359 * but removed and readded with the new number.
360 * The version number is especially useful when implementing iterators.
361 * In fact, we can associate a unique, monotonically increasing number to
362 * each object, which means that, within an iterator, we can store the
363 * version number of the current object, and easily look for the next one,
364 * which is the next one in the list with a higher number.
365 * Since all objects have a version >0, we can use 0 as a marker for
366 * 'we need the first object in the bucket'.
368 * \todo Linking and unlink objects is typically expensive, as it
369 * involves a malloc() of a small object which is very inefficient.
370 * To optimize this, we allocate larger arrays of bucket_entry's
371 * when we run out of them, and then manage our own freelist.
372 * This will be more efficient as we can do the freelist management while
373 * we hold the lock (that we need anyways).
375 struct ao2_container {
376 ao2_hash_fn *hash_fn;
377 ao2_callback_fn *cmp_fn;
379 /*! Number of elements in the container */
381 /*! described above */
384 struct bucket buckets[0];
388 * \brief always zero hash function
390 * it is convenient to have a hash function that always returns 0.
391 * This is basically used when we want to have a container that is
392 * a simple linked list.
396 static int hash_zero(const void *user_obj, const int flags)
402 * A container is just an object, after all!
404 static struct ao2_container *internal_ao2_container_alloc(struct ao2_container *c, const unsigned int n_buckets, ao2_hash_fn *hash_fn,
405 ao2_callback_fn *cmp_fn)
407 /* XXX maybe consistency check on arguments ? */
408 /* compute the container size */
413 c->version = 1; /* 0 is a reserved value here */
414 c->n_buckets = hash_fn ? n_buckets : 1;
415 c->hash_fn = hash_fn ? hash_fn : hash_zero;
419 ast_atomic_fetchadd_int(&ao2.total_containers, 1);
425 struct ao2_container *__ao2_container_alloc_debug(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
426 ao2_callback_fn *cmp_fn, char *tag, char *file, int line,
427 const char *funcname, int ref_debug)
429 /* XXX maybe consistency check on arguments ? */
430 /* compute the container size */
431 const unsigned int num_buckets = hash_fn ? n_buckets : 1;
432 size_t container_size = sizeof(struct ao2_container) + num_buckets * sizeof(struct bucket);
433 struct ao2_container *c = __ao2_alloc_debug(container_size, container_destruct_debug, tag, file, line, funcname, ref_debug);
435 return internal_ao2_container_alloc(c, num_buckets, hash_fn, cmp_fn);
438 struct ao2_container *__ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
439 ao2_callback_fn *cmp_fn)
441 /* XXX maybe consistency check on arguments ? */
442 /* compute the container size */
444 const unsigned int num_buckets = hash_fn ? n_buckets : 1;
445 size_t container_size = sizeof(struct ao2_container) + num_buckets * sizeof(struct bucket);
446 struct ao2_container *c = __ao2_alloc(container_size, container_destruct);
448 return internal_ao2_container_alloc(c, num_buckets, hash_fn, cmp_fn);
452 * return the number of elements in the container
454 int ao2_container_count(struct ao2_container *c)
460 * A structure to create a linked list of entries,
461 * used within a bucket.
462 * XXX \todo this should be private to the container code
464 struct bucket_entry {
465 AST_LIST_ENTRY(bucket_entry) entry;
467 struct astobj2 *astobj; /* pointer to internal data */
471 * link an object to a container
474 static struct bucket_entry *internal_ao2_link(struct ao2_container *c, void *user_data, int flags, const char *file, int line, const char *func)
477 /* create a new list entry */
478 struct bucket_entry *p;
479 struct astobj2 *obj = INTERNAL_OBJ(user_data);
484 if (INTERNAL_OBJ(c) == NULL)
487 p = ast_calloc(1, sizeof(*p));
491 i = abs(c->hash_fn(user_data, OBJ_POINTER));
493 if (!(flags & OBJ_NOLOCK)) {
498 p->version = ast_atomic_fetchadd_int(&c->version, 1);
499 AST_LIST_INSERT_TAIL(&c->buckets[i], p, entry);
500 ast_atomic_fetchadd_int(&c->elements, 1);
502 /* the last two operations (ao2_ref, ao2_unlock) must be done by the calling func */
506 void *__ao2_link_debug(struct ao2_container *c, void *user_data, int flags, char *tag, char *file, int line, const char *funcname)
508 struct bucket_entry *p = internal_ao2_link(c, user_data, flags, file, line, funcname);
511 __ao2_ref_debug(user_data, +1, tag, file, line, funcname);
512 if (!(flags & OBJ_NOLOCK)) {
519 void *__ao2_link(struct ao2_container *c, void *user_data, int flags)
521 struct bucket_entry *p = internal_ao2_link(c, user_data, flags, __FILE__, __LINE__, __PRETTY_FUNCTION__);
524 __ao2_ref(user_data, +1);
525 if (!(flags & OBJ_NOLOCK)) {
533 * \brief another convenience function is a callback that matches on address
535 int ao2_match_by_addr(void *user_data, void *arg, int flags)
537 return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
541 * Unlink an object from the container
542 * and destroy the associated * bucket_entry structure.
544 void *__ao2_unlink_debug(struct ao2_container *c, void *user_data, int flags, char *tag,
545 char *file, int line, const char *funcname)
547 if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */
550 flags |= (OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA);
552 __ao2_callback_debug(c, flags, ao2_match_by_addr, user_data, tag, file, line, funcname);
557 void *__ao2_unlink(struct ao2_container *c, void *user_data, int flags)
559 if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */
562 flags |= (OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA);
563 __ao2_callback(c, flags, ao2_match_by_addr, user_data);
569 * \brief special callback that matches all
571 static int cb_true(void *user_data, void *arg, int flags)
577 * \brief similar to cb_true, but is an ao2_callback_data_fn instead
579 static int cb_true_data(void *user_data, void *arg, void *data, int flags)
585 * Browse the container using different stategies accoding the flags.
586 * \return Is a pointer to an object or to a list of object if OBJ_MULTIPLE is
588 * Luckily, for debug purposes, the added args (tag, file, line, funcname)
589 * aren't an excessive load to the system, as the callback should not be
590 * called as often as, say, the ao2_ref func is called.
592 static void *internal_ao2_callback(struct ao2_container *c,
593 const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type,
594 char *tag, char *file, int line, const char *funcname)
596 int i, start, last; /* search boundaries */
598 ao2_callback_fn *cb_default = NULL;
599 ao2_callback_data_fn *cb_withdata = NULL;
600 struct ao2_container *multi_container = NULL;
601 struct ao2_iterator *multi_iterator = NULL;
603 if (INTERNAL_OBJ(c) == NULL) /* safety check on the argument */
607 * This logic is used so we can support OBJ_MULTIPLE with OBJ_NODATA
608 * turned off. This if statement checks for the special condition
609 * where multiple items may need to be returned.
611 if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
612 /* we need to return an ao2_iterator with the results,
613 * as there could be more than one. the iterator will
614 * hold the only reference to a container that has all the
615 * matching objects linked into it, so when the iterator
616 * is destroyed, the container will be automatically
619 if (!(multi_container = __ao2_container_alloc(1, NULL, NULL))) {
622 if (!(multi_iterator = ast_calloc(1, sizeof(*multi_iterator)))) {
623 ao2_ref(multi_container, -1);
628 /* override the match function if necessary */
629 if (cb_fn == NULL) { /* if NULL, match everything */
630 if (type == WITH_DATA) {
631 cb_withdata = cb_true_data;
633 cb_default = cb_true;
636 /* We do this here to avoid the per object casting penalty (even though
637 that is probably optimized away anyway). */
638 if (type == WITH_DATA) {
646 * XXX this can be optimized.
647 * If we have a hash function and lookup by pointer,
648 * run the hash function. Otherwise, scan the whole container
649 * (this only for the time being. We need to optimize this.)
651 if ((flags & (OBJ_POINTER | OBJ_KEY))) /* we know hash can handle this case */
652 start = i = c->hash_fn(arg, flags & (OBJ_POINTER | OBJ_KEY)) % c->n_buckets;
653 else /* don't know, let's scan all buckets */
654 start = i = -1; /* XXX this must be fixed later. */
656 /* determine the search boundaries: i..last-1 */
660 } else if ((flags & OBJ_CONTINUE)) {
667 if (!(flags & OBJ_NOLOCK)) {
668 ao2_lock(c); /* avoid modifications to the content */
671 for (; i < last ; i++) {
672 /* scan the list with prev-cur pointers */
673 struct bucket_entry *cur;
675 AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
676 int match = (CMP_MATCH | CMP_STOP);
678 if (type == WITH_DATA) {
679 match &= cb_withdata(EXTERNAL_OBJ(cur->astobj), arg, data, flags);
681 match &= cb_default(EXTERNAL_OBJ(cur->astobj), arg, flags);
684 /* we found the object, performing operations according flags */
685 if (match == 0) { /* no match, no stop, continue */
687 } else if (match == CMP_STOP) { /* no match but stop, we are done */
692 /* we have a match (CMP_MATCH) here */
693 if (!(flags & OBJ_NODATA)) { /* if must return the object, record the value */
694 /* it is important to handle this case before the unlink */
695 ret = EXTERNAL_OBJ(cur->astobj);
696 if (!(flags & (OBJ_UNLINK | OBJ_MULTIPLE))) {
698 __ao2_ref_debug(ret, 1, tag, file, line, funcname);
704 /* If we are in OBJ_MULTIPLE mode and OBJ_NODATE is off,
705 * link the object into the container that will hold the results.
707 if (ret && (multi_container != NULL)) {
709 __ao2_link_debug(multi_container, ret, flags, tag, file, line, funcname);
711 __ao2_link(multi_container, ret, flags);
716 if (flags & OBJ_UNLINK) { /* must unlink */
717 /* we are going to modify the container, so update version */
718 ast_atomic_fetchadd_int(&c->version, 1);
719 AST_LIST_REMOVE_CURRENT(entry);
720 /* update number of elements */
721 ast_atomic_fetchadd_int(&c->elements, -1);
723 /* - When unlinking and not returning the result, (OBJ_NODATA), the ref from the container
724 * must be decremented.
725 * - When unlinking with OBJ_MULTIPLE the ref from the original container
726 * must be decremented regardless if OBJ_NODATA is used. This is because the result is
727 * returned in a new container that already holds its own ref for the object. If the ref
728 * from the original container is not accounted for here a memory leak occurs. */
729 if (flags & (OBJ_NODATA | OBJ_MULTIPLE)) {
731 __ao2_ref_debug(EXTERNAL_OBJ(cur->astobj), -1, tag, file, line, funcname);
733 __ao2_ref(EXTERNAL_OBJ(cur->astobj), -1);
735 ast_free(cur); /* free the link record */
738 if ((match & CMP_STOP) || !(flags & OBJ_MULTIPLE)) {
739 /* We found our only (or last) match, so force an exit from
745 AST_LIST_TRAVERSE_SAFE_END;
751 if (i == c->n_buckets - 1 && (flags & OBJ_POINTER) && (flags & OBJ_CONTINUE)) {
752 /* Move to the beginning to ensure we check every bucket */
758 if (!(flags & OBJ_NOLOCK)) {
762 /* if multi_container was created, we are returning multiple objects */
763 if (multi_container != NULL) {
764 *multi_iterator = ao2_iterator_init(multi_container,
765 AO2_ITERATOR_DONTLOCK | AO2_ITERATOR_UNLINK | AO2_ITERATOR_MALLOCD);
766 ao2_ref(multi_container, -1);
767 return multi_iterator;
773 void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags,
774 ao2_callback_fn *cb_fn, void *arg, char *tag, char *file, int line,
775 const char *funcname)
777 return internal_ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, tag, file, line, funcname);
780 void *__ao2_callback(struct ao2_container *c, enum search_flags flags,
781 ao2_callback_fn *cb_fn, void *arg)
783 return internal_ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, NULL, NULL, 0, NULL);
786 void *__ao2_callback_data_debug(struct ao2_container *c,
787 const enum search_flags flags,
788 ao2_callback_data_fn *cb_fn, void *arg, void *data,
789 char *tag, char *file, int line, const char *funcname)
791 return internal_ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, tag, file, line, funcname);
794 void *__ao2_callback_data(struct ao2_container *c, const enum search_flags flags,
795 ao2_callback_data_fn *cb_fn, void *arg, void *data)
797 return internal_ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, NULL, NULL, 0, NULL);
801 * the find function just invokes the default callback with some reasonable flags.
803 void *__ao2_find_debug(struct ao2_container *c, const void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
805 void *arged = (void *) arg;/* Done to avoid compiler const warning */
807 return __ao2_callback_debug(c, flags, c->cmp_fn, arged, tag, file, line, funcname);
810 void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags)
812 void *arged = (void *) arg;/* Done to avoid compiler const warning */
814 return __ao2_callback(c, flags, c->cmp_fn, arged);
818 * initialize an iterator so we start from the first object
820 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
822 struct ao2_iterator a = {
833 * destroy an iterator
835 void ao2_iterator_destroy(struct ao2_iterator *i)
838 if (i->flags & AO2_ITERATOR_MALLOCD) {
846 * move to the next element in the container.
848 static void *internal_ao2_iterator_next(struct ao2_iterator *a, struct bucket_entry **q)
851 struct bucket_entry *p = NULL;
856 if (INTERNAL_OBJ(a->c) == NULL)
859 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
862 /* optimization. If the container is unchanged and
863 * we have a pointer, try follow it
865 if (a->c->version == a->c_version && (p = a->obj)) {
866 if ((p = AST_LIST_NEXT(p, entry)))
868 /* nope, start from the next bucket */
874 lim = a->c->n_buckets;
876 /* Browse the buckets array, moving to the next
877 * buckets if we don't find the entry in the current one.
878 * Stop when we find an element with version number greater
879 * than the current one (we reset the version to 0 when we
882 for (; a->bucket < lim; a->bucket++, a->version = 0) {
883 /* scan the current bucket */
884 AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
885 if (p->version > a->version)
892 ret = EXTERNAL_OBJ(p->astobj);
893 if (a->flags & AO2_ITERATOR_UNLINK) {
894 /* we are going to modify the container, so update version */
895 ast_atomic_fetchadd_int(&a->c->version, 1);
896 AST_LIST_REMOVE(&a->c->buckets[a->bucket], p, entry);
897 /* update number of elements */
898 ast_atomic_fetchadd_int(&a->c->elements, -1);
901 a->c_version = a->c->version;
904 a->version = p->version;
906 a->c_version = a->c->version;
907 /* inc refcount of returned object */
915 void *__ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname)
917 struct bucket_entry *p;
920 ret = internal_ao2_iterator_next(a, &p);
923 /* inc refcount of returned object */
924 __ao2_ref_debug(ret, 1, tag, file, line, funcname);
927 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
933 void *__ao2_iterator_next(struct ao2_iterator *a)
935 struct bucket_entry *p = NULL;
938 ret = internal_ao2_iterator_next(a, &p);
941 /* inc refcount of returned object */
945 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
951 /* callback for destroying container.
952 * we can make it simple as we know what it does
954 static int cd_cb(void *obj, void *arg, int flag)
960 static int cd_cb_debug(void *obj, void *arg, int flag)
962 __ao2_ref_debug(obj, -1, "deref object via container destroy", __FILE__, __LINE__, __PRETTY_FUNCTION__);
966 static void container_destruct(void *_c)
968 struct ao2_container *c = _c;
971 __ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
973 for (i = 0; i < c->n_buckets; i++) {
974 struct bucket_entry *current;
976 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
982 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
986 static void container_destruct_debug(void *_c)
988 struct ao2_container *c = _c;
991 __ao2_callback_debug(c, OBJ_UNLINK, cd_cb_debug, NULL, "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__);
993 for (i = 0; i < c->n_buckets; i++) {
994 struct bucket_entry *current;
996 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
1002 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
1007 static int print_cb(void *obj, void *arg, int flag)
1009 struct ast_cli_args *a = (struct ast_cli_args *) arg;
1010 char *s = (char *)obj;
1012 ast_cli(a->fd, "string <%s>\n", s);
1019 static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1023 e->command = "astobj2 show stats";
1024 e->usage = "Usage: astobj2 show stats\n"
1025 " Show astobj2 show stats\n";
1030 ast_cli(a->fd, "Objects : %d\n", ao2.total_objects);
1031 ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
1032 ast_cli(a->fd, "Memory : %d\n", ao2.total_mem);
1033 ast_cli(a->fd, "Locked : %d\n", ao2.total_locked);
1034 ast_cli(a->fd, "Refs : %d\n", ao2.total_refs);
1039 * This is testing code for astobj
1041 static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1043 struct ao2_container *c1;
1046 static int prof_id = -1;
1047 struct ast_cli_args fake_args = { a->fd, 0, NULL };
1051 e->command = "astobj2 test";
1052 e->usage = "Usage: astobj2 test <num>\n"
1053 " Runs astobj2 test. Creates 'num' objects,\n"
1054 " and test iterators, callbacks and may be other stuff\n";
1061 return CLI_SHOWUSAGE;
1065 prof_id = ast_add_profile("ao2_alloc", 0);
1067 ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
1068 lim = atoi(a->argv[2]);
1069 ast_cli(a->fd, "called astobj_test\n");
1071 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
1073 * allocate a container with no default callback, and no hash function.
1074 * No hash means everything goes in the same bucket.
1076 c1 = ao2_t_container_alloc(100, NULL /* no callback */, NULL /* no hash */,"test");
1077 ast_cli(a->fd, "container allocated as %p\n", c1);
1080 * fill the container with objects.
1081 * ao2_alloc() gives us a reference which we pass to the
1082 * container when we do the insert.
1084 for (i = 0; i < lim; i++) {
1085 ast_mark(prof_id, 1 /* start */);
1086 obj = ao2_t_alloc(80, NULL,"test");
1087 ast_mark(prof_id, 0 /* stop */);
1088 ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
1089 sprintf(obj, "-- this is obj %d --", i);
1091 /* At this point, the refcount on obj is 2 due to the allocation
1092 * and linking. We can go ahead and reduce the refcount by 1
1093 * right here so that when the container is unreffed later, the
1094 * objects will be freed
1096 ao2_t_ref(obj, -1, "test");
1098 ast_cli(a->fd, "testing callbacks\n");
1099 ao2_t_callback(c1, 0, print_cb, a, "test callback");
1100 ast_cli(a->fd, "testing iterators, remove every second object\n");
1102 struct ao2_iterator ai;
1105 ai = ao2_iterator_init(c1, 0);
1106 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
1107 ast_cli(a->fd, "iterator on <%s>\n", obj);
1109 ao2_t_unlink(c1, obj,"test");
1110 ao2_t_ref(obj, -1,"test");
1112 ao2_iterator_destroy(&ai);
1113 ast_cli(a->fd, "testing iterators again\n");
1114 ai = ao2_iterator_init(c1, 0);
1115 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
1116 ast_cli(a->fd, "iterator on <%s>\n", obj);
1117 ao2_t_ref(obj, -1,"test");
1119 ao2_iterator_destroy(&ai);
1121 ast_cli(a->fd, "testing callbacks again\n");
1122 ao2_t_callback(c1, 0, print_cb, a, "test callback");
1124 ast_verbose("now you should see an error message:\n");
1125 ao2_t_ref(&i, -1, ""); /* i is not a valid object so we print an error here */
1127 ast_cli(a->fd, "destroy container\n");
1128 ao2_t_ref(c1, -1, ""); /* destroy container */
1129 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
1133 static struct ast_cli_entry cli_astobj2[] = {
1134 AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
1135 AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
1137 #endif /* AO2_DEBUG */
1139 int astobj2_init(void)
1142 ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));