[asterisk-commits] trunk r11231 - in /trunk: indications.c io.c jitterbuf.c jitterbuf.h loader.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sun Feb 26 18:37:58 MST 2006


Author: russell
Date: Sun Feb 26 19:37:56 2006
New Revision: 11231

URL: http://svn.digium.com/view/asterisk?rev=11231&view=rev
Log:
conversions to allocation wrappers and various other coding guideliens fixes (issue #6582)

Modified:
    trunk/indications.c
    trunk/io.c
    trunk/jitterbuf.c
    trunk/jitterbuf.h
    trunk/loader.c

Modified: trunk/indications.c
URL: http://svn.digium.com/view/asterisk/trunk/indications.c?rev=11231&r1=11230&r2=11231&view=diff
==============================================================================
--- trunk/indications.c (original)
+++ trunk/indications.c Sun Feb 26 19:37:56 2006
@@ -113,10 +113,9 @@
 static void * playtones_alloc(struct ast_channel *chan, void *params)
 {
 	struct playtones_def *pd = params;
-	struct playtones_state *ps = malloc(sizeof(struct playtones_state));
-	if (!ps)
+	struct playtones_state *ps;
+	if (!(ps = ast_calloc(1, sizeof(*ps))))
 		return NULL;
-	memset(ps, 0, sizeof(struct playtones_state));
 	ps->origwfmt = chan->writeformat;
 	if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
 		ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
@@ -300,9 +299,7 @@
 				freq2 = 0;
 		}
 
-		d.items = realloc(d.items,(d.nitems+1)*sizeof(struct playtones_item));
-		if (d.items == NULL) {
-			ast_log(LOG_WARNING, "Realloc failed!\n");
+		if (!(d.items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items)))) {
 			return -1;
 		}
 		d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (freq1 / 8000.0)) * 32768.0;
@@ -433,7 +430,7 @@
 		zone->tones = tmp;
 	}
 	if (zone->ringcadence)
-		free((void*)zone->ringcadence);
+		free(zone->ringcadence);
 	free(zone);
 }
 
