8 /* FIXME: make allocation dynamic */
11 #ifdef IMAGER_DEBUG_MALLOC
22 static int malloc_need_init = 1;
30 malloc_entry malloc_pointers[MAXMAL];
35 /* Utility functions */
42 for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
53 if (malloc_pointers[i].ptr == p)
59 /* Takes a pointer to real start of array,
60 * sets the entries in the table, returns
61 * the offset corrected pointer */
65 set_entry(int i, char *buf, size_t size, char *file, int line) {
66 memset( buf, PADBYTE, UNDRRNVAL );
67 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
69 malloc_pointers[i].ptr = buf;
70 malloc_pointers[i].size = size;
71 sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
82 mm_log((0,"malloc_state()\n"));
84 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
85 mm_log((0,"%d: %d (0x%x) : %s\n", i, malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].comm));
86 total += malloc_pointers[i].size;
88 if (total == 0) mm_log((0,"No memory currently used!\n"))
89 else mm_log((0,"total: %d\n",total));
95 mymalloc_file_line(size_t size, char* file, int line) {
98 if (malloc_need_init) malloc_init();
100 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
102 if ( (i = find_ptr(NULL)) < 0 ) {
103 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
107 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
108 mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line));
112 buf = set_entry(i, buf, size, file, line);
113 mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
124 myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
128 if (malloc_need_init) malloc_init();
129 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
132 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
133 return mymalloc_file_line(newsize, file, line);
137 mm_log((1, "newsize = 0, sending request to free\n"));
138 myfree_file_line(ptr, file, line);
142 if ( (i = find_ptr(ptr)) == -1) {
143 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
147 if ( (buf = realloc(ptr-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
148 mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
152 buf = set_entry(i, buf, newsize, file, line);
153 mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
164 size_t s = malloc_pointers[idx].size;
165 unsigned char *pp = malloc_pointers[idx].ptr;
167 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
171 for(i=0;i<UNDRRNVAL;i++)
172 if (pp[-(1+i)] != PADBYTE)
173 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
175 for(i=0;i<OVERRNVAL;i++)
176 if (pp[s+i] != PADBYTE)
177 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
183 mm_log((1, "bndcheck_all()\n"));
184 for(idx=0; idx<MAXMAL; idx++)
185 if (malloc_pointers[idx].ptr)
194 myfree_file_line(void *p, char *file, int line) {
199 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
200 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
202 malloc_pointers[i].ptr = NULL;
207 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
210 mm_log((1, "myfree_file_line: freeing address %p\n", pp-UNDRRNVAL));
217 #define malloc_comm(a,b) (mymalloc(a))
221 printf("malloc_state: not in debug mode\n");
228 if ( (buf = malloc(size)) == NULL ) {
229 mm_log((1, "mymalloc: unable to malloc %d\n", size));
230 fprintf(stderr,"Unable to malloc %d.\n", size); exit(3);
232 mm_log((1, "mymalloc(size %d) -> %p\n", size, buf));
238 mm_log((1, "myfree(p %p)\n", p));
243 myrealloc(void *block, size_t size) {
246 mm_log((1, "myrealloc(block %p, size %u)\n", block, size));
247 if ((result = realloc(block, size)) == NULL) {
248 mm_log((1, "myrealloc: out of memory\n"));
249 fprintf(stderr, "Out of memory.\n");
255 #endif /* IMAGER_MALLOC_DEBUG */
262 /* Should these really be here? */
269 if (a<b) return a; else return b;
274 if (a>b) return a; else return b;