4.2 malloc - one last time

John P. Linderman jpl at allegra.UUCP
Thu Sep 19 02:37:09 AEST 1985


I got mail from Matt Crawford (ihnp4!oddjob!matt) asking why it was
that when he reallocated a huge area down to a small one with my
test program, the huge area didn't seem to be available for
subsequent allocations.  Simple.  My test program was buggy,
and passed the wrong pointer to realloc.  Here's the diff of a fix,
and a new feature.  If you compile both the test program and malloc
with -DMSTATS, the `m' command will use the mstats call in malloc
to describe space used and space free for the various bucket sizes.

John P. Linderman  allegra!jpl

*** old talloc.c	Tue Sep 17 08:45:35 1985
--- new talloc.c	Wed Sep 18 10:50:19 1985
***************
*** 93,99
  	    } else {
  		(void) cvnum(p, &n);
  		if (n > 0) last = n;
! 		if (p = realloc(p, last)) {
  		    (void) printf("%d: %d=>%d bytes from %#x to %#x\n",
  			i, a[i].z, last, a[i].p, p);
  		    a[i].p = p;

--- 93,99 -----
  	    } else {
  		(void) cvnum(p, &n);
  		if (n > 0) last = n;
! 		if (p = realloc(a[i].p, last)) {
  		    (void) printf("%d: %d=>%d bytes from %#x to %#x\n",
  			i, a[i].z, last, a[i].p, p);
  		    a[i].p = p;
***************
*** 111,116
  		}
  	    }
  	    break;
  	case 'q':
  	    exit(0);
  	case '?':

--- 111,121 -----
  		}
  	    }
  	    break;
+ #ifdef MSTATS
+ 	case 'm':
+ 	    (void) mstats(p+1);
+ 	    break;
+ #endif
  	case 'q':
  	    exit(0);
  	case '?':
***************
*** 116,121
  	case '?':
  	    (void) printf("a [N] => allocate N bytes, report index\n");
  	    (void) printf("f index => free space at index\n");
  	    (void) printf("r index [N] => reallocate N bytes at index\n");
  	    (void) printf("s => print summary of allocated storage\n");
  	    (void) printf("q => quit\n");

--- 121,129 -----
  	case '?':
  	    (void) printf("a [N] => allocate N bytes, report index\n");
  	    (void) printf("f index => free space at index\n");
+ #ifdef MSTATS
+ 	    (void) printf("m [title] => print malloc statistics\n");
+ #endif
  	    (void) printf("r index [N] => reallocate N bytes at index\n");
  	    (void) printf("s => print summary of allocated storage\n");
  	    (void) printf("q => quit\n");



More information about the Comp.lang.c mailing list