[asterisk-commits] rmudgett: trunk r366663 - in /trunk: include/asterisk/ main/ tests/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed May 16 11:34:46 CDT 2012
Author: rmudgett
Date: Wed May 16 11:34:42 2012
New Revision: 366663
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=366663
Log:
Change ao2 global array to ao2 global object holder.
Review: https://reviewboard.asterisk.org/r/1921/
Modified:
trunk/include/asterisk/astobj2.h
trunk/main/astobj2.c
trunk/tests/test_astobj2.c
Modified: trunk/include/asterisk/astobj2.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/astobj2.h?view=diff&rev=366663&r1=366662&r2=366663
==============================================================================
--- trunk/include/asterisk/astobj2.h (original)
+++ trunk/include/asterisk/astobj2.h Wed May 16 11:34:42 2012
@@ -565,130 +565,133 @@
void *ao2_object_get_lockaddr(void *obj);
-/*! Global ao2 array container base structure. */
+/*! Global ao2 object holder structure. */
struct ao2_global_obj {
- /*! Access lock to the global ao2 array container. */
+ /*! Access lock to the held ao2 object. */
ast_rwlock_t lock;
- /*! Number of elements in the global ao2 array container. */
- unsigned int num_elements;
- /*! Global ao2 array container array. */
- void *obj[0];
+ /*! Global ao2 object. */
+ void *obj;
};
/*!
- * \brief Define a structure to be used to hold a global array of ao2 objects, statically initialized.
+ * \brief Define a global object holder to be used to hold an ao2 object, statically initialized.
* \since 11.0
*
- * \param name This will be the name of the defined structure.
- * \param num_objects Number of ao2 objects to contain.
+ * \param name This will be the name of the object holder.
*
* \details
- * This macro creates a structure definition that can be used to
- * hold an array of ao2 objects accessible using an API. The
- * structure is allocated and initialized to be empty.
+ * This macro creates a global object holder that can be used to
+ * hold an ao2 object accessible using an API. The structure is
+ * allocated and initialized to be empty.
*
* Example usage:
* \code
- * static AO2_GLOBAL_OBJ_STATIC(global_cfg, 10);
+ * static AO2_GLOBAL_OBJ_STATIC(global_cfg);
* \endcode
*
- * This would define \c struct \c global_cfg, intended to hold
- * an array of ao2 objects accessible using an API.
+ * This defines global_cfg, intended to hold an ao2 object
+ * accessible using an API.
*/
#ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER
-#define AO2_GLOBAL_OBJ_STATIC(name, num_objects) \
- struct name { \
- struct ao2_global_obj global; \
- void *objs[num_objects]; \
- } name; \
+#define AO2_GLOBAL_OBJ_STATIC(name) \
+ struct ao2_global_obj name; \
static void __attribute__((constructor)) __init_##name(void) \
{ \
- unsigned int idx = (num_objects); \
- ast_rwlock_init(&name.global.lock); \
- name.global.num_elements = idx; \
- while (idx--) { \
- name.global.obj[idx] = NULL; \
- } \
+ ast_rwlock_init(&name.lock); \
+ name.obj = NULL; \
} \
static void __attribute__((destructor)) __fini_##name(void) \
{ \
- unsigned int idx = (num_objects); \
- while (idx--) { \
- if (name.global.obj[idx]) { \
- ao2_ref(name.global.obj[idx], -1); \
- name.global.obj[idx] = NULL; \
- } \
+ if (name.obj) { \
+ ao2_ref(name.obj, -1); \
+ name.obj = NULL; \
} \
- ast_rwlock_destroy(&name.global.lock); \
+ ast_rwlock_destroy(&name.lock); \
} \
struct __dummy_##name
#else
-#define AO2_GLOBAL_OBJ_STATIC(name, num_objects) \
- struct name { \
- struct ao2_global_obj global; \
- void *objs[num_objects]; \
- } name = { \
- .global.lock = AST_RWLOCK_INIT_VALUE, \
- .global.num_elements = (num_objects), \
+#define AO2_GLOBAL_OBJ_STATIC(name) \
+ struct ao2_global_obj name = { \
+ .lock = AST_RWLOCK_INIT_VALUE, \
}
#endif
/*!
- * \brief Release all global ao2 objects in the global array.
+ * \brief Release the ao2 object held in the global holder.
* \since 11.0
*
- * \param array Global ao2 object array container.
+ * \param holder Global ao2 object holder.
* \param tag used for debugging
*
* \return Nothing
*/
-#define ao2_t_global_obj_release(array, tag) \
- __ao2_global_obj_release(&array.global, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #array)
-#define ao2_global_obj_release(array) \
- __ao2_global_obj_release(&array.global, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #array)
-
-void __ao2_global_obj_release(struct ao2_global_obj *array, const char *tag, const char *file, int line, const char *func, const char *name);
-
-/*!
- * \brief Replace a global ao2 object in the global array.
+#define ao2_t_global_obj_release(holder, tag) \
+ __ao2_global_obj_release(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#define ao2_global_obj_release(holder) \
+ __ao2_global_obj_release(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+
+void __ao2_global_obj_release(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name);
+
+/*!
+ * \brief Replace an ao2 object in the global holder.
* \since 11.0
*
- * \param array Global ao2 object array container.
- * \param idx Index to replace in the array.
- * \param obj Object to put into the array. Can be NULL.
+ * \param holder Global ao2 object holder.
+ * \param obj Object to put into the holder. Can be NULL.
* \param tag used for debugging
*
* \note This function automatically increases the reference
- * count to account for the reference that the global array now
+ * count to account for the reference that the global holder now
* holds to the object.
*
- * \retval Reference to previous global ao2 object stored at the index.
+ * \retval Reference to previous global ao2 object stored.
* \retval NULL if no object available.
*/
-#define ao2_t_global_obj_replace(array, idx, obj, tag) \
- __ao2_global_obj_replace(&array.global, (idx), (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #array)
-#define ao2_global_obj_replace(array, idx, obj) \
- __ao2_global_obj_replace(&array.global, (idx), (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #array)
-
-void *__ao2_global_obj_replace(struct ao2_global_obj *array, unsigned int idx, void *obj, const char *tag, const char *file, int line, const char *func, const char *name);
-
-/*!
- * \brief Get a reference to the object stored in the ao2 global array.
+#define ao2_t_global_obj_replace(holder, obj, tag) \
+ __ao2_global_obj_replace(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#define ao2_global_obj_replace(holder, idx, obj) \
+ __ao2_global_obj_replace(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+
+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);
+
+/*!
+ * \brief Replace an ao2 object in the global holder, throwing away any old object.
* \since 11.0
*
- * \param array Global ao2 object array container.
- * \param idx Index to get an object reference in the array.
+ * \param holder Global ao2 object holder.
+ * \param obj Object to put into the holder. Can be NULL.
* \param tag used for debugging
*
- * \retval Reference to current global ao2 object stored at the index.
+ * \note This function automatically increases the reference
+ * count to account for the reference that the global holder now
+ * holds to the object. It also decreases the reference count
+ * of any object being replaced.
+ *
+ * \retval 0 The global object was previously empty
+ * \retval 1 The global object was not previously empty
+ */
+#define ao2_t_global_obj_replace_unref(holder, obj, tag) \
+ __ao2_global_obj_replace_unref(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#define ao2_global_obj_replace_unref(holder, obj) \
+ __ao2_global_obj_replace_unref(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+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);
+
+/*!
+ * \brief Get a reference to the object stored in the global holder.
+ * \since 11.0
+ *
+ * \param holder Global ao2 object holder.
+ * \param tag used for debugging
+ *
+ * \retval Reference to current ao2 object stored in the holder.
* \retval NULL if no object available.
*/
-#define ao2_t_global_obj_ref(array, idx, tag) \
- __ao2_global_obj_ref(&array.global, (idx), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #array)
-#define ao2_global_obj_ref(array, idx) \
- __ao2_global_obj_ref(&array.global, (idx), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #array)
-
-void *__ao2_global_obj_ref(struct ao2_global_obj *array, unsigned int idx, const char *tag, const char *file, int line, const char *func, const char *name);
+#define ao2_t_global_obj_ref(holder, tag) \
+ __ao2_global_obj_ref(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#define ao2_global_obj_ref(holder, idx) \
+ __ao2_global_obj_ref(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+
+void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name);
/*!
Modified: trunk/main/astobj2.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/astobj2.c?view=diff&rev=366663&r1=366662&r2=366663
==============================================================================
--- trunk/main/astobj2.c (original)
+++ trunk/main/astobj2.c Wed May 16 11:34:42 2012
@@ -634,40 +634,35 @@
}
-void __ao2_global_obj_release(struct ao2_global_obj *array, const char *tag, const char *file, int line, const char *func, const char *name)
-{
- unsigned int idx;
-
- if (!array) {
+void __ao2_global_obj_release(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name)
+{
+ if (!holder) {
/* For sanity */
return;
}
- if (__ast_rwlock_wrlock(file, line, func, &array->lock, name)) {
+ if (__ast_rwlock_wrlock(file, line, func, &holder->lock, name)) {
/* Could not get the write lock. */
return;
}
- /* Release all contained ao2 objects. */
- idx = array->num_elements;
- while (idx--) {
- if (array->obj[idx]) {
- __ao2_ref_debug(array->obj[idx], -1, tag, file, line, func);
- array->obj[idx] = NULL;
- }
- }
-
- __ast_rwlock_unlock(file, line, func, &array->lock, name);
-}
-
-void *__ao2_global_obj_replace(struct ao2_global_obj *array, unsigned int idx, void *obj, const char *tag, const char *file, int line, const char *func, const char *name)
+ /* Release the held ao2 object. */
+ if (holder->obj) {
+ __ao2_ref_debug(holder->obj, -1, tag, file, line, func);
+ holder->obj = NULL;
+ }
+
+ __ast_rwlock_unlock(file, line, func, &holder->lock, name);
+}
+
+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)
{
void *obj_old;
- if (!array || array->num_elements <= idx) {
+ if (!holder) {
/* For sanity */
return NULL;
}
- if (__ast_rwlock_wrlock(file, line, func, &array->lock, name)) {
+ if (__ast_rwlock_wrlock(file, line, func, &holder->lock, name)) {
/* Could not get the write lock. */
return NULL;
}
@@ -675,33 +670,45 @@
if (obj) {
__ao2_ref_debug(obj, +1, tag, file, line, func);
}
- obj_old = array->obj[idx];
- array->obj[idx] = obj;
-
- __ast_rwlock_unlock(file, line, func, &array->lock, name);
+ obj_old = holder->obj;
+ holder->obj = obj;
+
+ __ast_rwlock_unlock(file, line, func, &holder->lock, name);
return obj_old;
}
-void *__ao2_global_obj_ref(struct ao2_global_obj *array, unsigned int idx, const char *tag, const char *file, int line, const char *func, const char *name)
+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)
+{
+ void *obj_old;
+
+ obj_old = __ao2_global_obj_replace(holder, obj, tag, file, line, func, name);
+ if (obj_old) {
+ __ao2_ref_debug(obj_old, -1, tag, file, line, func);
+ return 1;
+ }
+ return 0;
+}
+
+void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name)
{
void *obj;
- if (!array || array->num_elements <= idx) {
+ if (!holder) {
/* For sanity */
return NULL;
}
- if (__ast_rwlock_rdlock(file, line, func, &array->lock, name)) {
+ if (__ast_rwlock_rdlock(file, line, func, &holder->lock, name)) {
/* Could not get the read lock. */
return NULL;
}
- obj = array->obj[idx];
+ obj = holder->obj;
if (obj) {
__ao2_ref_debug(obj, +1, tag, file, line, func);
}
- __ast_rwlock_unlock(file, line, func, &array->lock, name);
+ __ast_rwlock_unlock(file, line, func, &holder->lock, name);
return obj;
}
Modified: trunk/tests/test_astobj2.c
URL: http://svnview.digium.com/svn/asterisk/trunk/tests/test_astobj2.c?view=diff&rev=366663&r1=366662&r2=366663
==============================================================================
--- trunk/tests/test_astobj2.c (original)
+++ trunk/tests/test_astobj2.c Wed May 16 11:34:42 2012
@@ -567,7 +567,7 @@
return res;
}
-static AO2_GLOBAL_OBJ_STATIC(astobj2_array, 2);
+static AO2_GLOBAL_OBJ_STATIC(astobj2_holder);
AST_TEST_DEFINE(astobj2_test_3)
{
@@ -582,15 +582,15 @@
case TEST_INIT:
info->name = "astobj2_test3";
info->category = "/main/astobj2/";
- info->summary = "Test global ao2 array container";
+ info->summary = "Test global ao2 holder";
info->description =
- "This test is to see if the global ao2 array container works as intended.";
+ "This test is to see if the global ao2 holder works as intended.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
- /* Put an object in index 0 */
+ /* Put an object in the holder */
obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
if (!obj) {
ast_test_status_update(test, "ao2_alloc failed.\n");
@@ -599,7 +599,7 @@
}
obj->destructor_count = &destructor_count;
obj->i = ++num_objects;
- obj2 = ao2_t_global_obj_replace(astobj2_array, 0, obj, "Save object in index 0");
+ obj2 = ao2_t_global_obj_replace(astobj2_holder, obj, "Save object in the holder");
if (obj2) {
ast_test_status_update(test, "Returned object not expected.\n");
res = AST_TEST_FAIL;
@@ -608,7 +608,7 @@
/* Save object for next check. */
obj3 = obj;
- /* Replace an object in index 0 */
+ /* Replace an object in the holder */
obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
if (!obj) {
ast_test_status_update(test, "ao2_alloc failed.\n");
@@ -617,7 +617,7 @@
}
obj->destructor_count = &destructor_count;
obj->i = ++num_objects;
- obj2 = ao2_t_global_obj_replace(astobj2_array, 0, obj, "Replace object in index 0");
+ obj2 = ao2_t_global_obj_replace(astobj2_holder, obj, "Replace object in the holder");
if (!obj2) {
ast_test_status_update(test, "Expected an object.\n");
res = AST_TEST_FAIL;
@@ -634,7 +634,7 @@
obj2 = NULL;
ao2_ref(obj, -1);
- /* Put an object in index 1 */
+ /* Replace with unref of an object in the holder */
obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
if (!obj) {
ast_test_status_update(test, "ao2_alloc failed.\n");
@@ -643,25 +643,23 @@
}
obj->destructor_count = &destructor_count;
obj->i = ++num_objects;
- obj2 = ao2_t_global_obj_replace(astobj2_array, 1, obj, "Save object in index 1");
- if (obj2) {
- ao2_ref(obj2, -1);
- ast_test_status_update(test, "Returned object not expected.\n");
+ if (!ao2_t_global_obj_replace_unref(astobj2_holder, obj, "Replace w/ unref object in the holder")) {
+ ast_test_status_update(test, "Expected an object to be replaced.\n");
res = AST_TEST_FAIL;
goto cleanup;
}
/* Save object for next check. */
obj3 = obj;
- /* Get a reference to the object in index 1. */
- obj = ao2_t_global_obj_ref(astobj2_array, 1, "Get reference of index 1 object");
+ /* Get reference to held object. */
+ obj = ao2_t_global_obj_ref(astobj2_holder, "Get a held object reference");
if (!obj) {
ast_test_status_update(test, "Expected an object.\n");
res = AST_TEST_FAIL;
goto cleanup;
}
if (obj != obj3) {
- ast_test_status_update(test, "Returned object not expected.\n");
+ ast_test_status_update(test, "Referenced object not expected object.\n");
res = AST_TEST_FAIL;
goto cleanup;
}
@@ -670,8 +668,8 @@
ao2_ref(obj, -1);
obj = NULL;
- /* Release all objects in the global array. */
- ao2_t_global_obj_release(astobj2_array, "Check release all objects");
+ /* Release the object in the global holder. */
+ ao2_t_global_obj_release(astobj2_holder, "Check release all objects");
destructor_count += num_objects;
if (0 < destructor_count) {
ast_test_status_update(test,
@@ -695,7 +693,7 @@
if (obj3) {
ao2_t_ref(obj3, -1, "Test cleanup external object 3");
}
- ao2_t_global_obj_release(astobj2_array, "Test cleanup array");
+ ao2_t_global_obj_release(astobj2_holder, "Test cleanup holder");
return res;
}
More information about the asterisk-commits
mailing list