[Asterisk-code-review] res pjsip outbound registration.c: Eliminate simple RAII VAR... (asterisk[master])

Richard Mudgett asteriskteam at digium.com
Tue Jun 23 12:57:17 CDT 2015


Richard Mudgett has uploaded a new change for review.

  https://gerrit.asterisk.org/696

Change subject: res_pjsip_outbound_registration.c: Eliminate simple RAII_VAR() usage.
......................................................................

res_pjsip_outbound_registration.c: Eliminate simple RAII_VAR() usage.

Change-Id: I399cb9d61bbba706b48c98e0bf75e98984cd9a9e
---
M res/res_pjsip_outbound_registration.c
1 file changed, 48 insertions(+), 34 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/96/696/1

diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c
index d0b8687..d215bb5 100644
--- a/res/res_pjsip_outbound_registration.c
+++ b/res/res_pjsip_outbound_registration.c
@@ -360,9 +360,15 @@
 
 static struct sip_outbound_registration_state *get_state(const char *id)
 {
-	RAII_VAR(struct ao2_container *, states,
-		 ao2_global_obj_ref(current_states), ao2_cleanup);
-	return states ? ao2_find(states, id, OBJ_SEARCH_KEY) : NULL;
+	struct sip_outbound_registration_state *state = NULL;
+	struct ao2_container *states;
+
+	states = ao2_global_obj_ref(current_states);
+	if (states) {
+		state = ao2_find(states, id, OBJ_SEARCH_KEY);
+		ao2_ref(states, -1);
+	}
+	return state;
 }
 
 static struct ao2_container *get_registrations(void)
@@ -1085,8 +1091,8 @@
 /*! \brief Helper function which performs a single registration */
 static int sip_outbound_registration_perform(void *data)
 {
-	RAII_VAR(struct sip_outbound_registration_state *, state, data, ao2_cleanup);
-	RAII_VAR(struct sip_outbound_registration *, registration, ao2_bump(state->registration), ao2_cleanup);
+	struct sip_outbound_registration_state *state = data;
+	struct sip_outbound_registration *registration = ao2_bump(state->registration);
 	size_t i;
 
 	/* Just in case the client state is being reused for this registration, free the auth information */
@@ -1095,7 +1101,10 @@
 	AST_VECTOR_INIT(&state->client_state->outbound_auths, AST_VECTOR_SIZE(&registration->outbound_auths));
 	for (i = 0; i < AST_VECTOR_SIZE(&registration->outbound_auths); ++i) {
 		const char *name = ast_strdup(AST_VECTOR_GET(&registration->outbound_auths, i));
-		AST_VECTOR_APPEND(&state->client_state->outbound_auths, name);
+
+		if (name) {
+			AST_VECTOR_APPEND(&state->client_state->outbound_auths, name);
+		}
 	}
 	state->client_state->retry_interval = registration->retry_interval;
 	state->client_state->forbidden_retry_interval = registration->forbidden_retry_interval;
@@ -1108,6 +1117,8 @@
 
 	schedule_registration(state->client_state, (ast_random() % 10) + 1);
 
+	ao2_ref(registration, -1);
+	ao2_ref(state, -1);
 	return 0;
 }
 
@@ -1178,11 +1189,9 @@
 	}
 
 	ao2_lock(states);
-
 	if (state) {
 		ao2_unlink(states, state);
 	}
-
 	ao2_link(states, new_state);
 	ao2_unlock(states);
 
@@ -1223,23 +1232,22 @@
 
 static int unregister_task(void *obj)
 {
-	RAII_VAR(struct sip_outbound_registration_state*, state, obj, ao2_cleanup);
+	struct sip_outbound_registration_state *state = obj;
 	struct pjsip_regc *client = state->client_state->client;
 	pjsip_tx_data *tdata;
 	pjsip_regc_info info;
 
 	pjsip_regc_get_info(client, &info);
 	ast_debug(1, "Unregistering contacts with server '%s' from client '%s'\n",
-			state->registration->server_uri, state->registration->client_uri);
+		state->registration->server_uri, state->registration->client_uri);
 
 	cancel_registration(state->client_state);
 
-	if (pjsip_regc_unregister(client, &tdata) != PJ_SUCCESS) {
-		return 0;
+	if (pjsip_regc_unregister(client, &tdata) == PJ_SUCCESS) {
+		registration_client_send(state->client_state, tdata);
 	}
 
-	registration_client_send(state->client_state, tdata);
-
+	ao2_ref(state, -1);
 	return 0;
 }
 
@@ -1272,7 +1280,7 @@
 	int wordlen;
 	int which = 0;
 	struct sip_outbound_registration *registration;
