X-Git-Url: https://git.8kb.co.uk/?p=dataflex%2Fdf32func;a=blobdiff_plain;f=src%2Fc%2Fmemman.c;fp=src%2Fc%2Fmemman.c;h=2497fc4f6b187083460dab0e719709c1ad67b5fd;hp=0000000000000000000000000000000000000000;hb=21b727fd491be6f9953f1675b18385296cab0955;hpb=0342737c4763de343d9d87c0cb25a8e31f0211e7 diff --git a/src/c/memman.c b/src/c/memman.c new file mode 100644 index 0000000..2497fc4 --- /dev/null +++ b/src/c/memman.c @@ -0,0 +1,66 @@ +/*------------------------------------------------------------------------- + * memman.c + * wrappers around malloc/realloc/free + * + * Copyright (c) 2007-2015, glyn@8kb.co.uk + * Author: Glyn Astill + * + *------------------------------------------------------------------------- + */ + + +#include +#include +#include + +/* + * Wrappers around malloc/realloc/free + */ +void * wmalloc(unsigned int size) { + char *result; + + if ((result = malloc(size)) == NULL) { + fprintf(stderr, "Failed to malloc %d bytes\n", size); + exit(1); + } + return result; +} + +void * wrealloc(void *iptr, unsigned int size) { + char *result; + + assert(iptr != NULL); + + if ((result = realloc(iptr, size)) == NULL) { + fprintf(stderr, "Failed to realloc %d bytes\n", size); + exit(1); + } + return result; +} + +void wfree(void *iptr){ + assert(iptr != NULL); + + if (iptr) { + free(iptr); + } + iptr = NULL; +} + +/* + * Reallocate memory block pointed to by iptr in chunks of chunk_size when + * required_size is greater than value pointed to be allocated_size. + * Sets value of allocated_size to current allocation. + */ +void * reallocate_block(void *iptr, int *allocated_size, int required_size, int chunk_size) { + void *result; + + if (*allocated_size >= required_size) + return iptr; + + *allocated_size += (((required_size-*allocated_size)/chunk_size)+1)*chunk_size; + + result = wrealloc(iptr, *allocated_size); + + return result; +}