@@ -547,18 +544,13 @@
 	}
 	if (!ts) {
 		/* not there, we have to add */
-		ts = malloc(sizeof(struct tone_zone_sound));
-		if (!ts) {
-			ast_log(LOG_WARNING, "Out of memory\n");
+		if (!(ts = ast_malloc(sizeof(*ts)))) {
 			ast_mutex_unlock(&tzlock);
 			return -2;
 		}
 		ts->next = NULL;
 	}
-	ts->name = strdup(indication);
-	ts->data = strdup(tonelist);
-	if (ts->name==NULL || ts->data==NULL) {
-		ast_log(LOG_WARNING, "Out of memory\n");
+	if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
 		ast_mutex_unlock(&tzlock);
 		return -2;
 	}

Modified: trunk/io.c
URL: http://svn.digium.com/view/asterisk/trunk/io.c?rev=11231&r1=11230&r2=11231&view=diff
==============================================================================
--- trunk/io.c (original)
+++ trunk/io.c Sun Feb 26 19:37:56 2006
@@ -36,6 +36,7 @@
 
 #include "asterisk/io.h"
 #include "asterisk/logger.h"
+#include "asterisk/utils.h"
 
 #ifdef DEBUG_IO
 #define DEBUG DEBUG_M
@@ -82,25 +83,19 @@
 {
 	/* Create an I/O context */
 	struct io_context *tmp;
-	tmp = malloc(sizeof(struct io_context));
-	if (tmp) {
+	if ((tmp = ast_malloc(sizeof(*tmp)))) {
 		tmp->needshrink = 0;
 		tmp->fdcnt = 0;
 		tmp->maxfdcnt = GROW_SHRINK_SIZE/2;
 		tmp->current_ioc = -1;
-		tmp->fds = malloc((GROW_SHRINK_SIZE/2) * sizeof(struct pollfd));
-		if (!tmp->fds) {
+		if (!(tmp->fds = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->fds)))) {
 			free(tmp);
 			tmp = NULL;
 		} else {
-			memset(tmp->fds, 0, (GROW_SHRINK_SIZE / 2) * sizeof(struct pollfd));
-			tmp->ior =  malloc((GROW_SHRINK_SIZE / 2) * sizeof(struct io_rec));
-			if (!tmp->ior) {
+			if (!(tmp->ior = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->ior)))) {
 				free(tmp->fds);
 				free(tmp);
 				tmp = NULL;
-			} else {
-				memset(tmp->ior, 0, (GROW_SHRINK_SIZE / 2) * sizeof(struct io_rec));
 			}
 		}
 	}
@@ -126,25 +121,24 @@
 	void *tmp;
 	DEBUG(ast_log(LOG_DEBUG, "io_grow()\n"));
 	ioc->maxfdcnt += GROW_SHRINK_SIZE;
-	tmp = realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(struct io_rec));
-	if (tmp) {
-		ioc->ior = (struct io_rec *)tmp;
-		tmp = realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(struct pollfd));
-		if (tmp) {
+	if ((tmp = ast_realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(*ioc->ior)))) {
+		ioc->ior = tmp;
+		if ((tmp = ast_realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(*ioc->fds)))) {
 			ioc->fds = tmp;
 		} else {
 			/*
-			 * Not enough memory for the pollfd.  Not really any need
-			 * to shrink back the iorec's as we'll probably want to
-			 * grow them again soon when more memory is available, and
-			 * then they'll already be the right size
+			 * Failed to allocate enough memory for the pollfd.  Not
+			 * really any need to shrink back the iorec's as we'll
+			 * probably want to grow them again soon when more memory
+			 * is available, and then they'll already be the right size
 			 */
 			ioc->maxfdcnt -= GROW_SHRINK_SIZE;
 			return -1;
 		}
 	} else {
 		/*
-		 * Out of memory.  We return to the old size, and return a failure
+		 * Memory allocation failure.  We return to the old size, and 
+		 * return a failure
 		 */
 		ioc->maxfdcnt -= GROW_SHRINK_SIZE;
 		return -1;
@@ -180,10 +174,10 @@
 	ioc->fds[ioc->fdcnt].revents = 0;
 	ioc->ior[ioc->fdcnt].callback = callback;
 	ioc->ior[ioc->fdcnt].data = data;
-	ioc->ior[ioc->fdcnt].id = (int *)malloc(sizeof(int));
-	/* Bonk if we couldn't allocate an int */
-	if (!ioc->ior[ioc->fdcnt].id)
+	if (!(ioc->ior[ioc->fdcnt].id = ast_malloc(sizeof(*ioc->ior[ioc->fdcnt].id)))) {
+		/* Bonk if we couldn't allocate an int */
 		return NULL;
+	}
 	*(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt;
 	ret = ioc->ior[ioc->fdcnt].id;
 	ioc->fdcnt++;

Modified: trunk/jitterbuf.c
URL: http://svn.digium.com/view/asterisk/trunk/jitterbuf.c?rev=11231&r1=11230&r2=11231&view=diff
==============================================================================
--- trunk/jitterbuf.c (original)
+++ trunk/jitterbuf.c Sun Feb 26 19:37:56 2006
@@ -35,6 +35,7 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "jitterbuf.h"
+#include "asterisk/utils.h"
 
 /*! define these here, just for ancient compiler systems */
 #define JB_LONGMAX 2147483647L
@@ -73,7 +74,7 @@
 {
 	/* only save settings */
 	jb_conf s = jb->info.conf;
-	memset(jb,0,sizeof(jitterbuf));
+	memset(jb, 0, sizeof(*jb));
 	jb->info.conf = s;
 
 	/* initialize length */
@@ -85,9 +86,7 @@
 {
 	jitterbuf *jb;
 
-
-	jb = malloc(sizeof(jitterbuf));
-	if (!jb) 
+	if (!(jb = ast_malloc(sizeof(*jb)))) 
 		return NULL;
 
 	jb_reset(jb);
@@ -236,7 +235,7 @@
 				/* found where it fits */
 				if (toins > jb->hist_maxbuf[j]) {
 					/* move over */
-					memmove(jb->hist_maxbuf+j+1,jb->hist_maxbuf+j, (JB_HISTORY_MAXBUF_SZ-(j+1)) * sizeof(long));
+					memmove(jb->hist_maxbuf + j + 1, jb->hist_maxbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_maxbuf[0]));
 					/* insert */
 					jb->hist_maxbuf[j] = toins;
 
@@ -253,7 +252,7 @@
 				/* found where it fits */
 				if (toins < jb->hist_minbuf[j]) {
 					/* move over */
-					memmove(jb->hist_minbuf+j+1,jb->hist_minbuf+j, (JB_HISTORY_MAXBUF_SZ-(j+1)) * sizeof(long));
+					memmove(jb->hist_minbuf + j + 1, jb->hist_minbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_minbuf[0]));
 					/* insert */
 					jb->hist_minbuf[j] = toins;
 
@@ -321,21 +320,16 @@
 }
 
 /* returns 1 if frame was inserted into head of queue, 0 otherwise */
-static int queue_put(jitterbuf *jb, void *data, int type, long ms, long ts) 
+static int queue_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts) 
 {
 	jb_frame *frame;
 	jb_frame *p;
 	int head = 0;
 	long resync_ts = ts - jb->info.resync_offset;
 
-	frame = jb->free;
-	if (frame) {
+	if ((frame = jb->free)) {
 		jb->free = frame->next;
-	} else {
-		frame = malloc(sizeof(jb_frame));
-	}
-
-	if (!frame) {
+	} else if (!(frame = ast_malloc(sizeof(*frame)))) {
 		jb_err("cannot allocate frame\n");
 		return 0;
 	}
@@ -514,7 +508,7 @@
 }
 #endif
 
-int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now) 
+enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now) 
 {
 	jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now);
 
@@ -535,7 +529,7 @@
 }
 
 
