home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / clibs / HeapGraph / Example / c / Main
Encoding:
Text File  |  1994-09-02  |  1.9 KB  |  78 lines

  1. /* Prog to test the HeapDisplay/StubsHack library    */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #include "HeapGraph.HeapGraph.h"
  8.  
  9.  
  10. #define Utils_Rnd() ( (double) rand() / ( RAND_MAX+1.0))
  11.     /*double  Utils_Rnd( void);                */
  12.     /* Returns random double y, 0 <= y <1            */
  13.  
  14. #define Utils_RndInt( x) ( (int) (Utils_Rnd() * ((double) x)) )
  15.     /* int Utils_RndInt( int x);                */
  16.     /* Returns random integer from { 0, 1, ... x-1 }.    */
  17.     /* Have to use  ((double) x) instead of (double)(x)    */
  18.     /* in case x is 'a-b' - get a loss of precision warning    */
  19.     /* otherwise                        */
  20.  
  21.  
  22. #define N 8
  23.  
  24. int    main( void)
  25. {
  26. void        *pointers[ N];
  27. int        i;
  28. clock_t        t1 = clock(), t2;
  29. StubsHack_error    e;
  30.  
  31.  
  32. /**************************************************************************/
  33. /* This is the only non-standard part of this program...        ***/
  34. /**************************************************************************/
  35.                                     /**/
  36. e = HeapGraph_RedirectAllocFns( NULL);                    /**/
  37. if  ( e != StubsHack_error_OK)    {                    /**/
  38.     printf( "Couldn't redirect alloc fns, error %i\n", e);        /**/
  39.     exit( 1);                            /**/
  40.     }                                /**/
  41.     /* This is the only non-standard part of this program.    */    /**/
  42.                                     /**/
  43. /**************************************************************************/
  44.  
  45. printf( "Started...\n");
  46.  
  47. for ( i=0; i<N; i++)    pointers[i] = NULL;
  48.  
  49.  
  50. /* Let's do some allocating...    */
  51. for(;;)    {
  52.  
  53.     int    operation    = Utils_RndInt( 3);
  54.     int    size        = Utils_RndInt( 512) + 128;
  55.     int    p        = Utils_RndInt( N);
  56.  
  57.     if ( operation == 0 && pointers[ p] == NULL)
  58.         pointers[ p] = malloc( size);
  59.  
  60.     else if ( operation == 1 && pointers[ p] != NULL)
  61.         pointers[ p] = realloc( pointers[ p], size);
  62.  
  63.     else if ( operation == 2 && pointers[ p] != NULL)    {
  64.         free( pointers[ p]);
  65.         pointers[ p] = NULL;
  66.         }
  67.  
  68.     t2 = clock();
  69.     if ( t2-t1 > CLOCKS_PER_SEC*1)    {
  70.         printf( ".");
  71.         HeapGraph_Sendf( "This could be debugging info...\n");
  72.         t1 = t2;
  73.         }
  74.     }
  75.  
  76. return 0;
  77. }
  78.