[Asterisk-code-review] stasis bridges: Remove silly usage of RAII VAR. (asterisk[15])

Jenkins2 asteriskteam at digium.com
Thu Jan 11 06:29:52 CST 2018


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/7919 )

Change subject: stasis_bridges: Remove silly usage of RAII_VAR.
......................................................................

stasis_bridges: Remove silly usage of RAII_VAR.

Change-Id: I0fa7ab05454f183dc4ff10e26d18776d2b0fcf1f
---
M main/stasis_bridges.c
1 file changed, 58 insertions(+), 44 deletions(-)

Approvals:
  George Joseph: Looks good to me, but someone else must approve
  Richard Mudgett: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/main/stasis_bridges.c b/main/stasis_bridges.c
index d197e4d..4b68559 100644
--- a/main/stasis_bridges.c
+++ b/main/stasis_bridges.c
@@ -233,7 +233,7 @@
 
 struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge)
 {
-	RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
+	struct ast_bridge_snapshot *snapshot;
 	struct ast_bridge_channel *bridge_channel;
 
 	if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) {
@@ -249,17 +249,22 @@
 	if (ast_string_field_init(snapshot, 128)
 		|| ast_string_field_init_extended(snapshot, video_source_id)) {
 		ao2_ref(snapshot, -1);
+
 		return NULL;
 	}
 
 	snapshot->channels = ast_str_container_alloc(SNAPSHOT_CHANNELS_BUCKETS);
 	if (!snapshot->channels) {
+		ao2_ref(snapshot, -1);
+
 		return NULL;
 	}
 
 	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
 		if (ast_str_container_add(snapshot->channels,
 				ast_channel_uniqueid(bridge_channel->chan))) {
+			ao2_ref(snapshot, -1);
+
 			return NULL;
 		}
 	}
@@ -285,14 +290,13 @@
 			ast_channel_uniqueid(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc));
 	}
 
-	ao2_ref(snapshot, +1);
 	return snapshot;
 }
 
 void ast_bridge_publish_state(struct ast_bridge *bridge)
 {
-	RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct ast_bridge_snapshot *snapshot;
+	struct stasis_message *msg;
 
 	if (!ast_bridge_snapshot_type()) {
 		return;
@@ -306,17 +310,19 @@
 	}
 
 	msg = stasis_message_create(ast_bridge_snapshot_type(), snapshot);
+	ao2_ref(snapshot, -1);
 	if (!msg) {
 		return;
 	}
 
 	stasis_publish(ast_bridge_topic(bridge), msg);
+	ao2_ref(msg, -1);
 }
 
 static void bridge_publish_state_from_blob(struct ast_bridge *bridge,
 	struct ast_bridge_blob *obj)
 {
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct stasis_message *msg;
 
 	ast_assert(obj != NULL);
 
@@ -326,6 +332,7 @@
 	}
 
 	stasis_publish(ast_bridge_topic(bridge), msg);
+	ao2_ref(msg, -1);
 }
 
 /*! \brief Destructor for bridge merge messages */
