[Freeswitch-svn] [commit] r4794 - in freeswitch/trunk/src: . include

Freeswitch SVN mikej at freeswitch.org
Thu Mar 29 13:37:42 EDT 2007


Author: mikej
Date: Thu Mar 29 13:37:42 2007
New Revision: 4794

Modified:
   freeswitch/trunk/src/include/switch_utils.h
   freeswitch/trunk/src/switch_core.c
   freeswitch/trunk/src/switch_ivr.c

Log:
switch_malloc and switch_zmalloc macros that are fatal if malloc fails both in debug and release modes, switch_zmalloc includes a companion memset for the malloc'd block.

Modified: freeswitch/trunk/src/include/switch_utils.h
==============================================================================
--- freeswitch/trunk/src/include/switch_utils.h	(original)
+++ freeswitch/trunk/src/include/switch_utils.h	Thu Mar 29 13:37:42 2007
@@ -247,6 +247,16 @@
 #define SWITCH_READ_ACCEPTABLE(status) (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK)
 SWITCH_DECLARE(size_t) switch_url_encode(char *url, char *buf, size_t len);
 SWITCH_DECLARE(char *) switch_url_decode(char *s);
+
+/* malloc or DIE macros */
+#ifdef NDEBUG
+#define switch_malloc(ptr, len) (void)( (!!(ptr = malloc(len))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%s", __FILE__, __LINE__),abort(), 0), ptr )
+#define switch_zmalloc(ptr, len) (void)( (!!(ptr = malloc(len))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%s", __FILE__, __LINE__),abort(), 0), memset(ptr, 0, len))
+#else
+#define switch_malloc(ptr, len) (void)(assert(((ptr) = malloc((len)))),ptr)
+#define switch_zmalloc(ptr, len) (void)(assert((ptr = malloc(len))),memset(ptr, 0, len))
+#endif
+
 SWITCH_END_EXTERN_C
 
 #endif

Modified: freeswitch/trunk/src/switch_core.c
==============================================================================
--- freeswitch/trunk/src/switch_core.c	(original)
+++ freeswitch/trunk/src/switch_core.c	Thu Mar 29 13:37:42 2007
@@ -377,8 +377,7 @@
 	switch_core_scheduler_task_container_t *container, *tp;
 	
 	switch_mutex_lock(runtime.task_mutex);
-	assert((container = malloc(sizeof(*container))));
-	memset(container, 0, sizeof(*container));
+	switch_zmalloc(container, sizeof(*container));
 	assert(func);
 	container->func = func;	
 	time(&container->task.created);

Modified: freeswitch/trunk/src/switch_ivr.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr.c	(original)
+++ freeswitch/trunk/src/switch_ivr.c	Thu Mar 29 13:37:42 2007
@@ -3281,8 +3281,7 @@
 	struct hangup_helper *helper;
 	size_t len = sizeof(*helper);
 
-	assert((helper = malloc(len)));
-	memset(helper, 0, len);
+	switch_zmalloc(helper, len);
 	
 	switch_copy_string(helper->uuid_str, uuid, sizeof(helper->uuid_str));
 	helper->cause = cause;
@@ -3332,9 +3331,8 @@
 		len += strlen(context) + 1;
 	}
 
-	assert((helper = malloc(len)));
-	memset(helper, 0, len);
-	
+	switch_zmalloc(helper, len);
+
 	switch_copy_string(helper->uuid_str, uuid, sizeof(helper->uuid_str));
 	
 	cur = (char *) helper + sizeof(*helper);
@@ -3380,9 +3378,8 @@
 	struct broadcast_helper *helper;
 	size_t len = sizeof(*helper) + strlen(path) + 1;
 
-	assert((helper = malloc(len)));
-	memset(helper, 0, len);
-	
+	switch_zmalloc(helper, len);
+
 	switch_copy_string(helper->uuid_str, uuid, sizeof(helper->uuid_str));
 	helper->flags = flags;
 	helper->path = (char *) helper + sizeof(*helper);



More information about the Freeswitch-svn mailing list