nooc

nooc programming language compiler
git clone git://git.nihaljere.xyz/nooc
Log | Files | Refs | LICENSE

commit f2ef3b6f873ae9aae6ebb54b4f7f14f1c9c6da74
parent fcbcca3cd6b748c132cdd7b13c509e9482769e8e
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sun,  2 Jan 2022 23:28:30 -0600

add xrealloc

Diffstat:
Marray.c | 17+++++++----------
Mutil.c | 10++++++++++
Mutil.h | 1+
3 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/array.c b/array.c @@ -1,7 +1,10 @@ #include <stdbool.h> +#include <stdint.h> #include <stdlib.h> #include <string.h> +#include "nooc.h" +#include "util.h" #include "array.h" int @@ -13,11 +16,8 @@ _array_add(void **data, size_t *len, size_t *cap, void *new, size_t size, size_t *cap = *cap ? *cap * 2 : 1; } - if (need_realloc) { - *data = realloc(*data, size*(*cap)); - if (*data == NULL) - return -1; - } + if (need_realloc) + *data = xrealloc(*data, size*(*cap)); memcpy((char *)*data + size*(*len), new, size*count); *len += count; @@ -35,11 +35,8 @@ _array_zero(void **data, size_t *len, size_t *cap, size_t count) *cap = *cap ? *cap * 2 : 1; } - if (need_realloc) { - *data = realloc(*data, *cap); - if (*data == NULL) - return -1; - } + if (need_realloc) + *data = xrealloc(*data, *cap); memset((char *)*data + *len, 0, count); *len += count; diff --git a/util.c b/util.c @@ -176,6 +176,16 @@ xmalloc(size_t size) } void * +xrealloc(void *ptr, size_t size) +{ + char *p = realloc(ptr, size); + if (!p) + die("realloc failed!"); + + return p; +} + +void * xcalloc(size_t nelem, size_t elsize) { char *p = calloc(nelem, elsize); diff --git a/util.h b/util.h @@ -7,4 +7,5 @@ int slice_cmplit(struct slice *s1, char *s2); void error(size_t line, size_t col, const char *error, ...); void die(char *error); void *xmalloc(size_t size); +void *xrealloc(void *, size_t); void *xcalloc(size_t, size_t);