diff --git a/src/shared/util.c b/src/shared/util.c
index a70c709..7878552 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
#include "src/shared/util.h"
+void *btd_malloc(size_t size)
+{
+ if (__builtin_expect(!!size, 1)) {
+ void *ptr;
+
+ ptr = malloc(size);
+ if (ptr)
+ return ptr;
+
+ fprintf(stderr, "failed to allocate %zu bytes\n", size);
+ abort();
+ }
+
+ return NULL;
+}
+
void util_debug(util_debug_func_t function, void *user_data,
const char *format, ...)
{
diff --git a/src/shared/util.h b/src/shared/util.h
index 65f5359..ff705d0 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
#include <stdlib.h>
#include <alloca.h>
#include <byteswap.h>
+#include <string.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define le16_to_cpu(val) (val)
#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
#define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
-#define new0(t, n) ((t*) calloc((n), sizeof(t)))
+#define new0(type, count) \
+ (type *) (__extension__ ({ \
+ size_t __n = (size_t) (count); \
+ size_t __s = sizeof(type); \
+ void *__p; \
+ __p = btd_malloc(__n * __s); \
+ memset(__p, 0, __n * __s); \
+ __p; \
+ }))
+
#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
#define malloc0(n) (calloc((n), 1))
+void *btd_malloc(size_t size);
+
typedef void (*util_debug_func_t)(const char *str, void *user_data);
void util_debug(util_debug_func_t function, void *user_data,