home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / code / stubshack / HeapGraph / Example / c / Main
Encoding:
Text File  |  1994-11-24  |  1.6 KB  |  85 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. #include "HeapGraph.Debug.h"
  9.  
  10.  
  11.  
  12.  
  13. #define Utils_Rnd() ( (double) rand() / ( RAND_MAX+1.0))
  14.     /*double  Utils_Rnd( void);                */
  15.     /* Returns random double y, 0 <= y <1            */
  16.  
  17. #define Utils_RndInt( x) ( (int) (Utils_Rnd() * ((double) x)) )
  18.     /* int Utils_RndInt( int x);                */
  19.     /* Returns random integer from { 0, 1, ... x-1 }.    */
  20.     /* Have to use  ((double) x) instead of (double)(x)    */
  21.     /* in case x is 'a-b' - get a loss of precision warning    */
  22.     /* otherwise                        */
  23.  
  24.  
  25.  
  26.  
  27.  
  28. #define N 8
  29.  
  30.  
  31. int    main( void)
  32. {
  33. void    *pointers[ N];
  34. int    i;
  35. clock_t    t1, t2;
  36.  
  37.  
  38. HeapGraph_RedirectAllocFns( NULL);
  39.  
  40. printf( "Started...\n");
  41.  
  42.  
  43. for ( i=0; i<N; i++)    pointers[i] = NULL;
  44.  
  45. t1 = clock();
  46.  
  47. /* Let's do some allocating...    */
  48. for(;;)    {
  49.  
  50.     int    operation    = Utils_RndInt( 3);
  51.     int    size        = Utils_RndInt( 512) + 128;
  52.     int    p        = Utils_RndInt( N);
  53.  
  54.     HeapGraph_SetReference( Utils_RndInt( 8));
  55.  
  56.     if ( operation == 0 && pointers[ p] == NULL)
  57.         pointers[ p] = malloc( size);
  58.  
  59.     else if ( operation == 1 && pointers[ p] != NULL)
  60.         pointers[ p] = realloc( pointers[ p], size);
  61.  
  62.     else if ( operation == 2 && pointers[ p] != NULL)    {
  63.         free( pointers[ p]);
  64.         pointers[ p] = NULL;
  65.         }
  66.  
  67.     t2 = clock();
  68.     if ( t2-t1 > CLOCKS_PER_SEC*2)    {
  69.         printf( ".");
  70.         
  71.         Debug_Printf( "This text is from a call to Debug_Printf. ");
  72.         Debug_Printf( "The Last operation was type %i, pointers[p]=%x, p=%i\n", 
  73.             operation, pointers[p], p
  74.             );
  75.             
  76.         HeapGraph_Sendf( "This text is from a call to HeapGraph_Sendf\n");
  77.         
  78.         t1 = t2;
  79.         }
  80.     }
  81.  
  82.  
  83. return 0;
  84. }
  85.