home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / c / XMS11.ZIP / XMSDEMO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-31  |  3.7 KB  |  127 lines

  1. //********************************
  2. // Beginning of sample program to
  3. // demonstrate the usage of
  4. // extended memory using MS-XMS
  5. //*******************************
  6.  
  7. #include <iostream.h>
  8. #include <string.h>
  9. #include "xms.h"
  10.  
  11. void print_msg( char *str );
  12.  
  13. // Print a message with little box lines on top and below
  14. // very dumb, just used to make output look somewhat decent
  15. void print_msg( char *str )
  16. {
  17.     int i, len;
  18.     len = strlen( str );
  19.     cout << "\n";
  20.     for( i=0; i < len; i++ )
  21.         cout << "-";
  22.     cout << "\n" << str << "\n";
  23.     for( i=0; i < len; i++ )
  24.         cout << "-";
  25.     cout << "\n";
  26. }
  27.  
  28. int main( void )
  29. {
  30.     // To use the lib, declare an instance of the class
  31.     XMSDriver XMM;
  32.     long AMem;
  33.     char Dude[ 10 ] = "Excellent";
  34.     char ReadIn[ 22 ];
  35.     char Block[22] = "Hi From Extended Mem!";
  36.  
  37.     // EMBHandles array will hold five EMB handles
  38.     // i is a loop counter
  39.     unsigned EMBHandles[ 5 ], i;
  40.  
  41.     // Determine the presence and availability of extended memory
  42.     // (this section checks for 64K available)
  43.     if( !XMM.Inst() || (XMM.GetAvail() < 64) )
  44.     {
  45.         cout << "Can't continue the XMS demo.\n";
  46.         cout << "No extended memory manager installed, or\n";
  47.         cout << "not enough extended memory available (needs 64k).\n";
  48.         cout << "Add HIMEM.SYS to your config.sys file.\n";
  49.         return( 1 );
  50.     }
  51.     
  52.     print_msg( "Status Info" );
  53.     AMem = XMM.GetAvail( );            // Get largest available block size
  54.  
  55.     // Note that, due to fragmentation, the largest free block is
  56.     // not the same as the total sum of all free blocks...
  57.     cout << "Available Extended Memory = ";
  58.     cout << AMem << "\n";
  59.     cout << "Total Available = " << XMM.GetTotal( ) << "\n";
  60.  
  61.     print_msg( "High Memory Area Functions" );
  62.     cout << "Requesting HMA...\n";
  63.     if( XMM.ReqHMA( ) )                    // Request access to the HMA
  64.         cout << "HMA gotten!\n";        // Returns 'True' on success
  65.     else
  66.         cout << "HMA gotten - NOT!\n";    // Unable to get HMA
  67.  
  68.     // To use the High Memory Area, you must enable the A20 line
  69.     cout << "Enabling A20 line...\n";
  70.     if( XMM.A20Enable( ) )
  71.         cout << "A20 Enabled\n";
  72.     else
  73.         cout << "Error enabling A20\n";
  74.  
  75.     cout << "Checking current A20 state...\n";
  76.     if( XMM.A20Query( ) )
  77.     {
  78.         cout << "A20 Enabled (disabling now).\n";
  79.         XMM.A20Disable( );
  80.     }
  81.     else
  82.         cout << "A20 Disabled.\n";
  83.  
  84.     if( XMM.HMAInst( ) )
  85.     {
  86.         // Note that the built in HMARead and HMAWrite routines
  87.         // DO NOT enable the A20 line automatically
  88.         XMM.A20Enable( );
  89.         // Size, Index, Length, Data
  90.         cout << "Writing " << Dude << " to HMA...\n";
  91.         XMM.HMAWrite( 1, 5, 10, Dude );
  92.         cout << "Attempting to read in...\n";
  93.         XMM.HMARead( 1, 5, 10, ReadIn );
  94.         cout << '\'' << ReadIn << "\' was read in from HMA.\n";
  95.         XMM.A20Disable( );
  96.         cout << "Freeing HMA...\n";
  97.         XMM.RelHMA( );
  98.     }
  99.     
  100.     print_msg( "Extended Memory Block Functions" );
  101.     // Allocate five extended memory blocks
  102.     for( i = 0; i < 4; i++ )
  103.         EMBHandles[ i ] = XMM.EMBAlloc( (int) AMem / 5 );
  104.     for( i = 0; i < 4; i++ )
  105.         cout << "EMBHandle #" << i << " = " << EMBHandles[ i ] << "\n";
  106.     // Free all but one extended memory block (EMBHandles[0])
  107.     for( i = 1; i < 4; i++ )
  108.         XMM.EMBFree( EMBHandles[ i ] );
  109.  
  110.     AMem = (long) AMem / 5 + 32;        // Reallocate 32k more
  111.     if( XMM.EMBRealloc( EMBHandles[0], AMem + 32 ) )
  112.         cout << "Reallocated to " << AMem + 32 << "k bytes...\n";
  113.     else
  114.         cout << "Cannot reallocate...\n";
  115.  
  116.     // Write to and read from extended memory
  117.     cout << "Writing " << Block << " to extended memory...\n";
  118.     XMM.EMBMoveToExt( EMBHandles[0], 24, Block, strlen( Block )+1 );
  119.     XMM.EMBMoveToCon( ReadIn, EMBHandles[0], 24, strlen( Block )+1 );
  120.  
  121.     cout << "Read in from EMB...-> " << ReadIn << '\n';
  122.     
  123.     cout << "Freeing block...\n";
  124.     XMM.EMBFree( EMBHandles[0] );
  125. }
  126. // END XMSDEMO.CPP
  127.