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.
19 * \brief Functions implementing astobj2 objects.
21 * \author Richard Mudgett <rmudgett@digium.com>
25 <support_level>core</support_level>
30 ASTERISK_REGISTER_FILE()
32 #include "asterisk/_private.h"
33 #include "asterisk/astobj2.h"
34 #include "astobj2_private.h"
35 #include "astobj2_container_private.h"
36 #include "asterisk/cli.h"
37 #include "asterisk/paths.h"
39 /* Use ast_log_safe in place of ast_log. */
40 #define ast_log ast_log_safe
45 * astobj2 objects are always preceded by this data structure,
46 * which contains a reference counter,
47 * option flags and a pointer to a destructor.
48 * The refcount is used to decide when it is time to
49 * invoke the destructor.
50 * The magic number is used for consistency check.
54 ao2_destructor_fn destructor_fn;
55 /*! This field is used for astobj2 and ao2_weakproxy objects to reference each other */
57 /*! User data size for stats */
59 /*! The ao2 object option flags */
61 /*! magic number. This is used to verify that a pointer passed in is a
62 * valid astobj2 or ao2_weak reference */
66 #define AO2_MAGIC 0xa570b123
67 #define AO2_WEAK 0xa570b122
68 #define IS_AO2_MAGIC_BAD(p) (AO2_MAGIC != (p->priv_data.magic | 1))
71 * What an astobj2 object looks like: fixed-size private data
72 * followed by variable-size user data.
75 struct __priv_data priv_data;
79 struct ao2_weakproxy_notification {
80 ao2_weakproxy_notification_cb cb;
82 AST_LIST_ENTRY(ao2_weakproxy_notification) list;
85 static void weakproxy_run_callbacks(struct ao2_weakproxy *weakproxy);
87 struct ao2_lock_priv {
91 /* AstObj2 with recursive lock. */
93 struct ao2_lock_priv mutex;
94 struct __priv_data priv_data;
98 struct ao2_rwlock_priv {
100 /*! Count of the number of threads holding a lock on this object. -1 if it is the write lock. */
104 /* AstObj2 with RW lock. */
105 struct astobj2_rwlock {
106 struct ao2_rwlock_priv rwlock;
107 struct __priv_data priv_data;
112 struct ao2_stats ao2;
115 #define INTERNAL_OBJ_MUTEX(user_data) \
116 ((struct astobj2_lock *) (((char *) (user_data)) - sizeof(struct astobj2_lock)))
118 #define INTERNAL_OBJ_RWLOCK(user_data) \
119 ((struct astobj2_rwlock *) (((char *) (user_data)) - sizeof(struct astobj2_rwlock)))
121 #define INTERNAL_OBJ(user_data) \
122 (struct astobj2 *) ((char *) user_data - sizeof(struct astobj2))
125 * \brief convert from a pointer _p to a user-defined object
127 * \return the pointer to the astobj2 structure
129 #define __INTERNAL_OBJ_CHECK(user_data, file, line, func) \
131 struct astobj2 *p ## __LINE__; \
133 || !(p ## __LINE__ = INTERNAL_OBJ(user_data)) \
134 || IS_AO2_MAGIC_BAD(p ## __LINE__)) { \
135 log_bad_ao2(user_data, file, line, func); \
136 p ## __LINE__ = NULL; \
141 #define INTERNAL_OBJ_CHECK(user_data) \
142 __INTERNAL_OBJ_CHECK(user_data, __FILE__, __LINE__, __PRETTY_FUNCTION__)
145 * \brief convert from a pointer _p to an astobj2 object
147 * \return the pointer to the user-defined portion.
149 #define EXTERNAL_OBJ(_p) ((_p) == NULL ? NULL : (_p)->user_data)
151 int internal_is_ao2_object(void *user_data)
159 p = INTERNAL_OBJ(user_data);
161 return !p || IS_AO2_MAGIC_BAD(p) ? 0 : 1;
164 void log_bad_ao2(void *user_data, const char *file, int line, const char *func)
169 ast_log(__LOG_ERROR, file, line, func, "user_data is NULL\n");
173 p = INTERNAL_OBJ(user_data);
174 if (p->priv_data.magic) {
175 ast_log(__LOG_ERROR, file, line, func,
176 "bad magic number 0x%x for object %p\n",
177 p->priv_data.magic, user_data);
179 ast_log(__LOG_ERROR, file, line, func,
180 "bad magic number for object %p. Object is likely destroyed.\n",
185 int __ao2_lock(void *user_data, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var)
187 struct astobj2 *obj = __INTERNAL_OBJ_CHECK(user_data, file, line, func);
188 struct astobj2_lock *obj_mutex;
189 struct astobj2_rwlock *obj_rwlock;
197 switch (obj->priv_data.options & AO2_ALLOC_OPT_LOCK_MASK) {
198 case AO2_ALLOC_OPT_LOCK_MUTEX:
199 obj_mutex = INTERNAL_OBJ_MUTEX(user_data);
200 res = __ast_pthread_mutex_lock(file, line, func, var, &obj_mutex->mutex.lock);
203 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
207 case AO2_ALLOC_OPT_LOCK_RWLOCK:
208 obj_rwlock = INTERNAL_OBJ_RWLOCK(user_data);
210 case AO2_LOCK_REQ_MUTEX:
211 case AO2_LOCK_REQ_WRLOCK:
212 res = __ast_rwlock_wrlock(file, line, func, &obj_rwlock->rwlock.lock, var);
214 ast_atomic_fetchadd_int(&obj_rwlock->rwlock.num_lockers, -1);
216 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
220 case AO2_LOCK_REQ_RDLOCK:
221 res = __ast_rwlock_rdlock(file, line, func, &obj_rwlock->rwlock.lock, var);
223 ast_atomic_fetchadd_int(&obj_rwlock->rwlock.num_lockers, +1);
225 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
231 case AO2_ALLOC_OPT_LOCK_NOLOCK:
232 /* The ao2 object has no lock. */
235 ast_log(__LOG_ERROR, file, line, func, "Invalid lock option on ao2 object %p\n",
243 int __ao2_unlock(void *user_data, const char *file, const char *func, int line, const char *var)
245 struct astobj2 *obj = __INTERNAL_OBJ_CHECK(user_data, file, line, func);
246 struct astobj2_lock *obj_mutex;
247 struct astobj2_rwlock *obj_rwlock;
256 switch (obj->priv_data.options & AO2_ALLOC_OPT_LOCK_MASK) {
257 case AO2_ALLOC_OPT_LOCK_MUTEX:
258 obj_mutex = INTERNAL_OBJ_MUTEX(user_data);
259 res = __ast_pthread_mutex_unlock(file, line, func, var, &obj_mutex->mutex.lock);
262 ast_atomic_fetchadd_int(&ao2.total_locked, -1);
266 case AO2_ALLOC_OPT_LOCK_RWLOCK:
267 obj_rwlock = INTERNAL_OBJ_RWLOCK(user_data);
269 current_value = ast_atomic_fetchadd_int(&obj_rwlock->rwlock.num_lockers, -1) - 1;
270 if (current_value < 0) {
271 /* It was a WRLOCK that we are unlocking. Fix the count. */
272 ast_atomic_fetchadd_int(&obj_rwlock->rwlock.num_lockers, -current_value);
274 res = __ast_rwlock_unlock(file, line, func, &obj_rwlock->rwlock.lock, var);
277 ast_atomic_fetchadd_int(&ao2.total_locked, -1);
281 case AO2_ALLOC_OPT_LOCK_NOLOCK:
282 /* The ao2 object has no lock. */
285 ast_log(__LOG_ERROR, file, line, func, "Invalid lock option on ao2 object %p\n",
293 int __ao2_trylock(void *user_data, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var)
295 struct astobj2 *obj = __INTERNAL_OBJ_CHECK(user_data, file, line, func);
296 struct astobj2_lock *obj_mutex;
297 struct astobj2_rwlock *obj_rwlock;
305 switch (obj->priv_data.options & AO2_ALLOC_OPT_LOCK_MASK) {
306 case AO2_ALLOC_OPT_LOCK_MUTEX:
307 obj_mutex = INTERNAL_OBJ_MUTEX(user_data);
308 res = __ast_pthread_mutex_trylock(file, line, func, var, &obj_mutex->mutex.lock);
311 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
315 case AO2_ALLOC_OPT_LOCK_RWLOCK:
316 obj_rwlock = INTERNAL_OBJ_RWLOCK(user_data);
318 case AO2_LOCK_REQ_MUTEX:
319 case AO2_LOCK_REQ_WRLOCK:
320 res = __ast_rwlock_trywrlock(file, line, func, &obj_rwlock->rwlock.lock, var);
322 ast_atomic_fetchadd_int(&obj_rwlock->rwlock.num_lockers, -1);
324 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
328 case AO2_LOCK_REQ_RDLOCK:
329 res = __ast_rwlock_tryrdlock(file, line, func, &obj_rwlock->rwlock.lock, var);
331 ast_atomic_fetchadd_int(&obj_rwlock->rwlock.num_lockers, +1);
333 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
339 case AO2_ALLOC_OPT_LOCK_NOLOCK:
340 /* The ao2 object has no lock. */
343 ast_log(__LOG_ERROR, file, line, func, "Invalid lock option on ao2 object %p\n",
354 * \brief Adjust an object's lock to the requested level.
356 * \param user_data An ao2 object to adjust lock level.
357 * \param lock_how What level to adjust lock.
358 * \param keep_stronger TRUE if keep original lock level if it is stronger.
360 * \pre The ao2 object is already locked.
363 * An ao2 object with a RWLOCK will have its lock level adjusted
364 * to the specified level if it is not already there. An ao2
365 * object with a different type of lock is not affected.
367 * \return Original lock level.
369 enum ao2_lock_req __adjust_lock(void *user_data, enum ao2_lock_req lock_how, int keep_stronger)
371 struct astobj2 *obj = INTERNAL_OBJ(user_data);
372 struct astobj2_rwlock *obj_rwlock;
373 enum ao2_lock_req orig_lock;
375 switch (obj->priv_data.options & AO2_ALLOC_OPT_LOCK_MASK) {
376 case AO2_ALLOC_OPT_LOCK_RWLOCK:
377 obj_rwlock = INTERNAL_OBJ_RWLOCK(user_data);
378 if (obj_rwlock->rwlock.num_lockers < 0) {
379 orig_lock = AO2_LOCK_REQ_WRLOCK;
381 orig_lock = AO2_LOCK_REQ_RDLOCK;
384 case AO2_LOCK_REQ_MUTEX:
385 lock_how = AO2_LOCK_REQ_WRLOCK;
387 case AO2_LOCK_REQ_WRLOCK:
388 if (lock_how != orig_lock) {
389 /* Switch from read lock to write lock. */
390 ao2_unlock(user_data);
391 ao2_wrlock(user_data);
394 case AO2_LOCK_REQ_RDLOCK:
395 if (!keep_stronger && lock_how != orig_lock) {
396 /* Switch from write lock to read lock. */
397 ao2_unlock(user_data);
398 ao2_rdlock(user_data);
404 ast_log(LOG_ERROR, "Invalid lock option on ao2 object %p\n", user_data);
406 case AO2_ALLOC_OPT_LOCK_NOLOCK:
407 case AO2_ALLOC_OPT_LOCK_MUTEX:
408 orig_lock = AO2_LOCK_REQ_MUTEX;
415 void *ao2_object_get_lockaddr(void *user_data)
418 struct astobj2_lock *obj_mutex;
420 obj = INTERNAL_OBJ_CHECK(user_data);
427 switch (obj->priv_data.options & AO2_ALLOC_OPT_LOCK_MASK) {
428 case AO2_ALLOC_OPT_LOCK_MUTEX:
429 obj_mutex = INTERNAL_OBJ_MUTEX(user_data);
430 return &obj_mutex->mutex.lock;
438 int __ao2_ref(void *user_data, int delta,
439 const char *tag, const char *file, int line, const char *func)
441 struct astobj2 *obj = __INTERNAL_OBJ_CHECK(user_data, file, line, func);
442 struct astobj2_lock *obj_mutex;
443 struct astobj2_rwlock *obj_rwlock;
446 void *weakproxy = NULL;
449 if (ref_log && user_data) {
450 fprintf(ref_log, "%p,%d,%d,%s,%d,%s,**invalid**,%s\n",
451 user_data, delta, ast_get_tid(), file, line, func, tag ?: "");
458 /* if delta is 0, just return the refcount */
460 return obj->priv_data.ref_counter;
463 if (delta < 0 && obj->priv_data.magic == AO2_MAGIC && (weakproxy = obj->priv_data.weakptr)) {
467 /* we modify with an atomic operation the reference counter */
468 ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
469 current_value = ret + delta;
472 ast_atomic_fetchadd_int(&ao2.total_refs, delta);
476 if (current_value == 1) {
477 /* The only remaining reference is the one owned by the weak object */
478 struct astobj2 *internal_weakproxy;
480 internal_weakproxy = INTERNAL_OBJ_CHECK(weakproxy);
482 /* Unlink the obj from the weak proxy */
483 internal_weakproxy->priv_data.weakptr = NULL;
484 obj->priv_data.weakptr = NULL;
486 /* Notify the subscribers that weakproxy now points to NULL. */
487 weakproxy_run_callbacks(weakproxy);
489 /* weak is already unlinked from obj so this won't recurse */
490 ao2_ref(user_data, -1);
493 ao2_unlock(weakproxy);
495 if (current_value == 1) {
496 ao2_ref(weakproxy, -1);
500 if (0 < current_value) {
501 /* The object still lives. */
502 if (ref_log && tag) {
503 fprintf(ref_log, "%p,%s%d,%d,%s,%d,%s,%d,%s\n", user_data,
504 (delta < 0 ? "" : "+"), delta, ast_get_tid(),
505 file, line, func, ret, tag);
511 /* this case must never happen */
512 if (current_value < 0) {
513 ast_log(__LOG_ERROR, file, line, func,
514 "Invalid refcount %d on ao2 object %p\n", current_value, user_data);
516 /* Log to ref_log invalid even if (tag == NULL) */
517 fprintf(ref_log, "%p,%d,%d,%s,%d,%s,**invalid**,%s\n",
518 user_data, delta, ast_get_tid(), file, line, func, tag ?: "");
522 /* stop here even if assert doesn't DO_CRASH */
526 /* last reference, destroy the object */
527 if (obj->priv_data.destructor_fn != NULL) {
528 obj->priv_data.destructor_fn(user_data);
532 ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
533 ast_atomic_fetchadd_int(&ao2.total_objects, -1);
536 /* In case someone uses an object after it's been freed */
537 obj->priv_data.magic = 0;
539 switch (obj->priv_data.options & AO2_ALLOC_OPT_LOCK_MASK) {
540 case AO2_ALLOC_OPT_LOCK_MUTEX:
541 obj_mutex = INTERNAL_OBJ_MUTEX(user_data);
542 ast_mutex_destroy(&obj_mutex->mutex.lock);
546 case AO2_ALLOC_OPT_LOCK_RWLOCK:
547 obj_rwlock = INTERNAL_OBJ_RWLOCK(user_data);
548 ast_rwlock_destroy(&obj_rwlock->rwlock.lock);
550 ast_free(obj_rwlock);
552 case AO2_ALLOC_OPT_LOCK_NOLOCK:
556 ast_log(__LOG_ERROR, file, line, func,
557 "Invalid lock option on ao2 object %p\n", user_data);
561 if (ref_log && tag) {
562 fprintf(ref_log, "%p,%d,%d,%s,%d,%s,**destructor**,%s\n",
563 user_data, delta, ast_get_tid(), file, line, func, tag);
570 void __ao2_cleanup_debug(void *obj, const char *tag, const char *file, int line, const char *function)
573 __ao2_ref(obj, -1, tag, file, line, function);
577 void __ao2_cleanup(void *obj)
584 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options,
585 const char *tag, const char *file, int line, const char *func)
589 struct astobj2_lock *obj_mutex;
590 struct astobj2_rwlock *obj_rwlock;
592 switch (options & AO2_ALLOC_OPT_LOCK_MASK) {
593 case AO2_ALLOC_OPT_LOCK_MUTEX:
594 #if defined(__AST_DEBUG_MALLOC)
595 obj_mutex = __ast_calloc(1, sizeof(*obj_mutex) + data_size, file, line, func);
597 obj_mutex = ast_calloc(1, sizeof(*obj_mutex) + data_size);
599 if (obj_mutex == NULL) {
603 ast_mutex_init(&obj_mutex->mutex.lock);
604 obj = (struct astobj2 *) &obj_mutex->priv_data;
606 case AO2_ALLOC_OPT_LOCK_RWLOCK:
607 #if defined(__AST_DEBUG_MALLOC)
608 obj_rwlock = __ast_calloc(1, sizeof(*obj_rwlock) + data_size, file, line, func);
610 obj_rwlock = ast_calloc(1, sizeof(*obj_rwlock) + data_size);
612 if (obj_rwlock == NULL) {
616 ast_rwlock_init(&obj_rwlock->rwlock.lock);
617 obj = (struct astobj2 *) &obj_rwlock->priv_data;
619 case AO2_ALLOC_OPT_LOCK_NOLOCK:
620 #if defined(__AST_DEBUG_MALLOC)
621 obj = __ast_calloc(1, sizeof(*obj) + data_size, file, line, func);
623 obj = ast_calloc(1, sizeof(*obj) + data_size);
630 /* Invalid option value. */
631 ast_log(__LOG_DEBUG, file, line, func, "Invalid lock option requested\n");
635 /* Initialize common ao2 values. */
636 obj->priv_data.ref_counter = 1;
637 obj->priv_data.destructor_fn = destructor_fn; /* can be NULL */
638 obj->priv_data.data_size = data_size;
639 obj->priv_data.options = options;
640 obj->priv_data.magic = AO2_MAGIC;
643 ast_atomic_fetchadd_int(&ao2.total_objects, 1);
644 ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
645 ast_atomic_fetchadd_int(&ao2.total_refs, 1);
648 if (ref_log && tag) {
649 fprintf(ref_log, "%p,+1,%d,%s,%d,%s,**constructor**,%s\n",
650 EXTERNAL_OBJ(obj), ast_get_tid(), file, line, func, tag);
654 /* return a pointer to the user data */
655 return EXTERNAL_OBJ(obj);
658 unsigned int ao2_options_get(void *obj)
660 struct astobj2 *orig_obj;
662 orig_obj = INTERNAL_OBJ_CHECK(obj);
666 return orig_obj->priv_data.options;
670 void __ao2_global_obj_release(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name)
674 ast_log(LOG_ERROR, "Must be called with a global object!\n");
678 if (__ast_rwlock_wrlock(file, line, func, &holder->lock, name)) {
679 /* Could not get the write lock. */
684 /* Release the held ao2 object. */
686 __ao2_ref(holder->obj, -1, tag, file, line, func);
690 __ast_rwlock_unlock(file, line, func, &holder->lock, name);
693 void *__ao2_global_obj_replace(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name)
699 ast_log(LOG_ERROR, "Must be called with a global object!\n");
703 if (__ast_rwlock_wrlock(file, line, func, &holder->lock, name)) {
704 /* Could not get the write lock. */
710 __ao2_ref(obj, +1, tag, file, line, func);
712 obj_old = holder->obj;
715 __ast_rwlock_unlock(file, line, func, &holder->lock, name);
720 int __ao2_global_obj_replace_unref(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name)
724 obj_old = __ao2_global_obj_replace(holder, obj, tag, file, line, func, name);
726 __ao2_ref(obj_old, -1, tag, file, line, func);
732 void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name)
738 ast_log(LOG_ERROR, "Must be called with a global object!\n");
743 if (__ast_rwlock_rdlock(file, line, func, &holder->lock, name)) {
744 /* Could not get the read lock. */
751 __ao2_ref(obj, +1, tag, file, line, func);
754 __ast_rwlock_unlock(file, line, func, &holder->lock, name);
760 static void weakproxy_run_callbacks(struct ao2_weakproxy *weakproxy)
762 struct ao2_weakproxy_notification *destroyed_cb;
764 while ((destroyed_cb = AST_LIST_REMOVE_HEAD(&weakproxy->destroyed_cb, list))) {
765 destroyed_cb->cb(weakproxy, destroyed_cb->data);
766 ast_free(destroyed_cb);
770 void *__ao2_weakproxy_alloc(size_t data_size, ao2_destructor_fn destructor_fn,
771 const char *tag, const char *file, int line, const char *func)
773 struct ao2_weakproxy *weakproxy;
775 if (data_size < sizeof(*weakproxy)) {
777 ast_log(LOG_ERROR, "Requested data_size smaller than minimum.\n");
781 weakproxy = __ao2_alloc(data_size, destructor_fn, AO2_ALLOC_OPT_LOCK_MUTEX,
782 tag, file, line, func);
785 struct astobj2 *weakproxy_internal;
787 /* Just created weakproxy, no need to check if it's valid. */
788 weakproxy_internal = INTERNAL_OBJ(weakproxy);
789 weakproxy_internal->priv_data.magic = AO2_WEAK;
795 int __ao2_weakproxy_set_object(void *weakproxy, void *obj, int flags,
796 const char *tag, const char *file, int line, const char *func)
798 struct astobj2 *weakproxy_internal = __INTERNAL_OBJ_CHECK(weakproxy, file, line, func);
799 struct astobj2 *obj_internal = __INTERNAL_OBJ_CHECK(obj, file, line, func);
802 if (!weakproxy_internal
803 || weakproxy_internal->priv_data.magic != AO2_WEAK) {
808 || obj_internal->priv_data.weakptr
809 || obj_internal->priv_data.magic != AO2_MAGIC) {
813 if (!(flags & OBJ_NOLOCK)) {
817 if (!weakproxy_internal->priv_data.weakptr) {
818 __ao2_ref(obj, +1, tag, file, line, func);
819 __ao2_ref(weakproxy, +1, tag, file, line, func);
821 weakproxy_internal->priv_data.weakptr = obj;
822 obj_internal->priv_data.weakptr = weakproxy;
827 if (!(flags & OBJ_NOLOCK)) {
828 ao2_unlock(weakproxy);
829 /* It is possible for obj to be accessed now. It's allowed
830 * for weakproxy to already be in a container. Another thread
831 * could have been waiting for a lock on weakproxy to retreive
839 int __ao2_weakproxy_ref_object(void *weakproxy, int delta, int flags,
840 const char *tag, const char *file, int line, const char *func)
842 struct astobj2 *internal = __INTERNAL_OBJ_CHECK(weakproxy, file, line, func);
845 if (!internal || internal->priv_data.magic != AO2_WEAK) {
846 /* This method is meant to be run on weakproxy objects! */
850 /* We have a weak object, grab lock. */
851 if (!(flags & OBJ_NOLOCK)) {
855 if (internal->priv_data.weakptr) {
856 ret = __ao2_ref(internal->priv_data.weakptr, delta, tag, file, line, func);
859 if (!(flags & OBJ_NOLOCK)) {
860 ao2_unlock(weakproxy);
866 void *__ao2_weakproxy_get_object(void *weakproxy, int flags,
867 const char *tag, const char *file, int line, const char *func)
869 struct astobj2 *internal = __INTERNAL_OBJ_CHECK(weakproxy, file, line, func);
872 if (!internal || internal->priv_data.magic != AO2_WEAK) {
873 /* This method is meant to be run on weakproxy objects! */
877 /* We have a weak object, grab reference to object within lock */
878 if (!(flags & OBJ_NOLOCK)) {
882 obj = internal->priv_data.weakptr;
884 __ao2_ref(obj, +1, tag, file, line, func);
887 if (!(flags & OBJ_NOLOCK)) {
888 ao2_unlock(weakproxy);
894 void *__ao2_get_weakproxy(void *obj, const char *tag, const char *file, int line, const char *func)
896 struct astobj2 *obj_internal = __INTERNAL_OBJ_CHECK(obj, file, line, func);
898 if (!obj_internal || obj_internal->priv_data.magic != AO2_MAGIC) {
899 /* This method is meant to be run on normal ao2 objects! */
903 if (!obj_internal->priv_data.weakptr) {
907 __ao2_ref(obj_internal->priv_data.weakptr, +1, tag, file, line, func);
908 return obj_internal->priv_data.weakptr;
911 int ao2_weakproxy_subscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags)
913 struct astobj2 *weakproxy_internal = INTERNAL_OBJ_CHECK(weakproxy);
916 if (!weakproxy_internal || weakproxy_internal->priv_data.magic != AO2_WEAK) {
920 if (!(flags & OBJ_NOLOCK)) {
924 if (weakproxy_internal->priv_data.weakptr) {
925 struct ao2_weakproxy *weak = weakproxy;
926 struct ao2_weakproxy_notification *sub = ast_calloc(1, sizeof(*sub));
931 AST_LIST_INSERT_TAIL(&weak->destroyed_cb, sub, list);
939 if (!(flags & OBJ_NOLOCK)) {
940 ao2_unlock(weakproxy);
946 int ao2_weakproxy_unsubscribe(void *weakproxy, ao2_weakproxy_notification_cb destroyed_cb, void *data, int flags)
948 struct astobj2 *internal_weakproxy = INTERNAL_OBJ_CHECK(weakproxy);
949 struct ao2_weakproxy *weak;
950 struct ao2_weakproxy_notification *sub;
953 if (!internal_weakproxy || internal_weakproxy->priv_data.magic != AO2_WEAK || !destroyed_cb) {
957 if (!(flags & OBJ_NOLOCK)) {
962 AST_LIST_TRAVERSE_SAFE_BEGIN(&weak->destroyed_cb, sub, list) {
963 if (sub->cb == destroyed_cb && sub->data == data) {
964 AST_LIST_REMOVE_CURRENT(list);
967 if (!(flags & OBJ_MULTIPLE)) {
972 AST_LIST_TRAVERSE_SAFE_END;
974 if (!(flags & OBJ_NOLOCK)) {
975 ao2_unlock(weakproxy);
983 static int print_cb(void *obj, void *arg, int flag)
985 struct ast_cli_args *a = (struct ast_cli_args *) arg;
986 char *s = (char *)obj;
988 ast_cli(a->fd, "string <%s>\n", s);
995 static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
999 e->command = "astobj2 show stats";
1000 e->usage = "Usage: astobj2 show stats\n"
1001 " Show astobj2 show stats\n";
1006 ast_cli(a->fd, "Objects : %d\n", ao2.total_objects);
1007 ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
1008 ast_cli(a->fd, "Memory : %d\n", ao2.total_mem);
1009 ast_cli(a->fd, "Locked : %d\n", ao2.total_locked);
1010 ast_cli(a->fd, "Refs : %d\n", ao2.total_refs);
1015 * This is testing code for astobj
1017 static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1019 struct ao2_container *c1;
1020 struct ao2_container *c2;
1023 static int prof_id = -1;
1024 struct ast_cli_args fake_args = { a->fd, 0, NULL };
1028 e->command = "astobj2 test";
1029 e->usage = "Usage: astobj2 test <num>\n"
1030 " Runs astobj2 test. Creates 'num' objects,\n"
1031 " and test iterators, callbacks and maybe other stuff\n";
1038 return CLI_SHOWUSAGE;
1041 if (prof_id == -1) {
1042 prof_id = ast_add_profile("ao2_alloc", 0);
1045 ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
1046 lim = atoi(a->argv[2]);
1047 ast_cli(a->fd, "called astobj_test\n");
1049 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
1051 * Allocate a list container.
1053 c1 = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL /* no sort */,
1054 NULL /* no callback */, "test");
1055 ast_cli(a->fd, "container allocated as %p\n", c1);
1058 * fill the container with objects.
1059 * ao2_alloc() gives us a reference which we pass to the
1060 * container when we do the insert.
1062 for (i = 0; i < lim; i++) {
1063 ast_mark(prof_id, 1 /* start */);
1064 obj = ao2_t_alloc(80, NULL,"test");
1065 ast_mark(prof_id, 0 /* stop */);
1066 ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
1067 sprintf(obj, "-- this is obj %d --", i);
1069 /* At this point, the refcount on obj is 2 due to the allocation
1070 * and linking. We can go ahead and reduce the refcount by 1
1071 * right here so that when the container is unreffed later, the
1072 * objects will be freed
1074 ao2_t_ref(obj, -1, "test");
1077 ast_cli(a->fd, "testing callbacks\n");
1078 ao2_t_callback(c1, 0, print_cb, a, "test callback");
1080 ast_cli(a->fd, "testing container cloning\n");
1081 c2 = ao2_container_clone(c1, 0);
1082 if (ao2_container_count(c1) != ao2_container_count(c2)) {
1083 ast_cli(a->fd, "Cloned container does not have the same number of objects!\n");
1085 ao2_t_callback(c2, 0, print_cb, a, "test callback");
1087 ast_cli(a->fd, "testing iterators, remove every second object\n");
1089 struct ao2_iterator ai;
1092 ai = ao2_iterator_init(c1, 0);
1093 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
1094 ast_cli(a->fd, "iterator on <%s>\n", obj);
1096 ao2_t_unlink(c1, obj,"test");
1097 ao2_t_ref(obj, -1,"test");
1099 ao2_iterator_destroy(&ai);
1100 ast_cli(a->fd, "testing iterators again\n");
1101 ai = ao2_iterator_init(c1, 0);
1102 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
1103 ast_cli(a->fd, "iterator on <%s>\n", obj);
1104 ao2_t_ref(obj, -1,"test");
1106 ao2_iterator_destroy(&ai);
1109 ast_cli(a->fd, "testing callbacks again\n");
1110 ao2_t_callback(c1, 0, print_cb, a, "test callback");
1112 ast_verbose("now you should see an error and possible assertion failure messages:\n");
1113 ao2_t_ref(&i, -1, ""); /* i is not a valid object so we print an error here */
1115 ast_cli(a->fd, "destroy container\n");
1116 ao2_t_ref(c1, -1, ""); /* destroy container */
1117 ao2_t_ref(c2, -1, ""); /* destroy container */
1118 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
1121 #endif /* AO2_DEBUG */
1123 #if defined(AO2_DEBUG)
1124 static struct ast_cli_entry cli_astobj2[] = {
1125 AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
1126 AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
1128 #endif /* AO2_DEBUG */
1130 static void astobj2_cleanup(void)
1132 #if defined(AO2_DEBUG)
1133 ast_cli_unregister_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
1136 if (ast_opt_ref_debug) {
1142 int astobj2_init(void)
1144 char ref_filename[1024];
1146 if (ast_opt_ref_debug) {
1147 snprintf(ref_filename, sizeof(ref_filename), "%s/refs", ast_config_AST_LOG_DIR);
1148 ref_log = fopen(ref_filename, "w");
1150 ast_log(LOG_ERROR, "Could not open ref debug log file: %s\n", ref_filename);
1154 if (container_init() != 0) {
1159 #if defined(AO2_DEBUG)
1160 ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
1161 #endif /* defined(AO2_DEBUG) */
1163 ast_register_cleanup(astobj2_cleanup);