diff -r 000000000000 -r c2f4c0285180 gclib/mem.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gclib/mem.c Tue Jan 24 23:54:05 2012 +0000 @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +/* + * A memory allocator that aborts on failure (so that the caller never + * needs to handle out of memory, which we assume is very unlikely to + * happen under normal circumstances on any modern machine). + */ +void *mem_alloc(size_t nmemb,size_t size) +{ + void *ptr=malloc(nmemb*size); + if (!ptr) + { + fprintf(stderr, + "Not enough memory to allocate %lu elements of %lu bytes.\n", + (unsigned long)nmemb,(unsigned long)size); + abort(); + } + return ptr; +} + +/* + * As mem_new, but new memory is cleared to zero. + */ +void *mem_alloc0(size_t nmemb,size_t size) +{ + void *ptr=calloc(nmemb,size); + if (!ptr) + { + fprintf(stderr, + "Not enough memory to allocate %lu elements of %lu bytes.\n", + (unsigned long)nmemb,(unsigned long)size); + abort(); + } + return ptr; +} + +/* + * Grow or shrink a memory block, aborting on failure. + */ +void *mem_realloc(void *ptr,size_t nmemb,size_t size) +{ + ptr=realloc(ptr,nmemb*size); + if (!ptr) + { + fprintf(stderr, + "Not enough memory to allocate %lu elements of %lu bytes.\n", + (unsigned long)nmemb,(unsigned long)size); + abort(); + } + return ptr; +}