From cb6bf3094ed715817bcab9a1c951e49c730dd817 Mon Sep 17 00:00:00 2001 From: Corey Farrell <git@cfware.com> Date: Sun, 12 Apr 2015 04:22:41 -0400 Subject: [PATCH] astobj2: Add support for weakproxy objects. This implements "weak" references. The weakproxy object is a real ao2 with normal reference counting of its own. When a weakproxy is pointed to a normal object they hold references to each other. The normal object is automatically freed when a single reference remains (the weakproxy). The weakproxy also supports subscriptions that will notify callbacks when it does not point to any real object. ASTERISK-24936 #close Reported by: Corey Farrell Change-Id: Ib9f73c02262488d314d9d9d62f58165b9ec43c67 --- include/asterisk/astobj2.h | 150 +++++++++++++++++++++ main/astobj2.c | 238 ++++++++++++++++++++++++++++++++- tests/test_astobj2_weaken.c | 259 ++++++++++++++++++++++++++++++++++++ 3 files changed, 645 insertions(+), 2 deletions(-) create mode 100644 tests/test_astobj2_weaken.c diff --git a/include/asterisk/astobj2.h b/include/asterisk/astobj2.h index 692cc7cb44..1c3c2e8ad7 100644 --- a/include/asterisk/astobj2.h +++ b/include/asterisk/astobj2.h @@ -19,6 +19,7 @@ #include "asterisk/compat.h" #include "asterisk/lock.h" +#include "asterisk/linkedlists.h" /*! \file * \ref AstObj2 @@ -578,6 +579,155 @@ int __ao2_ref(void *o, int delta); /*! @} */ +/*! \brief ao2_weakproxy + * + * @{ + */ +struct ao2_weakproxy_notification; +typedef void (*ao2_weakproxy_notification_cb)(void *weakproxy, void *data); + +/*! \brief This struct should be opaque, but it's size is needed. */ +struct ao2_weakproxy { + AST_LIST_HEAD_NOLOCK(, ao2_weakproxy_notification) destroyed_cb; +}; + +/*! \brief Macro which must be used at the beginning of weakproxy capable objects. + * + * \note The primary purpose of user defined fields on weakproxy objects is to hold + * immutable container keys for the real object. + */ +#define AO2_WEAKPROXY() struct ao2_weakproxy __weakproxy##__LINE__ + +/*! + * \since 14.0.0 + * \brief Allocate an ao2_weakproxy object + * + * \param data_size The sizeof() of the user-defined structure. + * \param destructor_fn The destructor function (can be NULL) + * + * \note "struct ao2_weakproxy" must be the first field of any object. + * This can be done by using AO2_WEAKPROXY to declare your structure. + */ +void *__ao2_weakproxy_alloc(size_t data_size, ao2_destructor_fn destructor_fn, + const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result; + +#define ao2_weakproxy_alloc(data_size, destructor_fn) \ + __ao2_weakproxy_alloc(data_size, destructor_fn, "", __FILE__, __LINE__, __PRETTY_FUNCTION__) + +#define ao2_t_weakproxy_alloc(data_size, destructor_fn, tag) \ + __ao2_weakproxy_alloc(data_size, destructor_fn, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__) + +/*! + * \since 14.0.0 + * \brief Associate weakproxy with obj. + * + * \param weakproxy An object created by ao2_weakproxy_alloc. + * \param obj An ao2 object not created by ao2_weakproxy_alloc. + * \param flags OBJ_NOLOCK to avoid locking weakproxy. + * + * \retval 0 Success + * \retval -1 Failure + * + * \note obj must be newly created, this procedure is not thread safe + * if any other code can reach obj before this procedure ends. + * + * \note weakproxy may be previously existing, but must not currently + * have an object set. + * + * \note The only way to unset an object is for it to be destroyed. + * Any call to this function while an object is already set will fail. + */ +int __ao2_weakproxy_set_object(void *weakproxy, void *obj, int flags, + const char *tag, const char *file, int line, const char *func); + +#define ao2_weakproxy_set_object(weakproxy, obj, flags) \ + __ao2_weakproxy_set_object(weakproxy, obj, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__) + +#define ao2_t_weakproxy_set_object(weakproxy, obj, flags, tag) \ + __ao2_weakproxy_set_object(weakproxy, obj, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__) + +/*! + * \since 14.0.0 + * \brief Get the object associated with weakproxy. + * + * \param weakproxy The weakproxy to read from. + * \param flags OBJ_NOLOCK to avoid locking weakproxy. + * + * \return A reference to the object previously set by ao2_weakproxy_set_object. + * \retval NULL Either no object was set or the previously set object has been freed. + */ +void *__ao2_weakproxy_get_object(void *weakproxy, int flags, + const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result; + +#define ao2_weakproxy_get_object(weakproxy, flags) \ + __ao2_weakproxy_get_object(weakproxy, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__) + +#define ao2_t_weakproxy_get_object(weakproxy, flags, tag) \ + __ao2_weakproxy_get_object(weakproxy, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__) + +/*! + * \since 14.0.0 + * \brief Request notification when weakproxy points to NULL. + * + * \param weakproxy The weak object + * \param cb Procedure to call when no real object is associated + * \param data Passed to cb + * \param flags OBJ_NOLOCK to avoid locking weakproxy. + * + * \retval 0 Success + * \retval -1 Failure + * + * \note This procedure will allow the same cb / data pair to be added to + * the same weakproxy multiple times. + * + * \note It is the caller's responsibility to ensure that *data is valid + * until after cb() is run or ao2_weakproxy_unsubscribe is called. + * + * \note If the weakproxy currently points to NULL the callback will be run immediately, + * without being added to the subscriber list. + */ +int ao2_weakproxy_subscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags); + +/*! + * \since 14.0.0 + * \brief Remove notification of real object destruction. + * + * \param weakproxy The weak object + * \param cb Callback to remove from destroy notification list + * \param data Data pointer to match + * \param flags OBJ_NOLOCK to avoid locking weakproxy. + * OBJ_MULTIPLE to remove all copies of the same cb / data pair. + * + * \return The number of subscriptions removed. + * \retval 0 cb / data pair not found, nothing removed. + * \retval -1 Failure due to invalid parameters. + * + * \note Unless flags includes OBJ_MULTIPLE, this will only remove a single copy + * of the cb / data pair. If it was subscribed multiple times it must be + * unsubscribed as many times. The OBJ_MULTIPLE flag can be used to remove + * matching subscriptions. + */ +int ao2_weakproxy_unsubscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags); + +/*! + * \since 14.0.0 + * \brief Get the weakproxy attached to obj + * + * \param obj The object to retreive a weakproxy from + * + * \return The weakproxy object + */ +void *__ao2_get_weakproxy(void *obj, + const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result; + +#define ao2_get_weakproxy(obj) \ + __ao2_get_weakproxy(obj, "", __FILE__, __LINE__, __PRETTY_FUNCTION__) + +#define ao2_t_get_weakproxy(obj, tag) \ + __ao2_get_weakproxy(obj, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__) +/*! @} */ + + /*! \brief Which lock to request. */ enum ao2_lock_req { /*! Request the mutex lock be acquired. */ diff --git a/main/astobj2.c b/main/astobj2.c index 0f6ed739be..edf1e95aba 100644 --- a/main/astobj2.c +++ b/main/astobj2.c @@ -52,16 +52,20 @@ static FILE *ref_log; struct __priv_data { int ref_counter; ao2_destructor_fn destructor_fn; + /*! This field is used for astobj2 and ao2_weakproxy objects to reference each other */ + void *weakptr; /*! User data size for stats */ size_t data_size; /*! The ao2 object option flags */ uint32_t options; /*! magic number. This is used to verify that a pointer passed in is a - * valid astobj2 */ + * valid astobj2 or ao2_weak reference */ uint32_t magic; }; #define AO2_MAGIC 0xa570b123 +#define AO2_WEAK 0xa570b122 +#define IS_AO2_MAGIC_BAD(p) (AO2_MAGIC != (p->priv_data.magic | 1)) /*! * What an astobj2 object looks like: fixed-size private data @@ -72,6 +76,14 @@ struct astobj2 { void *user_data[0]; }; +struct ao2_weakproxy_notification { + ao2_weakproxy_notification_cb cb; + void *data; + AST_LIST_ENTRY(ao2_weakproxy_notification) list; +}; + +static void weakproxy_run_callbacks(struct ao2_weakproxy *weakproxy); + struct ao2_lock_priv { ast_mutex_t lock; }; @@ -121,7 +133,7 @@ static struct astobj2 *INTERNAL_OBJ(void *user_data) } p = (struct astobj2 *) ((char *) user_data - sizeof(*p)); - if (AO2_MAGIC != p->priv_data.magic) { + if (IS_AO2_MAGIC_BAD(p)) { if (p->priv_data.magic) { ast_log(LOG_ERROR, "bad magic number 0x%x for object %p\n", p->priv_data.magic, user_data); @@ -407,6 +419,7 @@ static int internal_ao2_ref(void *user_data, int delta, const char *file, int li struct astobj2_rwlock *obj_rwlock; int current_value; int ret; + void *weakproxy = NULL; if (obj == NULL) { ast_assert(0); @@ -418,6 +431,10 @@ static int internal_ao2_ref(void *user_data, int delta, const char *file, int li return obj->priv_data.ref_counter; } + if (delta < 0 && obj->priv_data.magic == AO2_MAGIC && (weakproxy = obj->priv_data.weakptr)) { + ao2_lock(weakproxy); + } + /* we modify with an atomic operation the reference counter */ ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta); current_value = ret + delta; @@ -426,6 +443,29 @@ static int internal_ao2_ref(void *user_data, int delta, const char *file, int li ast_atomic_fetchadd_int(&ao2.total_refs, delta); #endif + if (weakproxy) { + if (current_value == 1) { + /* The only remaining reference is the one owned by the weak object */ + struct astobj2 *internal_weakproxy = INTERNAL_OBJ(weakproxy); + + /* Unlink the obj from the weak proxy */ + internal_weakproxy->priv_data.weakptr = NULL; + obj->priv_data.weakptr = NULL; + + /* Notify the subscribers that weakproxy now points to NULL. */ + weakproxy_run_callbacks(weakproxy); + + /* weak is already unlinked from obj so this won't recurse */ + ao2_ref(user_data, -1); + } + + ao2_unlock(weakproxy); + + if (current_value == 1) { + ao2_ref(weakproxy, -1); + } + } + if (0 < current_value) { /* The object still lives. */ return ret; @@ -736,6 +776,200 @@ void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const return obj; } + +static void weakproxy_run_callbacks(struct ao2_weakproxy *weakproxy) +{ + struct ao2_weakproxy_notification *destroyed_cb; + + while ((destroyed_cb = AST_LIST_REMOVE_HEAD(&weakproxy->destroyed_cb, list))) { + destroyed_cb->cb(weakproxy, destroyed_cb->data); + ast_free(destroyed_cb); + } +} + +void *__ao2_weakproxy_alloc(size_t data_size, ao2_destructor_fn destructor_fn, + const char *tag, const char *file, int line, const char *func) +{ + struct ao2_weakproxy *weakproxy; + + if (data_size < sizeof(*weakproxy)) { + ast_assert(0); + ast_log(LOG_ERROR, "Requested data_size smaller than minimum.\n"); + return NULL; + } + + weakproxy = __ao2_alloc_debug(data_size, destructor_fn, AO2_ALLOC_OPT_LOCK_MUTEX, + tag, file, line, func, 1); + + if (weakproxy) { + struct astobj2 *weakproxy_internal = INTERNAL_OBJ(weakproxy); + + weakproxy_internal->priv_data.magic = AO2_WEAK; + } + + return weakproxy; +} + +int __ao2_weakproxy_set_object(void *weakproxy, void *obj, int flags, + const char *tag, const char *file, int line, const char *func) +{ + struct astobj2 *weakproxy_internal = INTERNAL_OBJ(weakproxy); + struct astobj2 *obj_internal = INTERNAL_OBJ(obj); + int ret = -1; + + if (!weakproxy_internal + || weakproxy_internal->priv_data.magic != AO2_WEAK) { + return -1; + } + + if (!obj_internal + || obj_internal->priv_data.weakptr + || obj_internal->priv_data.magic != AO2_MAGIC) { + return -1; + } + + if (!(flags & OBJ_NOLOCK)) { + ao2_lock(weakproxy); + } + + if (!weakproxy_internal->priv_data.weakptr) { + __ao2_ref_debug(obj, +1, tag, file, line, func); + __ao2_ref_debug(weakproxy, +1, tag, file, line, func); + + weakproxy_internal->priv_data.weakptr = obj; + obj_internal->priv_data.weakptr = weakproxy; + + ret = 0; + } + + if (!(flags & OBJ_NOLOCK)) { + ao2_unlock(weakproxy); + /* It is possible for obj to be accessed now. It's allowed + * for weakproxy to already be in a container. Another thread + * could have been waiting for a lock on weakproxy to retreive + * the object. + */ + } + + return ret; +} + +void *__ao2_weakproxy_get_object(void *weakproxy, int flags, + const char *tag, const char *file, int line, const char *func) +{ + struct astobj2 *internal = INTERNAL_OBJ(weakproxy); + void *obj; + + if (!internal || internal->priv_data.magic != AO2_WEAK) { + /* This method is meant to be run on weakproxy objects! */ + return NULL; + } + + /* We have a weak object, grab reference to object within lock */ + if (!(flags & OBJ_NOLOCK)) { + ao2_lock(weakproxy); + } + + obj = internal->priv_data.weakptr; + if (obj) { + __ao2_ref_debug(obj, +1, tag, file, line, func); + } + + if (!(flags & OBJ_NOLOCK)) { + ao2_unlock(weakproxy); + } + + return obj; +} + +void *__ao2_get_weakproxy(void *obj, const char *tag, const char *file, int line, const char *func) +{ + struct astobj2 *obj_internal = INTERNAL_OBJ(obj); + + if (!obj_internal || obj_internal->priv_data.magic != AO2_MAGIC) { + /* This method is meant to be run on normal ao2 objects! */ + return NULL; + } + + if (!obj_internal->priv_data.weakptr) { + return NULL; + } + + __ao2_ref_debug(obj_internal->priv_data.weakptr, +1, tag, file, line, func); + return obj_internal->priv_data.weakptr; +} + +int ao2_weakproxy_subscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags) +{ + struct astobj2 *weakproxy_internal = INTERNAL_OBJ(weakproxy); + int ret = -1; + + if (!weakproxy_internal || weakproxy_internal->priv_data.magic != AO2_WEAK) { + return -1; + } + + if (!(flags & OBJ_NOLOCK)) { + ao2_lock(weakproxy); + } + + if (weakproxy_internal->priv_data.weakptr) { + struct ao2_weakproxy *weak = weakproxy; + struct ao2_weakproxy_notification *sub = ast_calloc(1, sizeof(*sub)); + + if (sub) { + sub->cb = cb; + sub->data = data; + AST_LIST_INSERT_TAIL(&weak->destroyed_cb, sub, list); + ret = 0; + } + } else { + cb(weakproxy, data); + ret = 0; + } + + if (!(flags & OBJ_NOLOCK)) { + ao2_unlock(weakproxy); + } + + return ret; +} + +int ao2_weakproxy_unsubscribe(void *weakproxy, ao2_weakproxy_notification_cb destroyed_cb, void *data, int flags) +{ + struct astobj2 *internal_weakproxy = INTERNAL_OBJ(weakproxy); + struct ao2_weakproxy *weak; + struct ao2_weakproxy_notification *sub; + int ret = 0; + + if (!internal_weakproxy || internal_weakproxy->priv_data.magic != AO2_WEAK || !destroyed_cb) { + return -1; + } + + if (!(flags & OBJ_NOLOCK)) { + ao2_lock(weakproxy); + } + + weak = weakproxy; + AST_LIST_TRAVERSE_SAFE_BEGIN(&weak->destroyed_cb, sub, list) { + if (sub->cb == destroyed_cb && sub->data == data) { + AST_LIST_REMOVE_CURRENT(list); + ast_free(sub); + ret++; + if (!(flags & OBJ_MULTIPLE)) { + break; + } + } + } + AST_LIST_TRAVERSE_SAFE_END; + + if (!(flags & OBJ_NOLOCK)) { + ao2_unlock(weakproxy); + } + + return ret; +} + + #ifdef AO2_DEBUG static int print_cb(void *obj, void *arg, int flag) { diff --git a/tests/test_astobj2_weaken.c b/tests/test_astobj2_weaken.c new file mode 100644 index 0000000000..b68aeda3a9 --- /dev/null +++ b/tests/test_astobj2_weaken.c @@ -0,0 +1,259 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2015, CFWare, LLC + * + * Corey Farrell <git@cfware.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! + * \file + * \brief astobj2 weakproxy test module + * + * \author Corey Farrell <git@cfware.com> + */ + +/*** MODULEINFO + <depend>TEST_FRAMEWORK</depend> + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419175 $") + +#include "asterisk/utils.h" +#include "asterisk/module.h" +#include "asterisk/test.h" +#include "asterisk/astobj2.h" + +static int destructor_called; +static int weakproxydestroyed; + +static void test_obj_destructor(void *obj) +{ + destructor_called++; +} + +static void weakproxy_destructor(void *obj) +{ + weakproxydestroyed++; +} + +static void test_obj_destroy_notify(void *obj, void *data) +{ + int *i = data; + + ++*i; +} + +struct my_weakproxy { + AO2_WEAKPROXY(); + int f1; +}; + +AST_TEST_DEFINE(astobj2_weak1) +{ + void *obj1 = NULL; + void *obj2 = NULL; + void *obj3 = NULL; + void *strong1 = NULL; + struct my_weakproxy *weakref1 = NULL; + struct my_weakproxy *weakref2 = NULL; + int notify0_called = 0; + int notify1_called = 0; + int notify2_called = 0; + int notify3_called = 0; + + switch (cmd) { + case TEST_INIT: + info->name = "astobj2_weak1"; + info->category = "/main/astobj2/"; + info->summary = "Test ao2 weak objects"; + info->description = "Test ao2 weak objects."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + destructor_called = weakproxydestroyed = 0; + obj1 = ao2_t_alloc(0, test_obj_destructor, "obj1"); + if (!obj1) { + return AST_TEST_FAIL; + } + + weakref1 = ao2_t_weakproxy_alloc(sizeof(*weakref1), weakproxy_destructor, "weakref1"); + if (!weakref1) { + ast_test_status_update(test, "Failed to allocate weakref1.\n"); + goto fail_cleanup; + } + weakref1->f1 = 5315; + + if (ao2_weakproxy_subscribe(weakref1, test_obj_destroy_notify, ¬ify0_called, 0)) { + ast_test_status_update(test, "Failed to subscribe to weakref1.\n"); + goto fail_cleanup; + } + if (!notify0_called) { + ast_test_status_update(test, "Subscribe failed to immediately run callback for empty weakproxy.\n"); + goto fail_cleanup; + } + + if (ao2_t_weakproxy_set_object(weakref1, obj1, 0, "set weakref1 to obj1")) { + ast_test_status_update(test, "Failed to set obj1 on weakref1.\n"); + goto fail_cleanup; + } + + if (ao2_weakproxy_subscribe(weakref1, test_obj_destroy_notify, ¬ify1_called, 0)) { + ast_test_status_update(test, "Failed to add a subscription to weakref1.\n"); + goto fail_cleanup; + } + + weakref2 = ao2_t_get_weakproxy(obj1, "get weakref2 from obj1"); + if (weakref1 != weakref2) { + ast_test_status_update(test, "weakref1 != weakref2.\n"); + goto fail_cleanup; + } + + if (ao2_weakproxy_subscribe(weakref2, test_obj_destroy_notify, ¬ify2_called, 0)) { + ast_test_status_update(test, "Failed to add a subscription to weakref2.\n"); + goto fail_cleanup; + } + + if (ao2_weakproxy_subscribe(weakref2, test_obj_destroy_notify, ¬ify2_called, 0)) { + ast_test_status_update(test, "Failed to add a duplicate subscription to weakref2.\n"); + goto fail_cleanup; + } + + if (ao2_weakproxy_subscribe(weakref2, test_obj_destroy_notify, ¬ify2_called, 0)) { + ast_test_status_update(test, "Failed to add a second duplicate subscription to weakref2.\n"); + goto fail_cleanup; + } + + if (ao2_weakproxy_unsubscribe(weakref2, test_obj_destroy_notify, ¬ify2_called, 0) != 1) { + ast_test_status_update(test, "Failed to remove a subscription to weakref2.\n"); + goto fail_cleanup; + } + + ao2_t_cleanup(weakref1, "weakref1"); + ao2_t_cleanup(weakref2, "weakref2"); + + weakref2 = ao2_t_get_weakproxy(obj1, "get weakref2 from obj1"); + if (weakref1 != weakref2) { + weakref1 = NULL; + ast_test_status_update(test, "weakref1 != weakref2.\n"); + goto fail_cleanup; + } + weakref1 = NULL; + + obj2 = ao2_t_alloc(0, NULL, "obj2"); + if (obj2) { + int ret = ao2_t_weakproxy_set_object(weakref2, obj2, 0, "set weakref2 to obj2"); + + ao2_ref(obj2, -1); + if (!ret) { + ast_test_status_update(test, "Set obj2 to weakref2 when it already had an object.\n"); + goto fail_cleanup; + } + } + + if (ao2_weakproxy_subscribe(weakref2, test_obj_destroy_notify, ¬ify3_called, 0)) { + ast_test_status_update(test, "Failed to add a subscription to weakref2.\n"); + goto fail_cleanup; + } + + if (ao2_weakproxy_subscribe(weakref2, test_obj_destroy_notify, ¬ify3_called, 0)) { + ast_test_status_update(test, "Failed to add a duplicate subscription to weakref2.\n"); + goto fail_cleanup; + } + + if (ao2_weakproxy_unsubscribe(weakref2, test_obj_destroy_notify, ¬ify3_called, OBJ_MULTIPLE) != 2) { + ast_test_status_update(test, "Failed to remove the correct number of subscriptions to weakref2.\n"); + goto fail_cleanup; + } + + if (destructor_called || notify1_called || notify2_called || notify3_called) { + ast_test_status_update(test, "Destructor or notifications called early.\n"); + goto fail_cleanup; + } + + strong1 = ao2_t_weakproxy_get_object(weakref2, 0, "get strong1 from weakref2"); + ao2_t_cleanup(strong1, "strong1"); + + if (obj1 != strong1) { + ast_test_status_update(test, "obj1 != strong1.\n"); + goto fail_cleanup; + } + + if (destructor_called || notify1_called || notify2_called || notify3_called) { + ast_test_status_update(test, "Destructor or notification called early.\n"); + goto fail_cleanup; + } + + ao2_t_ref(obj1, -1, "obj1"); + obj1 = NULL; + + if (destructor_called != 1 || notify1_called != 1 || notify2_called != 2 || notify3_called != 0) { + ast_test_status_update(test, "Destructor or notification not called the expected number of times.\n"); + goto fail_cleanup; + } + + if (ao2_t_weakproxy_get_object(weakref2, 0, "impossible get of weakref2") != NULL) { + ast_test_status_update(test, "Get object on weakref2 worked when it shouldn't\n"); + goto fail_cleanup; + } + + obj3 = ao2_t_alloc(0, test_obj_destructor, "obj3"); + if (!obj3) { + ast_test_status_update(test, "Failed to allocate obj3.\n"); + goto fail_cleanup; + } + + if (ao2_t_weakproxy_set_object(weakref2, obj3, 0, "set weakref2 to obj3")) { + ast_test_status_update(test, "Failed to set obj3 to weakref2.\n"); + goto fail_cleanup; + } + + ao2_ref(obj3, -1); + ao2_t_ref(weakref2, -1, "weakref2"); + + if (!weakproxydestroyed) { + ast_test_status_update(test, "Destructor never called for weakproxy, likely a leak.\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; + +fail_cleanup: + ao2_cleanup(obj1); + ao2_cleanup(obj3); + ao2_cleanup(weakref1); + ao2_cleanup(weakref2); + + return AST_TEST_FAIL; +} + +static int unload_module(void) +{ + AST_TEST_UNREGISTER(astobj2_weak1); + + return 0; +} + +static int load_module(void) +{ + AST_TEST_REGISTER(astobj2_weak1); + + return AST_MODULE_LOAD_SUCCESS; +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ASTOBJ2 Weak Reference Unit Tests"); -- GitLab