[asterisk-commits] file: branch file/sorceryx4 r389049 - in /team/file/sorceryx4: include/asteri...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat May 18 11:55:07 CDT 2013


Author: file
Date: Sat May 18 11:55:02 2013
New Revision: 389049

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=389049
Log:
Add a generic sorcery object allocation function.

This serves a few functions:
1. It ensures sorcery objects get allocated without locks (since they aren't needed)
2. It allows sorcery object information to be kept private
3. It allows binary compatibility if the sorcery object information structure changes
4. It allows sorcery to add information that may need to be freed when the object is freed

Modified:
    team/file/sorceryx4/include/asterisk/sorcery.h
    team/file/sorceryx4/main/sorcery.c
    team/file/sorceryx4/res/res_sip/config_auth.c
    team/file/sorceryx4/res/res_sip/config_domain_aliases.c
    team/file/sorceryx4/res/res_sip/config_transport.c
    team/file/sorceryx4/res/res_sip/location.c
    team/file/sorceryx4/res/res_sip/sip_configuration.c
    team/file/sorceryx4/tests/test_sorcery.c

Modified: team/file/sorceryx4/include/asterisk/sorcery.h
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx4/include/asterisk/sorcery.h?view=diff&rev=389049&r1=389048&r2=389049
==============================================================================
--- team/file/sorceryx4/include/asterisk/sorcery.h (original)
+++ team/file/sorceryx4/include/asterisk/sorcery.h Sat May 18 11:55:02 2013
@@ -247,13 +247,13 @@
 	void (*loaded)(const char *object_type);
 };
 
+/*! \brief Opaque structure for internal sorcery object */
+struct ast_sorcery_object;
+
 /*! \brief Structure which contains details about a sorcery object */
 struct ast_sorcery_object_details {
-	/*! \brief Unique identifier of this object */
-	char id[AST_UUID_STR_LEN];
-
-	/*! \brief Type of object */
-	char type[MAX_OBJECT_TYPE];
+	/*! \brief Pointer to internal sorcery object information */
+	struct ast_sorcery_object *object;
 };
 
 /*! \brief Macro which must be used at the beginning of each sorcery capable object */
@@ -529,6 +529,17 @@
 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes);
 
 /*!
+ * \brief Allocate a generic sorcery capable object
+ *
+ * \param size Size of the object
+ * \param destructor Optional destructor function
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ */
+void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor);
+
+/*!
  * \brief Allocate an object
  *
  * \param sorcery Pointer to a sorcery structure

Modified: team/file/sorceryx4/main/sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx4/main/sorcery.c?view=diff&rev=389049&r1=389048&r2=389049
==============================================================================
--- team/file/sorceryx4/main/sorcery.c (original)
+++ team/file/sorceryx4/main/sorcery.c Sat May 18 11:55:02 2013
@@ -58,6 +58,18 @@
 /*! \brief Thread pool for observers */
 static struct ast_threadpool *threadpool;
 
