Message-ID: <38655BD4.E9FDBB28@teleline.es> Date: Sun, 26 Dec 1999 01:05:40 +0100 From: Mariano Alvarez =?iso-8859-1?Q?Fern=E1ndez?= X-Mailer: Mozilla 4.5 [es] (Win95; I) X-Accept-Language: es MIME-Version: 1.0 To: djgpp Subject: Mini malloc debuger Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp AT delorie DOT com When I switched from BC++ to DJGPP, the most annoying thing was the lack of a coreleft routine, because I used it like a mini-malloc debuger. When I made a new function to my programs, I saved the coreleft value before and after of using the new function, if they aren't identical I knew I had a memory hole. Searching for available malloc debugers for DJGPP, I found they are not easely usable or you must to modify your code a lot. Because the DJGPP sources are available I began to try to track the used heap modifying malloc.c, but it's difficult to understand it. Next, I tried whit the alternate malloc routine fmalloc.c, it is clear and easy to modify. I added some global variables to fmalloc.c: long malloc_info_memory; // The real heap memory used long malloc_info_nomem; // Times malloc return NULL long malloc_info_nmalloc; // Times malloc was called long malloc_info_nfree; // Times free was called long malloc_info_nfree0; // Times free was called whith pointer to 0 Now, I use my modified fmalloc.c like a mini-malloc debuger. It's easy to use, I add the fmalloc.o file to my projects, and I use a simple routine to show the global variables linked to a hot key. When I'm satisfied, I delete the fmalloc.o file and comment out the show routine. Here are the diffs, first let's me correct a bug in fmalloc.c, it don't check for no memory: *** src/libc/ansi/stdlib/fmalloc.old Tue Jul 28 15:26:06 1998 --- src/libc/ansi/stdlib/fmalloc.c Wed Oct 20 19:20:36 1999 *************** *** 53,58 **** --- 53,61 ---- size = bucket2size[b]+4; rv = (char *)sbrk(size); + if (rv == (char *)(-1)){ + return 0; + } *(int *)rv = b; rv += 4; *************** *** 81,86 **** --- 84,91 ---- if (size <= oldsize) return ptr; newptr = (char *)malloc(size); + if( newptr == 0 ) + return 0; memcpy(newptr, ptr, oldsize); free(ptr); return newptr; And here my modifications: *** src/libc/ansi/stdlib/fmalloc.old Wed Oct 20 19:20:36 1999 --- src/libc/ansi/stdlib/fmalloc.c Mon Sep 13 17:23:38 1999 *************** *** 4,9 **** --- 4,15 ---- #include #include + long malloc_info_memory = 0; + long malloc_info_nomem = 0; + long malloc_info_nmalloc = 0; + long malloc_info_nfree = 0; + long malloc_info_nfree0 = 0; + char *buckets[32] = {0}; int bucket2size[32] = {0}; *************** *** 43,64 **** --- 49,74 ---- if (bucket2size[0] == 0) init_buckets(); + malloc_info_nmalloc++; b = size2bucket(size); if (buckets[b]) { rv = buckets[b]; buckets[b] = *(char **)rv; + malloc_info_memory += bucket2size[b]; return rv; } size = bucket2size[b]+4; rv = (char *)sbrk(size); if (rv == (char *)(-1)){ + malloc_info_nomem++; return 0; } *(int *)rv = b; rv += 4; + malloc_info_memory += bucket2size[b]; return rv; } *************** *** 66,74 **** free(void *ptr) { int b; ! if (ptr == 0) return; b = *(int *)((char *)ptr-4); *(char **)ptr = buckets[b]; buckets[b] = ptr; } --- 76,88 ---- free(void *ptr) { int b; ! if (ptr == 0){ ! malloc_info_nfree0++; return; + } b = *(int *)((char *)ptr-4); + malloc_info_memory -= bucket2size[b]; + malloc_info_nfree++; *(char **)ptr = buckets[b]; buckets[b] = ptr; } P.D. Sorry for my bad english.