[asterisk-commits] tilghman: trunk r268817 - in /trunk/channels: chan_sip.c sip/include/sip.h

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jun 7 17:47:25 CDT 2010


Author: tilghman
Date: Mon Jun  7 17:47:13 2010
New Revision: 268817

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=268817
Log:
Mailbox list would previously grow at each reload, containing duplicates.

Also, optimize the allocation of mailboxes to avoid additional memory structures.

(closes issue #16320)
 Reported by: Marquis
 Patches: 
       20100525__issue16320.diff.txt uploaded by tilghman (license 14)

Modified:
    trunk/channels/chan_sip.c
    trunk/channels/sip/include/sip.h

Modified: trunk/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_sip.c?view=diff&rev=268817&r1=268816&r2=268817
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Mon Jun  7 17:47:13 2010
@@ -4145,10 +4145,6 @@
 /*! Destroy mailbox subscriptions */
 static void destroy_mailbox(struct sip_mailbox *mailbox)
 {
-	if (mailbox->mailbox)
-		ast_free(mailbox->mailbox);
-	if (mailbox->context)
-		ast_free(mailbox->context);
 	if (mailbox->event_sub)
 		ast_event_unsubscribe(mailbox->event_sub);
 	ast_free(mailbox);
@@ -24933,17 +24929,35 @@
 
 	while ((mbox = context = strsep(&next, ","))) {
 		struct sip_mailbox *mailbox;
-
-		if (!(mailbox = ast_calloc(1, sizeof(*mailbox))))
+		int duplicate = 0;
+
+		strsep(&context, "@");
+
+		if (ast_strlen_zero(mbox)) {
 			continue;
-
-		strsep(&context, "@");
-		if (ast_strlen_zero(mbox)) {
-			ast_free(mailbox);
+		}
+
+		/* Check whether the mailbox is already in the list */
+		AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
+			if (!strcmp(mailbox->mailbox, mbox) && !strcmp(S_OR(mailbox->context, ""), S_OR(context, ""))) {
+				duplicate = 1;
+				mailbox->delme = 0;
+				break;
+			}
+		}
+		if (duplicate) {
 			continue;
 		}
-		mailbox->mailbox = ast_strdup(mbox);
-		mailbox->context = ast_strdup(context);
+
+		if (!(mailbox = ast_calloc(1, sizeof(*mailbox) + strlen(mbox) + strlen(S_OR(context, ""))))) {
+			continue;
+		}
+
+		if (!ast_strlen_zero(context)) {
+			mailbox->context = mailbox->mailbox + strlen(mbox) + 1;
+			strcpy(mailbox->context, context); /* SAFE */
+		}
+		strcpy(mailbox->mailbox, mbox); /* SAFE */
 
 		AST_LIST_INSERT_TAIL(&peer->mailboxes, mailbox, entry);
 	}
@@ -25034,6 +25048,13 @@
 	peer->auth = NULL;
 	peer->default_outbound_transport = 0;
 	peer->transports = 0;
+
+	if (!devstate_only) {
+		struct sip_mailbox *mailbox;
+		AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
+			mailbox->delme = 1;
+		}
+	}
 
 	for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
 		if (!devstate_only) {
@@ -25413,6 +25434,17 @@
 		}
 	}
 
+	if (!devstate_only) {
+		struct sip_mailbox *mailbox;
+		AST_LIST_TRAVERSE_SAFE_BEGIN(&peer->mailboxes, mailbox, entry) {
+			if (mailbox->delme) {
+				AST_LIST_REMOVE_CURRENT(entry);
+				ast_free(mailbox);
+			}
+		}
+		AST_LIST_TRAVERSE_SAFE_END;
+	}
+
 	if (!can_parse_xml && (ast_get_cc_agent_policy(peer->cc_params) == AST_CC_AGENT_NATIVE)) {
 		ast_log(LOG_WARNING, "Peer %s has a cc_agent_policy of 'native' but required libxml2 dependency is not installed. Changing policy to 'never'\n", peer->name);
 		ast_set_cc_agent_policy(peer->cc_params, AST_CC_AGENT_NEVER);

Modified: trunk/channels/sip/include/sip.h
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/sip/include/sip.h?view=diff&rev=268817&r1=268816&r2=268817
==============================================================================
--- trunk/channels/sip/include/sip.h (original)
+++ trunk/channels/sip/include/sip.h Mon Jun  7 17:47:13 2010
@@ -1112,11 +1112,12 @@
  * too much effort ...
  */
 struct sip_mailbox {
-	char *mailbox;
-	char *context;
 	/*! Associated MWI subscription */
 	struct ast_event_sub *event_sub;
 	AST_LIST_ENTRY(sip_mailbox) entry;
+	unsigned int delme:1;
+	char *context;
+	char mailbox[2];
 };
 
 /*! \brief Structure for SIP peer data, we place calls to peers if registered  or fixed IP address (host)




More information about the asterisk-commits mailing list