home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / umb1.cpp < prev   
Encoding:
C/C++ Source or Header  |  1991-08-27  |  3.6 KB  |  110 lines

  1. /* UMB1.CPP:  This program overloads the C++ new and delete operators
  2.               to take advantage of Upper Memory Blocks if available
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <alloc.h>
  8. #include <dos.h>
  9.  
  10. #if !defined(__COMPACT__) && !defined(__LARGE__) && !defined(__HUGE__)
  11. #error Usage of UMB Pointers via operator new requires far Data Memory Model
  12. #endif
  13.  
  14. #define  NOTIMP 0x80                            // Not Implemented
  15. #define  TOOBIG 0xB0                            // A smaller block is available
  16. #define  NOUMB  0xB1                            // No UMBs are available
  17. #define  BADUMB 0xB2                            // Invalid UMB specified
  18.  
  19. typedef void ( * FARFPTR )();                   // Function Pointer
  20. extern  FARFPTR _new_handler;                   // Pointer to New Handler
  21.  
  22. enum  { FALSE = 0x00, TRUE = 0x01 };
  23. static  int     UseXMSFlag = 0x00  ;            // Flags presence of XMM
  24. static  FARFPTR XMSEntryPoint = NULL ;          // Entry Point to XMS Driver
  25.  
  26. int InitXMS()
  27. {
  28.     _AX = 0x4300;                               // Multplx ID+Presence Request
  29.     geninterrupt( 0x2F );                       // Make Multiplex Call
  30.     if ( _AL == 0x80 )                          // _AL == 0x80 -> XMM Present
  31.     {
  32.         unsigned Seg,  Off;
  33.         _AX  = 0x4310;                          // Multplx ID+Address Request
  34.         geninterrupt( 0x2F );                   // Make Multiplex Call
  35.         Seg  = _ES;                             // Save Returned Segment
  36.         Off  = _BX;                             // Save Returned Offset
  37.         XMSEntryPoint= FARFPTR(MK_FP(Seg, Off));// Save XMM Entry Point
  38.         UseXMSFlag++;                           // Flag Usage of XMS
  39.         return ( TRUE );                        // XMM Present !
  40.     }
  41.     return ( FALSE );
  42. }
  43.  
  44.  
  45. int UMBStats( int & status )
  46. {
  47.     _DX = 0xFFFF;                               // Request FFFFh Paragraphs
  48.     _AH = 0x10;                                 // Request UMB Function
  49.     XMSEntryPoint();                            // Call Upon XMM
  50.     status = _DX;                               // Save DX: Possible size info
  51.     return ( int( _BL ));                       // Return Stats
  52. }
  53.  
  54. void * AllocUMB( size_t size )
  55. {
  56.     size <<= 4;                                 // Convert to Paragraph
  57.     size++;                                     // Adjust for possible carry
  58.     _DX = size;                                 // DX ==  Requested Size
  59.     _AH = 0x10;                                 // Request UMB Function
  60.     XMSEntryPoint();                            // Call Upon XMM
  61.     if ( _AX == 0x0001 )                        // Was request granted ?
  62.     {
  63.         _DX =  _BX;                             // Move Segment to DX
  64.         _AX = 0x0000;                           // Clear Offset
  65.         return(void *)(MK_FP(_DX, _AX));        // Make/Return Far Pointer
  66.     }
  67.     else
  68.         return(void *)NULL;                     // Return NULLL
  69. }
  70.  
  71. int FreeUMB( void  * UMBPtr )
  72. {
  73.     _DX = FP_SEG( UMBPtr );
  74.     _AH = 0x11;
  75.     XMSEntryPoint();
  76.     if ( _AX == 0x0001 )
  77.         return( 0x00 );
  78.     else
  79.         return( int( _BL ));
  80. }
  81.  
  82. void * operator new ( size_t size )
  83. {
  84.     void  * ptr;
  85.     if (UseXMSFlag != 00)
  86.     {
  87.         ptr  = AllocUMB( size );
  88.         if ( ptr != NULL )
  89.             return  ptr ;
  90.     }
  91.     while( ((ptr = malloc(size)) == NULL ) && _new_handler != NULL)
  92.         _new_handler();
  93.     return ptr;
  94. }
  95.  
  96.  
  97. void  operator delete ( void * ptr )
  98. {
  99.     if ((UseXMSFlag != 0x00) && (FP_SEG(ptr) > 0xA000))
  100.         FreeUMB( ptr );
  101.     else
  102.         free( ptr );
  103. }
  104.  
  105. int main(void)
  106. {
  107.     return 0;
  108. }
  109.  
  110.