replace malloc with xmalloc
This commit is contained in:
parent
b80e5a9c4e
commit
41a5dba208
6
ir.c
6
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);
|
||||||
|
|
||||||
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->len = 0;
|
||||||
v->cap = cap;
|
v->cap = cap;
|
||||||
}
|
}
|
||||||
@ -50,13 +50,13 @@ void irtoks_term(IRToks *v) {
|
|||||||
|
|
||||||
void irtoks_app(IRToks *v, IRTok t) {
|
void irtoks_app(IRToks *v, IRTok t) {
|
||||||
if (v->len+1 > v->cap)
|
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;
|
v->toks[v->len++] = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
void irtoks_app_irtoks(IRToks *v, IRToks *other) {
|
void irtoks_app_irtoks(IRToks *v, IRToks *other) {
|
||||||
if (v->len+other->len > v->cap)
|
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++)
|
for (size_t i = 0; i < other->len; i++)
|
||||||
v->toks[v->len++] = other->toks[i];
|
v->toks[v->len++] = other->toks[i];
|
||||||
}
|
}
|
||||||
|
2
map.c
2
map.c
@ -14,7 +14,7 @@ static void init_with_cap(Map *m, size_t val_size, size_t cap) {
|
|||||||
m->len = 0;
|
m->len = 0;
|
||||||
m->cap = cap;
|
m->cap = cap;
|
||||||
m->val_size = val_size;
|
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->slots = data;
|
||||||
m->vals = m->slots + cap;
|
m->vals = m->slots + cap;
|
||||||
for (size_t i = 0; i < cap; i++) {
|
for (size_t i = 0; i < cap; i++) {
|
||||||
|
24
util.c
24
util.c
@ -49,8 +49,28 @@ void set_err(const char *fmt, ...) {
|
|||||||
va_end(va);
|
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 *pool_new(size_t init_cap) {
|
||||||
Pool *p = malloc(sizeof(Pool) + init_cap);
|
Pool *p = xmalloc(sizeof(Pool) + init_cap);
|
||||||
p->len = 0;
|
p->len = 0;
|
||||||
p->cap = init_cap;
|
p->cap = init_cap;
|
||||||
p->data = p + 1;
|
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 *sndup(const char *s, size_t n) {
|
||||||
char *ret = malloc(n+1);
|
char *ret = xmalloc(n+1);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
memcpy(ret, s, n);
|
memcpy(ret, s, n);
|
||||||
ret[n] = 0;
|
ret[n] = 0;
|
||||||
|
5
util.h
5
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);}}
|
#define TRY_RET_ELSE(expr, ret, onerr) {expr; if (err) {onerr; return (ret);}}
|
||||||
void set_err(const char *fmt, ...);
|
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_NUM(c) (c >= '0' && c <= '9')
|
||||||
#define IS_ALPHA(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
|
#define IS_ALPHA(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
|
||||||
#define IS_ALNUM(c) (IS_ALPHA(c) || IS_NUM(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. */
|
/* Useful for efficiently allocating lots of data that can all be freed at once afterwards. */
|
||||||
typedef struct Pool {
|
typedef struct Pool {
|
||||||
struct Pool *next;
|
struct Pool *next;
|
||||||
|
Loading…
Reference in New Issue
Block a user