@@ -342,7 +349,7 @@
 /*! \brief Bridge merge message creation helper */
 static struct ast_bridge_merge_message *bridge_merge_message_create(struct ast_bridge *to, struct ast_bridge *from)
 {
-	RAII_VAR(struct ast_bridge_merge_message *, msg, NULL, ao2_cleanup);
+	struct ast_bridge_merge_message *msg;
 
 	msg = ao2_alloc(sizeof(*msg), bridge_merge_message_dtor);
 	if (!msg) {
@@ -350,16 +357,13 @@
 	}
 
 	msg->to = ast_bridge_snapshot_create(to);
-	if (!msg->to) {
-		return NULL;
-	}
-
 	msg->from = ast_bridge_snapshot_create(from);
-	if (!msg->from) {
+	if (!msg->to || !msg->from) {
+		ao2_ref(msg, -1);
+
 		return NULL;
 	}
 
-	ao2_ref(msg, +1);
 	return msg;
 }
 
@@ -368,16 +372,17 @@
 	const struct stasis_message_sanitizer *sanitize)
 {
 	struct ast_bridge_merge_message *merge = stasis_message_data(msg);
-	RAII_VAR(struct ast_json *, json_bridge_to,
-		ast_bridge_snapshot_to_json(merge->to, sanitize), ast_json_unref);
-	RAII_VAR(struct ast_json *, json_bridge_from,
-		ast_bridge_snapshot_to_json(merge->from, sanitize), ast_json_unref);
+	struct ast_json *json_bridge_to = ast_bridge_snapshot_to_json(merge->to, sanitize);
+	struct ast_json *json_bridge_from = ast_bridge_snapshot_to_json(merge->from, sanitize);
 
 	if (!json_bridge_to || !json_bridge_from) {
+		ast_json_unref(json_bridge_to);
+		ast_json_unref(json_bridge_from);
+
 		return NULL;
 	}
 
-	return ast_json_pack("{s: s, s: o, s: O, s: O}",
+	return ast_json_pack("{s: s, s: o, s: o, s: o}",
 		"type", "BridgeMerged",
 		"timestamp", ast_json_timeval(*stasis_message_timestamp(msg), NULL),
 		"bridge", json_bridge_to,
@@ -386,8 +391,8 @@
 
 void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from)
 {
-	RAII_VAR(struct ast_bridge_merge_message *, merge_msg, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct ast_bridge_merge_message *merge_msg;
+	struct stasis_message *msg;
 
 	if (!ast_bridge_merge_message_type()) {
 		return;
@@ -404,11 +409,13 @@
 	}
 
 	msg = stasis_message_create(ast_bridge_merge_message_type(), merge_msg);
+	ao2_ref(merge_msg, -1);
 	if (!msg) {
 		return;
 	}
 
 	stasis_publish(ast_bridge_topic_all(), msg);
+	ao2_ref(msg, -1);
 }
 
 static void bridge_blob_dtor(void *obj)
@@ -428,8 +435,8 @@
 	struct ast_channel *chan,
 	struct ast_json *blob)
 {
-	RAII_VAR(struct ast_bridge_blob *, obj, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct ast_bridge_blob *obj;
+	struct stasis_message *msg;
 
 	if (!message_type) {
 		return NULL;
@@ -443,6 +450,8 @@
 	if (bridge) {
 		obj->bridge = ast_bridge_snapshot_create(bridge);
 		if (obj->bridge == NULL) {
+			ao2_ref(obj, -1);
+
 			return NULL;
 		}
 	}
@@ -450,6 +459,8 @@
 	if (chan) {
 		obj->channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan));
 		if (obj->channel == NULL) {
+			ao2_ref(obj, -1);
+
 			return NULL;
 		}
 	}
@@ -459,19 +470,16 @@
 	}
 
 	msg = stasis_message_create(message_type, obj);
-	if (!msg) {
-		return NULL;
-	}
+	ao2_ref(obj, -1);
 
-	ao2_ref(msg, +1);
 	return msg;
 }
 
 void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *chan,
 		struct ast_channel *swap)
 {
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
-	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+	struct stasis_message *msg;
+	struct ast_json *blob = NULL;
 
 	if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) {
 		return;
@@ -485,6 +493,7 @@
 	}
 
 	msg = ast_bridge_blob_create(ast_channel_entered_bridge_type(), bridge, chan, blob);
+	ast_json_unref(blob);
 	if (!msg) {
 		return;
 	}
@@ -492,11 +501,12 @@
 	/* enter blob first, then state */
 	stasis_publish(ast_bridge_topic(bridge), msg);
 	bridge_publish_state_from_blob(bridge, stasis_message_data(msg));
