6 /* cheap version of Imager's logging */
7 char const *last_file; int last_line;
9 static void do_log(int level, char const *fmt, ...);
16 #define mm_log(x) ((last_file = __FILE__), (last_line = __LINE__), do_log x )
28 static int malloc_need_init = 1;
39 malloc_entry malloc_pointers[MAXMAL];
43 /* Utility functions */
50 for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
61 if (malloc_pointers[i].ptr == p)
67 /* Takes a pointer to real start of array,
68 * sets the entries in the table, returns
69 * the offset corrected pointer */
73 set_entry(int i, char *buf, size_t size, char const *file, int line) {
74 memset( buf, PADBYTE, UNDRRNVAL );
75 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
77 malloc_pointers[i].ptr = buf;
78 malloc_pointers[i].size = size;
79 sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
87 mm_log((0,"malloc_state()\n"));
89 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
90 mm_log((0,"%d: %d (0x%x) : %s\n", i, malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].comm));
91 total += malloc_pointers[i].size;
93 if (total == 0) mm_log((0,"No memory currently used!\n"));
94 else mm_log((0,"total: %d\n",total));
100 mymalloc_file_line(size_t size, char const* file, int line) {
103 if (malloc_need_init) malloc_init();
105 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
107 if ( (i = find_ptr(NULL)) < 0 ) {
108 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
112 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
113 mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line));
117 buf = set_entry(i, buf, size, file, line);
118 mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
123 (mymalloc)(int size) {
124 return mymalloc_file_line(size, "unknown", 0);
128 myrealloc_file_line(void *ptr, size_t newsize, char const * file, int line) {
132 if (malloc_need_init) malloc_init();
133 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
136 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
137 return mymalloc_file_line(newsize, file, line);
141 mm_log((1, "newsize = 0, sending request to free\n"));
142 myfree_file_line(ptr, file, line);
146 if ( (i = find_ptr(ptr)) == -1) {
147 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
151 if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
152 mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
156 buf = set_entry(i, buf, newsize, file, line);
157 mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
162 (myrealloc)(void *ptr, size_t newsize) {
163 return myrealloc_file_line(ptr, newsize, "unknown", 0);
170 size_t s = malloc_pointers[idx].size;
171 unsigned char *pp = malloc_pointers[idx].ptr;
173 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
177 for(i=0;i<UNDRRNVAL;i++)
178 if (pp[-(1+i)] != PADBYTE)
179 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
181 for(i=0;i<OVERRNVAL;i++)
182 if (pp[s+i] != PADBYTE)
183 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
189 mm_log((1, "bndcheck_all()\n"));
190 for(idx=0; idx<MAXMAL; idx++)
191 if (malloc_pointers[idx].ptr)
196 myfree_file_line(void *p, char const *file, int line) {
201 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
202 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
204 malloc_pointers[i].ptr = NULL;
208 mm_log((1, "myfree_file_line: freeing address %p (real %p) (%s:%d)\n", pp, pp-UNDRRNVAL, file, line));
211 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
212 fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
221 (myfree)(void *block) {
222 myfree_file_line(block, "unknown", 0);
227 #define malloc_comm(a,b) (mymalloc(a))
234 mymalloc(size_t size) {
238 fprintf(stderr, "Attempt to allocate size %d\n", size);
242 if ( (buf = malloc(size)) == NULL ) {
243 mm_log((1, "mymalloc: unable to malloc %d\n", size));
244 fprintf(stderr,"Unable to malloc %d.\n", size); exit(3);
246 mm_log((1, "mymalloc(size %d) -> %p\n", size, buf));
251 mymalloc_file_line(size_t size, char *file, int line) {
252 return mymalloc(size);
257 mm_log((1, "myfree(p %p)\n", p));
262 myfree_file_line(void *p, char *file, int line) {
267 myrealloc(void *block, size_t size) {
270 mm_log((1, "myrealloc(block %p, size %u)\n", block, size));
271 if ((result = realloc(block, size)) == NULL) {
272 mm_log((1, "myrealloc: out of memory\n"));
273 fprintf(stderr, "Out of memory.\n");
280 myrealloc_file_line(void *block, size_t newsize, char *file, int size) {
281 return myrealloc(block, newsize);
284 #endif /* IMAGER_MALLOC_DEBUG */
290 char *log_env = getenv("MEM_DEBUG");
294 if (strcmp(log_env, "STDERR") == 0) {
298 log_file = fopen(log_env, "w+");
300 fprintf(stderr, "Could not open log %s: %s\n", log_env, strerror(errno));
306 do_log(int level, char const *fmt, ...) {
307 if (!log_file) setup_log();
312 fprintf(log_file, "[%s:%d] %d:", last_file, last_line, level);
314 vfprintf(stderr, fmt, ap);