]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
switch_malloc and switch_zmalloc macros that are fatal if malloc fails both in debug...
authorMichael Jerris <mike@jerris.com>
Thu, 29 Mar 2007 17:37:42 +0000 (17:37 +0000)
committerMichael Jerris <mike@jerris.com>
Thu, 29 Mar 2007 17:37:42 +0000 (17:37 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@4794 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_utils.h
src/switch_core.c
src/switch_ivr.c

index 8584b41c7d148d126a6abd3157a9b82129e318cb..9ef1f9a2116b0d47879cf8cc6eecbaa63806f548 100644 (file)
@@ -247,6 +247,16 @@ SWITCH_DECLARE(switch_status_t) switch_string_match(const char *string, size_t s
 #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
index b7c6d83ef2ca84767bd4ea859d08e8118526d994..20412a4e65963fd4e0860b2a9f420b221d3c9c8d 100644 (file)
@@ -377,8 +377,7 @@ SWITCH_DECLARE(uint32_t) switch_core_scheduler_add_task(time_t task_runtime,
        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);
index 677c01a2ff373a686fd49316dc2ed90c5cc7c35f..039dcf9712a192606b575a44febb0fde8f386e23 100644 (file)
@@ -3281,8 +3281,7 @@ SWITCH_DECLARE(uint32_t) switch_ivr_schedule_hangup(time_t runtime, char *uuid,
        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 @@ SWITCH_DECLARE(uint32_t) switch_ivr_schedule_transfer(time_t runtime, char *uuid
                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 @@ SWITCH_DECLARE(uint32_t) switch_ivr_schedule_broadcast(time_t runtime, char *uui
        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);