From c9c03998cc9c57acbf1998a0cbc0bc1fa6d24edb Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Wed, 29 Apr 2015 00:35:22 -0400 Subject: Astobj2: Add ao2_weakproxy_ref_object function. This function allows code to run ao2_ref against the real object associated with a weakproxy. It is useful when all of the following conditions are true: * You have a pointer to weakproxy. * You do not have or need a pointer to the real object. * You need to ensure the real object exists and is not destroyed during a process. In this case it's wasteful to store a pointer to the real object just for the sake of releasing it later. Change-Id: I38a319b83314de75be74207a8771aab269bcca46 --- main/astobj2.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'main/astobj2.c') diff --git a/main/astobj2.c b/main/astobj2.c index c5b5cd957..f9dd8d490 100644 --- a/main/astobj2.c +++ b/main/astobj2.c @@ -836,6 +836,33 @@ int __ao2_weakproxy_set_object(void *weakproxy, void *obj, int flags, return ret; } +int __ao2_weakproxy_ref_object(void *weakproxy, int delta, int flags, + const char *tag, const char *file, int line, const char *func) +{ + struct astobj2 *internal = __INTERNAL_OBJ_CHECK(weakproxy, file, line, func); + int ret = -1; + + if (!internal || internal->priv_data.magic != AO2_WEAK) { + /* This method is meant to be run on weakproxy objects! */ + return -2; + } + + /* We have a weak object, grab lock. */ + if (!(flags & OBJ_NOLOCK)) { + ao2_lock(weakproxy); + } + + if (internal->priv_data.weakptr) { + ret = __ao2_ref(internal->priv_data.weakptr, delta, tag, file, line, func); + } + + if (!(flags & OBJ_NOLOCK)) { + ao2_unlock(weakproxy); + } + + return ret; +} + void *__ao2_weakproxy_get_object(void *weakproxy, int flags, const char *tag, const char *file, int line, const char *func) { -- cgit v1.2.3