1 /* NOT copyright by SoftQuad Inc. -- msb, 1988 */
3 static char *SQ_SccsId
= "@(#)mtest3.c 1.2 88/08/25";
7 ** looptest.c -- intensive allocator tester
12 ** 4-Feb-1987 rtech!daveb
18 # include <sys/vadvise.h>
25 # define MAXITER 1000000 /* main loop iterations */
26 # define MAXOBJS 1000 /* objects in pool */
27 # define BIGOBJ 90000 /* max size of a big object */
28 # define TINYOBJ 80 /* max size of a small object */
29 # define BIGMOD 100 /* 1 in BIGMOD is a BIGOBJ */
30 # define STATMOD 10000 /* interation interval for status */
36 register int **objs
; /* array of objects */
37 register int *sizes
; /* array of object sizes */
38 register int n
; /* iteration counter */
39 register int i
; /* object index */
40 register int size
; /* object size */
41 register int r
; /* random number */
43 int objmax
; /* max size this iteration */
44 int cnt
; /* number of allocated objects */
45 int nm
= 0; /* number of mallocs */
46 int nre
= 0; /* number of reallocs */
47 int nal
; /* number of allocated objects */
48 int nfre
; /* number of free list objects */
49 long alm
; /* memory in allocated objects */
50 long frem
; /* memory in free list */
51 long startsize
; /* size at loop start */
52 long endsize
; /* size at loop exit */
53 long maxiter
= 0; /* real max # iterations */
55 extern char end
; /* memory before heap */
62 /* your milage may vary... */
67 maxiter
= atol (argv
[1]);
71 printf("MAXITER %d MAXOBJS %d ", maxiter
, MAXOBJS
);
72 printf("BIGOBJ %d, TINYOBJ %d, nbig/ntiny 1/%d\n",
73 BIGOBJ
, TINYOBJ
, BIGMOD
);
76 if( NULL
== (objs
= (int **)calloc( MAXOBJS
, sizeof( *objs
) ) ) )
78 fprintf(stderr
, "Can't allocate memory for objs array\n");
82 if( NULL
== ( sizes
= (int *)calloc( MAXOBJS
, sizeof( *sizes
) ) ) )
84 fprintf(stderr
, "Can't allocate memory for sizes array\n");
88 /* as per recent discussion on net.lang.c, calloc does not
89 ** necessarily fill in NULL pointers...
91 for( i
= 0; i
< MAXOBJS
; i
++ )
94 startsize
= sbrk(0) - &end
;
95 printf( "Memory use at start: %d bytes\n", startsize
);
98 printf("Starting the test...\n");
100 for( n
= 0; n
< maxiter
; n
++ )
104 printf("%d iterations\n", n
);
108 /* determine object of interst and it's size */
111 objmax
= ( r
% BIGMOD
) ? TINYOBJ
: BIGOBJ
;
113 i
= r
% (MAXOBJS
- 1);
115 /* either replace the object of get a new one */
117 if( objs
[ i
] == NULL
)
119 objs
[ i
] = (int *)malloc( size
);
124 /* don't keep bigger objects around */
125 if( size
> sizes
[ i
] )
127 objs
[ i
] = (int *)realloc( objs
[ i
], size
);
133 objs
[ i
] = (int *)malloc( size
);
139 if( objs
[ i
] == NULL
)
141 printf("\nCouldn't allocate %d byte object!\n",
149 for( i
= 0; i
< MAXOBJS
; i
++ )
153 printf( "Did %d iterations, %d objects, %d mallocs, %d reallocs\n",
155 printf( "Memory use at end: %d bytes\n", sbrk(0) - &end
);
158 /* free all the objects */
159 for( i
= 0; i
< MAXOBJS
; i
++ )
160 if( objs
[ i
] != NULL
)
163 endsize
= sbrk(0) - &end
;
164 printf( "Memory use after free: %d bytes\n", endsize
);
167 if( startsize
!= endsize
)
168 printf("startsize %d != endsize %d\n", startsize
, endsize
);