[asterisk-commits] russell: branch russell/chanspy_fixes r101368 - /team/russell/chanspy_fixes/a...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jan 30 18:28:36 CST 2008


Author: russell
Date: Wed Jan 30 18:28:36 2008
New Revision: 101368

URL: http://svn.digium.com/view/asterisk?view=rev&rev=101368
Log:
At the suggestion of Josh, change this so it uses stack memory instead of heap.
I'm not sure why this didn't occur to me.  Thanks, Josh.  :)

Modified:
    team/russell/chanspy_fixes/apps/app_chanspy.c

Modified: team/russell/chanspy_fixes/apps/app_chanspy.c
URL: http://svn.digium.com/view/asterisk/team/russell/chanspy_fixes/apps/app_chanspy.c?view=diff&rev=101368&r1=101367&r2=101368
==============================================================================
--- team/russell/chanspy_fixes/apps/app_chanspy.c (original)
+++ team/russell/chanspy_fixes/apps/app_chanspy.c Wed Jan 30 18:28:36 2008
@@ -415,25 +415,16 @@
 	}
 	ast_mutex_unlock(&chanspy_ds->lock);
 
-	ast_mutex_destroy(&chanspy_ds->lock);
-	ast_free(chanspy_ds);
-
 	return NULL;
 }
 
 /*! \note Returns the channel in the chanspy_ds locked as well as the chanspy_ds locked */
-static struct chanspy_ds *setup_chanspy_ds(struct ast_channel *chan)
-{
-	struct chanspy_ds *chanspy_ds;
+static struct chanspy_ds *setup_chanspy_ds(struct ast_channel *chan, struct chanspy_ds *chanspy_ds)
+{
 	struct ast_datastore *datastore = NULL;
 
-	if (!(chanspy_ds = ast_calloc(1, sizeof(*chanspy_ds)))) {
-		ast_channel_unlock(chan);
-		return NULL;
-	}
-	
-	ast_mutex_init(&chanspy_ds->lock);
 	ast_mutex_lock(&chanspy_ds->lock);
+
 	chanspy_ds->chan = chan;
 
 	if (!(datastore = ast_channel_datastore_alloc(&chanspy_ds_info, NULL))) {
@@ -451,7 +442,7 @@
 
 static struct chanspy_ds *next_channel(struct ast_channel *chan,
 	const struct ast_channel *last, const char *spec,
-	const char *exten, const char *context)
+	const char *exten, const char *context, struct chanspy_ds *chanspy_ds)
 {
 	struct ast_channel *this;
 
@@ -471,7 +462,7 @@
 		goto redo;
 	}
 
-	return setup_chanspy_ds(this);
+	return setup_chanspy_ds(this, chanspy_ds);
 }
 
 static int common_exec(struct ast_channel *chan, const struct ast_flags *flags,
@@ -486,6 +477,9 @@
 	char *ptr;
 	int num;
 	int num_spyed_upon = 1;
+	struct chanspy_ds chanspy_ds;
+
+	ast_mutex_init(&chanspy_ds.lock);
 
 	if (chan->_state != AST_STATE_UP)
 		ast_answer(chan);
@@ -518,11 +512,11 @@
 		waitms = 100;
 		num_spyed_upon = 0;
 
-		for (peer_chanspy_ds = next_channel(chan, prev, spec, exten, context);
+		for (peer_chanspy_ds = next_channel(chan, prev, spec, exten, context, &chanspy_ds);
 		     peer_chanspy_ds;
 			 chanspy_ds_free(peer_chanspy_ds), prev = peer,
 		     peer_chanspy_ds = next_chanspy_ds ? next_chanspy_ds : 
-			 	next_channel(chan, prev, spec, exten, context), next_chanspy_ds = NULL) {
+			 	next_channel(chan, prev, spec, exten, context, &chanspy_ds), next_chanspy_ds = NULL) {
 			const char *group;
 			int igrp = !mygroup;
 			char *groups[25];
@@ -617,7 +611,7 @@
 				snprintf(nameprefix, AST_NAME_STRLEN, "%s/%d", spec, res);
 
 				if ((next = ast_get_channel_by_name_prefix_locked(nameprefix, strlen(nameprefix)))) {
-					next_chanspy_ds = setup_chanspy_ds(next);
+					next_chanspy_ds = setup_chanspy_ds(next, &chanspy_ds);
 				} else {
 					/* stay on this channel, if it is still valid */
 
@@ -626,6 +620,10 @@
 						ast_channel_lock(peer_chanspy_ds->chan);
 						next_chanspy_ds = peer_chanspy_ds;
 						peer_chanspy_ds = NULL;
+					} else {
+						/* the channel is gone */
+						ast_mutex_unlock(&peer_chanspy_ds->lock);
+						next_chanspy_ds = NULL;
 					}
 				}
 
@@ -639,6 +637,8 @@
 	ast_clear_flag(chan, AST_FLAG_SPYING);
 
 	ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
+
+	ast_mutex_destroy(&chanspy_ds.lock);
 
 	return res;
 }




More information about the asterisk-commits mailing list