ali@0: #include ali@0: #include ali@0: #include ali@5: #include ali@0: ali@0: /* ali@0: * A memory allocator that aborts on failure (so that the caller never ali@0: * needs to handle out of memory, which we assume is very unlikely to ali@0: * happen under normal circumstances on any modern machine). ali@0: */ ali@0: void *mem_alloc(size_t nmemb,size_t size) ali@0: { ali@0: void *ptr=malloc(nmemb*size); ali@0: if (!ptr) ali@0: { ali@0: fprintf(stderr, ali@0: "Not enough memory to allocate %lu elements of %lu bytes.\n", ali@0: (unsigned long)nmemb,(unsigned long)size); ali@0: abort(); ali@0: } ali@0: return ptr; ali@0: } ali@0: ali@0: /* ali@0: * As mem_new, but new memory is cleared to zero. ali@0: */ ali@0: void *mem_alloc0(size_t nmemb,size_t size) ali@0: { ali@0: void *ptr=calloc(nmemb,size); ali@0: if (!ptr) ali@0: { ali@0: fprintf(stderr, ali@0: "Not enough memory to allocate %lu elements of %lu bytes.\n", ali@0: (unsigned long)nmemb,(unsigned long)size); ali@0: abort(); ali@0: } ali@0: return ptr; ali@0: } ali@0: ali@0: /* ali@0: * Grow or shrink a memory block, aborting on failure. ali@0: */ ali@0: void *mem_realloc(void *ptr,size_t nmemb,size_t size) ali@0: { ali@0: ptr=realloc(ptr,nmemb*size); ali@0: if (!ptr) ali@0: { ali@0: fprintf(stderr, ali@0: "Not enough memory to allocate %lu elements of %lu bytes.\n", ali@0: (unsigned long)nmemb,(unsigned long)size); ali@0: abort(); ali@0: } ali@0: return ptr; ali@0: }