home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / testmalloc.c < prev    next >
C/C++ Source or Header  |  1995-04-26  |  6KB  |  275 lines

  1. /* NOT copyright by SoftQuad Inc. -- msb, 1988 */
  2. /*
  3.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  4.  *
  5.  * This software may be distributed freely as long as the following conditions
  6.  * are met:
  7.  *         * the distribution, or any derivative thereof, may not be
  8.  *          included as part of a commercial product
  9.  *        * full source code is provided including this copyright
  10.  *        * there is no charge for the software itself (there may be
  11.  *          a minimal charge for the copying or distribution effort)
  12.  *        * this copyright notice is not modified or removed from any
  13.  *          source file
  14.  */
  15. #ifndef lint
  16. static char *SQ_SccsId = "@(#)mtest3.c    1.2 88/08/25";
  17. #endif
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. /*
  21. ** looptest.c -- intensive allocator tester 
  22. **
  23. ** Usage:  looptest
  24. **
  25. ** History:
  26. **    4-Feb-1987 rtech!daveb 
  27. */
  28.  
  29. #ifndef HAS_VADVISE
  30. #ifdef random
  31. #undef random
  32. #endif
  33. # define random    rand
  34. #else
  35. # include <sys/vadvise.h>
  36. #endif
  37.  
  38. # include <math.h>
  39. # include <stdio.h>
  40. # include <signal.h>
  41. # include <setjmp.h>
  42.  
  43. #include "malloc.h"
  44.  
  45. # define MAXITER    1000000        /* main loop iterations */
  46. # define MAXOBJS    1000        /* objects in pool */
  47. # define BIGOBJ        90000        /* max size of a big object */
  48. # define TINYOBJ    80        /* max size of a small object */
  49. # define BIGMOD        100        /* 1 in BIGMOD is a BIGOBJ */
  50. # define STATMOD    10000        /* interation interval for status */
  51.  
  52. int
  53. main( argc, argv )
  54. int argc;
  55. char **argv;
  56. {
  57.     register int     **objs;    /* array of objects */
  58.     register SIZETYPE *sizes;    /* array of object sizes */
  59.     register SIZETYPE n;        /* iteration counter */
  60.     register SIZETYPE i;        /* object index */
  61.     register SIZETYPE size;        /* object size */
  62.     register SIZETYPE r;        /* random number */
  63.  
  64.     SIZETYPE objmax;        /* max size this iteration */
  65.     long cnt;            /* number of allocated objects */
  66.     long nm = 0;            /* number of mallocs */
  67.     long nre = 0;            /* number of reallocs */
  68.     long startsize;            /* size at loop start */
  69.     long endsize;            /* size at loop exit */
  70.     long maxiter = 0;        /* real max # iterations */
  71.  
  72.     double * dblptr;        /* pointer for doubleword test */
  73.  
  74.     extern char end;        /* memory before heap */
  75.     char *sbrk();
  76.     long atol();
  77.     union dbmalloptarg m;
  78.  
  79. #ifdef HAS_VADVISE
  80.     /* your milage may vary... */
  81.     vadvise( VA_ANOM );
  82. #endif
  83.  
  84.     /*
  85.      * test that allocation can be used for doubleword manipulation
  86.      */
  87.     dblptr = (double *) malloc(sizeof(double));
  88.     if( (dblptr == 0) || ((*dblptr=10.0) != 10.0) )
  89.     {
  90.         fprintf(stderr,"Doubleword test failed\n");
  91.         exit(1);
  92.     }
  93.     free( (DATATYPE *) dblptr);
  94.  
  95.     if (argc > 1)
  96.         maxiter = atol (argv[1]);
  97.     if (maxiter <= 0)
  98.         maxiter = MAXITER;
  99.  
  100.     printf("MAXITER %ld MAXOBJS %d ", maxiter, MAXOBJS );
  101.     printf("BIGOBJ %ld, TINYOBJ %d, nbig/ntiny 1/%d\n",
  102.     BIGOBJ, TINYOBJ, BIGMOD );
  103.     fflush( stdout );
  104.  
  105.     if( NULL == (objs = (int **)calloc( MAXOBJS, sizeof( *objs ) ) ) )
  106.     {
  107.         fprintf(stderr, "Can't allocate memory for objs array\n");
  108.         exit(1);
  109.     }
  110.  
  111.     if( NULL == ( sizes = (SIZETYPE *)calloc( MAXOBJS, sizeof( *sizes ) ) ))
  112.     {
  113.         fprintf(stderr, "Can't allocate memory for sizes array\n");
  114.         exit(1);
  115.     }
  116.  
  117.     /* as per recent discussion on net.lang.c, calloc does not 
  118.     ** necessarily fill in NULL pointers...
  119.     */
  120.     for( i = 0; i < MAXOBJS; i++ )
  121.         objs[ i ] = NULL;
  122.  
  123.     startsize = sbrk(0) - &end;
  124.     printf( "Memory use at start: %ld bytes\n", startsize );
  125.     fflush(stdout);
  126.  
  127.     printf("Starting the test...\n");
  128.     fflush(stdout);
  129.     for( n = 0; n < maxiter ; n++ )
  130.     {
  131.         if( !(n % STATMOD) )
  132.         {
  133.             printf("%ld iterations\n", n);
  134.             fflush(stdout);
  135.         }
  136.  
  137.         /* determine object of interest and its size */
  138.  
  139.         r = random();
  140.         objmax = ( r % BIGMOD ) ? TINYOBJ : BIGOBJ;
  141.         size = r % objmax;
  142.         i = r % (MAXOBJS - 1);
  143.  
  144.         /*
  145.          * make sure size is at least one byte
  146.          */
  147.         if( size == 0 )
  148.         {
  149.             size++;
  150.         }
  151.         /* either replace the object or get a new one */
  152.  
  153.         if( objs[ i ] == NULL )
  154.         {
  155.             if( (i % 5) == 0 )
  156.             {
  157.                 objs[ i ] = (int *)memalign(1024, size );
  158.             }
  159.             else if( (i % 4) == 0 )
  160.             {
  161.                 objs[ i ] = (int *)calloc(1, size );
  162.             }
  163.             else if( (i%3) == 0 )
  164.             {
  165.                 objs[ i ] = (int *)XtCalloc(1, size );
  166.             }
  167.             else if( (i%2) == 0 )
  168.             {
  169.                 objs[ i ] = (int *)XtMalloc( size );
  170.             }
  171.             else
  172.             {
  173.                 objs[ i ] = (int *)malloc( size );
  174.             }
  175.             nm++;
  176.         }
  177.         else
  178.         {
  179.             /* don't keep bigger objects around */
  180.             if( size > sizes[ i ] )
  181.             {
  182.                 if( (i & 0x01) == 0 )
  183.                 {
  184.                     objs[ i ] = (int *)realloc(
  185.                             (DATATYPE *) objs[ i ], size );
  186.                 }
  187.                 else
  188.                 {
  189.                     objs[ i ] = (int *)XtRealloc(
  190.                             (DATATYPE *) objs[ i ], size );
  191.                 }
  192.                 nre++;
  193.             }
  194.             else
  195.             {
  196.                 if( (i & 0x01) == 0 )
  197.                 {
  198.                     free( (DATATYPE *) objs[ i ] );
  199.                 }
  200.                 else
  201.                 {
  202.                     XtFree( (DATATYPE *) objs[ i ] );
  203.                 }
  204.                 objs[ i ] = (int *)malloc( size );
  205.                 nm++;
  206.             }
  207.         }
  208.  
  209.         sizes[ i ] = size;
  210.         if( objs[ i ] == NULL )
  211.         {
  212.             printf("\nCouldn't allocate %ld byte object!\n", 
  213.                 size );
  214.             break;
  215.         }
  216.     } /* for() */
  217.  
  218.     printf( "\n" );
  219.     cnt = 0;
  220.     for( i = 0; i < MAXOBJS; i++ )
  221.         if( objs[ i ] )
  222.             cnt++;
  223.  
  224.     printf( "Did %ld iterations, %d objects, %d mallocs, %d reallocs\n",
  225.         n, cnt, nm, nre );
  226.     printf( "Memory use at end: %ld bytes\n", sbrk(0) - &end );
  227.     fflush( stdout );
  228.  
  229.     /* free all the objects */
  230.     for( i = 0; i < MAXOBJS; i++ )
  231.     {
  232.         if( objs[ i ] != NULL )
  233.         {
  234.                 if( (i & 0x01) == 0 )
  235.                 {
  236.                     free( (DATATYPE *) objs[ i ] );
  237.                 }
  238.                 else
  239.                 {
  240.                     XtFree( (DATATYPE *) objs[ i ] );
  241.                 }
  242.         }
  243.     }
  244.  
  245.     endsize = sbrk(0) - &end;
  246.     printf( "Memory use after free: %ld bytes\n", endsize );
  247.     fflush( stdout );
  248.  
  249.     if( startsize != endsize )
  250.         printf("startsize %ld != endsize %d\n", startsize, endsize );
  251.  
  252.     free( (DATATYPE *) objs );
  253.     free( (DATATYPE *) sizes );
  254.  
  255.     /*
  256.      * make sure detail mode is enabled
  257.      */    
  258.     m.i = 1;
  259.     dbmallopt(MALLOC_DETAIL,&m);
  260.  
  261.     malloc_dump(2);
  262.     exit( 0 );
  263.     /*NOTREACHED*/
  264. }
  265.  
  266. /*
  267.  * Fake X Windows stuff in order to get xmalloc stuff to link correctly.
  268.  */
  269. char * XtCXtToolkitError = "junk string";
  270. int
  271. XtErrorMsg()
  272. {
  273.     return(0);
  274. }
  275.