[asterisk-commits] trunk r10141 - /trunk/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Feb 14 18:48:55 MST 2006


Author: kpfleming
Date: Tue Feb 14 19:48:54 2006
New Revision: 10141

URL: http://svn.digium.com/view/asterisk?rev=10141&view=rev
Log:
more memory allocation wrapper conversion

Modified:
    trunk/astmm.c
    trunk/autoservice.c
    trunk/callerid.c
    trunk/cdr.c
    trunk/channel.c
    trunk/chanvars.c

Modified: trunk/astmm.c
URL: http://svn.digium.com/view/asterisk/trunk/astmm.c?rev=10141&r1=10140&r2=10141&view=diff
==============================================================================
--- trunk/astmm.c (original)
+++ trunk/astmm.c Tue Feb 14 19:48:54 2006
@@ -41,13 +41,15 @@
 
 #define SOME_PRIME 563
 
-#define FUNC_CALLOC	1
-#define FUNC_MALLOC	2
-#define FUNC_REALLOC	3
-#define FUNC_STRDUP	4
-#define FUNC_STRNDUP	5
-#define FUNC_VASPRINTF	6
-#define FUNC_ASPRINTF   7
+enum func_type {
+	FUNC_CALLOC = 1,
+	FUNC_MALLOC,
+	FUNC_REALLOC,
+	FUNC_STRDUP,
+	FUNC_STRNDUP,
+	FUNC_VASPRINTF,
+	FUNC_ASPRINTF
+};
 
 /* Undefine all our macros */
 #undef malloc
@@ -68,7 +70,7 @@
 	char file[40];
 	char func[40];
 	int lineno;
-	int which;
+	enum func_type which;
 	size_t len;
 	unsigned int fence;
 	unsigned char data[0];
@@ -80,13 +82,13 @@
 AST_MUTEX_DEFINE_STATIC(reglock);
 AST_MUTEX_DEFINE_STATIC(showmemorylock);
 
-static inline void *__ast_alloc_region(size_t size, int which, const char *file, int lineno, const char *func)
+static inline void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func)
 {
 	struct ast_region *reg;
 	void *ptr = NULL;
 	unsigned int *fence;
 	int hash;
-	reg = malloc(size + sizeof(struct ast_region) + sizeof(unsigned int));
+	reg = malloc(size + sizeof(*reg) + sizeof(*fence));
 	ast_mutex_lock(&reglock);
 	if (reg) {
 		ast_copy_string(reg->file, file, sizeof(reg->file));
@@ -106,9 +108,9 @@
 	}
 	ast_mutex_unlock(&reglock);
 	if (!reg) {
-		fprintf(stderr, "Out of memory :(\n");
+		fprintf(stderr, "Memory allocation failure\n");
 		if (mmlog) {
-			fprintf(mmlog, "%ld - Out of memory\n", time(NULL));
+			fprintf(mmlog, "%ld - Memory allocation failure\n", time(NULL));
 			fflush(mmlog);
 		}
 	}
@@ -184,8 +186,7 @@
 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) 
 {
 	void *ptr;
-	ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func);
-	if (ptr) 
+	if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func))) 
 		memset(ptr, 0, size * nmemb);
 	return ptr;
 }
@@ -204,19 +205,15 @@
 {
 	void *tmp;
 	size_t len = 0;
-	if (ptr) {
-		len = __ast_sizeof_region(ptr);
-		if (!len) {
-			fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
-			if (mmlog) {
-				fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
-				fflush(mmlog);
-			}
-			return NULL;
-		}
-	}
-	tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func);
-	if (tmp) {
+	if (ptr && !(len = __ast_sizeof_region(ptr))) {
+		fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
+		if (mmlog) {
+			fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
+			fflush(mmlog);
+		}
+		return NULL;
+	}
+	if ((tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func))) {
 		if (len > size)
 			len = size;
 		if (ptr) {
@@ -234,8 +231,7 @@
 	if (!s)
 		return NULL;
 	len = strlen(s) + 1;
-	ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
-	if (ptr)
+	if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func)))
 		strcpy(ptr, s);
 	return ptr;
 }
