7 /* cheap version of Imager's logging */
8 char const *last_file; int last_line;
10 static void do_log(int level, char const *fmt, ...);
17 #define mm_log(x) ((last_file = __FILE__), (last_line = __LINE__), do_log x )
29 static int malloc_need_init = 1;
40 malloc_entry malloc_pointers[MAXMAL];
44 /* Utility functions */
51 for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
62 if (malloc_pointers[i].ptr == p)
68 /* Takes a pointer to real start of array,
69 * sets the entries in the table, returns
70 * the offset corrected pointer */
74 set_entry(int i, char *buf, size_t size, char const *file, int line) {
75 memset( buf, PADBYTE, UNDRRNVAL );
76 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
78 malloc_pointers[i].ptr = buf;
79 malloc_pointers[i].size = size;
80 sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
88 mm_log((0,"malloc_state()\n"));
90 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
91 mm_log((0,"%d: %d (0x%x) : %s\n", i, malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].comm));
92 total += malloc_pointers[i].size;
94 if (total == 0) mm_log((0,"No memory currently used!\n"));
95 else mm_log((0,"total: %d\n",total));
101 mymalloc_file_line(size_t size, char const* file, int line) {
104 if (malloc_need_init) malloc_init();
106 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
108 if ( (i = find_ptr(NULL)) < 0 ) {
109 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
113 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
114 mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line));
118 buf = set_entry(i, buf, size, file, line);
119 mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
124 (mymalloc)(int size) {
125 return mymalloc_file_line(size, "unknown", 0);
129 myrealloc_file_line(void *ptr, size_t newsize, char const * file, int line) {
133 if (malloc_need_init) malloc_init();
134 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
137 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
138 return mymalloc_file_line(newsize, file, line);
142 mm_log((1, "newsize = 0, sending request to free\n"));
143 myfree_file_line(ptr, file, line);
147 if ( (i = find_ptr(ptr)) == -1) {
148 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
152 if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
153 mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
157 buf = set_entry(i, buf, newsize, file, line);
158 mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
163 (myrealloc)(void *ptr, size_t newsize) {
164 return myrealloc_file_line(ptr, newsize, "unknown", 0);
171 size_t s = malloc_pointers[idx].size;
172 unsigned char *pp = malloc_pointers[idx].ptr;
174 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
178 for(i=0;i<UNDRRNVAL;i++)
179 if (pp[-(1+i)] != PADBYTE)
180 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
182 for(i=0;i<OVERRNVAL;i++)
183 if (pp[s+i] != PADBYTE)
184 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
190 mm_log((1, "bndcheck_all()\n"));
191 for(idx=0; idx<MAXMAL; idx++)
192 if (malloc_pointers[idx].ptr)
197 myfree_file_line(void *p, char const *file, int line) {
202 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
203 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
205 malloc_pointers[i].ptr = NULL;
209 mm_log((1, "myfree_file_line: freeing address %p (real %p) (%s:%d)\n", pp, pp-UNDRRNVAL, file, line));
212 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
213 fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
222 (myfree)(void *block) {
223 myfree_file_line(block, "unknown", 0);
228 #define malloc_comm(a,b) (mymalloc(a))
235 mymalloc(size_t size) {
239 fprintf(stderr, "Attempt to allocate size %d\n", size);
243 if ( (buf = malloc(size)) == NULL ) {
244 mm_log((1, "mymalloc: unable to malloc %d\n", size));
245 fprintf(stderr,"Unable to malloc %d.\n", size); exit(3);
247 mm_log((1, "mymalloc(size %d) -> %p\n", size, buf));
252 mymalloc_file_line(size_t size, char *file, int line) {
253 return mymalloc(size);
258 mm_log((1, "myfree(p %p)\n", p));
263 myfree_file_line(void *p, char *file, int line) {
268 myrealloc(void *block, size_t size) {
271 mm_log((1, "myrealloc(block %p, size %u)\n", block, size));
272 if ((result = realloc(block, size)) == NULL) {
273 mm_log((1, "myrealloc: out of memory\n"));
274 fprintf(stderr, "Out of memory.\n");
281 myrealloc_file_line(void *block, size_t newsize, char *file, int size) {
282 return myrealloc(block, newsize);
285 #endif /* IMAGER_MALLOC_DEBUG */
291 char *log_env = getenv("MEM_DEBUG");
295 if (strcmp(log_env, "STDERR") == 0) {
299 log_file = fopen(log_env, "w+");
301 fprintf(stderr, "Could not open log %s: %s\n", log_env, strerror(errno));
307 do_log(int level, char const *fmt, ...) {
308 if (!log_file) setup_log();
313 fprintf(log_file, "[%s:%d] %d:", last_file, last_line, level);
315 vfprintf(stderr, fmt, ap);