From 41a5dba20882cca2847d9201e11507b7ef408d34 Mon Sep 17 00:00:00 2001 From: r4 Date: Wed, 22 Dec 2021 13:10:00 +0100 Subject: [PATCH] replace malloc with xmalloc --- ir.c | 6 +++--- map.c | 2 +- util.c | 24 ++++++++++++++++++++++-- util.h | 5 ++++- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/ir.c b/ir.c index d4a5ea2..735569c 100644 --- a/ir.c +++ b/ir.c @@ -21,7 +21,7 @@ const char *irinstr_str[IRInstrEnumSize] = { static void irtoks_init_with_cap(IRToks *v, size_t cap); static void irtoks_init_with_cap(IRToks *v, size_t cap) { - v->toks = malloc(sizeof(IRTok) * cap); + v->toks = xmalloc(sizeof(IRTok) * cap); v->len = 0; v->cap = cap; } @@ -50,13 +50,13 @@ void irtoks_term(IRToks *v) { void irtoks_app(IRToks *v, IRTok t) { if (v->len+1 > v->cap) - v->toks = realloc(v->toks, sizeof(IRTok) * (v->cap *= 2)); + v->toks = xrealloc(v->toks, sizeof(IRTok) * (v->cap *= 2)); v->toks[v->len++] = t; } void irtoks_app_irtoks(IRToks *v, IRToks *other) { if (v->len+other->len > v->cap) - v->toks = realloc(v->toks, sizeof(IRTok) * (other->len + (v->cap *= 2))); + v->toks = xrealloc(v->toks, sizeof(IRTok) * (other->len + (v->cap *= 2))); for (size_t i = 0; i < other->len; i++) v->toks[v->len++] = other->toks[i]; } diff --git a/map.c b/map.c index 99573a0..191118b 100644 --- a/map.c +++ b/map.c @@ -14,7 +14,7 @@ static void init_with_cap(Map *m, size_t val_size, size_t cap) { m->len = 0; m->cap = cap; m->val_size = val_size; - void *data = malloc(sizeof(MapSlot) * cap + val_size * cap); + void *data = xmalloc(sizeof(MapSlot) * cap + val_size * cap); m->slots = data; m->vals = m->slots + cap; for (size_t i = 0; i < cap; i++) { diff --git a/util.c b/util.c index aec093b..ed12f57 100644 --- a/util.c +++ b/util.c @@ -49,8 +49,28 @@ void set_err(const char *fmt, ...) { va_end(va); } +#define XMALLOC_ERR "Failed to allocate %zu bytes: Out of memory\n" + +void *xmalloc(size_t size) { + void *ret = malloc(size); + if (!ret) { + fprintf(stderr, XMALLOC_ERR, size); + abort(); + } + return ret; +} + +void *xrealloc(void *ptr, size_t size) { + void *ret = realloc(ptr, size); + if (!ret) { + fprintf(stderr, XMALLOC_ERR, size); + abort(); + } + return ret; +} + Pool *pool_new(size_t init_cap) { - Pool *p = malloc(sizeof(Pool) + init_cap); + Pool *p = xmalloc(sizeof(Pool) + init_cap); p->len = 0; p->cap = init_cap; p->data = p + 1; @@ -82,7 +102,7 @@ void *pool_alloc(Pool *p, size_t bytes) { } char *sndup(const char *s, size_t n) { - char *ret = malloc(n+1); + char *ret = xmalloc(n+1); if (ret) { memcpy(ret, s, n); ret[n] = 0; diff --git a/util.h b/util.h index ede033f..a985dd1 100644 --- a/util.h +++ b/util.h @@ -37,12 +37,15 @@ extern size_t err_ln, err_col; #define TRY_RET_ELSE(expr, ret, onerr) {expr; if (err) {onerr; return (ret);}} void set_err(const char *fmt, ...); -#define ASSERT_UNREACHED() { fprintf(stderr, "Illegal code position reached in %s:%d\n", __FILE__, __LINE__); exit(1); } +#define ASSERT_UNREACHED() { fprintf(stderr, "Illegal code position reached in %s:%d\n", __FILE__, __LINE__); abort(); } #define IS_NUM(c) (c >= '0' && c <= '9') #define IS_ALPHA(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') #define IS_ALNUM(c) (IS_ALPHA(c) || IS_NUM(c)) +void *xmalloc(size_t size); +void *xrealloc(void *ptr, size_t size); + /* Useful for efficiently allocating lots of data that can all be freed at once afterwards. */ typedef struct Pool { struct Pool *next;