home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctdemo.zip / classes / @CLSLIB1.ZIP / cls / runtime / test / heap_t1.cpp next >
Encoding:
C/C++ Source or Header  |  1995-03-30  |  7.0 KB  |  351 lines

  1. #ifndef INC_HEAP_T1
  2. #define INC_HEAP_T1
  3.  
  4. #ifndef __FIRST__
  5. #define __FIRST__
  6. #define __IMPL__HEAP_T1
  7. #endif
  8.  
  9. /////V Heap_t1 PCM f:\cls_ibm\cls\runtime\test 1  PM 30.03.95 10:03:08
  10. /*
  11. /////H
  12. 18.12.94 01:57 PM 0 copied from: Heap_t1 TOS f:\cls_ibm\cls\runtime\test 0 PM 19.10.94 00:46:26
  13. 30.03.95 10:34 PM 1 archived: Heap_t1 PCM f:\cls_ibm\cls\runtime\test 1  PM 30.03.95 10:03:08
  14. /////
  15. */
  16.  
  17. /////1
  18. #undef inline
  19.  
  20. #include <bsa.h>
  21.  
  22. /////I stdlib.h @ @ @ @ pre 
  23. #include <stdlib.h>
  24.  
  25. /////I stdio.h @ @ @ @ pre 
  26. #include <stdio.h>
  27.  
  28. /////I os2.h @ @ INCL_DOS @ @ pre 
  29. #define INCL_DOS
  30. #include <os2.h>
  31. #undef INCL_DOS
  32.  
  33. /////I Heap @ @ @ @ class pre 
  34. #include <Heap.cpp>
  35.  
  36. /////I XH @ @ @ @ class pre 
  37. #include <XH.cpp>
  38.  
  39. /////T Pre pre 
  40. struct X {
  41.     int Num;
  42.     int Data1;
  43.     char Data2;
  44.  
  45.     X( int i)
  46.     { Num = i; }
  47. };
  48. /////
  49.  
  50.  
  51. #ifndef __INLINE__
  52. #define inline
  53. #endif
  54.  
  55. /////C Heap_t1 @ @ use:__HEAPTRACE__ define:_VIO_ app 
  56. class Heap_t1
  57.  
  58. {
  59.  
  60. public:
  61. /////D Array @  @ class public 
  62.     static void*  Array [100000];
  63.  
  64.  
  65. public:
  66.     static int  heap1 ();
  67.     static int  heap2 ();
  68.     static int  malloc1 ();
  69.     static int  malloc2 ();
  70.     static void   printStatistics ();
  71. };
  72.  
  73.  
  74. /////2
  75. #undef inline
  76.  
  77.  
  78. #if (defined __INLINE__) || (defined __IMPL__HEAP_T1)
  79.  
  80. #ifndef __INLINE__
  81. #define inline
  82. #endif
  83.  
  84. /////
  85. #endif
  86.  
  87. /////3
  88. #undef inline
  89.  
  90. #ifdef __IMPL__HEAP_T1
  91. /////
  92. void*  Heap_t1::Array [100000];
  93.  
  94. /////F heap1 @ @ class public 
  95. int Heap_t1:: heap1 ()
  96. {
  97.     const int rounds = 100000;
  98.  
  99.     DATETIME dt;
  100.     ULONG     hs1, hs2;
  101.     void       *p, *pBeg, *pTop;
  102.     
  103.     DosGetDateTime (&dt);
  104.     hs1 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  105.  
  106.     pBeg = NEW_VEC( int, 2);
  107.  
  108.     for (int i=0; i< rounds; i++){
  109.         p = NEW_VEC( int, 2);
  110.         DELETE_VEC (p);
  111.     }
  112.  
  113.     pTop = NEW_VEC( int, 2);
  114.  
  115.     DosGetDateTime( &dt);
  116.     hs2 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  117.  
  118.     printf("%u CT heap manager allocs (immediate delete) : %u ms\n", rounds, (hs2 - hs1) * 10);
  119.     printf("used memory range: %d bytes\n", (ULONG)pTop - (ULONG)pBeg);
  120.     
  121.     DELETE_VEC( pBeg);
  122.     DELETE_VEC( pTop);
  123.  
  124.     return 1;
  125. }
  126.  
  127. /////F heap2 @ @ class public 
  128. int Heap_t1:: heap2 ()
  129. {
  130.     const ULONG rounds = 100000;
  131.  
  132.     DATETIME dt;
  133.     ULONG     i, hs1, hs2, hs3;
  134.     void     *pBeg, *pTop;
  135.     
  136.     DosGetDateTime (&dt);
  137.     hs1 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  138.  
  139.     pBeg = NEW_VEC( int, 2);
  140.  
  141.     for (i=0; i< rounds; i++){
  142.         Array[i] = NEW_VEC( int, 2);
  143.     }
  144.  
  145.     DosGetDateTime (&dt);
  146.     hs2 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  147.  
  148.     for (i=0; i< rounds; i++){
  149.         DELETE_VEC( Array[i]);
  150.     }
  151.  
  152.     pTop = NEW_VEC( int, 2);
  153.  
  154.     DosGetDateTime( &dt);
  155.     hs3 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  156.  
  157.     printf("%u CT heap manager allocs (all new / all del) : %u / %u ms\n", rounds, (hs2 - hs1) * 10, (hs3 - hs2) * 10);
  158.     printf("used memory range: %d bytes\n", (ULONG)pTop - (ULONG)pBeg);
  159.  
  160.     DELETE_VEC( pBeg);
  161.     DELETE_VEC( pTop);
  162.  
  163.     return 1;
  164. }
  165.  
  166. /////
  167. #ifdef __APPCLASS__
  168. /////F main @ @ use:__HEAPTRACE__ global usesDefine 
  169. int main ( int argc, char* argv[] )
  170. {
  171.     /*
  172.         *** CThrough heap manager demo app ***
  173.     */
  174.         
  175.     int     i, n;
  176.     void *p;
  177.     char *pStr;
  178.  
  179.     //--- set XH's exception handler
  180.     REGISTER_XCPT;
  181.  
  182.     //--- don't trace that big ones
  183.     Heap::stopTrace();
  184.  
  185. #ifdef __HEAPTRACE__
  186.     printf("heaptrace mode on !\nto test performance, build app without heaptrace\n\n");
  187. #endif
  188.  
  189.     //--- performance and memory occupation tests 
  190.     printf("\nallocation performance test ( CT heap manager <-> malloc)\n");
  191.     printf("-----------------------------------------------------------\n");
  192.     Heap_t1::heap1();
  193.     Heap_t1::malloc1();
  194.     Heap_t1::heap2();
  195.     Heap_t1::malloc2();
  196.  
  197.     printf("\npress enter to continue\n");
  198.     getchar();
  199.  
  200.     //--- trace again
  201.     Heap::startTrace();
  202.  
  203.     //--- object creation and fragmentation test ( trace first 100)
  204.     printf("\nfragmentation test\n");
  205.     printf("---------------------\n");
  206.     for ( i=0; i < 30000; i++) {
  207.         if ( i == 100)
  208.             Heap::stopTrace();
  209.         Heap_t1::Array[i] = NEW(X, (i));
  210.     }
  211.  
  212.     //--- trace again
  213.     Heap::startTrace();
  214.  
  215.     //--- dump array part
  216.     for ( i=95; i < 100; i++) {
  217.         n = ((X*)Heap_t1::Array[i])->Num;
  218.         printf("array ixd: %d at address %8X\n", n, Heap_t1::Array[i]);
  219.     }
  220.  
  221.     //--- memory fragmentation test
  222.     printf("\ndelete array index 99 and 97\n");
  223.     DELETE( Heap_t1::Array[99]);
  224.     DELETE( Heap_t1::Array[97]);
  225.  
  226.     p = NEW ( X, (1111));
  227.     printf("new entry at address: %8X value: %d\n\n", p, ((X*)p)->Num);
  228.  
  229.     //--- big alloc test
  230.     p = NEW_VEC (int, 5000);
  231.  
  232.     //--- bad memory access test ( recognized only in trace mode)
  233.     pStr = NEW_STR( "1234567");
  234.     *(pStr+8) = '9';
  235.  
  236.     printf("\npress enter to continue\n");
  237.     getchar();
  238.  
  239.     //--- statistics
  240.     Heap_t1::printStatistics ();
  241.     
  242.     //--- unset XH's exception handler
  243.     UNREGISTER_XCPT;
  244.  
  245.     return 0;    
  246.  
  247. }
  248. /////
  249. #endif
  250.  
  251. /////F malloc1 @ @ class public 
  252. int Heap_t1:: malloc1 ()
  253. {
  254.     const int rounds = 100000;
  255.  
  256.     DATETIME dt;
  257.     ULONG     hs1, hs2;
  258.     void      *p, *pBeg, *pTop;
  259.     
  260.     DosGetDateTime (&dt);
  261.     hs1 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  262.  
  263.     pBeg = malloc(8);
  264.  
  265.     for (int i=0; i< rounds; i++){
  266.         p = malloc(8);
  267.         free( p);
  268.     }
  269.  
  270.     pTop = malloc(8);
  271.  
  272.     DosGetDateTime( &dt);
  273.     hs2 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  274.  
  275.     printf("%u standard mallocs (immediate free) : %u ms\n", rounds, (hs2 - hs1) * 10);
  276.     printf("used memory range: %d bytes\n", (ULONG)pTop - (ULONG)pBeg);
  277.  
  278.     free( pBeg);
  279.     free( pTop);
  280.  
  281.     return 1;
  282. }
  283.  
  284. /////F malloc2 @ @ class public 
  285. int Heap_t1:: malloc2 ()
  286. {
  287.     const ULONG rounds = 100000;
  288.  
  289.     DATETIME dt;
  290.     ULONG     i, hs1, hs2, hs3;
  291.     void     *pBeg, *pTop;
  292.     
  293.     DosGetDateTime (&dt);
  294.     hs1 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  295.  
  296.     pBeg = malloc(8);
  297.  
  298.     for (i=0; i< rounds; i++){
  299.         Array[i] = malloc( 8);;
  300.     }
  301.  
  302.     DosGetDateTime (&dt);
  303.     hs2 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  304.  
  305.     for (i=0; i< rounds; i++){
  306.         free( Array[i]);
  307.     }
  308.  
  309.     pTop = malloc(8);
  310.  
  311.     DosGetDateTime( &dt);
  312.     hs3 = ((ULONG)dt.seconds * 100) + dt.hundredths;
  313.  
  314.     printf("%u standard mallocs (all malloc / all free) : %u / %u ms\n", rounds, (hs2 - hs1) * 10, (hs3 - hs2) * 10);
  315.     printf("used memory range: %d bytes\n", (ULONG)pTop - (ULONG)pBeg);
  316.  
  317.     free( pBeg);
  318.     free( pTop);
  319.  
  320.     return 1;
  321. }
  322.  
  323. /////F printStatistics @ @ class public 
  324. void Heap_t1:: printStatistics ()
  325. {
  326.     int i;
  327.  
  328.     printf ("\n--- current heap allocations: ---\n\n");
  329.     printf ("tile  allocs  cluster      base       top    commit[pgs]      used   [%]\n" );
  330.     printf ("------------------------------------------------------------------------\n" );
  331.  
  332.     for ( i=0; i<MAXBLOCKS; i++){
  333.         printf("%4d  %6d  %7d  %8X  %8X  %8d[%3d]  %8d   %3d\n", 
  334.                 Heap::tileSize(i), Heap::tileAllocs(i), Heap::tileClusters(i),
  335.                 Heap::pMemBase(i), Heap::pMemTop(i),
  336.                 Heap::bytesCommitted(i), Heap::pagesCommitted(i), Heap::bytesUsed(i),
  337.                 Heap::occupation(i), Heap::pagesLeft(i) );
  338.     }
  339.  
  340.     printf("\n>%d  %d (min: %d  max: %d)\n", Heap::tileSize(MAXBLOCKS-1), Heap::bigAllocs(),
  341.                 Heap::bytesMinBig(), Heap::bytesMaxBig() );
  342.  
  343.  
  344.     printf("%s\n", Heap::logSpaceAccess() );
  345. }
  346.  
  347. /////
  348. #endif
  349.  
  350. #endif
  351.