[asterisk-commits] trunk - r8044 in /trunk/apps: app_queue.c app_rpt.c app_voicemail.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Jan 12 21:25:25 CST 2006


Author: bweschke
Date: Thu Jan 12 21:25:23 2006
New Revision: 8044

URL: http://svn.digium.com/view/asterisk?rev=8044&view=rev
Log:
 More memory wrapper and cleanup work. #6226 w/one very minor compile fix mod in app_rpt.c


Modified:
    trunk/apps/app_queue.c
    trunk/apps/app_rpt.c
    trunk/apps/app_voicemail.c

Modified: trunk/apps/app_queue.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_queue.c?rev=8044&r1=8043&r2=8044&view=diff
==============================================================================
--- trunk/apps/app_queue.c (original)
+++ trunk/apps/app_queue.c Thu Jan 12 21:25:23 2006
@@ -514,9 +514,8 @@
 	struct statechange *sc;
 	pthread_t t;
 	pthread_attr_t attr;
-
-	sc = ast_calloc(1, sizeof(*sc) + strlen(dev) + 1);
-	if (sc) {
+	
+	if ((sc = ast_calloc(1, sizeof(*sc) + strlen(dev) + 1))) {
 		sc->state = state;
 		strcpy(sc->dev, dev);
 		pthread_attr_init(&attr);
@@ -535,9 +534,7 @@
 	
 	/* Add a new member */
 
-	cur = ast_calloc(1, sizeof(*cur));
-
-	if (cur) {
+	if ((cur = ast_calloc(1, sizeof(*cur)))) {
 		cur->penalty = penalty;
 		cur->paused = paused;
 		ast_copy_string(cur->interface, interface, sizeof(cur->interface));
@@ -553,8 +550,7 @@
 {
 	struct ast_call_queue *q;
 
-	q = ast_calloc(1, sizeof(*q));
-	if (q) {
+	if ((q = ast_calloc(1, sizeof(*q)))) {
 		ast_mutex_init(&q->lock);
 		ast_copy_string(q->name, queuename, sizeof(q->name));
 	}
@@ -827,8 +823,7 @@
 
 	/* Create a new queue if an in-core entry does not exist yet. */
 	if (!q) {
-		q = alloc_queue(queuename);
-		if (!q)
+		if (!(q = alloc_queue(queuename)))
 			return NULL;
 		ast_mutex_lock(&q->lock);
 		clear_queue(q);
@@ -1704,11 +1699,8 @@
 						if (in->cid.cid_ani) {
 							if (o->chan->cid.cid_ani)
 								free(o->chan->cid.cid_ani);
-							o->chan->cid.cid_ani = ast_calloc(1, strlen(in->cid.cid_ani) + 1);
-							if (o->chan->cid.cid_ani)
+							if ((o->chan->cid.cid_ani = ast_calloc(1, strlen(in->cid.cid_ani) + 1)))
 								strncpy(o->chan->cid.cid_ani, in->cid.cid_ani, strlen(in->cid.cid_ani) + 1);
-							else
-								ast_log(LOG_WARNING, "Out of memory\n");
 						}
 						if (o->chan->cid.cid_rdnis) 
 							free(o->chan->cid.cid_rdnis);
@@ -2060,12 +2052,10 @@
 		announce = announceoverride;
 
 	while(cur) {
-		tmp = ast_calloc(1, sizeof(*tmp));
-		if (!tmp) {
+		if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
 			ast_mutex_unlock(&qe->parent->lock);
 			if (use_weight) 
 				ast_mutex_unlock(&qlock);
-			ast_log(LOG_WARNING, "Out of memory\n");
 			goto out;
 		}
 		tmp->stillgoing = -1;
@@ -3204,7 +3194,9 @@
 			}
 			if (!q) {
 				/* Make one then */
-				q = alloc_queue(cat);
+				if (!(q = alloc_queue(cat))) {
+					/* TODO: Handle memory allocation failure */
+				}
 				new = 1;
 			} else
 				new = 0;
@@ -3681,8 +3673,8 @@
 		}
 	case 7:
 		if (state < 100) {	/* 0-99 */
-			char *num = ast_malloc(3);
-			if (num) {
+			char *num;
+			if ((num = ast_malloc(3))) {
 				sprintf(num, "%d", state);
 			}
 			return num;

Modified: trunk/apps/app_rpt.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_rpt.c?rev=8044&r1=8043&r2=8044&view=diff
==============================================================================
--- trunk/apps/app_rpt.c (original)
+++ trunk/apps/app_rpt.c Thu Jan 12 21:25:23 2006
@@ -1041,8 +1041,7 @@
 	
 	
 	/* allocate a pseudo-channel thru asterisk */
-	mychannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL);
-	if (!mychannel)
+	if (!(mychannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL)))
 	{
 		fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
 		ast_mutex_lock(&myrpt->lock);
@@ -1312,11 +1311,9 @@
 			{
 				l = l->next;
 				continue;
-			}
-			m = ast_malloc(sizeof(*m));
-			if (!m)
+			}			
+			if (!(m = ast_malloc(sizeof(*m))))
 			{
-				ast_log(LOG_WARNING, "Cannot alloc memory on %s\n", mychannel->name);
 				ast_mutex_lock(&myrpt->lock);
 				remque((struct qelem *)mytele);
 				ast_mutex_unlock(&myrpt->lock);
@@ -1563,16 +1560,12 @@
 struct rpt_tele *tele;
 struct rpt_link *mylink = (struct rpt_link *) data;
 pthread_attr_t attr;
-
-	tele = ast_malloc(sizeof(*tele));
-	if (!tele)
-	{
-		ast_log(LOG_WARNING, "Unable to allocate memory\n");
+	
+	if (!(tele = ast_calloc(1, sizeof(*tele))))
+	{
 		pthread_exit(NULL);
 		return;
 	}
-	/* zero it out */
-	memset((char *)tele,0,sizeof(struct rpt_tele));
 	tele->rpt = myrpt;
 	tele->mode = mode;
 	ast_mutex_lock(&myrpt->lock);
@@ -1605,8 +1598,7 @@
 
 	myrpt->mydtmf = 0;
 	/* allocate a pseudo-channel thru asterisk */
-	mychannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL);
-	if (!mychannel)
+	if (!(mychannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL)))
 	{
 		fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
 		pthread_exit(NULL);
@@ -1624,8 +1616,7 @@
 		pthread_exit(NULL);
 	}
 	/* allocate a pseudo-channel thru asterisk */
-	genchannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL);
-	if (!genchannel)
+	if (!(genchannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL)))
 	{
 		fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
 		ast_hangup(mychannel);
@@ -1951,10 +1942,8 @@
 			} else
 				ast_mutex_unlock(&myrpt->lock);
 			strncpy(myrpt->lastlinknode,digitbuf,MAXNODESTR - 1);
-			/* establish call in monitor mode */
-			l = ast_calloc(1, sizeof(*l));
-			if (!l){
-				ast_log(LOG_WARNING, "Unable to malloc\n");
+			/* establish call in monitor mode */			
+			if (!(l = ast_calloc(1, sizeof(*l)))) {
 				return DC_ERROR;
 			}
 			/* zero the silly thing */
@@ -1993,8 +1982,7 @@
 				return DC_ERROR;
 			}
 			/* allocate a pseudo-channel thru asterisk */
-			l->pchan = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL);
-			if (!l->pchan){
+			if (!(l->pchan = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL))) {
 				fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
 				ast_hangup(l->chan);
 				free(l);
@@ -2065,9 +2053,7 @@
 				ast_mutex_unlock(&myrpt->lock);
 			strncpy(myrpt->lastlinknode,digitbuf,MAXNODESTR - 1);
 			/* establish call in tranceive mode */
-			l = ast_calloc(1, sizeof(*l));
-			if (!l){
-				ast_log(LOG_WARNING, "Unable to malloc\n");
+			if (!(l = ast_calloc(1, sizeof(*l)))) {
 				return(DC_ERROR);
 			}
 			l->mode = 1;
@@ -2107,8 +2093,7 @@
 				return DC_ERROR;
 			}
 			/* allocate a pseudo-channel thru asterisk */
-			l->pchan = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL);
-			if (!l->pchan){
+			if (!(l->pchan = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL))) {
 				fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
 				ast_hangup(l->chan);
 				free(l);
@@ -4711,8 +4696,7 @@
 	ast_indicate(myrpt->txchannel,AST_CONTROL_RADIO_KEY);
 	ast_indicate(myrpt->txchannel,AST_CONTROL_RADIO_UNKEY);
 	/* allocate a pseudo-channel thru asterisk */
-	myrpt->pchannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL);
-	if (!myrpt->pchannel)
+	if (!(myrpt->pchannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL)))
 	{
 		fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
 		ast_mutex_unlock(&myrpt->lock);
@@ -4760,8 +4744,7 @@
 	/* save pseudo channel conference number */
 	myrpt->conf = ci.confno;
 	/* allocate a pseudo-channel thru asterisk */
-	myrpt->txpchannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL);
-	if (!myrpt->txpchannel)
+	if (!(myrpt->txpchannel = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL)))
 	{
 		fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
 		ast_mutex_unlock(&myrpt->lock);
@@ -6014,9 +5997,7 @@
 		}
 
 		l=strlen(options)+2;
-		orig_s=ast_malloc(l);
-		if(!orig_s) {
-			ast_log(LOG_WARNING, "Out of memory\n");
+		if (!(orig_s = ast_malloc(l))) {
 			return -1;
 		}
 		s=orig_s;
@@ -6226,10 +6207,8 @@
 		} else 
 			ast_mutex_unlock(&myrpt->lock);
 		/* establish call in tranceive mode */
-		l = ast_calloc(1, sizeof(*l));
-		if (!l)
-		{
-			ast_log(LOG_WARNING, "Unable to malloc\n");
+		if (!(l = ast_calloc(1, sizeof(*l))))
+		{
 			pthread_exit(NULL);
 		}
 		l->mode = 1;
@@ -6242,8 +6221,7 @@
 		ast_set_read_format(l->chan,AST_FORMAT_SLINEAR);
 		ast_set_write_format(l->chan,AST_FORMAT_SLINEAR);
 		/* allocate a pseudo-channel thru asterisk */
-		l->pchan = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL);
-		if (!l->pchan)
+		if (!(l->pchan = ast_request("zap",AST_FORMAT_SLINEAR,"pseudo",NULL)))
 		{
 			fprintf(stderr,"rpt:Sorry unable to obtain pseudo channel\n");
 			pthread_exit(NULL);

Modified: trunk/apps/app_voicemail.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_voicemail.c?rev=8044&r1=8043&r2=8044&view=diff
==============================================================================
--- trunk/apps/app_voicemail.c (original)
+++ trunk/apps/app_voicemail.c Thu Jan 12 21:25:23 2006
@@ -532,12 +532,7 @@
 	struct ast_variable *var, *tmp;
 	struct ast_vm_user *retval;
 
-	if (ivm)
-		retval=ivm;
-	else
-		retval=ast_calloc(1, sizeof(*retval));
-
-	if (retval) {
+	if ((retval = (ivm ? ivm : ast_calloc(1, sizeof(*retval))))) {
 		if (!ivm)
 			ast_set_flag(retval, VM_ALLOCED);	
 		else
@@ -596,12 +591,8 @@
 		cur=cur->next;
 	}
 	if (cur) {
-		if (ivm)
-			vmu = ivm;
-		else
-			/* Make a copy, so that on a reload, we have no race */
-			vmu = ast_malloc(sizeof(*vmu));
-		if (vmu) {
+		/* Make a copy, so that on a reload, we have no race */
+		if ((vmu = (ivm ? ivm : ast_malloc(sizeof(*vmu))))) {
 			memcpy(vmu, cur, sizeof(*vmu));
 			ast_set2_flag(vmu, !ivm, VM_ALLOCED);	
 			vmu->next = NULL;
@@ -1462,7 +1453,7 @@
 	int txtsize = 0;
 
 	txtsize = (strlen(file) + 5)*sizeof(char);
-	txt = (char *)alloca(txtsize);
+	txt = alloca(txtsize);
 	/* Sprintf here would safe because we alloca'd exactly the right length,
 	 * but trying to eliminate all sprintf's anyhow
 	 */
@@ -1689,8 +1680,8 @@
 		strftime(date, sizeof(date), emaildateformat, &tm);
 
 		if (*fromstring) {
-			struct ast_channel *ast = ast_channel_alloc(0);
-			if (ast) {
+			struct ast_channel *ast;
+			if ((ast = ast_channel_alloc(0))) {
 				char *passdata;
 				int vmlen = strlen(fromstring)*3 + 200;
 				if ((passdata = alloca(vmlen))) {
@@ -1706,8 +1697,8 @@
 		fprintf(p, "To: %s <%s>\n", vmu->fullname, vmu->email);
 
 		if (emailsubject) {
-			struct ast_channel *ast = ast_channel_alloc(0);
-			if (ast) {
+			struct ast_channel *ast;
+			if ((ast = ast_channel_alloc(0))) {
 				char *passdata;
 				int vmlen = strlen(emailsubject)*3 + 200;
 				if ((passdata = alloca(vmlen))) {
@@ -1738,8 +1729,8 @@
 		}
 		fprintf(p, "Content-Type: text/plain; charset=%s\nContent-Transfer-Encoding: 8bit\n\n", charset);
 		if (emailbody) {
-			struct ast_channel *ast = ast_channel_alloc(0);
-			if (ast) {
+			struct ast_channel *ast;
+			if ((ast = ast_channel_alloc(0))) {
 				char *passdata;
 				int vmlen = strlen(emailbody)*3 + 200;
 				if ((passdata = alloca(vmlen))) {
@@ -1807,8 +1798,8 @@
 		fprintf(p, "Date: %s\n", date);
 
 		if (*pagerfromstring) {
-			struct ast_channel *ast = ast_channel_alloc(0);
-			if (ast) {
+			struct ast_channel *ast;
+			if ((ast = ast_channel_alloc(0))) {
 				char *passdata;
 				int vmlen = strlen(fromstring)*3 + 200;
 				if ((passdata = alloca(vmlen))) {
@@ -1824,8 +1815,8 @@
 			fprintf(p, "From: Asterisk PBX <%s>\n", who);
 		fprintf(p, "To: %s\n", pager);
                if (pagersubject) {
-                       struct ast_channel *ast = ast_channel_alloc(0);
-                       if (ast) {
+                       struct ast_channel *ast;
+                       if ((ast = ast_channel_alloc(0))) {
                                char *passdata;
                                int vmlen = strlen(pagersubject)*3 + 200;
                                if ((passdata = alloca(vmlen))) {
@@ -1840,8 +1831,8 @@
                        fprintf(p, "Subject: New VM\n\n");
 		strftime(date, sizeof(date), "%A, %B %d, %Y at %r", &tm);
                if (pagerbody) {
-                       struct ast_channel *ast = ast_channel_alloc(0);
-                       if (ast) {
+                       struct ast_channel *ast;
+                       if ((ast = ast_channel_alloc(0))) {
                                char *passdata;
                                int vmlen = strlen(pagerbody)*3 + 200;
                                if ((passdata = alloca(vmlen))) {
@@ -5138,8 +5129,12 @@
 	if (!valid)
 		goto out;
 
-	vms.deleted = calloc(vmu->maxmsg, sizeof(int));
-	vms.heard = calloc(vmu->maxmsg, sizeof(int));
+	if (!(vms.deleted = ast_calloc(vmu->maxmsg, sizeof(int)))) {
+		/* TODO: Handle memory allocation failure */
+	}
+	if (!(vms.heard = ast_calloc(vmu->maxmsg, sizeof(int)))) {
+		/* TODO: Handle memory allocation failure */
+	}
 	
 	/* Set language from config to override channel language */
 	if (!ast_strlen_zero(vmu->language))
@@ -5592,8 +5587,7 @@
 	struct ast_vm_user *vmu;
 
 	ast_copy_string(tmp, data, sizeof(tmp));
-	vmu = ast_calloc(1, sizeof(*vmu));
-	if (vmu) {
+	if ((vmu = ast_calloc(1, sizeof(*vmu)))) {
 		ast_copy_string(vmu->context, context, sizeof(vmu->context));
 		ast_copy_string(vmu->mailbox, mbox, sizeof(vmu->mailbox));
 
@@ -6129,8 +6123,7 @@
 					/* Timezones in this context */
 					while (var) {
 						struct vm_zone *z;
-						z = ast_malloc(sizeof(*z));
-						if (z != NULL) {
+						if ((z = ast_malloc(sizeof(*z)))) {
 							char *msg_format, *timezone;
 							msg_format = ast_strdupa(var->value);
 							if (msg_format != NULL) {
@@ -6156,8 +6149,7 @@
 								free(z);
 								return -1;
 							}
-						} else {
-							ast_log(LOG_WARNING, "Out of memory while reading voicemail config\n");
+						} else {						
 							return -1;
 						}
 						var = var->next;



More information about the asterisk-commits mailing list