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, ...);
12 #define mm_log(x) ((last_file = __FILE__), (last_line = __LINE__), do_log x )
30 static int malloc_need_init = 1;
41 malloc_entry malloc_pointers[MAXMAL];
45 /* Utility functions */
52 for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
63 if (malloc_pointers[i].ptr == p)
69 /* Takes a pointer to real start of array,
70 * sets the entries in the table, returns
71 * the offset corrected pointer */
75 set_entry(int i, char *buf, size_t size, char const *file, int line) {
76 memset( buf, PADBYTE, UNDRRNVAL );
77 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
79 malloc_pointers[i].ptr = buf;
80 malloc_pointers[i].size = size;
81 sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
89 mm_log((0,"malloc_state()\n"));
91 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
92 mm_log((0,"%d: %d (0x%x) : %s\n", i, malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].comm));
93 total += malloc_pointers[i].size;
95 if (total == 0) mm_log((0,"No memory currently used!\n"));
96 else mm_log((0,"total: %d\n",total));
102 mymalloc_file_line(size_t size, char const* file, int line) {
105 if (malloc_need_init) malloc_init();
107 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
109 if ( (i = find_ptr(NULL)) < 0 ) {
110 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
114 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
115 mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line));
119 buf = set_entry(i, buf, size, file, line);
120 mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
125 (mymalloc)(int size) {
126 return mymalloc_file_line(size, "unknown", 0);
130 myrealloc_file_line(void *ptr, size_t newsize, char const * file, int line) {
134 if (malloc_need_init) malloc_init();
135 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
138 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
139 return mymalloc_file_line(newsize, file, line);
143 mm_log((1, "newsize = 0, sending request to free\n"));
144 myfree_file_line(ptr, file, line);
148 if ( (i = find_ptr(ptr)) == -1) {
149 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
153 if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
154 mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
158 buf = set_entry(i, buf, newsize, file, line);
159 mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
164 (myrealloc)(void *ptr, size_t newsize) {
165 return myrealloc_file_line(ptr, newsize, "unknown", 0);
172 size_t s = malloc_pointers[idx].size;
173 unsigned char *pp = malloc_pointers[idx].ptr;
175 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
179 for(i=0;i<UNDRRNVAL;i++)
180 if (pp[-(1+i)] != PADBYTE)
181 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
183 for(i=0;i<OVERRNVAL;i++)
184 if (pp[s+i] != PADBYTE)
185 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
191 mm_log((1, "bndcheck_all()\n"));
192 for(idx=0; idx<MAXMAL; idx++)
193 if (malloc_pointers[idx].ptr)
198 myfree_file_line(void *p, char const *file, int line) {
203 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
204 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
206 malloc_pointers[i].ptr = NULL;
210 mm_log((1, "myfree_file_line: freeing address %p (real %p) (%s:%d)\n", pp, pp-UNDRRNVAL, file, line));
213 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
214 fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
223 (myfree)(void *block) {
224 myfree_file_line(block, "unknown", 0);
229 #define malloc_comm(a,b) (mymalloc(a))
236 mymalloc(size_t size) {
240 fprintf(stderr, "Attempt to allocate size %d\n", (int)size);
244 if ( (buf = malloc(size)) == NULL ) {
245 mm_log((1, "mymalloc: unable to malloc %d\n", (int)size));
246 fprintf(stderr,"Unable to malloc %d.\n", (int)size); exit(3);
248 mm_log((1, "mymalloc(size %d) -> %p\n", (int)size, buf));
253 mymalloc_file_line(size_t size, char *file, int line) {
254 return mymalloc(size);
259 mm_log((1, "myfree(p %p)\n", p));
264 myfree_file_line(void *p, char *file, int line) {
269 myrealloc(void *block, size_t size) {
272 mm_log((1, "myrealloc(block %p, size %u)\n", block, size));
273 if ((result = realloc(block, size)) == NULL) {
274 mm_log((1, "myrealloc: out of memory\n"));
275 fprintf(stderr, "Out of memory.\n");
282 myrealloc_file_line(void *block, size_t newsize, char *file, int size) {
283 return myrealloc(block, newsize);
286 #endif /* IMAGER_MALLOC_DEBUG */
292 char *log_env = getenv("MEM_DEBUG");
296 if (strcmp(log_env, "STDERR") == 0) {
300 log_file = fopen(log_env, "w+");
302 fprintf(stderr, "Could not open log %s: %s\n", log_env, strerror(errno));
308 do_log(int level, char const *fmt, ...) {
309 if (!log_file) setup_log();
314 fprintf(log_file, "[%s:%d] %d:", last_file, last_line, level);
316 vfprintf(stderr, fmt, ap);