From 03b598c965020825e9ffa88a8d32b8c1170f2419 Mon Sep 17 00:00:00 2001 From: kramm Date: Sun, 6 Nov 2005 22:55:33 +0000 Subject: [PATCH] memory handling routines --- lib/mem.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/mem.h | 13 ++++++++++ 2 files changed, 99 insertions(+) create mode 100644 lib/mem.c create mode 100644 lib/mem.h diff --git a/lib/mem.c b/lib/mem.c new file mode 100644 index 0000000..98f4b1d --- /dev/null +++ b/lib/mem.c @@ -0,0 +1,86 @@ +#include +#include +#include + +// memory allocation + +void rfx_free(void*ptr) +{ + if(!ptr) + return; + free(ptr); +} + +void* rfx_alloc(int size) +{ + void*ptr; + if(size == 0) { + //*(int*)0 = 0xdead; + //fprintf(stderr, "Warning: Zero alloc\n"); + return 0; + } + + ptr = malloc(size); + if(!ptr) { + fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size); + /* TODO: we should send a signal, so that the debugger kicks in? */ + exit(1); + } + return ptr; +} +void* rfx_realloc(void*data, int size) +{ + void*ptr; + if(size == 0) { + //*(int*)0 = 0xdead; + //fprintf(stderr, "Warning: Zero realloc\n"); + rfx_free(data); + return 0; + } + if(!data) { + ptr = malloc(size); + } else { + ptr = realloc(data, size); + } + + if(!ptr) { + fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size); + /* TODO: we should send a signal, so that the debugger kicks in? */ + exit(1); + } + return ptr; +} +void* rfx_calloc(int size) +{ + void*ptr; + if(size == 0) { + //*(int*)0 = 0xdead; + //fprintf(stderr, "Warning: Zero alloc\n"); + return 0; + } +#ifdef HAVE_CALLOC + ptr = calloc(size); +#else + ptr = malloc(size); +#endif + if(!ptr) { + fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size); + /* TODO: we should send a signal, so that the debugger kicks in? */ + exit(1); + } +#ifndef HAVE_CALLOC + memset(ptr, 0, size); +#endif + return ptr; +} + +#ifdef MEMORY_INFO +long rfx_memory_used() +{ +} + +char* rfx_memory_used_str() +{ +} +#endif + diff --git a/lib/mem.h b/lib/mem.h new file mode 100644 index 0000000..d7a4bfe --- /dev/null +++ b/lib/mem.h @@ -0,0 +1,13 @@ +#ifndef __mem_h__ +#define __mem_h__ + +#define ALLOC_ARRAY(type, num) (((type)*)rfxalloc(sizeof(type)*(num))) +void* rfx_alloc(int size); +void* rfx_calloc(int size); +void* rfx_realloc(void*data, int size); +void rfx_free(void*data); +#ifdef MEMORY_INFO +long rfx_memory_used(); +char* rfx_memory_used_str(); +#endif +#endif //__mem_h__ -- 1.7.10.4