[asterisk-commits] trunk r16195 - /trunk/pbx.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Mar 29 12:54:46 MST 2006


Author: russell
Date: Wed Mar 29 13:54:41 2006
New Revision: 16195

URL: http://svn.digium.com/view/asterisk?rev=16195&view=rev
Log:
convert calloc and malloc+memset to ast_calloc and remove duplicate error messages

Modified:
    trunk/pbx.c

Modified: trunk/pbx.c
URL: http://svn.digium.com/view/asterisk/trunk/pbx.c?rev=16195&r1=16194&r2=16195&view=diff
==============================================================================
--- trunk/pbx.c (original)
+++ trunk/pbx.c Wed Mar 29 13:54:41 2006
@@ -1822,8 +1822,7 @@
 		}
 	
 		/* Now insert the callback */
-		cblist = calloc(1, sizeof(struct ast_state_cb));
-		if (!cblist) {
+		if (!(cblist = ast_calloc(1, sizeof(*cblist)))) {
 			AST_LIST_UNLOCK(&hints);	
 			return -1;
 		}
@@ -1862,8 +1861,7 @@
 	}
 
 	/* Now insert the callback in the callback list  */
-	cblist = calloc(1, sizeof(struct ast_state_cb));
-	if (!cblist) {
+	if (!(cblist = ast_calloc(1, sizeof(*cblist)))) {
 		AST_LIST_UNLOCK(&hints);
 		return -1;
 	}
@@ -1958,11 +1956,8 @@
 	if (option_debug > 1)
 		ast_log(LOG_DEBUG, "HINTS: Adding hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
 
-	hint = calloc(1, sizeof(struct ast_hint));
-	if (!hint) {
+	if (!(hint = ast_calloc(1, sizeof(*hint)))) {
 		AST_LIST_UNLOCK(&hints);
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "HINTS: Out of memory...\n");
 		return -1;
 	}
 	/* Initialize and insert new item at the top */
@@ -2098,11 +2093,8 @@
 	/* A little initial setup here */
 	if (c->pbx)
 		ast_log(LOG_WARNING, "%s already has PBX structure??\n", c->name);
-	c->pbx = calloc(1, sizeof(struct ast_pbx));
-	if (!c->pbx) {
-		ast_log(LOG_ERROR, "Out of memory\n");
+	if (!(c->pbx = ast_calloc(1, sizeof(*c->pbx))))
 		return -1;
-	}
 	if (c->amaflags) {
 		if (!c->cdr) {
 			c->cdr = ast_cdr_alloc();
@@ -2720,9 +2712,7 @@
 		}
 	}
 	
-	tmp = calloc(1, length);
-	if (!tmp) {
-		ast_log(LOG_ERROR, "Out of memory\n");
+	if (!(tmp = ast_calloc(1, length))) {
 		ast_mutex_unlock(&applock);
 		return -1;
 	}
@@ -3493,8 +3483,7 @@
 			return NULL;
 		}
 	}
-	tmp = calloc(1, length);
-	if (tmp) {
+	if ((tmp = ast_calloc(1, length))) {
 		ast_mutex_init(&tmp->lock);
 		strcpy(tmp->name, name);
 		tmp->root = NULL;
@@ -3507,8 +3496,7 @@
 			ast_log(LOG_DEBUG, "Registered context '%s'\n", tmp->name);
 		else if (option_verbose > 2)
 			ast_verbose( VERBOSE_PREFIX_3 "Registered extension context '%s'\n", tmp->name);
-	} else
-		ast_log(LOG_ERROR, "Out of memory\n");
+	}
 	
 	if (!extcontexts)
 		ast_mutex_unlock(&conlock);
@@ -3544,11 +3532,8 @@
 	AST_LIST_TRAVERSE(&hints, hint, list) {
 		if (hint->callbacks && !strcmp(registrar, hint->exten->parent->registrar)) {
 			length = strlen(hint->exten->exten) + strlen(hint->exten->parent->name) + 2 + sizeof(*this);
-			this = calloc(1, length);
-			if (!this) {
-				ast_log(LOG_WARNING, "Could not allocate memory to preserve hint\n");
+			if (!(this = ast_calloc(1, length)))
 				continue;
-			}
 			this->callbacks = hint->callbacks;
 			hint->callbacks = NULL;
 			this->laststate = hint->laststate;
@@ -3914,11 +3899,8 @@
 	length += 2 * (strlen(value) + 1);
 
 	/* allocate new include structure ... */
-	if (!(new_include = calloc(1, length))) {
-		ast_log(LOG_ERROR, "Out of memory\n");
-		errno = ENOMEM;
+	if (!(new_include = ast_calloc(1, length)))
 		return -1;
-	}
 	
 	/* Fill in this structure. Use 'p' for assignments, as the fields
 	 * in the structure are 'const char *'
@@ -4025,11 +4007,8 @@
 	}
 
 	/* allocate new sw structure ... */
-	if (!(new_sw = calloc(1, length))) {
-		ast_log(LOG_ERROR, "Out of memory\n");
-		errno = ENOMEM;
+	if (!(new_sw = ast_calloc(1, length)))
 		return -1;
-	}
 	
 	/* ... fill in this structure ... */
 	p = new_sw->stuff;
@@ -4168,12 +4147,8 @@
 	int length;
 	length = sizeof(struct ast_ignorepat);
 	length += strlen(value) + 1;
-	ignorepat = calloc(1, length);
-	if (!ignorepat) {
-		ast_log(LOG_ERROR, "Out of memory\n");
-		errno = ENOMEM;
+	if (!(ignorepat = ast_calloc(1, length)))
 		return -1;
-	}
 	/* The cast to char * is because we need to write the initial value.
 	 * The field is not supposed to be modified otherwise
 	 */
@@ -4411,39 +4386,34 @@
 	/* Be optimistic:  Build the extension structure first */
 	if (datad == NULL)
 		datad = null_datad;
-	tmp = calloc(1, length);
-	if (tmp) {
-		/* use p as dst in assignments, as the fields are const char * */
-		p = tmp->stuff;
-		if (label) {
-			tmp->label = p;
-			strcpy(p, label);
-			p += strlen(label) + 1;
-		}
-		tmp->exten = p;
-		p += ext_strncpy(p, extension, strlen(extension) + 1) + 1;
-		tmp->priority = priority;
-		tmp->cidmatch = p;	/* but use p for assignments below */
-		if (callerid) {
-			p += ext_strncpy(p, callerid, strlen(callerid) + 1) + 1;
-			tmp->matchcid = 1;
-		} else {
-			*p++ = '\0';
-			tmp->matchcid = 0;
-		}
-		tmp->app = p;
-		strcpy(p, application);
-		tmp->parent = con;
-		tmp->data = data;
-		tmp->datad = datad;
-		tmp->registrar = registrar;
-		tmp->peer = NULL;
-		tmp->next =  NULL;
+	if (!(tmp = ast_calloc(1, length)))
+		return -1;
+		
+	/* use p as dst in assignments, as the fields are const char * */
+	p = tmp->stuff;
+	if (label) {
+		tmp->label = p;
+		strcpy(p, label);
+		p += strlen(label) + 1;
+	}
+	tmp->exten = p;
+	p += ext_strncpy(p, extension, strlen(extension) + 1) + 1;
+	tmp->priority = priority;
+	tmp->cidmatch = p;	/* but use p for assignments below */
+	if (callerid) {
+		p += ext_strncpy(p, callerid, strlen(callerid) + 1) + 1;
+		tmp->matchcid = 1;
 	} else {
-		ast_log(LOG_ERROR, "Out of memory\n");
-		errno = ENOMEM;
-		return -1;
-	}
+		*p++ = '\0';
+		tmp->matchcid = 0;
+	}
+	tmp->app = p;
+	strcpy(p, application);
+	tmp->parent = con;
+	tmp->data = data;
+	tmp->datad = datad;
+	tmp->registrar = registrar;
+	
 	if (ast_mutex_lock(&con->lock)) {
 		free(tmp);
 		/* And properly destroy the data */
@@ -4665,17 +4635,14 @@
 {
 	/* allocate a channel */
 	struct ast_channel *chan = ast_channel_alloc(0);
-	if (!chan) {
-		/* allocation of the channel failed, let some peeps know */
-		ast_log(LOG_WARNING, "Unable to allocate channel structure for CDR record\n");
+	
+	if (!chan)
 		return -1;  /* failure */
-	}
 
 	chan->cdr = ast_cdr_alloc();   /* allocate a cdr for the channel */
 
 	if (!chan->cdr) {
 		/* allocation of the cdr failed */
-		ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
 		ast_channel_free(chan);   /* free the channel */
 		return -1;                /* return failure */
 	}
@@ -4714,7 +4681,6 @@
 				chan->cdr = ast_cdr_alloc();   /* allocate a cdr for the channel */
 				if (!chan->cdr) {
 					/* allocation of the cdr failed */
-					ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
 					free(chan->pbx);
 					res = -1;
 					goto outgoing_exten_cleanup;
@@ -4788,17 +4754,14 @@
 					if (account)
 						ast_cdr_setaccount(chan, account);
 					ast_pbx_run(chan);	
-				} else 
-					ast_log(LOG_WARNING, "Can't allocate the channel structure, skipping execution of extension 'failed'\n");
+				} 
 			}
 		}
 	} else {
-		as = malloc(sizeof(struct async_stat));
-		if (!as) {
+		if (!(as = ast_calloc(1, sizeof(*as)))) {
 			res = -1;
 			goto outgoing_exten_cleanup;
 		}	
-		memset(as, 0, sizeof(struct async_stat));
 		chan = ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name);
 		if (channel) {
 			*channel = chan;
@@ -4887,7 +4850,6 @@
 				chan->cdr = ast_cdr_alloc();   /* allocate a cdr for the channel */
 				if(!chan->cdr) {
 					/* allocation of the cdr failed */
-					ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
 					free(chan->pbx);
 					res = -1;
 					goto outgoing_app_cleanup;
@@ -4903,9 +4865,7 @@
 				res = 0;
 				if (option_verbose > 3)
 					ast_verbose(VERBOSE_PREFIX_4 "Channel %s was answered.\n", chan->name);
-				tmp = malloc(sizeof(struct app_tmp));
-				if (tmp) {
-					memset(tmp, 0, sizeof(struct app_tmp));
+				if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
 					ast_copy_string(tmp->app, app, sizeof(tmp->app));
 					if (appdata)
 						ast_copy_string(tmp->data, appdata, sizeof(tmp->data));
@@ -4932,7 +4892,6 @@
 						}
 					}
 				} else {
-					ast_log(LOG_ERROR, "Out of memory :(\n");
 					res = -1;
 				}
 			} else {
@@ -4960,12 +4919,10 @@
 		}
 
 	} else {
-		as = malloc(sizeof(struct async_stat));
-		if (!as) {
+		if (!(as = ast_calloc(1, sizeof(*as)))) {
 			res = -1;
 			goto outgoing_app_cleanup;
 		}
-		memset(as, 0, sizeof(struct async_stat));
 		chan = __ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name, &oh);
 		if (!chan) {
 			free(as);



More information about the asterisk-commits mailing list