-static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl) 
+static enum jb_return_code _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl) 
 {
 	jb_frame *frame;
 	long diff;
@@ -775,9 +769,9 @@
 	}
 }
 
-int jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl) 
-{
-	int ret = _jb_get(jb,frameout,now,interpl);
+enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl) 
+{
+	enum jb_return_code ret = _jb_get(jb, frameout, now, interpl);
 #if 0
 	static int lastts=0;
 	int thists = ((ret == JB_OK) || (ret == JB_DROP)) ? frameout->ts : 0;
@@ -791,7 +785,7 @@
 	return ret;
 }
 
-int jb_getall(jitterbuf *jb, jb_frame *frameout) 
+enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout) 
 {
 	jb_frame *frame;
 	frame = queue_getall(jb);
@@ -805,7 +799,7 @@
 }
 
 
-int jb_getinfo(jitterbuf *jb, jb_info *stats) 
+enum jb_return_code jb_getinfo(jitterbuf *jb, jb_info *stats) 
 {
 
 	history_get(jb);
@@ -815,7 +809,7 @@
 	return JB_OK;
 }
 
-int jb_setconf(jitterbuf *jb, jb_conf *conf) 
+enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf) 
 {
 	/* take selected settings from the struct */
 

Modified: trunk/jitterbuf.h
URL: http://svn.digium.com/view/asterisk/trunk/jitterbuf.h?rev=11231&r1=11230&r2=11231&view=diff
==============================================================================
--- trunk/jitterbuf.h (original)
+++ trunk/jitterbuf.h Sun Feb 26 19:37:56 2006
@@ -35,21 +35,23 @@
 	/* ms between growing and shrinking; may not be honored if jitterbuffer runs out of space */
 #define JB_ADJUST_DELAY 40
 
+enum jb_return_code {
+	/* return codes */
+	JB_OK,            /* 0 */
+	JB_EMPTY,         /* 1 */
+	JB_NOFRAME,       /* 2 */
+	JB_INTERP,        /* 3 */
+	JB_DROP,          /* 4 */
+	JB_SCHED          /* 5 */
+};
 
-/* return codes */
-#define JB_OK		0
-#define JB_EMPTY	1
-#define JB_NOFRAME	2
-#define JB_INTERP	3
-#define JB_DROP		4
-#define JB_SCHED	5
-
-/* frame types */
-#define JB_TYPE_CONTROL	0
-#define JB_TYPE_VOICE	1
-#define JB_TYPE_VIDEO	2  /* reserved */
-#define JB_TYPE_SILENCE	3
-
+enum jb_frame_type {
+	/* frame types */
+	JB_TYPE_CONTROL,  /* 0            */
+	JB_TYPE_VOICE,    /* 1            */
+	JB_TYPE_VIDEO,    /* 2 - reserved */
+	JB_TYPE_SILENCE   /* 3            */
+};
 
 typedef struct jb_conf {
 	/* settings */
@@ -85,10 +87,10 @@
 } jb_info;
 
 typedef struct jb_frame {
-	void *data;		/* the frame data */
-	long ts;	/* the relative delivery time expected */
-	long ms;	/* the time covered by this frame, in sec/8000 */
-	int  type;	/* the type of frame */
+	void *data;               /* the frame data */
+	long ts;                  /* the relative delivery time expected */
+	long ms;                  /* the time covered by this frame, in sec/8000 */
+	enum jb_frame_type type;  /* the type of frame */
 	struct jb_frame *next, *prev;
 } jb_frame;
 
@@ -125,7 +127,7 @@
  * JB_DROP: Drop this frame immediately
  * JB_SCHED: Frame added. Call jb_next() to get a new time for the next frame
  */
-int 			jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now);
+enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now);
 
 /* get a frame for time now (receiver's time)  return value is one of
  * JB_OK:  You've got frame!
@@ -134,20 +136,20 @@
  * JB_INTERP: Please interpolate an interpl-length frame for this time (either we need to grow, or there was a lost frame) 
  * JB_EMPTY: The jb is empty.
  */
