void *lib_AllocMem(unsigned long size, unsigned long attributes) { #ifdef LIB_DEBUG void *ptr; if (attributes & MEMF_CLEAR) { ptr = lib_debug_libc_calloc(1, size); } else { ptr = lib_debug_libc_malloc(size); } #else void *ptr = AllocMem(size, attributes); #endif #ifndef __OS2__ if (ptr == NULL && size > 0) { fprintf(stderr, "error: lib_AllocMem failed\n"); exit(-1); } #endif #ifdef LIB_DEBUG lib_debug_alloc(ptr, size, 1); #endif return ptr; }
/* like malloc, but abort on out of memory. */ void *lib_malloc(size_t size) { #ifdef LIB_DEBUG void *ptr = lib_debug_libc_malloc(size); #else void *ptr = malloc(size); #endif #ifndef __OS2__ if (ptr == NULL && size > 0) { fprintf(stderr, "error: lib_malloc failed\n"); exit(-1); } #endif #ifdef LIB_DEBUG lib_debug_alloc(ptr, size, 3); #endif #if 0 /* clear/fill the block - this should only ever be used for debugging! */ if (ptr) { memset(ptr, 0, size); } #endif return ptr; }
void *lib_AllocVec(unsigned long size, unsigned long attributes) { #ifdef LIB_DEBUG void *ptr; if (attributes & MEMF_CLEAR) { ptr = lib_debug_libc_calloc(1, size); } else { ptr = lib_debug_libc_malloc(size); } #else void *ptr = AllocVec(size, attributes); #endif #ifndef __OS2__ if (ptr == NULL && size > 0) { exit(-1); } #endif #ifdef LIB_DEBUG lib_debug_alloc(ptr, size, 1); #endif return ptr; }
/* Like calloc, but abort if not enough memory is available. */ void *lib_calloc(size_t nmemb, size_t size) { #ifdef LIB_DEBUG void *ptr = lib_debug_libc_calloc(nmemb, size); #else void *ptr = calloc(nmemb, size); #endif #ifndef __OS2__ if (ptr == NULL && (size * nmemb) > 0) exit(-1); #endif #ifdef LIB_DEBUG lib_debug_alloc(ptr, size * nmemb, 1); #endif return ptr; }
/* Like realloc, but abort if not enough memory is available. */ void *lib_realloc(void *ptr, size_t size) { #ifdef LIB_DEBUG void *new_ptr = lib_debug_libc_realloc(ptr, size); #else void *new_ptr = realloc(ptr, size); #endif #ifndef __OS2__ if (new_ptr == NULL) exit(-1); #endif #ifdef LIB_DEBUG lib_debug_free(ptr, 1, 0); lib_debug_alloc(new_ptr, size, 1); #endif return new_ptr; }
/* Like calloc, but abort if not enough memory is available. */ void *lib_calloc(size_t nmemb, size_t size) { #ifdef LIB_DEBUG void *ptr = lib_debug_libc_calloc(nmemb, size); #else void *ptr = calloc(nmemb, size); #endif #ifndef __OS2__ if (ptr == NULL && (size * nmemb) > 0) { fprintf(stderr, "error: lib_calloc failed\n"); exit(-1); } #endif #ifdef LIB_DEBUG lib_debug_alloc(ptr, size * nmemb, 1); #endif return ptr; }
/* Like realloc, but abort if not enough memory is available. */ void *lib_realloc(void *ptr, size_t size) { #ifdef LIB_DEBUG void *new_ptr = lib_debug_libc_realloc(ptr, size); #else void *new_ptr = realloc(ptr, size); #endif #ifndef __OS2__ if (new_ptr == NULL) { fprintf(stderr, "error: lib_realloc failed\n"); exit(-1); } #endif #ifdef LIB_DEBUG lib_debug_free(ptr, 1, 0); lib_debug_alloc(new_ptr, size, 1); #endif return new_ptr; }
void *lib_malloc(size_t size) { #ifdef LIB_DEBUG void *ptr = lib_debug_libc_malloc(size); #else void *ptr = malloc(size); #endif #ifndef __OS2__ if (ptr == NULL && size > 0) { exit(-1); } #endif #ifdef LIB_DEBUG lib_debug_alloc(ptr, size, 3); #endif if (ptr) { memset(ptr, 0, size); } return ptr; }