calc

Simple math expression parser.
Log | Files | Refs | LICENSE

commit 1622bdeb6d723f97b57cdce0fb45fdafd29dba4e
parent 4ba2b388903f6177d3e2f0d218213f2ffe21b99c
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Fri, 26 Oct 2018 21:03:48 -0700

Wrap allocators

Diffstat:
Mcalc.c | 24++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/calc.c b/calc.c @@ -10,6 +10,26 @@ struct BufHdr { uint8_t buf[]; }; +void *cmalloc(size_t num_bytes) +{ + void *ptr = malloc(num_bytes); + if (!ptr) { + fprintf(stderr, "crealloc failed\n"); + exit(1); + } + return ptr; +} + +void *crealloc(void *ptr, size_t num_bytes) +{ + ptr = realloc(ptr, num_bytes); + if (!ptr) { + fprintf(stderr, "crealloc failed\n"); + exit(1); + } + return ptr; +} + #define buf__hdr(b) ((struct BufHdr *)((uint8_t *)(b) - offsetof(struct BufHdr, buf))) #define buf_cap(b) ((b) ? buf__hdr(b)->cap : 0) #define buf_len(b) ((b) ? buf__hdr(b)->len : 0) @@ -28,11 +48,11 @@ void *buf__grow(void *buf, size_t new_length, size_t element_size) struct BufHdr *new_hdr = NULL; if (buf) { - new_hdr = realloc(buf__hdr(buf), new_size); + new_hdr = crealloc(buf__hdr(buf), new_size); } else { - new_hdr = malloc(new_size); + new_hdr = cmalloc(new_size); new_hdr->len = 0; } new_hdr->cap = new_cap;