home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <malloc.h>
- #include <signal.h>
- char *calloc();
-
- #define NSEGS 512 /* must be power of two - this program allocates
- up to NSEGS * 1K bytes */
-
- char *p[NSEGS];
-
- #ifdef DEBUG
- void fault(no)
- int no;
- {
- fprintf(stderr,"Caught signal number %d\n",no);
- dump_malloc();
- abort();
- }
- #endif
-
-
- void print_pointers()
- {
- int i;
- fprintf(stderr,"\n Pointers are : ");
- for (i=0; i<NSEGS; i++)
- fputc( (p[i]==NULL ? 'F':'T'), stderr);
- fputc('\n',stderr);
- }
-
-
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int j,k;
- unsigned s,fill;
- long i;
-
- #ifdef DEBUG
- signal(SIGINT,fault);
- #endif
-
- for (i=0; i<NSEGS; i++)
- p[i]=NULL;
-
- for (i=0; i<100000; i++)
- {
- k= rand() & (NSEGS - 1); /* choose a pointer */
- if (p[k] == NULL)
- {
- int call_type;
- /* print_pointers(); */
- s=(unsigned) rand() & 0x3ff; /* pick a size */
- fprintf(stderr," %ld : Allocating a size of %u to pointer %d ",i,s,k);
-
- call_type = rand() & 1;
- p[k] = ( call_type ? malloc(s) : calloc(s,1) );
- fprintf(stderr,"and getting %lx using %s\n",
- p[k], ( call_type ? "malloc" : "calloc"));
- for (fill=0; fill<s; fill++) /* fill the buffer */
- p[k] [fill] = (char) (i & 0xff);
-
- }
- j=rand() & (NSEGS-1); /* choose another pointer */
- if (p[j] != NULL)
- {
- fprintf(stderr," %ld : freeing %lx from pointer %d\n",i,p[j],j);
- free(p[j]);
- p[j]=NULL;
- }
-
- j=rand() & (NSEGS-1);
- if (p[j] != NULL)
- {
- s=rand()&0x3ff;
- fprintf(stderr," %ld : realloc %lx to size %d on pointer %d\n",
- i, p[j], s,j);
- p[j]=realloc(p[j], s);
- }
-
- }
- }
-