#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; }