[asterisk-commits] trunk - r8410 /trunk/res/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sat Jan 21 15:09:09 MST 2006


Author: russell
Date: Sat Jan 21 16:09:06 2006
New Revision: 8410

URL: http://svn.digium.com/view/asterisk?rev=8410&view=rev
Log:
- conversions to allocation wrappers
- replace malloc/memset with ast_calloc
- replace malloc/ast_copy_string with ast_strdup
(based on patch from issue #6299)

Modified:
    trunk/res/res_crypto.c
    trunk/res/res_features.c
    trunk/res/res_indications.c
    trunk/res/res_monitor.c
    trunk/res/res_musiconhold.c
    trunk/res/res_osp.c

Modified: trunk/res/res_crypto.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_crypto.c?rev=8410&r1=8409&r2=8410&view=diff
==============================================================================
--- trunk/res/res_crypto.c (original)
+++ trunk/res/res_crypto.c Sat Jan 21 16:09:06 2006
@@ -215,13 +215,10 @@
 	/* Make fname just be the normal name now */
 	*c = '\0';
 	if (!key) {
-		key = (struct ast_key *)malloc(sizeof(struct ast_key));
-		if (!key) {
-			ast_log(LOG_WARNING, "Out of memory\n");
+		if (!(key = ast_calloc(1, sizeof(*key)))) {
 			fclose(f);
 			return NULL;
 		}
-		memset(key, 0, sizeof(struct ast_key));
 	}
 	/* At this point we have a key structure (old or new).  Time to
 	   fill it with what we know */

Modified: trunk/res/res_features.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_features.c?rev=8410&r1=8409&r2=8410&view=diff
==============================================================================
--- trunk/res/res_features.c (original)
+++ trunk/res/res_features.c Sat Jan 21 16:09:06 2006
@@ -263,13 +263,10 @@
 	int i,x,parking_range;
 	char exten[AST_MAX_EXTENSION];
 	struct ast_context *con;
-
-	pu = malloc(sizeof(struct parkeduser));
-	if (!pu) {
-		ast_log(LOG_WARNING, "Out of memory\n");
+	
+	if (!(pu = ast_calloc(1, sizeof(*pu)))) {
 		return -1;
 	}
-	memset(pu, 0, sizeof(struct parkeduser));
 	ast_mutex_lock(&parking_lock);
 	parking_range = parking_stop - parking_start+1;
 	for (i = 0; i < parking_range; i++) {
@@ -386,8 +383,7 @@
 	struct ast_frame *f;
 
 	/* Make a new, fake channel that we'll use to masquerade in the real one */
-	chan = ast_channel_alloc(0);
-	if (chan) {
+	if ((chan = ast_channel_alloc(0))) {
 		/* Let us keep track of the channel name */
 		snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name);
 
@@ -785,10 +781,8 @@
 				newchan->_state = AST_STATE_UP;
 				ast_clear_flag(newchan, AST_FLAGS_ALL);	
 				newchan->_softhangup = 0;
-
-				tobj = malloc(sizeof(struct ast_bridge_thread_obj));
-				if (tobj) {
-					memset(tobj,0,sizeof(struct ast_bridge_thread_obj));
+				
+				if ((tobj = ast_calloc(1, sizeof(*tobj)))) {
 					tobj->chan = xferchan;
 					tobj->peer = newchan;
 					tobj->bconfig = *config;
@@ -800,7 +794,6 @@
 					}
 					ast_bridge_call_thread_launch(tobj);
 				} else {
-					ast_log(LOG_WARNING, "Out of memory!\n");
 					ast_hangup(xferchan);
 					ast_hangup(newchan);
 				}
@@ -1214,10 +1207,7 @@
 		*outstate = state;
 
 	if (chan && res <= 0) {
-		if (!chan->cdr) {
-			chan->cdr = ast_cdr_alloc();
-		}
-		if (chan->cdr) {
+		if (chan->cdr || (chan->cdr = ast_cdr_alloc())) {
 			char tmp[256];
 			ast_cdr_init(chan->cdr, chan);
 			snprintf(tmp, 256, "%s/%s", type, (char *)data);
@@ -2088,21 +2078,19 @@
 			}
 
 			{
-				struct ast_call_feature *feature=find_feature(var->name);
-				int mallocd=0;
+				struct ast_call_feature *feature;
+				int mallocd = 0;
 				
-				if (!feature) {
-					feature=malloc(sizeof(struct ast_call_feature));
-					mallocd=1;
-				}
-				if (!feature) {
-					ast_log(LOG_NOTICE, "Malloc failed at feature mapping\n");
-					free(tmp_val);
-					var = var->next;
-					continue;
-				}
-
-				memset(feature,0,sizeof(struct ast_call_feature));
+				if (!(feature = find_feature(var->name))) {
+					mallocd = 1;
+					
+					if (!(feature = ast_calloc(1, sizeof(*feature)))) {
+						free(tmp_val);
+						var = var->next;
+						continue;					
+					}
+				}
+
 				ast_copy_string(feature->sname,var->name,FEATURE_SNAME_LEN);
 				ast_copy_string(feature->app,app,FEATURE_APP_LEN);
 				ast_copy_string(feature->exten, exten,FEATURE_EXTEN_LEN);

Modified: trunk/res/res_indications.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_indications.c?rev=8410&r1=8409&r2=8410&view=diff
==============================================================================
--- trunk/res/res_indications.c (original)
+++ trunk/res/res_indications.c Sat Jan 21 16:09:06 2006
@@ -47,7 +47,7 @@
 #include "asterisk/module.h"
 #include "asterisk/translate.h"
 #include "asterisk/indications.h"
-
+#include "asterisk/utils.h"
 
 /* Globals */
 static const char dtext[] = "Indications Configuration";
@@ -94,13 +94,10 @@
 	if (!tz) {
 		/* country does not exist, create it */
 		ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]);
-
-		tz = malloc(sizeof(struct tone_zone));
-		if (!tz) {
-			ast_log(LOG_WARNING, "Out of memory\n");
+		
+		if (!(tz = ast_calloc(1, sizeof(*tz)))) {
 			return -1;
 		}
-		memset(tz,0,sizeof(struct tone_zone));
 		ast_copy_string(tz->country,argv[2],sizeof(tz->country));
 		if (ast_register_indication_country(tz)) {
 			ast_log(LOG_WARNING, "Unable to register new country\n");
@@ -257,14 +254,11 @@
 		if (!strcasecmp(cxt, "general")) {
 			cxt = ast_category_browse(cfg, cxt);
 			continue;
-		}
-		tones = malloc(sizeof(struct tone_zone));
-		if (!tones) {
-			ast_log(LOG_WARNING,"Out of memory\n");
+		}		
+		if (!(tones = ast_calloc(1, sizeof(*tones)))) {
 			ast_config_destroy(cfg);
 			return -1;
 		}
-		memset(tones,0,sizeof(struct tone_zone));
 		ast_copy_string(tones->country,cxt,sizeof(tones->country));
 
 		v = ast_variable_browse(cfg, cxt);
@@ -281,10 +275,8 @@
 						ast_log(LOG_WARNING,"Invalid ringcadence given '%s' at line %d.\n",ring,v->lineno);
 						ring = strsep(&c,",");
 						continue;
-					}
-					tmp = realloc(tones->ringcadence,(tones->nrringcadence+1)*sizeof(int));
-					if (!tmp) {
-						ast_log(LOG_WARNING, "Out of memory\n");
+					}					
+					if (!(tmp = ast_realloc(tones->ringcadence, (tones->nrringcadence + 1) * sizeof(int)))) {
 						ast_config_destroy(cfg);
 						return -1;
 					}
@@ -299,13 +291,11 @@
 				c = countries;
 				country = strsep(&c,",");
 				while (country) {
-					struct tone_zone* azone = malloc(sizeof(struct tone_zone));
-					if (!azone) {
-						ast_log(LOG_WARNING,"Out of memory\n");
+					struct tone_zone* azone;
+					if (!(azone = ast_calloc(1, sizeof(*azone)))) {
 						ast_config_destroy(cfg);
 						return -1;
 					}
-					memset(azone,0,sizeof(struct tone_zone));
 					ast_copy_string(azone->country, country, sizeof(azone->country));
 					ast_copy_string(azone->alias, cxt, sizeof(azone->alias));
 					if (ast_register_indication_country(azone)) {
@@ -325,10 +315,8 @@
 						goto out;
 					}
 				}
-				/* not there, add it to the back */
-				ts = malloc(sizeof(struct tone_zone_sound));
-				if (!ts) {
-					ast_log(LOG_WARNING, "Out of memory\n");
+				/* not there, add it to the back */				
+				if (!(ts = ast_malloc(sizeof(*ts)))) {
 					ast_config_destroy(cfg);
 					return -1;
 				}

Modified: trunk/res/res_monitor.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_monitor.c?rev=8410&r1=8409&r2=8410&view=diff
==============================================================================
--- trunk/res/res_monitor.c (original)
+++ trunk/res/res_monitor.c Sat Jan 21 16:09:06 2006
@@ -150,12 +150,10 @@
 			}
 		}
 
-		monitor = malloc(sizeof(struct ast_channel_monitor));
-		if (!monitor) {
+		if (!(monitor = ast_calloc(1, sizeof(*monitor)))) {
 			UNLOCK_IF_NEEDED(&chan->lock, need_lock);
 			return -1;
 		}
-		memset(monitor, 0, sizeof(struct ast_channel_monitor));
 
 		/* Determine file names */
 		if (!ast_strlen_zero(fname_base)) {
@@ -416,8 +414,8 @@
 	if (urlprefix) {
 		snprintf(tmp,sizeof(tmp) - 1,"%s/%s.%s",urlprefix,fname_base,
 			((strcmp(format,"gsm")) ? "wav" : "gsm"));
-		if (!chan->cdr)
-			chan->cdr = ast_cdr_alloc();
+		if (!chan->cdr && !(chan->cdr = ast_cdr_alloc()))
+			return -1;
 		ast_cdr_setuserfield(chan, tmp);
 	}
 	if (waitforbridge) {
@@ -491,17 +489,15 @@
 	}
 
 	if (ast_strlen_zero(fname)) {
-		/* No filename base specified, default to channel name as per CLI */
-		fname = malloc (FILENAME_MAX);
-		if (!fname) {
+		/* No filename base specified, default to channel name as per CLI */		
+		if (!(fname = ast_strdup(c->name))) {
 			astman_send_error(s, m, "Could not start monitoring channel");
 			ast_mutex_unlock(&c->lock);
 			return 0;
 		}
-		memset(fname, 0, FILENAME_MAX);
-		ast_copy_string(fname, c->name, FILENAME_MAX);
 		/* Channels have the format technology/channel_name - have to replace that /  */
-		if ((d=strchr(fname, '/'))) *d='-';
+		if ((d = strchr(fname, '/'))) 
+			*d = '-';
 	}
 	
 	if (ast_monitor_start(c, format, fname, 1)) {

Modified: trunk/res/res_musiconhold.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_musiconhold.c?rev=8410&r1=8409&r2=8410&view=diff
==============================================================================
--- trunk/res/res_musiconhold.c (original)
+++ trunk/res/res_musiconhold.c Sat Jan 21 16:09:06 2006
@@ -279,18 +279,17 @@
 {
 	struct moh_files_state *state;
 	struct mohclass *class = params;
-	int allocated = 0;
-
-	if (!chan->music_state && (state = malloc(sizeof(struct moh_files_state)))) {
+
+	if (!chan->music_state && (state = ast_calloc(1, sizeof(*state)))) {
 		chan->music_state = state;
-		allocated = 1;
+		state->class = class;
 	} else 
 		state = chan->music_state;
 
 	if (state) {
-		if (allocated || state->class != class) {
+		if (state->class != class) {
 			/* initialize */
-			memset(state, 0, sizeof(struct moh_files_state));
+			memset(state, 0, sizeof(*state));
 			state->class = class;
 		}
 
@@ -611,11 +610,9 @@
 static struct mohdata *mohalloc(struct mohclass *cl)
 {
 	struct mohdata *moh;
-	long flags;
-	moh = malloc(sizeof(struct mohdata));
-	if (!moh)
+	long flags;	
+	if (!(moh = ast_calloc(1, sizeof(*moh))))
 		return NULL;
-	memset(moh, 0, sizeof(struct mohdata));
 	if (pipe(moh->pipe)) {
 		ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno));
 		free(moh);
@@ -669,8 +666,7 @@
 	struct mohdata *res;
 	struct mohclass *class = params;
 
-	res = mohalloc(class);
-	if (res) {
+	if ((res = mohalloc(class))) {
 		res->origwfmt = chan->writeformat;
 		if (ast_set_write_format(chan, class->format)) {
 			ast_log(LOG_WARNING, "Unable to set channel '%s' to format '%s'\n", chan->name, ast_codec2str(class->format));
@@ -907,14 +903,8 @@
 {
 	struct mohclass *class;
 
-	class = malloc(sizeof(struct mohclass));
-
-	if (!class)
-		return NULL;
-
-	memset(class, 0, sizeof(struct mohclass));
-
-	class->format = AST_FORMAT_SLINEAR;
+	if ((class = ast_calloc(1, sizeof(*class))))		
+		class->format = AST_FORMAT_SLINEAR;
 
 	return class;
 }
@@ -937,10 +927,8 @@
 
 	cat = ast_category_browse(cfg, NULL);
 	for (; cat; cat = ast_category_browse(cfg, cat)) {
-		if (strcasecmp(cat, "classes") && strcasecmp(cat, "moh_files")) {
-			class = moh_class_malloc();
-			if (!class) {
-				ast_log(LOG_WARNING, "Out of memory!\n");
+		if (strcasecmp(cat, "classes") && strcasecmp(cat, "moh_files")) {			
+			if (!(class = moh_class_malloc())) {
 				break;
 			}				
 			ast_copy_string(class->name, cat, sizeof(class->name));	
@@ -1005,10 +993,8 @@
 			args = strchr(data, ',');
 			if (args)
 				*args++ = '\0';
-			if (!(get_mohbyname(var->name))) {
-				class = moh_class_malloc();
-				if (!class) {
-					ast_log(LOG_WARNING, "Out of memory!\n");
+			if (!(get_mohbyname(var->name))) {			
+				if (!(class = moh_class_malloc())) {
 					return numclasses;
 				}
 				
@@ -1033,10 +1019,8 @@
 		if (!(get_mohbyname(var->name))) {
 			args = strchr(var->value, ',');
 			if (args)
-				*args++ = '\0';
-			class = moh_class_malloc();
-			if (!class) {
-				ast_log(LOG_WARNING, "Out of memory!\n");
+				*args++ = '\0';			
+			if (!(class = moh_class_malloc())) {
 				return numclasses;
 			}
 			

Modified: trunk/res/res_osp.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_osp.c?rev=8410&r1=8409&r2=8410&view=diff
==============================================================================
--- trunk/res/res_osp.c (original)
+++ trunk/res/res_osp.c Sat Jan 21 16:09:06 2006
@@ -124,12 +124,9 @@
 	ast_mutex_unlock(&osplock);
 	if (!osp) {
 		mallocd = 1;
-		osp = malloc(sizeof(struct osp_provider));
-		if (!osp) {
-			ast_log(LOG_WARNING, "Out of memory!\n");
+		if (!(osp = ast_calloc(1, sizeof(*osp)))) {
 			return -1;
 		}
-		memset(osp, 0, sizeof(struct osp_provider));
 		osp->handle = -1;
 	}
 	ast_copy_string(osp->name, cat, sizeof(osp->name));



More information about the asterisk-commits mailing list