add heap allocation API that aborts on failure
This closes
#include <stdlib.h>
#include <assert.h>
#include <memory.h>
+#include <string.h>
+#include <stdio.h>
#define CT_ASSERT(exp) { assert(exp); }
@@ -217,4 +219,53 @@ do { \
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
+//
+// Heap allocation with abort() on failure
+//
+
+static inline void *qd_malloc(size_t size)
+{
+ assert(size);
+ void *ptr = malloc(size);
+ if (!ptr) {
+ perror("qd_malloc");
+ abort();
+ }
+ return ptr;
+}
+
+static inline void *qd_calloc(size_t nmemb, size_t size)
+{
+ assert(nmemb && size);
+ void *ptr = calloc(nmemb, size);
+ if (!ptr) {
+ perror("qd_calloc");
+ abort();
+ }
+ return ptr;
+}
+
+static inline void *qd_realloc(void *buf, size_t size)
+{
+ assert(buf && size);
+ void *ptr = realloc(buf, size);
+ if (!ptr) {
+ perror("qd_realloc");
+ abort();
+ }
+ return ptr;
+}
+
+static inline char *qd_strdup(const char *s)
+{
+ assert(s);
+ char *ptr = strdup(s);
+ if (!ptr) {
+ perror("qd_strdup");
+ abort();
+ }
+ return ptr;
+}
+
+
#endif