@@ -249,8 +245,7 @@
 	len = strlen(s) + 1;
 	if (len > n)
 		len = n;
-	ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func);
-	if (ptr)
+	if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func)))
 		strcpy(ptr, s);
 	return ptr;
 }
@@ -266,8 +261,7 @@
 	va_copy(ap2, ap);
 	size = vsnprintf(&s, 1, fmt, ap2);
 	va_end(ap2);
-	*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func);
-	if (!*strp) {
+	if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func))) {
 		va_end(ap);
 		return -1;
 	}
@@ -287,8 +281,7 @@
 	va_copy(ap2, ap);
 	size = vsnprintf(&s, 1, fmt, ap2);
 	va_end(ap2);
-	*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func);
-	if (!*strp) {
+	if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func))) {
 		va_end(ap);
 		return -1;
 	}
@@ -377,8 +370,8 @@
 					cur = cur->next;
 				}
 				if (!cur) {
-					cur = alloca(sizeof(struct file_summary));
-					memset(cur, 0, sizeof(struct file_summary));
+					cur = alloca(sizeof(*cur));
+					memset(cur, 0, sizeof(*cur));
 					ast_copy_string(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn));
 					cur->next = list;
 					list = cur;

Modified: trunk/autoservice.c
URL: http://svn.digium.com/view/asterisk/trunk/autoservice.c?rev=10141&r1=10140&r2=10141&view=diff
==============================================================================
--- trunk/autoservice.c (original)
+++ trunk/autoservice.c Tue Feb 14 19:48:54 2006
@@ -107,23 +107,20 @@
 	/* XXX if found, we return -1, why ??? */
 
 	/* If not, start autoservice on channel */
-	if (!as) {
-		as = calloc(1, sizeof(struct asent));
-		if (as) {
-			as->chan = chan;
-			AST_LIST_INSERT_HEAD(&aslist, as, list);
-			res = 0;
-			if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
-				if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
-					ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
-					/* There will only be a single member in the list at this point,
-					   the one we just added. */
-					AST_LIST_REMOVE(&aslist, as, list);
-					free(as);
-					res = -1;
-				} else
-					pthread_kill(asthread, SIGURG);
-			}
+	if (!as && (as = ast_calloc(1, sizeof(*as)))) {
+		as->chan = chan;
+		AST_LIST_INSERT_HEAD(&aslist, as, list);
+		res = 0;
+		if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
+			if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
+				ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
+				/* There will only be a single member in the list at this point,
+				   the one we just added. */
+				AST_LIST_REMOVE(&aslist, as, list);
+				free(as);
+				res = -1;
+			} else
+				pthread_kill(asthread, SIGURG);
 		}
 	}
 	AST_LIST_UNLOCK(&aslist);

