update dev300-m58
[ooovba.git] / dmake / dbug / malloc / testmlc.c
blobddd8afd4a0574b3a1942923563001606bc12670b
1 /* NOT copyright by SoftQuad Inc. -- msb, 1988 */
2 #ifndef lint
3 static char *SQ_SccsId = "@(#)mtest3.c 1.2 88/08/25";
4 #endif
5 #include <stdio.h>
6 /*
7 ** looptest.c -- intensive allocator tester
8 **
9 ** Usage: looptest
11 ** History:
12 ** 4-Feb-1987 rtech!daveb
15 # ifdef SYS5
16 # define random rand
17 # else
18 # include <sys/vadvise.h>
19 # endif
21 # include <stdio.h>
22 # include <signal.h>
23 # include <setjmp.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 */
32 main( argc, argv )
33 int argc;
34 char **argv;
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 */
56 char *calloc();
57 char *malloc();
58 char *sbrk();
59 long atol();
61 # ifndef SYS5
62 /* your milage may vary... */
63 vadvise( VA_ANOM );
64 # endif
66 if (argc > 1)
67 maxiter = atol (argv[1]);
68 if (maxiter <= 0)
69 maxiter = MAXITER;
71 printf("MAXITER %d MAXOBJS %d ", maxiter, MAXOBJS );
72 printf("BIGOBJ %d, TINYOBJ %d, nbig/ntiny 1/%d\n",
73 BIGOBJ, TINYOBJ, BIGMOD );
74 fflush( stdout );
76 if( NULL == (objs = (int **)calloc( MAXOBJS, sizeof( *objs ) ) ) )
78 fprintf(stderr, "Can't allocate memory for objs array\n");
79 exit(1);
82 if( NULL == ( sizes = (int *)calloc( MAXOBJS, sizeof( *sizes ) ) ) )
84 fprintf(stderr, "Can't allocate memory for sizes array\n");
85 exit(1);
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++ )
92 objs[ i ] = NULL;
94 startsize = sbrk(0) - &end;
95 printf( "Memory use at start: %d bytes\n", startsize );
96 fflush(stdout);
98 printf("Starting the test...\n");
99 fflush(stdout);
100 for( n = 0; n < maxiter ; n++ )
102 if( !(n % STATMOD) )
104 printf("%d iterations\n", n);
105 fflush(stdout);
108 /* determine object of interst and it's size */
110 r = random();
111 objmax = ( r % BIGMOD ) ? TINYOBJ : BIGOBJ;
112 size = r % objmax;
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 );
120 nm++;
122 else
124 /* don't keep bigger objects around */
125 if( size > sizes[ i ] )
127 objs[ i ] = (int *)realloc( objs[ i ], size );
128 nre++;
130 else
132 free( objs[ i ] );
133 objs[ i ] = (int *)malloc( size );
134 nm++;
138 sizes[ i ] = size;
139 if( objs[ i ] == NULL )
141 printf("\nCouldn't allocate %d byte object!\n",
142 size );
143 break;
145 } /* for() */
147 printf( "\n" );
148 cnt = 0;
149 for( i = 0; i < MAXOBJS; i++ )
150 if( objs[ i ] )
151 cnt++;
153 printf( "Did %d iterations, %d objects, %d mallocs, %d reallocs\n",
154 n, cnt, nm, nre );
155 printf( "Memory use at end: %d bytes\n", sbrk(0) - &end );
156 fflush( stdout );
158 /* free all the objects */
159 for( i = 0; i < MAXOBJS; i++ )
160 if( objs[ i ] != NULL )
161 free( objs[ i ] );
163 endsize = sbrk(0) - &end;
164 printf( "Memory use after free: %d bytes\n", endsize );
165 fflush( stdout );
167 if( startsize != endsize )
168 printf("startsize %d != endsize %d\n", startsize, endsize );
170 free( objs );
171 free( sizes );
173 malloc_dump(2);
174 exit( 0 );