[asterisk-commits] rmudgett: trunk r433623 - in /trunk: ./ res/res_pjsip_registrar_expire.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Mar 27 16:06:24 CDT 2015


Author: rmudgett
Date: Fri Mar 27 16:06:22 2015
New Revision: 433623

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=433623
Log:
res_pjsip_registrar_expire.c: Made use ao2 container template routines and eliminated some RAII_VAR() usage.

* Converted the contact_autoexpire container to use the ao2 template hash
and cmp functions.  Also made use the OBJ_SEARCH_xxx names instead of the
deprecated names.

* Eliminates several unnecessary uses of RAII_VAR().

Review: https://reviewboard.asterisk.org/r/4524/
........

Merged revisions 433622 from http://svn.asterisk.org/svn/asterisk/branches/13

Modified:
    trunk/   (props changed)
    trunk/res/res_pjsip_registrar_expire.c

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-13-merged' - no diff available.

Modified: trunk/res/res_pjsip_registrar_expire.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_pjsip_registrar_expire.c?view=diff&rev=433623&r1=433622&r2=433623
==============================================================================
--- trunk/res/res_pjsip_registrar_expire.c (original)
+++ trunk/res/res_pjsip_registrar_expire.c Fri Mar 27 16:06:22 2015
@@ -57,32 +57,76 @@
 /*! \brief Hashing function for contact auto-expiration */
 static int contact_expiration_hash(const void *obj, const int flags)
 {
-	const struct contact_expiration *expiration = obj;
-	const char *id = obj;
-
-	return ast_str_hash(flags & OBJ_KEY ? id : ast_sorcery_object_get_id(expiration->contact));
+	const struct contact_expiration *object;
+	const char *key;
+
+	switch (flags & OBJ_SEARCH_MASK) {
+	case OBJ_SEARCH_KEY:
+		key = obj;
+		break;
+	case OBJ_SEARCH_OBJECT:
+		object = obj;
+		key = ast_sorcery_object_get_id(object->contact);
+		break;
+	default:
+		/* Hash can only work on something with a full key. */
+		ast_assert(0);
+		return 0;
+	}
+	return ast_str_hash(key);
 }
 
 /*! \brief Comparison function for contact auto-expiration */
 static int contact_expiration_cmp(void *obj, void *arg, int flags)
 {
-	struct contact_expiration *expiration1 = obj, *expiration2 = arg;
-	const char *id = arg;
-
-	return !strcmp(ast_sorcery_object_get_id(expiration1->contact), flags & OBJ_KEY ? id :
-		       ast_sorcery_object_get_id(expiration2->contact)) ? CMP_MATCH | CMP_STOP : 0;
+	const struct contact_expiration *object_left = obj;
+	const struct contact_expiration *object_right = arg;
+	const char *right_key = arg;
+	int cmp;
+
+	switch (flags & OBJ_SEARCH_MASK) {
+	case OBJ_SEARCH_OBJECT:
+		right_key = ast_sorcery_object_get_id(object_right->contact);
+		/* Fall through */
+	case OBJ_SEARCH_KEY:
+		cmp = strcmp(ast_sorcery_object_get_id(object_left->contact), right_key);
+		break;
+	case OBJ_SEARCH_PARTIAL_KEY:
+		/*
+		 * We could also use a partial key struct containing a length
+		 * so strlen() does not get called for every comparison instead.
+		 */
+		cmp = strncmp(ast_sorcery_object_get_id(object_left->contact), right_key,
+			strlen(right_key));
+		break;
+	default:
+		/*
+		 * What arg points to is specific to this traversal callback
+		 * and has no special meaning to astobj2.
+		 */
+		cmp = 0;
+		break;
+	}
+	if (cmp) {
+		return 0;
+	}
+	/*
+	 * At this point the traversal callback is identical to a sorted
+	 * container.
+	 */
+	return CMP_MATCH;
 }
 
 /*! \brief Scheduler function which deletes a contact */
 static int contact_expiration_expire(const void *data)
 {
-	RAII_VAR(struct contact_expiration *, expiration, (void*)data, ao2_cleanup);
+	struct contact_expiration *expiration = (void *) data;
 
 	expiration->sched = -1;
 
 	/* This will end up invoking the deleted observer callback, which will perform the unlinking and such */
 	ast_sorcery_delete(ast_sip_get_sorcery(), expiration->contact);
-
+	ao2_ref(expiration, -1);
 	return 0;
 }
 
