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; }
static void *lib_debug_libc_realloc(void *ptr, size_t size) { #if LIB_DEBUG_GUARD > 0 char *new_ptr = NULL; if (size > 0) { new_ptr = lib_debug_libc_malloc(size); if (ptr != NULL) { size_t old_size; old_size = (size_t)lib_debug_guard_size_get((char *)ptr); if (size >= old_size) { memcpy(new_ptr, ptr, old_size); } else { memcpy(new_ptr, ptr, size); } } } if (ptr != NULL) { lib_debug_libc_free(ptr); } return (void *)new_ptr; #else return realloc(ptr, size); #endif }
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; }