home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MONTE.ZIP / SUBALLOC / SUBALLOC.C < prev   
Text File  |  1993-03-05  |  2KB  |  71 lines

  1. // suballoc.c
  2.  
  3. // Shows the use of OS2 suballocation package for heap management.
  4. // Especially good for the suballocation of shared memory,
  5. // although this sample only shows it for process private space.
  6.  
  7. // It is preferable to have the suballocation package commit pages
  8. // as needed (the DOSSUB_SPARSE_OBJ flag).  Since you can't grow
  9. // memory objects, you can overestimate without a penalty because the
  10. // suballocation package will commit pages as needed.
  11.  
  12.  
  13.  
  14. // os2 includes
  15. #define INCL_DOS
  16. #include <os2.h>
  17.  
  18. // c includes
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <assert.h>
  23.  
  24. // 100K constant for memory object size
  25. #define SIZE_100K        (100*1024)
  26.  
  27.  
  28. // ----------------------------------------------------------------------
  29.  
  30.  
  31. int main ( void )
  32. {
  33.   APIRET      rc;
  34.   PVOID       pvBase;
  35.   PVOID       apv[ 100 ];
  36.   int         i;
  37.  
  38.   // allocate a 100K memory object; note: no PAG_COMMIT FLAG
  39.   rc = DosAllocMem(  &pvBase, SIZE_100K, PAG_WRITE | PAG_READ );
  40.   assert( rc == 0 );
  41.  
  42.   // initialize OS2 suballocation package for this process alone
  43.   rc = DosSubSetMem( pvBase, DOSSUB_INIT | DOSSUB_SPARSE_OBJ, SIZE_100K );
  44.   assert( rc == 0 );
  45.  
  46.   // suballocate memory 1K at a time.
  47.   for( i = 0; i < 100; i++ ) {
  48.     rc = DosSubAllocMem( pvBase, &apv[ i ], 1024 );
  49.     if( rc != 0 ) {
  50.       // 64 bytes are reserved by OS2 for suballocation management
  51.       // this test failed when i == 99; rc == 311
  52.       printf( "DosSubAllocMem rc %d;   i == %d\n", rc, i );
  53.       break;
  54.     }
  55.   }
  56.  
  57.   // free the suballocs;  length of alloc required here
  58.   do {
  59.     rc = DosSubFreeMem(  pvBase, apv[ --i ], 1024 );
  60.     assert( rc == 0 );
  61.   } while( i );
  62.  
  63.   // free the memory object
  64.   rc = DosFreeMem( pvBase );
  65.   assert( rc == 0 );
  66.  
  67.   printf( "normal completion\n" );
  68.   return 0;
  69. }
  70.  
  71.