@@ -90,14 +134,16 @@
 static void contact_expiration_observer_created(const void *object)
 {
 	const struct ast_sip_contact *contact = object;
-	RAII_VAR(struct contact_expiration *, expiration, NULL, ao2_cleanup);
+	struct contact_expiration *expiration;
 	int expires = MAX(0, ast_tvdiff_ms(contact->expiration_time, ast_tvnow()));
 
 	if (ast_tvzero(contact->expiration_time)) {
 		return;
 	}
 
-	if (!(expiration = ao2_alloc_options(sizeof(*expiration), contact_expiration_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK))) {
+	expiration = ao2_alloc_options(sizeof(*expiration), contact_expiration_destroy,
+		AO2_ALLOC_OPT_LOCK_NOLOCK);
+	if (!expiration) {
 		return;
 	}
 
@@ -106,39 +152,46 @@
 
 	ao2_ref(expiration, +1);
 	if ((expiration->sched = ast_sched_add(sched, expires, contact_expiration_expire, expiration)) < 0) {
-		ao2_cleanup(expiration);
+		ao2_ref(expiration, -1);
 		ast_log(LOG_ERROR, "Scheduled expiration for contact '%s' could not be performed, contact may persist past life\n",
 			ast_sorcery_object_get_id(contact));
-		return;
-	}
-
-	ao2_link(contact_autoexpire, expiration);
+	} else {
+		ao2_link(contact_autoexpire, expiration);
+	}
+	ao2_ref(expiration, -1);
 }
 
 /*! \brief Observer callback for when a contact is updated */
 static void contact_expiration_observer_updated(const void *object)
 {
 	const struct ast_sip_contact *contact = object;
-	RAII_VAR(struct contact_expiration *, expiration, ao2_find(contact_autoexpire, ast_sorcery_object_get_id(contact), OBJ_KEY), ao2_cleanup);
+	struct contact_expiration *expiration;
 	int expires = MAX(0, ast_tvdiff_ms(contact->expiration_time, ast_tvnow()));
 
+	expiration = ao2_find(contact_autoexpire, ast_sorcery_object_get_id(contact),
+		OBJ_SEARCH_KEY);
 	if (!expiration) {
 		return;
 	}
 
-	AST_SCHED_REPLACE_UNREF(expiration->sched, sched, expires, contact_expiration_expire, expiration, ao2_cleanup(expiration), ao2_cleanup(expiration), ao2_ref(expiration, +1));
+	AST_SCHED_REPLACE_UNREF(expiration->sched, sched, expires, contact_expiration_expire,
+		expiration, ao2_cleanup(expiration), ao2_cleanup(expiration), ao2_ref(expiration, +1));
+	ao2_ref(expiration, -1);
 }
 
 /*! \brief Observer callback for when a contact is deleted */
 static void contact_expiration_observer_deleted(const void *object)
 {
-	RAII_VAR(struct contact_expiration *, expiration, ao2_find(contact_autoexpire, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
-
+	struct contact_expiration *expiration;
+
+	expiration = ao2_find(contact_autoexpire, ast_sorcery_object_get_id(object),
+		OBJ_SEARCH_KEY | OBJ_UNLINK);
 	if (!expiration) {
 		return;
 	}
 
 	AST_SCHED_DEL_UNREF(sched, expiration->sched, ao2_cleanup(expiration));
+	ao2_ref(expiration, -1);
 }
 
 /*! \brief Observer callbacks for autoexpiring contacts */
@@ -166,13 +219,16 @@
 /*! \brief Initialize auto-expiration of any existing contacts */
 static void contact_expiration_initialize_existing(void)
 {
-	RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
-
-	if (!(contacts = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "contact", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) {
+	struct ao2_container *contacts;
+
+	contacts = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "contact",
+		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
+	if (!contacts) {
 		return;
 	}
 
 	ao2_callback(contacts, OBJ_NODATA, contact_expiration_setup, NULL);
+	ao2_ref(contacts, -1);
 }
 
 static int unload_observer_delete(void *obj, void *arg, int flags)




More information about the asterisk-commits mailing list