home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / malloc.uport / tcheck.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-03  |  1.5 KB  |  85 lines

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <signal.h>
  4. char *calloc();
  5.  
  6. #define NSEGS 512   /* must be power of two  - this program allocates
  7.             up to NSEGS * 1K bytes            */
  8.  
  9. char *p[NSEGS];
  10.  
  11. #ifdef DEBUG
  12. void fault(no)
  13. int no;
  14. {
  15.     fprintf(stderr,"Caught signal number %d\n",no);
  16.     dump_malloc();
  17.     abort();
  18. }
  19. #endif
  20.  
  21.  
  22. void print_pointers()
  23. {
  24.     int i;
  25.     fprintf(stderr,"\n Pointers are : ");
  26.     for (i=0; i<NSEGS; i++)
  27.         fputc( (p[i]==NULL ? 'F':'T'), stderr);
  28.     fputc('\n',stderr);
  29. }
  30.         
  31.  
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     int j,k; 
  38.     unsigned s,fill;
  39.     long i;
  40.  
  41. #ifdef DEBUG
  42.     signal(SIGINT,fault);
  43. #endif
  44.  
  45.     for (i=0; i<NSEGS; i++)
  46.         p[i]=NULL;
  47.  
  48.     for (i=0; i<100000; i++)
  49.     {
  50.         k= rand() & (NSEGS - 1); /* choose a pointer */
  51.         if (p[k] == NULL)
  52.         {
  53.             int call_type;
  54. /*            print_pointers(); */
  55.             s=(unsigned) rand() & 0x3ff;  /* pick a size */
  56.             fprintf(stderr," %ld : Allocating a size of %u to pointer %d ",i,s,k);
  57.  
  58.             call_type = rand() & 1;
  59.             p[k] = ( call_type ? malloc(s) : calloc(s,1) );
  60.             fprintf(stderr,"and getting %lx using %s\n",
  61.                 p[k], ( call_type ? "malloc" : "calloc"));
  62.             for (fill=0; fill<s; fill++)  /* fill the buffer */
  63.                 p[k] [fill] = (char) (i & 0xff);
  64.  
  65.         }
  66.         j=rand() & (NSEGS-1);   /* choose another pointer */
  67.         if (p[j] != NULL)
  68.         {
  69.             fprintf(stderr," %ld : freeing %lx from pointer %d\n",i,p[j],j);
  70.             free(p[j]);
  71.             p[j]=NULL;
  72.         }
  73.  
  74.         j=rand() & (NSEGS-1);
  75.         if (p[j] != NULL)
  76.         {
  77.             s=rand()&0x3ff;
  78.             fprintf(stderr," %ld : realloc %lx to size %d on pointer %d\n", 
  79.                 i, p[j], s,j);
  80.             p[j]=realloc(p[j], s);
  81.         }
  82.  
  83.     }
  84. }
  85.