From 6e84acb58056ce793a08041d202bb96861874025 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 2 Jun 2013 07:07:50 +0000 Subject: [PATCH] Add alloc0 and new0 functions --- cals/ical.c | 4 ++-- src/util.c | 9 +++++++++ src/util.h | 5 +++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cals/ical.c b/cals/ical.c index 2a48bad..33df76c 100644 --- a/cals/ical.c +++ b/cals/ical.c @@ -118,7 +118,7 @@ static event_t *to_event(ical_inst *inst) { icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY); - event_t *event = calloc(1, sizeof(event_t)); + event_t *event = new0(event_t); event->name = icalcomponent_get_summary(inst->comp); event->desc = icalcomponent_get_description(inst->comp); event->loc = icalcomponent_get_location(inst->comp); @@ -155,7 +155,7 @@ static todo_t *to_todo(ical_inst *inst) icalproperty *cat = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY); icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY); - todo_t *todo = calloc(1, sizeof(todo_t)); + todo_t *todo = new0(todo_t); todo->name = icalcomponent_get_summary(inst->comp); todo->desc = icalcomponent_get_description(inst->comp); todo->cat = icalproperty_get_value_as_string(cat); diff --git a/src/util.c b/src/util.c index e3f3a43..131e9de 100644 --- a/src/util.c +++ b/src/util.c @@ -75,6 +75,15 @@ void strsub(char *str, char find, char repl) *cur = repl; } +/* Memory functions */ +void *alloc0(int size) +{ + void *data = calloc(1, size); + if (!data) + error("memory allocation failed"); + return data; +} + /* Debugging functions */ void debug(char *fmt, ...) { diff --git a/src/util.h b/src/util.h index a424b7b..d0baa3b 100644 --- a/src/util.h +++ b/src/util.h @@ -22,12 +22,17 @@ #define ROUND(x) ((int)((x)+0.5)) #define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0])) +#define new0(type) alloc0(sizeof(type)) + /* Debug functions */ void util_init(void); /* Stirng functions */ void strsub(char *str, char find, char repl); +/* Memory functions */ +void *alloc0(int size); + /* Debug functions */ void debug(char *fmt, ...); void error(char *fmt, ...); -- 2.43.2