[Asterisk-code-review] res pjsip session.c: Restructure ast sip session alloc() (asterisk[13])

Richard Mudgett asteriskteam at digium.com
Fri Apr 21 14:45:23 CDT 2017


Richard Mudgett has uploaded a new change for review. ( https://gerrit.asterisk.org/5513 )

Change subject: res_pjsip_session.c: Restructure ast_sip_session_alloc()
......................................................................

res_pjsip_session.c: Restructure ast_sip_session_alloc()

* Restructure ast_sip_session_alloc() to need less cleanup on off nominal
error paths.

* Made ast_sip_session_alloc() and ast_sip_session_create_outgoing() avoid
unnecessary ref manipulation to return a session.  This is faster than
calling a function.  That function may do logging of the ref changes with
REF_DEBUG enabled.

Change-Id: I2a0affc4be51013d3f0485782c96b8fee3ddb00a
---
M res/res_pjsip_session.c
1 file changed, 45 insertions(+), 37 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/13/5513/1

diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index 0a18a80..a3110f5 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -1326,9 +1326,7 @@
 	ao2_cleanup(session->req_caps);
 	ao2_cleanup(session->direct_media_cap);
 
-	if (session->dsp) {
-		ast_dsp_free(session->dsp);
-	}
+	ast_dsp_free(session->dsp);
 
 	if (session->inv_session) {
 		pjsip_dlg_dec_session(session->inv_session->dlg, &session_module);
@@ -1398,6 +1396,7 @@
 	struct ast_sip_contact *contact, pjsip_inv_session *inv_session, pjsip_rx_data *rdata)
 {
 	RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
+	struct ast_sip_session *ret_session;
 	struct ast_sip_session_supplement *iter;
 	int dsp_features = 0;
 
@@ -1405,10 +1404,37 @@
 	if (!session) {
 		return NULL;
 	}
+
 	AST_LIST_HEAD_INIT(&session->supplements);
+	AST_LIST_HEAD_INIT_NOLOCK(&session->delayed_requests);
+	ast_party_id_init(&session->id);
+
+	session->direct_media_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	if (!session->direct_media_cap) {
+		return NULL;
+	}
+	session->req_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	if (!session->req_caps) {
+		return NULL;
+	}
 	session->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp);
 	if (!session->datastores) {
 		return NULL;
+	}
+
+	if (endpoint->dtmf == AST_SIP_DTMF_INBAND || endpoint->dtmf == AST_SIP_DTMF_AUTO) {
+		dsp_features |= DSP_FEATURE_DIGIT_DETECT;
+	}
+	if (endpoint->faxdetect) {
+		dsp_features |= DSP_FEATURE_FAX_DETECT;
+	}
+	if (dsp_features) {
+		session->dsp = ast_dsp_new();
+		if (!session->dsp) {
+			return NULL;
+		}
+
+		ast_dsp_set_features(session->dsp, dsp_features);
 	}
 
 	session->endpoint = ao2_bump(endpoint);
@@ -1447,30 +1473,6 @@
 	inv_session->mod_data[session_module.id] = ao2_bump(session);
 	session->contact = ao2_bump(contact);
 	session->inv_session = inv_session;
-	session->req_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
-	if (!session->req_caps) {
-		/* Release the ref held by session->inv_session */
-		ao2_ref(session, -1);
-		return NULL;
-	}
-
-	if ((endpoint->dtmf == AST_SIP_DTMF_INBAND) || (endpoint->dtmf == AST_SIP_DTMF_AUTO)) {
-		dsp_features |= DSP_FEATURE_DIGIT_DETECT;
-	}
-
-	if (endpoint->faxdetect) {
-		dsp_features |= DSP_FEATURE_FAX_DETECT;
-	}
-
-	if (dsp_features) {
-		if (!(session->dsp = ast_dsp_new())) {
-			/* Release the ref held by session->inv_session */
-			ao2_ref(session, -1);
-			return NULL;
-		}
-
-		ast_dsp_set_features(session->dsp, dsp_features);
-	}
 
 	if (add_supplements(session)) {
 		/* Release the ref held by session->inv_session */
@@ -1482,11 +1484,11 @@
 			iter->session_begin(session);
 		}
 	}
-	session->direct_media_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
-	AST_LIST_HEAD_INIT_NOLOCK(&session->delayed_requests);
-	ast_party_id_init(&session->id);
-	ao2_ref(session, +1);
-	return session;
+
+	/* Avoid unnecessary ref manipulation to return a session */
+	ret_session = session;
+	session = NULL;
+	return ret_session;
 }
 
 /*! \brief struct controlling the suspension of the session's serializer. */
@@ -1702,6 +1704,7 @@
 	pjsip_dialog *dlg;
 	struct pjsip_inv_session *inv_session;
 	RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
+	struct ast_sip_session *ret_session;
 
 	/* If no location has been provided use the AOR list from the endpoint itself */
 	if (location || !contact) {
@@ -1758,14 +1761,17 @@
 	if (ast_format_cap_count(req_caps)) {
 		/* get joint caps between req_caps and endpoint caps */
 		struct ast_format_cap *joint_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
-		ast_format_cap_get_compatible(req_caps, session->endpoint->media.codecs, joint_caps);
+
+		ast_format_cap_get_compatible(req_caps, endpoint->media.codecs, joint_caps);
 
 		/* if joint caps */
 		if (ast_format_cap_count(joint_caps)) {
 			/* copy endpoint caps into session->req_caps */
-			ast_format_cap_append_from_cap(session->req_caps, session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
+			ast_format_cap_append_from_cap(session->req_caps,
+				endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
 			/* replace instances of joint caps equivalents in session->req_caps */
-			ast_format_cap_replace_from_cap(session->req_caps, joint_caps, AST_MEDIA_TYPE_UNKNOWN);
+			ast_format_cap_replace_from_cap(session->req_caps, joint_caps,
+				AST_MEDIA_TYPE_UNKNOWN);
 		}
 		ao2_cleanup(joint_caps);
 	}
@@ -1779,8 +1785,10 @@
 		return NULL;
 	}
 
-	ao2_ref(session, +1);
-	return session;
+	/* Avoid unnecessary ref manipulation to return a session */
+	ret_session = session;
+	session = NULL;
+	return ret_session;
 }
 
 void ast_sip_session_terminate(struct ast_sip_session *session, int response)

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

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



More information about the asterisk-code-review mailing list