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 = abs(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;
612 struct ao2_container *multi_container = NULL;
613 struct ao2_iterator *multi_iterator = NULL;
615 if (INTERNAL_OBJ(c) == NULL) /* safety check on the argument */
619 * This logic is used so we can support OBJ_MULTIPLE with OBJ_NODATA
620 * turned off. This if statement checks for the special condition
621 * where multiple items may need to be returned.
623 if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
624 /* we need to return an ao2_iterator with the results,
625 * as there could be more than one. the iterator will
626 * hold the only reference to a container that has all the
627 * matching objects linked into it, so when the iterator
628 * is destroyed, the container will be automatically
631 if (!(multi_container = __ao2_container_alloc(1, NULL, NULL))) {
634 if (!(multi_iterator = ast_calloc(1, sizeof(*multi_iterator)))) {
635 ao2_ref(multi_container, -1);
640 /* override the match function if necessary */
641 if (cb_fn == NULL) { /* if NULL, match everything */
642 if (type == WITH_DATA) {
643 cb_withdata = cb_true_data;
645 cb_default = cb_true;
648 /* We do this here to avoid the per object casting penalty (even though
649 that is probably optimized away anyway). */
650 if (type == WITH_DATA) {
658 * XXX this can be optimized.
659 * If we have a hash function and lookup by pointer,
660 * run the hash function. Otherwise, scan the whole container
661 * (this only for the time being. We need to optimize this.)
663 if ((flags & OBJ_POINTER)) /* we know hash can handle this case */
664 start = i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
665 else /* don't know, let's scan all buckets */
666 start = i = -1; /* XXX this must be fixed later. */
668 /* determine the search boundaries: i..last-1 */
672 } else if ((flags & OBJ_CONTINUE)) {
678 ao2_lock(c); /* avoid modifications to the content */
680 for (; i < last ; i++) {
681 /* scan the list with prev-cur pointers */
682 struct bucket_list *cur;
684 AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
685 int match = (CMP_MATCH | CMP_STOP);
687 if (type == WITH_DATA) {
688 match &= cb_withdata(EXTERNAL_OBJ(cur->astobj), arg, data, flags);
690 match &= cb_default(EXTERNAL_OBJ(cur->astobj), arg, flags);
693 /* we found the object, performing operations according flags */
694 if (match == 0) { /* no match, no stop, continue */
696 } else if (match == CMP_STOP) { /* no match but stop, we are done */
701 /* we have a match (CMP_MATCH) here */
702 if (!(flags & OBJ_NODATA)) { /* if must return the object, record the value */
703 /* it is important to handle this case before the unlink */
704 ret = EXTERNAL_OBJ(cur->astobj);
705 if (!(flags & (OBJ_UNLINK | OBJ_MULTIPLE))) {
707 __ao2_ref_debug(ret, 1, tag, file, line, funcname);
713 /* If we are in OBJ_MULTIPLE mode and OBJ_NODATE is off,
714 * link the object into the container that will hold the results.
716 if (ret && (multi_container != NULL)) {
717 __ao2_link(multi_container, ret);
721 if (flags & OBJ_UNLINK) { /* must unlink */
722 /* we are going to modify the container, so update version */
723 ast_atomic_fetchadd_int(&c->version, 1);
724 AST_LIST_REMOVE_CURRENT(entry);
725 /* update number of elements */
726 ast_atomic_fetchadd_int(&c->elements, -1);
728 /* - When unlinking and not returning the result, (OBJ_NODATA), the ref from the container
729 * must be decremented.
730 * - When unlinking with OBJ_MULTIPLE the ref from the original container
731 * must be decremented regardless if OBJ_NODATA is used. This is because the result is
732 * returned in a new container that already holds its own ref for the object. If the ref
733 * from the original container is not accounted for here a memory leak occurs. */
734 if (flags & (OBJ_NODATA | OBJ_MULTIPLE)) {
736 __ao2_ref_debug(EXTERNAL_OBJ(cur->astobj), -1, tag, file, line, funcname);
738 __ao2_ref(EXTERNAL_OBJ(cur->astobj), -1);
740 ast_free(cur); /* free the link record */
743 if ((match & CMP_STOP) || !(flags & OBJ_MULTIPLE)) {
744 /* We found our only (or last) match, so force an exit from
750 AST_LIST_TRAVERSE_SAFE_END;
756 if (i == c->n_buckets - 1 && (flags & OBJ_POINTER) && (flags & OBJ_CONTINUE)) {
757 /* Move to the beginning to ensure we check every bucket */
764 /* if multi_container was created, we are returning multiple objects */
765 if (multi_container != NULL) {
766 *multi_iterator = ao2_iterator_init(multi_container,
767 AO2_ITERATOR_DONTLOCK | AO2_ITERATOR_UNLINK | AO2_ITERATOR_MALLOCD);
768 ao2_ref(multi_container, -1);
769 return multi_iterator;
775 void *__ao2_callback_debug(struct ao2_container *c,
776 const enum search_flags flags,
777 ao2_callback_fn *cb_fn, void *arg,
778 char *tag, char *file, int line, const char *funcname)
780 return internal_ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, tag, file, line, funcname);
783 void *__ao2_callback(struct ao2_container *c, const enum search_flags flags,
784 ao2_callback_fn *cb_fn, void *arg)
786 return internal_ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, NULL, NULL, 0, NULL);
789 void *__ao2_callback_data_debug(struct ao2_container *c,
790 const enum search_flags flags,
791 ao2_callback_data_fn *cb_fn, void *arg, void *data,
792 char *tag, char *file, int line, const char *funcname)
794 return internal_ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, tag, file, line, funcname);
797 void *__ao2_callback_data(struct ao2_container *c, const enum search_flags flags,
798 ao2_callback_data_fn *cb_fn, void *arg, void *data)
800 return internal_ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, NULL, NULL, 0, NULL);
804 * the find function just invokes the default callback with some reasonable flags.
806 void *__ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
808 return __ao2_callback_debug(c, flags, c->cmp_fn, arg, tag, file, line, funcname);
811 void *__ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
813 return __ao2_callback(c, flags, c->cmp_fn, arg);
817 * initialize an iterator so we start from the first object
819 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
821 struct ao2_iterator a = {
832 * destroy an iterator
834 void ao2_iterator_destroy(struct ao2_iterator *i)
837 if (i->flags & AO2_ITERATOR_MALLOCD) {
845 * move to the next element in the container.
847 static void *internal_ao2_iterator_next(struct ao2_iterator *a, struct bucket_list **q)
850 struct bucket_list *p = NULL;
855 if (INTERNAL_OBJ(a->c) == NULL)
858 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
861 /* optimization. If the container is unchanged and
862 * we have a pointer, try follow it
864 if (a->c->version == a->c_version && (p = a->obj)) {
865 if ((p = AST_LIST_NEXT(p, entry)))
867 /* nope, start from the next bucket */
873 lim = a->c->n_buckets;
875 /* Browse the buckets array, moving to the next
876 * buckets if we don't find the entry in the current one.
877 * Stop when we find an element with version number greater
878 * than the current one (we reset the version to 0 when we
881 for (; a->bucket < lim; a->bucket++, a->version = 0) {
882 /* scan the current bucket */
883 AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
884 if (p->version > a->version)
891 ret = EXTERNAL_OBJ(p->astobj);
892 if (a->flags & AO2_ITERATOR_UNLINK) {
893 /* we are going to modify the container, so update version */
894 ast_atomic_fetchadd_int(&a->c->version, 1);
895 AST_LIST_REMOVE(&a->c->buckets[a->bucket], p, entry);
896 /* update number of elements */
897 ast_atomic_fetchadd_int(&a->c->elements, -1);
900 a->c_version = a->c->version;
903 a->version = p->version;
905 a->c_version = a->c->version;
906 /* inc refcount of returned object */
914 void *__ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname)
916 struct bucket_list *p;
919 ret = internal_ao2_iterator_next(a, &p);
922 /* inc refcount of returned object */
923 __ao2_ref_debug(ret, 1, tag, file, line, funcname);
926 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
932 void *__ao2_iterator_next(struct ao2_iterator *a)
934 struct bucket_list *p = NULL;
937 ret = internal_ao2_iterator_next(a, &p);
940 /* inc refcount of returned object */
944 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
950 /* callback for destroying container.
951 * we can make it simple as we know what it does
953 static int cd_cb(void *obj, void *arg, int flag)
959 static int cd_cb_debug(void *obj, void *arg, int flag)
961 __ao2_ref_debug(obj, -1, "deref object via container destroy", __FILE__, __LINE__, __PRETTY_FUNCTION__);
965 static void container_destruct(void *_c)
967 struct ao2_container *c = _c;
970 __ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
972 for (i = 0; i < c->n_buckets; i++) {
973 struct bucket_list *current;
975 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
981 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
985 static void container_destruct_debug(void *_c)
987 struct ao2_container *c = _c;
990 __ao2_callback_debug(c, OBJ_UNLINK, cd_cb_debug, NULL, "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__);
992 for (i = 0; i < c->n_buckets; i++) {
993 struct bucket_list *current;
995 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
1001 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
1006 static int print_cb(void *obj, void *arg, int flag)
1008 struct ast_cli_args *a = (struct ast_cli_args *) arg;
1009 char *s = (char *)obj;
1011 ast_cli(a->fd, "string <%s>\n", s);
1018 static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1022 e->command = "astobj2 show stats";
1023 e->usage = "Usage: astobj2 show stats\n"
1024 " Show astobj2 show stats\n";
1029 ast_cli(a->fd, "Objects : %d\n", ao2.total_objects);
1030 ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
1031 ast_cli(a->fd, "Memory : %d\n", ao2.total_mem);
1032 ast_cli(a->fd, "Locked : %d\n", ao2.total_locked);
1033 ast_cli(a->fd, "Refs : %d\n", ao2.total_refs);
1038 * This is testing code for astobj
1040 static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1042 struct ao2_container *c1;
1045 static int prof_id = -1;
1046 struct ast_cli_args fake_args = { a->fd, 0, NULL };
1050 e->command = "astobj2 test";
1051 e->usage = "Usage: astobj2 test <num>\n"
1052 " Runs astobj2 test. Creates 'num' objects,\n"
1053 " and test iterators, callbacks and may be other stuff\n";
1060 return CLI_SHOWUSAGE;
1064 prof_id = ast_add_profile("ao2_alloc", 0);
1066 ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
1067 lim = atoi(a->argv[2]);
1068 ast_cli(a->fd, "called astobj_test\n");
1070 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
1072 * allocate a container with no default callback, and no hash function.
1073 * No hash means everything goes in the same bucket.
1075 c1 = ao2_t_container_alloc(100, NULL /* no callback */, NULL /* no hash */,"test");
1076 ast_cli(a->fd, "container allocated as %p\n", c1);
1079 * fill the container with objects.
1080 * ao2_alloc() gives us a reference which we pass to the
1081 * container when we do the insert.
1083 for (i = 0; i < lim; i++) {
1084 ast_mark(prof_id, 1 /* start */);
1085 obj = ao2_t_alloc(80, NULL,"test");
1086 ast_mark(prof_id, 0 /* stop */);
1087 ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
1088 sprintf(obj, "-- this is obj %d --", i);
1090 /* At this point, the refcount on obj is 2 due to the allocation
1091 * and linking. We can go ahead and reduce the refcount by 1
1092 * right here so that when the container is unreffed later, the
1093 * objects will be freed
1095 ao2_t_ref(obj, -1, "test");
1097 ast_cli(a->fd, "testing callbacks\n");
1098 ao2_t_callback(c1, 0, print_cb, a, "test callback");
1099 ast_cli(a->fd, "testing iterators, remove every second object\n");
1101 struct ao2_iterator ai;
1104 ai = ao2_iterator_init(c1, 0);
1105 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
1106 ast_cli(a->fd, "iterator on <%s>\n", obj);
1108 ao2_t_unlink(c1, obj,"test");
1109 ao2_t_ref(obj, -1,"test");
1111 ao2_iterator_destroy(&ai);
1112 ast_cli(a->fd, "testing iterators again\n");
1113 ai = ao2_iterator_init(c1, 0);
1114 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
1115 ast_cli(a->fd, "iterator on <%s>\n", obj);
1116 ao2_t_ref(obj, -1,"test");
1118 ao2_iterator_destroy(&ai);
1120 ast_cli(a->fd, "testing callbacks again\n");
1121 ao2_t_callback(c1, 0, print_cb, a, "test callback");
1123 ast_verbose("now you should see an error message:\n");
1124 ao2_t_ref(&i, -1, ""); /* i is not a valid object so we print an error here */
1126 ast_cli(a->fd, "destroy container\n");
1127 ao2_t_ref(c1, -1, ""); /* destroy container */
1128 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
1132 static struct ast_cli_entry cli_astobj2[] = {
1133 AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
1134 AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
1136 #endif /* AO2_DEBUG */
1138 int astobj2_init(void)
1141 ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));