diff options
author | Niels Laukens <niobos@rockbox.org> | 2005-07-06 11:03:20 +0000 |
---|---|---|
committer | Niels Laukens <niobos@rockbox.org> | 2005-07-06 11:03:20 +0000 |
commit | d1c294c17de95615b7af428da938b686830b42df (patch) | |
tree | 950080f5b6c9503c090df6e4f0929f13eae8891e /apps/tagdb/malloc.c | |
parent | 5e9f52f6d1f3356bc6df75a675e1a2d5cdbf9d77 (diff) | |
download | rockbox-d1c294c17de95615b7af428da938b686830b42df.tar.gz rockbox-d1c294c17de95615b7af428da938b686830b42df.zip |
Initial import of tagdb
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7039 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/tagdb/malloc.c')
-rw-r--r-- | apps/tagdb/malloc.c | 131 |
1 files changed, 131 insertions, 0 deletions
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 @@ | |||
1 | #include "config.h" | ||
2 | #include "malloc.h" | ||
3 | |||
4 | #undef malloc | ||
5 | #undef free | ||
6 | #undef realloc | ||
7 | |||
8 | #undef DEBUGF | ||
9 | #define DEBUGF(...) | ||
10 | |||
11 | #include <stdio.h> | ||
12 | #include <stdlib.h> | ||
13 | |||
14 | static size_t total=0; | ||
15 | static size_t max_total=0; | ||
16 | |||
17 | struct size_array { | ||
18 | void *ptr; | ||
19 | size_t size; | ||
20 | } sizes[1000]; | ||
21 | #define NOT_FOUND 1001 | ||
22 | static unsigned long count=0; | ||
23 | |||
24 | int out_of_memory = 1000000; | ||
25 | |||
26 | void *do_malloc(size_t size) { | ||
27 | void *ret; | ||
28 | if(total + size > out_of_memory) { | ||
29 | DEBUGF("malloc(%d), total=%d: FAILED: simulating out-of-memory\n", size, total+size); | ||
30 | return NULL; | ||
31 | } | ||
32 | |||
33 | ret = malloc(size); | ||
34 | if( ret == NULL ) { | ||
35 | DEBUGF("malloc(%d), total=%d FAILED\n", size, total+size); | ||
36 | return NULL; | ||
37 | } else { | ||
38 | total += size; | ||
39 | max_total = ( total > max_total ? total : max_total ); | ||
40 | sizes[count].ptr = ret; | ||
41 | sizes[count].size = size; | ||
42 | DEBUGF("malloc(%d), total=%d OK => 0x%08lx (%lu)\n", size, total, (unsigned long)ret, count); | ||
43 | count++; | ||
44 | if(count == NOT_FOUND) { | ||
45 | fprintf(stderr, "MALLOC MEMORY FULL!!!!!!! FAILING\n"); | ||
46 | free(ret); | ||
47 | count--; | ||
48 | return NULL; | ||
49 | } | ||
50 | return ret; | ||
51 | } | ||
52 | } | ||
53 | |||
54 | static unsigned long find(void* ptr) { | ||
55 | unsigned long i; | ||
56 | for(i=0; i<count; i++) { | ||
57 | if( ptr == sizes[i].ptr ) { | ||
58 | return i; | ||
59 | } | ||
60 | } | ||
61 | return NOT_FOUND; | ||
62 | } | ||
63 | |||
64 | void do_free(void *ptr) { | ||
65 | unsigned long i; | ||
66 | |||
67 | i = find(ptr); | ||
68 | if( i == NOT_FOUND ) { | ||
69 | DEBUGF("free(%08lx) (?) ptr unknown\n", (unsigned long)ptr); | ||
70 | free(ptr); | ||
71 | } else { | ||
72 | total -= sizes[i].size; | ||
73 | DEBUGF("free(%08lx) (%lu, %dbytes) => total=%u\n", (unsigned long)ptr, i, sizes[i].size, total); | ||
74 | free(ptr); | ||
75 | sizes[i].ptr = NULL; // delete | ||
76 | sizes[i].size = 0; | ||
77 | } | ||
78 | } | ||
79 | |||
80 | void *do_realloc(void *ptr, size_t size) { | ||
81 | void *ret; | ||
82 | unsigned long i; | ||
83 | |||
84 | if( ptr == NULL ) { | ||
85 | DEBUGF("realloc()=>"); | ||
86 | return do_malloc(size); | ||
87 | } | ||
88 | |||
89 | i = find(ptr); | ||
90 | |||
91 | if( i == NOT_FOUND ) { | ||
92 | DEBUGF("realloc(%08lx, %d) (?) ptr unknown ", (unsigned long)ptr, size); | ||
93 | } else { | ||
94 | DEBUGF("realloc(%08lx, %d) (%lu, %dbytes) => total=%d ", (unsigned long)ptr, size, i, sizes[i].size, total+size-sizes[i].size); | ||
95 | } | ||
96 | |||
97 | if(total + size - sizes[i].size > out_of_memory) { | ||
98 | DEBUGF("FAILED: simulating out-of-memory\n"); | ||
99 | return NULL; | ||
100 | } | ||
101 | |||
102 | ret = realloc(ptr, size); | ||
103 | if( ret == NULL && size != 0) { // realloc(x, 0) returns NULL, but is valid! | ||
104 | DEBUGF("FAILED\n"); | ||
105 | } else { | ||
106 | total += size - sizes[i].size; | ||
107 | max_total = ( total > max_total ? total : max_total ); | ||
108 | sizes[i].ptr = ret; // update the ptr if realloc changed it | ||
109 | sizes[i].size = size; | ||
110 | DEBUGF("=> %08lx\n", (unsigned long)ret); | ||
111 | } | ||
112 | return ret; | ||
113 | } | ||
114 | |||
115 | void malloc_stats() { | ||
116 | unsigned long i, j; | ||
117 | |||
118 | printf("malloc stats:\n"); | ||
119 | printf(" Total number of allocated items: %lu\n", count); | ||
120 | printf(" Current number of allocated items: "); | ||
121 | j=0; | ||
122 | for(i=0; i<count; i++) { | ||
123 | if( sizes[i].ptr != NULL) { | ||
124 | printf("%lu ", i); | ||
125 | j++; | ||
126 | } | ||
127 | } | ||
128 | printf("=> %lu items\n", j); | ||
129 | printf(" Maximum amount of allocated memory: %dbytes\n", max_total); | ||
130 | printf(" Current amount of allocated memory: %dbytes\n", total); | ||
131 | } | ||