Modified: trunk/callerid.c
URL: http://svn.digium.com/view/asterisk/trunk/callerid.c?rev=10141&r1=10140&r2=10141&view=diff
==============================================================================
--- trunk/callerid.c (original)
+++ trunk/callerid.c Tue Feb 14 19:48:54 2006
@@ -131,32 +131,29 @@
 struct callerid_state *callerid_new(int cid_signalling)
 {
 	struct callerid_state *cid;
-	cid = malloc(sizeof(struct callerid_state));
-	if (cid) {
-		memset(cid, 0, sizeof(struct callerid_state));
-		cid->fskd.spb = 7;		/* 1200 baud */
-		cid->fskd.hdlc = 0;		/* Async */
-		cid->fskd.nbit = 8;		/* 8 bits */
-		cid->fskd.nstop = 1;	/* 1 stop bit */
-		cid->fskd.paridad = 0;	/* No parity */
-		cid->fskd.bw=1;			/* Filter 800 Hz */
-		if (cid_signalling == 2) { /* v23 signalling */
+
+	if ((cid = ast_calloc(1, sizeof(*cid)))) {
+		cid->fskd.spb = 7.0;          	/* 1200 baud */
+		/* cid->fskd.hdlc = 0; */     	/* Async */
+		cid->fskd.nbit = 8;           	/* 8 bits */
+		cid->fskd.nstop = 1.0;        	/* 1 stop bit */
+		/* cid->fskd.paridad = 0; */  	/* No parity */
+		cid->fskd.bw = 1;             	/* Filter 800 Hz */
+		if (cid_signalling == 2) {    	/* v23 signalling */
 			cid->fskd.f_mark_idx =  4;	/* 1300 Hz */
 			cid->fskd.f_space_idx = 5;	/* 2100 Hz */
-		} else { /* Bell 202 signalling as default */ 
+		} else {                      	/* Bell 202 signalling as default */
 			cid->fskd.f_mark_idx =  2;	/* 1200 Hz */
 			cid->fskd.f_space_idx = 3;	/* 2200 Hz */
 		}
-		cid->fskd.pcola = 0;		/* No clue */
-		cid->fskd.cont = 0;			/* Digital PLL reset */
-		cid->fskd.x0 = 0.0;
-		cid->fskd.state = 0;
-		memset(cid->name, 0, sizeof(cid->name));
-		memset(cid->number, 0, sizeof(cid->number));
+		/* cid->fskd.pcola = 0; */    	/* No clue */
+		/* cid->fskd.cont = 0.0; */   	/* Digital PLL reset */
+		/* cid->fskd.x0 = 0.0; */
+		/* cid->fskd.state = 0; */
 		cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
-		cid->pos = 0;
-	} else
-		ast_log(LOG_WARNING, "Out of memory\n");
+		/* cid->pos = 0; */
+	}
+
 	return cid;
 }
 
@@ -284,15 +281,14 @@
 	int b2 ;
 	int res;
 	int x;
-	short *buf = malloc(2 * len + cid->oldlen);
-	short *obuf = buf;
-
-	if (!buf) {
-		ast_log(LOG_WARNING, "Out of memory\n");
+	short *buf;
+	short *obuf;
+
+	if (!(buf = ast_calloc(1, 2 * len + cid->oldlen))) {
 		return -1;
 	}
 
-	memset(buf, 0, 2 * len + cid->oldlen);
+	obuf = buf;
 	memcpy(buf, cid->oldstuff, cid->oldlen);
 	mylen += cid->oldlen/2;
 
@@ -534,15 +530,17 @@
 	int b = 'X';
 	int res;
 	int x;
-	short *buf = malloc(2 * len + cid->oldlen);
-	short *obuf = buf;
-	if (!buf) {
-		ast_log(LOG_WARNING, "Out of memory\n");
+	short *buf;
+	short *obuf;
+
+	if (!(buf = ast_calloc(1, 2 * len + cid->oldlen))) {
 		return -1;
 	}
-	memset(buf, 0, 2 * len + cid->oldlen);
+
+	obuf = buf;
 	memcpy(buf, cid->oldstuff, cid->oldlen);
 	mylen += cid->oldlen/2;
+
 	for (x=0;x<len;x++) 
 		buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
 	while(mylen >= 160) {

Modified: trunk/cdr.c
URL: http://svn.digium.com/view/asterisk/trunk/cdr.c?rev=10141&r1=10140&r2=10141&view=diff
==============================================================================
--- trunk/cdr.c (original)
+++ trunk/cdr.c Tue Feb 14 19:48:54 2006
@@ -129,11 +129,9 @@
 		return -1;
 	}
 
-	i = malloc(sizeof(*i));
-	if (!i) 	
+	if (!(i = ast_calloc(1, sizeof(*i)))) 	
 		return -1;
 
-	memset(i, 0, sizeof(*i));
 	i->be = be;
 	ast_copy_string(i->name, name, sizeof(i->name));
 	ast_copy_string(i->desc, desc, sizeof(i->desc));
@@ -172,7 +170,6 @@
 	struct ast_cdr *newcdr;
 
 	if (!(newcdr = ast_cdr_alloc())) {
-		ast_log(LOG_ERROR, "Memory Error!\n");
 		return NULL;
 	}
 
@@ -438,13 +435,7 @@
 
 struct ast_cdr *ast_cdr_alloc(void)
 {
-	struct ast_cdr *cdr;
-
-	cdr = malloc(sizeof(*cdr));
-	if (cdr)
-		memset(cdr, 0, sizeof(*cdr));
-
-	return cdr;
+	return ast_calloc(1, sizeof(struct ast_cdr));
 }
 
 void ast_cdr_start(struct ast_cdr *cdr)
@@ -876,9 +867,7 @@
 static int init_batch(void)
 {
 	/* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
-	batch = malloc(sizeof(*batch));
-	if (!batch) {
-		ast_log(LOG_WARNING, "CDR: out of memory while trying to handle batched records, data will most likely be lost\n");
+	if (!(batch = ast_malloc(sizeof(*batch)))) {
 		return -1;
 	}
 
@@ -986,15 +975,12 @@
 	if (option_debug)
 		ast_log(LOG_DEBUG, "CDR detaching from this thread\n");
 
-	/* we'll need a new tail for every CDR */
-	newtail = malloc(sizeof(*newtail));
-	if (!newtail) {
-		ast_log(LOG_WARNING, "CDR: out of memory while trying to detach, will try in this thread instead\n");
+	/* we'll need a new tail for every CDR */	
+	if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
 		post_cdr(cdr);
 		ast_cdr_free(cdr);
 		return;
 	}
-	memset(newtail, 0, sizeof(*newtail));
 
 	/* don't traverse a whole list (just keep track of the tail) */
 	ast_mutex_lock(&cdr_batch_lock);

Modified: trunk/channel.c
URL: http://svn.digium.com/view/asterisk/trunk/channel.c?rev=10141&r1=10140&r2=10141&view=diff
==============================================================================
--- trunk/channel.c (original)
+++ trunk/channel.c Tue Feb 14 19:48:54 2006
@@ -417,10 +417,8 @@
 			return -1;
 		}
 	}
-
-	chan = malloc(sizeof(*chan));
-	if (!chan) {
-		ast_log(LOG_WARNING, "Out of memory\n");
+	
+	if (!(chan = ast_malloc(sizeof(*chan)))) {
 		AST_LIST_UNLOCK(&channels);
 		return -1;
 	}
@@ -607,15 +605,11 @@
 		return NULL;
 	}
 
-	tmp = malloc(sizeof(struct ast_channel));
-	if (!tmp) {
-		ast_log(LOG_WARNING, "Channel allocation failed: Out of memory\n");
+	if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
 		return NULL;
 	}
 
-	memset(tmp, 0, sizeof(struct ast_channel));
-	tmp->sched = sched_context_create();
-	if (!tmp->sched) {
+	if (!(tmp->sched = sched_context_create())) {
 		ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
 		free(tmp);
 		return NULL;
@@ -702,8 +696,7 @@
 	int qlen = 0;
 
 	/* Build us a copy and free the original one */
-	f = ast_frdup(fin);
-	if (!f) {
+	if (!(f = ast_frdup(fin))) {
 		ast_log(LOG_WARNING, "Unable to duplicate frame\n");
 		return -1;
 	}
@@ -1048,8 +1041,7 @@
 	}
 
 	if (!chan->spies) {
-		if (!(chan->spies = calloc(1, sizeof(*chan->spies)))) {
-			ast_log(LOG_WARNING, "Memory allocation failure\n");
+		if (!(chan->spies = ast_calloc(1, sizeof(*chan->spies)))) {
 			return -1;
 		}
 
@@ -1478,9 +1470,8 @@
 	}
 
 	ast_prod(chan);
-	if (gen->alloc) {
-		if (!(chan->generatordata = gen->alloc(chan, params)))
-			res = -1;
+	if (gen->alloc && !(chan->generatordata = gen->alloc(chan, params))) {
+		res = -1;
 	}
 	
 	if (!res) {
@@ -1520,9 +1511,7 @@
 	} *fdmap;
 
 	sz = n * AST_MAX_FDS + nfds;
-	pfds = alloca(sizeof(struct pollfd) * sz);
-	fdmap = alloca(sizeof(struct fdmap) * sz);
-	if (!pfds || !fdmap) {
+	if (!(pfds = alloca(sizeof(*pfds) * sz)) || !(fdmap = alloca(sizeof(*fdmap) * sz))) {
 		ast_log(LOG_ERROR, "Out of memory\n");
 		*outfd = -1;
 		return NULL;
@@ -2510,10 +2499,8 @@
 	if (outstate)
 		*outstate = state;
 	if (chan && res <= 0) {
-		if (!chan->cdr) {
-			chan->cdr = ast_cdr_alloc();
-			if (chan->cdr)
-				ast_cdr_init(chan->cdr, chan);
+		if (!chan->cdr && (chan->cdr = ast_cdr_alloc())) {
+			ast_cdr_init(chan->cdr, chan);
 		}
 		if (chan->cdr) {
 			char tmp[256];
@@ -2525,8 +2512,7 @@
 			/* If the cause wasn't handled properly */
 			if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
 				ast_cdr_failed(chan->cdr);
-		} else 
-			ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
+		}
 		ast_hangup(chan);
 		chan = NULL;
 	}
@@ -3663,8 +3649,7 @@
 	struct tonepair_state *ts;
 	struct tonepair_def *td = params;
 
-	ts = calloc(1, sizeof(struct tonepair_state));
-	if (!ts)
+	if (!(ts = ast_calloc(1, sizeof(*ts))))
 		return NULL;
 	ts->origwfmt = chan->writeformat;
 	if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
@@ -4074,8 +4059,7 @@
 {
 	struct ast_silence_generator *state;
 
-	if (!(state = calloc(1, sizeof(*state)))) {
-		ast_log(LOG_WARNING, "Could not allocate state structure\n");
+	if (!(state = ast_calloc(1, sizeof(*state)))) {
 		return NULL;
 	}
 

Modified: trunk/chanvars.c
URL: http://svn.digium.com/view/asterisk/trunk/chanvars.c?rev=10141&r1=10140&r2=10141&view=diff
==============================================================================
--- trunk/chanvars.c (original)
+++ trunk/chanvars.c Tue Feb 14 19:48:54 2006
@@ -33,23 +33,21 @@
 #include "asterisk/chanvars.h"
 #include "asterisk/logger.h"
 #include "asterisk/strings.h"
+#include "asterisk/utils.h"
 
 struct ast_var_t *ast_var_assign(const char *name, const char *value)
-{
-	int i;
+{	
 	struct ast_var_t *var;
-	
-	var = calloc(sizeof(struct ast_var_t) + strlen(name) + 1 + strlen(value) + 1, sizeof(char));
+	int name_len = strlen(name) + 1;
+	int value_len = strlen(value) + 1;
 
-	if (var == NULL) {
-		ast_log(LOG_WARNING, "Out of memory\n");
+	if (!(var = ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char)))) {
 		return NULL;
 	}
 
-	i = strlen(name) + 1;
-	ast_copy_string(var->name, name, i);
-	var->value = var->name + i;
-	ast_copy_string(var->value, value, strlen(value) + 1);
+	ast_copy_string(var->name, name, name_len);
+	var->value = var->name + name_len;
+	ast_copy_string(var->value, value, value_len);
 	
 	return var;
 }	



More information about the asterisk-commits mailing list