+/*! \brief Structure for internal sorcery object information */
+struct ast_sorcery_object {
+	/*! \brief Unique identifier of this object */
+	char id[AST_UUID_STR_LEN];
+
+	/*! \brief Type of object */
+	char type[MAX_OBJECT_TYPE];
+
+	/*! \brief Optional object destructor */
+	ao2_destructor_fn destructor;
+};
+
 /*! \brief Structure for registered object type */
 struct ast_sorcery_object_type {
 	/*! \brief Unique name of the object type */
@@ -784,7 +796,7 @@
 struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object)
 {
 	const struct ast_sorcery_object_details *details = object;
-	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
 	struct ao2_iterator i;
 	struct ast_sorcery_object_field *object_field;
 	struct ast_variable *fields = NULL;
@@ -836,7 +848,7 @@
 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
 {
 	const struct ast_sorcery_object_details *details = object;
-	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
 	struct ao2_iterator i;
 	struct ast_sorcery_object_field *object_field;
 	struct ast_json *json = ast_json_object_create();
@@ -898,7 +910,7 @@
 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
 {
 	const struct ast_sorcery_object_details *details = object;
-	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
 	RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
 	struct ast_variable *field;
 	int res = 0;
@@ -914,7 +926,7 @@
 	}
 
 	for (; field; field = field->next) {
-		if ((res = aco_process_var(&object_type->type, details->id, field, object))) {
+		if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
 			break;
 		}
 	}
@@ -977,6 +989,30 @@
 	return res;
 }
 
+static void sorcery_object_destructor(void *object)
+{
+	struct ast_sorcery_object_details *details = object;
+
+	if (details->object->destructor) {
+		details->object->destructor(object);
+	}
+}
+
+void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
+{
+	void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
+	struct ast_sorcery_object_details *details = object;
+
+	if (!object) {
+		return NULL;
+	}
+
+	details->object = object + size;
+	details->object->destructor = destructor;
+
+	return object;
+}
+
 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
 {
 	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
@@ -988,12 +1024,12 @@
 	}
 
 	if (ast_strlen_zero(id)) {
-		ast_uuid_generate_str(details->id, sizeof(details->id));
+		ast_uuid_generate_str(details->object->id, sizeof(details->object->id));
 	} else {
-		ast_copy_string(details->id, id, sizeof(details->id));
-	}
-
-	ast_copy_string(details->type, type, sizeof(details->type));
+		ast_copy_string(details->object->id, id, sizeof(details->object->id));
+	}
+
+	ast_copy_string(details->object->type, type, sizeof(details->object->type));
 
 	if (aco_set_defaults(&object_type->type, id, details)) {
 		ao2_ref(details, -1);
@@ -1006,8 +1042,8 @@
 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
 {
 	const struct ast_sorcery_object_details *details = object;
-	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
-	struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->type, details->id);
+	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
+	struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
 	RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
 	int res = 0;
 
@@ -1222,7 +1258,7 @@
 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
 {
 	const struct ast_sorcery_object_details *details = object;
-	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
 	RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
 	struct sorcery_details sdetails = {
 		.sorcery = sorcery,
@@ -1281,7 +1317,7 @@
 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
 {
 	const struct ast_sorcery_object_details *details = object;
-	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
 	RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
 	struct sorcery_details sdetails = {
 		.sorcery = sorcery,
@@ -1340,7 +1376,7 @@
 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
 {
 	const struct ast_sorcery_object_details *details = object;
-	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
 	RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
 	struct sorcery_details sdetails = {
 		.sorcery = sorcery,
@@ -1371,13 +1407,13 @@
 const char *ast_sorcery_object_get_id(const void *object)
 {
 	const struct ast_sorcery_object_details *details = object;
-	return details->id;
+	return details->object->id;
 }
 
 const char *ast_sorcery_object_get_type(const void *object)
 {
 	const struct ast_sorcery_object_details *details = object;
-	return details->type;
+	return details->object->type;
 }
 
 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)

Modified: team/file/sorceryx4/res/res_sip/config_auth.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx4/res/res_sip/config_auth.c?view=diff&rev=389049&r1=389048&r2=389049
==============================================================================
--- team/file/sorceryx4/res/res_sip/config_auth.c (original)
+++ team/file/sorceryx4/res/res_sip/config_auth.c Sat May 18 11:55:02 2013
@@ -32,7 +32,7 @@
 
 static void *auth_alloc(const char *name)
 {
-	struct ast_sip_auth *auth = ao2_alloc(sizeof(*auth), auth_destroy);
+	struct ast_sip_auth *auth = ast_sorcery_generic_alloc(sizeof(*auth), auth_destroy);
 
 	if (!auth) {
 		return NULL;

Modified: team/file/sorceryx4/res/res_sip/config_domain_aliases.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx4/res/res_sip/config_domain_aliases.c?view=diff&rev=389049&r1=389048&r2=389049
==============================================================================
--- team/file/sorceryx4/res/res_sip/config_domain_aliases.c (original)
+++ team/file/sorceryx4/res/res_sip/config_domain_aliases.c Sat May 18 11:55:02 2013
@@ -33,7 +33,7 @@
 
 static void *domain_alias_alloc(const char *name)
 {
-	struct ast_sip_domain_alias *alias = ao2_alloc(sizeof(*alias), domain_alias_destroy);
+        struct ast_sip_domain_alias *alias = ast_sorcery_generic_alloc(sizeof(*alias), domain_alias_destroy);
 
 	if (!alias) {
 		return NULL;

Modified: team/file/sorceryx4/res/res_sip/config_transport.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx4/res/res_sip/config_transport.c?view=diff&rev=389049&r1=389048&r2=389049
==============================================================================
--- team/file/sorceryx4/res/res_sip/config_transport.c (original)
+++ team/file/sorceryx4/res/res_sip/config_transport.c Sat May 18 11:55:02 2013
@@ -62,7 +62,7 @@
 /*! \brief Allocator for transport */
 static void *transport_alloc(const char *name)
 {
-	struct ast_sip_transport *transport = ao2_alloc(sizeof(*transport), transport_destroy);
+	struct ast_sip_transport *transport = ast_sorcery_generic_alloc(sizeof(*transport), transport_destroy);
 
 	if (!transport) {
 		return NULL;

Modified: team/file/sorceryx4/res/res_sip/location.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx4/res/res_sip/location.c?view=diff&rev=389049&r1=389048&r2=389049
==============================================================================
--- team/file/sorceryx4/res/res_sip/location.c (original)
+++ team/file/sorceryx4/res/res_sip/location.c Sat May 18 11:55:02 2013
@@ -37,7 +37,7 @@
 /*! \brief Allocator for AOR */
 static void *aor_alloc(const char *name)
 {
-	struct ast_sip_aor *aor = ao2_alloc_options(sizeof(struct ast_sip_aor), aor_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
+	struct ast_sip_aor *aor = ast_sorcery_generic_alloc(sizeof(struct ast_sip_aor), aor_destroy);
 	if (!aor) {
 		return NULL;
 	}
@@ -56,7 +56,7 @@
 /*! \brief Allocator for contact */
 static void *contact_alloc(const char *name)
 {
-	struct ast_sip_contact *contact = ao2_alloc_options(sizeof(*contact), contact_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
+	struct ast_sip_contact *contact = ast_sorcery_generic_alloc(sizeof(*contact), contact_destroy);
 
 	if (!contact) {
 		return NULL;

Modified: team/file/sorceryx4/res/res_sip/sip_configuration.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx4/res/res_sip/sip_configuration.c?view=diff&rev=389049&r1=389048&r2=389049
==============================================================================
--- team/file/sorceryx4/res/res_sip/sip_configuration.c (original)
+++ team/file/sorceryx4/res/res_sip/sip_configuration.c Sat May 18 11:55:02 2013
@@ -278,7 +278,7 @@
 
 static void *sip_nat_hook_alloc(const char *name)
 {
-	return ao2_alloc(sizeof(struct ast_sip_nat_hook), NULL);
+	return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
 }
 
 int ast_res_sip_initialize_configuration(void)
@@ -408,7 +408,7 @@
 
 void *ast_sip_endpoint_alloc(const char *name)
 {
-	struct ast_sip_endpoint *endpoint = ao2_alloc(sizeof(*endpoint), endpoint_destructor);
+	struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
 	if (!endpoint) {
 		return NULL;
 	}

Modified: team/file/sorceryx4/tests/test_sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx4/tests/test_sorcery.c?view=diff&rev=389049&r1=389048&r2=389049
==============================================================================
--- team/file/sorceryx4/tests/test_sorcery.c (original)
+++ team/file/sorceryx4/tests/test_sorcery.c Sat May 18 11:55:02 2013
@@ -49,7 +49,7 @@
 /*! \brief Internal function to allocate a test object */
 static void *test_sorcery_object_alloc(const char *id)
 {
-	return ao2_alloc(sizeof(struct test_sorcery_object), NULL);
+	return ast_sorcery_generic_alloc(sizeof(struct test_sorcery_object), NULL);
 }
 
 /*! \brief Internal function for object set transformation */




More information about the asterisk-commits mailing list