-int			jb_get(jitterbuf *jb, jb_frame *frame, long now, long interpl);
+enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frame, long now, long interpl);
 
 /* unconditionally get frames from jitterbuf until empty */
-int jb_getall(jitterbuf *jb, jb_frame *frameout);
+enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout);
 
 /* when is the next frame due out, in receiver's time (0=EMPTY) 
  * This value may change as frames are added (esp non-audio frames) */
 long			jb_next(jitterbuf *jb);
 
 /* get jitterbuf info: only "statistics" may be valid */
-int			jb_getinfo(jitterbuf *jb, jb_info *stats);
+enum jb_return_code jb_getinfo(jitterbuf *jb, jb_info *stats);
 
 /* set jitterbuf conf */
-int			jb_setconf(jitterbuf *jb, jb_conf *conf);
+enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf);
 
 typedef 		void (*jb_output_function_t)(const char *fmt, ...);
 void 			jb_setoutput(jb_output_function_t err, jb_output_function_t warn, jb_output_function_t dbg);

Modified: trunk/loader.c
URL: http://svn.digium.com/view/asterisk/trunk/loader.c?rev=11231&r1=11230&r2=11231&view=diff
==============================================================================
--- trunk/loader.c (original)
+++ trunk/loader.c Sun Feb 26 19:37:56 2006
@@ -50,6 +50,7 @@
 #include <dlfcn.h>
 #endif
 #include "asterisk/md5.h"
+#include "asterisk/utils.h"
 
 #ifndef RTLD_NOW
 #define RTLD_NOW 0
@@ -290,8 +291,7 @@
 
 	if (name[0] == '_')
 		name++;
-	n1 = alloca(strlen(name)+2); /* room for leading '_' and final '\0' */
-	if (n1 == NULL)
+	if (!(n1 = alloca(strlen(name) + 2))) /* room for leading '_' and final '\0' */
 		return NULL;
 	n1[0] = '_';
 	strcpy(n1+1, name);
@@ -341,9 +341,7 @@
 		AST_LIST_UNLOCK(&module_list);
 		return -1;
 	}
-	cur = calloc(1, sizeof(struct module));	
-	if (!cur) {
-		ast_log(LOG_WARNING, "Out of memory\n");
+	if (!(cur = ast_calloc(1, sizeof(*cur)))) {
 		AST_LIST_UNLOCK(&module_list);
 		return -1;
 	}
@@ -582,8 +580,8 @@
 int ast_loader_register(int (*v)(void)) 
 {
 	/* XXX Should be more flexible here, taking > 1 verboser XXX */
-	struct loadupdate *tmp = malloc(sizeof (struct loadupdate));
-	if (!tmp)
+	struct loadupdate *tmp;	
+	if (!(tmp = ast_malloc(sizeof(*tmp))))
 		return -1;
 	tmp->updater = v;
 	if (AST_LIST_LOCK(&module_list))



More information about the asterisk-commits mailing list