diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h index 8584b41c7d..9ef1f9a211 100644 --- a/src/include/switch_utils.h +++ b/src/include/switch_utils.h @@ -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 diff --git a/src/switch_core.c b/src/switch_core.c index b7c6d83ef2..20412a4e65 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -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); diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 677c01a2ff..039dcf9712 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -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);