-	RAII_VAR(struct ao2_container *, registrations, NULL, ao2_cleanup);
+	struct ao2_container *registrations;
 	struct ao2_iterator i;
 
 	if (pos != 3) {
@@ -1289,22 +1297,25 @@
 	i = ao2_iterator_init(registrations, 0);
 	while ((registration = ao2_iterator_next(&i))) {
 		const char *name = ast_sorcery_object_get_id(registration);
+
 		if (!strncasecmp(word, name, wordlen) && ++which > state) {
 			result = ast_strdup(name);
 		}
 
-		ao2_cleanup(registration);
+		ao2_ref(registration, -1);
 		if (result) {
 			break;
 		}
 	}
 	ao2_iterator_destroy(&i);
+
+	ao2_ref(registrations, -1);
 	return result;
 }
 
 static char *cli_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-	RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup);
+	struct sip_outbound_registration_state *state;
 	const char *registration_name;
 
 	switch (cmd) {
@@ -1332,15 +1343,15 @@
 
 	if (queue_unregister(state)) {
 		ast_cli(a->fd, "Failed to queue unregistration");
-		return 0;
 	}
 
+	ao2_ref(state, -1);
 	return CLI_SUCCESS;
 }
 
 static char *cli_register(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-	RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup);
+	struct sip_outbound_registration_state *state;
 	const char *registration_name;
 
 	switch (cmd) {
@@ -1372,20 +1383,18 @@
 	 */
 	if (queue_unregister(state)) {
 		ast_cli(a->fd, "Failed to queue unregistration");
-		return 0;
-	}
-	if (queue_register(state)) {
+	} else if (queue_register(state)) {
 		ast_cli(a->fd, "Failed to queue registration");
-		return 0;
 	}
 
+	ao2_ref(state, -1);
 	return CLI_SUCCESS;
 }
 
 static int ami_unregister(struct mansession *s, const struct message *m)
 {
 	const char *registration_name = astman_get_header(m, "Registration");
-	RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup);
+	struct sip_outbound_registration_state *state;
 
 	if (ast_strlen_zero(registration_name)) {
 		astman_send_error(s, m, "Registration parameter missing.");
@@ -1400,17 +1409,18 @@
 
 	if (queue_unregister(state)) {
 		astman_send_ack(s, m, "Failed to queue unregistration");
-		return 0;
+	} else {
+		astman_send_ack(s, m, "Unregistration sent");
 	}
 
-	astman_send_ack(s, m, "Unregistration sent");
+	ao2_ref(state, -1);
 	return 0;
 }
 
 static int ami_register(struct mansession *s, const struct message *m)
 {
 	const char *registration_name = astman_get_header(m, "Registration");
-	RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup);
+	struct sip_outbound_registration_state *state;
 
 	if (ast_strlen_zero(registration_name)) {
 		astman_send_error(s, m, "Registration parameter missing.");
@@ -1428,14 +1438,13 @@
 	 */
 	if (queue_unregister(state)) {
 		astman_send_ack(s, m, "Failed to queue unregistration");
-		return 0;
-	}
-	if (queue_register(state)) {
+	} else if (queue_register(state)) {
 		astman_send_ack(s, m, "Failed to queue unregistration");
-		return 0;
+	} else {
+		astman_send_ack(s, m, "Reregistration sent");
 	}
 
-	astman_send_ack(s, m, "Reregistration sent");
+	ao2_ref(state, -1);
 	return 0;
 }
 
@@ -1449,7 +1458,7 @@
 static int ami_outbound_registration_task(void *obj)
 {
 	struct sip_ami_outbound *ami = obj;
-	RAII_VAR(struct ast_str *, buf, NULL, ast_free);
+	struct ast_str *buf;
 	struct sip_outbound_registration_state *state;
 
 	buf = ast_sip_create_ami_event("OutboundRegistrationDetail", ami->ami);
@@ -1477,6 +1486,8 @@
 	}
 
 	astman_append(ami->ami->s, "%s\r\n", ast_str_buffer(buf));
+	ast_free(buf);
+
 	return ast_sip_format_auths_ami(&ami->registration->outbound_auths, ami->ami);
 }
 
@@ -1494,8 +1505,9 @@
 {
 	struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
 	struct sip_ami_outbound ami_outbound = { .ami = &ami };
-	RAII_VAR(struct ao2_container *, regs, get_registrations(), ao2_cleanup);
+	struct ao2_container *regs;
 
+	regs = get_registrations();
 	if (!regs) {
 		astman_send_error(s, m, "Unable to retrieve "
 				  "outbound registrations\n");
@@ -1515,6 +1527,8 @@
 		ami_outbound.registered,
 		ami_outbound.not_registered);
 	astman_send_list_complete_end(s);
+
+	ao2_ref(regs, -1);
 	return 0;
 }
 

-- 
To view, visit https://gerrit.asterisk.org/696
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I399cb9d61bbba706b48c98e0bf75e98984cd9a9e
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>



More information about the asterisk-code-review mailing list