+	ao2_ref(msg, -1);
 }
 
 void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *chan)
 {
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct stasis_message *msg;
 
 	if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) {
 		return;
@@ -509,6 +519,7 @@
 	/* state first, then leave blob (opposite of enter, preserves nesting of events) */
 	bridge_publish_state_from_blob(bridge, stasis_message_data(msg));
 	stasis_publish(ast_bridge_topic(bridge), msg);
+	ao2_ref(msg, -1);
 }
 
 static struct ast_json *simple_bridge_channel_event(
@@ -518,16 +529,17 @@
 	const struct timeval *tv,
 	const struct stasis_message_sanitizer *sanitize)
 {
-	RAII_VAR(struct ast_json *, json_bridge,
-		ast_bridge_snapshot_to_json(bridge_snapshot, sanitize), ast_json_unref);
-	RAII_VAR(struct ast_json *, json_channel,
-		ast_channel_snapshot_to_json(channel_snapshot, sanitize), ast_json_unref);
+	struct ast_json *json_bridge = ast_bridge_snapshot_to_json(bridge_snapshot, sanitize);
+	struct ast_json *json_channel = ast_channel_snapshot_to_json(channel_snapshot, sanitize);
 
 	if (!json_bridge || !json_channel) {
+		ast_json_unref(json_bridge);
+		ast_json_unref(json_channel);
+
 		return NULL;
 	}
 
-	return ast_json_pack("{s: s, s: o, s: O, s: O}",
+	return ast_json_pack("{s: s, s: o, s: o, s: o}",
 		"type", type,
 		"timestamp", ast_json_timeval(*tv, NULL),
 		"bridge", json_bridge,
@@ -557,9 +569,10 @@
 static struct ast_json *container_to_json_array(struct ao2_container *items,
 	const struct stasis_message_sanitizer *sanitize)
 {
-	RAII_VAR(struct ast_json *, json_items, ast_json_array_create(), ast_json_unref);
+	struct ast_json *json_items = ast_json_array_create();
 	char *item;
 	struct ao2_iterator it;
+
 	if (!json_items) {
 		return NULL;
 	}
@@ -573,12 +586,14 @@
 		if (ast_json_array_append(json_items, ast_json_string_create(item))) {
 			ao2_cleanup(item);
 			ao2_iterator_destroy(&it);
+			ast_json_unref(json_items);
+
 			return NULL;
 		}
 	}
 	ao2_iterator_destroy(&it);
 
-	return ast_json_ref(json_items);
+	return json_items;
 }
 
 static const char *capability2str(uint32_t capabilities)
@@ -594,7 +609,7 @@
 	const struct ast_bridge_snapshot *snapshot,
 	const struct stasis_message_sanitizer *sanitize)
 {
-	RAII_VAR(struct ast_json *, json_bridge, NULL, ast_json_unref);
+	struct ast_json *json_bridge;
 	struct ast_json *json_channels;
 
 	if (snapshot == NULL) {
@@ -625,7 +640,7 @@
 			ast_json_string_create(snapshot->video_source_id));
 	}
 
-	return ast_json_ref(json_bridge);
+	return json_bridge;
 }
 
 /*!
@@ -1225,7 +1240,7 @@
 
 void ast_bridge_publish_attended_transfer(struct ast_attended_transfer_message *transfer_msg)
 {
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct stasis_message *msg;
 
 	msg = stasis_message_create(ast_attended_transfer_type(), transfer_msg);
 	if (!msg) {
@@ -1233,11 +1248,12 @@
 	}
 
 	stasis_publish(ast_bridge_topic_all(), msg);
+	ao2_ref(msg, -1);
 }
 
 struct ast_bridge_snapshot *ast_bridge_snapshot_get_latest(const char *uniqueid)
 {
-	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+	struct stasis_message *message;
 	struct ast_bridge_snapshot *snapshot;
 
 	ast_assert(!ast_strlen_zero(uniqueid));
@@ -1249,11 +1265,9 @@
 		return NULL;
 	}
 
-	snapshot = stasis_message_data(message);
-	if (!snapshot) {
-		return NULL;
-	}
-	ao2_ref(snapshot, +1);
+	snapshot = ao2_bump(stasis_message_data(message));
+	ao2_ref(message, -1);
+
 	return snapshot;
 }
 

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

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: merged
Gerrit-Change-Id: I0fa7ab05454f183dc4ff10e26d18776d2b0fcf1f
Gerrit-Change-Number: 7919
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180111/aeb0309d/attachment-0001.html>


More information about the asterisk-code-review mailing list