From d1c294c17de95615b7af428da938b686830b42df Mon Sep 17 00:00:00 2001 From: Niels Laukens Date: Wed, 6 Jul 2005 11:03:20 +0000 Subject: Initial import of tagdb git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7039 a1c6a512-1295-4272-9138-f99709370657 --- apps/tagdb/malloc.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 apps/tagdb/malloc.c (limited to 'apps/tagdb/malloc.c') diff --git a/apps/tagdb/malloc.c b/apps/tagdb/malloc.c new file mode 100644 index 0000000000..78d24f9d4e --- /dev/null +++ b/apps/tagdb/malloc.c @@ -0,0 +1,131 @@ +#include "config.h" +#include "malloc.h" + +#undef malloc +#undef free +#undef realloc + +#undef DEBUGF +#define DEBUGF(...) + +#include +#include + +static size_t total=0; +static size_t max_total=0; + +struct size_array { + void *ptr; + size_t size; +} sizes[1000]; +#define NOT_FOUND 1001 +static unsigned long count=0; + +int out_of_memory = 1000000; + +void *do_malloc(size_t size) { + void *ret; + if(total + size > out_of_memory) { + DEBUGF("malloc(%d), total=%d: FAILED: simulating out-of-memory\n", size, total+size); + return NULL; + } + + ret = malloc(size); + if( ret == NULL ) { + DEBUGF("malloc(%d), total=%d FAILED\n", size, total+size); + return NULL; + } else { + total += size; + max_total = ( total > max_total ? total : max_total ); + sizes[count].ptr = ret; + sizes[count].size = size; + DEBUGF("malloc(%d), total=%d OK => 0x%08lx (%lu)\n", size, total, (unsigned long)ret, count); + count++; + if(count == NOT_FOUND) { + fprintf(stderr, "MALLOC MEMORY FULL!!!!!!! FAILING\n"); + free(ret); + count--; + return NULL; + } + return ret; + } +} + +static unsigned long find(void* ptr) { + unsigned long i; + for(i=0; i total=%u\n", (unsigned long)ptr, i, sizes[i].size, total); + free(ptr); + sizes[i].ptr = NULL; // delete + sizes[i].size = 0; + } +} + +void *do_realloc(void *ptr, size_t size) { + void *ret; + unsigned long i; + + if( ptr == NULL ) { + DEBUGF("realloc()=>"); + return do_malloc(size); + } + + i = find(ptr); + + if( i == NOT_FOUND ) { + DEBUGF("realloc(%08lx, %d) (?) ptr unknown ", (unsigned long)ptr, size); + } else { + DEBUGF("realloc(%08lx, %d) (%lu, %dbytes) => total=%d ", (unsigned long)ptr, size, i, sizes[i].size, total+size-sizes[i].size); + } + + if(total + size - sizes[i].size > out_of_memory) { + DEBUGF("FAILED: simulating out-of-memory\n"); + return NULL; + } + + ret = realloc(ptr, size); + if( ret == NULL && size != 0) { // realloc(x, 0) returns NULL, but is valid! + DEBUGF("FAILED\n"); + } else { + total += size - sizes[i].size; + max_total = ( total > max_total ? total : max_total ); + sizes[i].ptr = ret; // update the ptr if realloc changed it + sizes[i].size = size; + DEBUGF("=> %08lx\n", (unsigned long)ret); + } + return ret; +} + +void malloc_stats() { + unsigned long i, j; + + printf("malloc stats:\n"); + printf(" Total number of allocated items: %lu\n", count); + printf(" Current number of allocated items: "); + j=0; + for(i=0; i %lu items\n", j); + printf(" Maximum amount of allocated memory: %dbytes\n", max_total); + printf(" Current amount of allocated memory: %dbytes\n", total); +} -- cgit v1.2.3