home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / WINLBSRC.ZIP / FHEAP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.0 KB  |  165 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - fheap.cpp
  3.  *
  4.  * function(s)
  5.  *        _farmallocf  - allocate far heap space
  6.  *        _farcallocf  - allocate far heap space and zero fill
  7.  *        _farfreef    - free far heap space
  8.  *        _farreallocf - resize far heap block
  9.  *-----------------------------------------------------------------------*/
  10.  
  11. /*
  12.  *      C/C++ Run Time Library - Version 5.0
  13.  *
  14.  *      Copyright (c) 1991, 1992 by Borland International
  15.  *      All Rights Reserved.
  16.  *
  17.  */
  18.  
  19. // FHEAP.CPP
  20.  
  21. #include <windows.h>
  22. #include <alloc.h>
  23. #include <dos.h>
  24. #include <mem.h>
  25.  
  26. extern "C"
  27. {
  28.     extern unsigned _WinAllocFlag;
  29.  
  30.     void far _RLSMEM();
  31.     void far _GETMEM();
  32.     void far _SIZMEM();
  33.  
  34.     void far * _farmallocf (unsigned long size, unsigned flags);
  35.     void far * _farcallocf (unsigned long nitems,
  36.                             unsigned long size, unsigned flags);
  37.     void _farfreef (void far *block, unsigned flags);
  38.     void far * _farreallocf (void far *block, unsigned long size,
  39.                              unsigned flags);
  40. }
  41.  
  42. void far * _FARFUNC _farmallocf(unsigned long size, unsigned flags)
  43. {
  44.     if (size == 0)
  45.         return NULL;
  46.  
  47.     unsigned OldWinAllocFlags = _WinAllocFlag;
  48.     _WinAllocFlag = flags;
  49.  
  50.     _DX = (unsigned short)(size >> 16);
  51.     _AX = (unsigned short)size;
  52.  
  53.     asm push si
  54.     _GETMEM();
  55.     asm pop si
  56.  
  57.     void far * pMem = (void _es *)_DI;
  58.  
  59.     _WinAllocFlag = OldWinAllocFlags;
  60.  
  61.     return pMem;
  62. }
  63.  
  64.  
  65. void far * _FARFUNC _farcallocf(unsigned long nitems, unsigned long size,
  66.                                unsigned flags)
  67. {
  68.     unsigned long RealSize = size * nitems;
  69.  
  70.     unsigned OldWinAllocFlags = _WinAllocFlag;
  71.     _WinAllocFlag = flags | GMEM_ZEROINIT;
  72.  
  73.     _DX = (unsigned short)(RealSize >> 16);
  74.     _AX = (unsigned short)RealSize;
  75.  
  76.     asm push si
  77.     _GETMEM();
  78.     asm pop si
  79.  
  80.     void far * pMem = (void _es *)_DI;
  81.  
  82.     if (RealSize < 0xFFFF && pMem)
  83.         _fmemset(pMem, 0, (size_t)RealSize);
  84.  
  85.     _WinAllocFlag = OldWinAllocFlags;
  86.  
  87.     return pMem;
  88. }
  89.  
  90.  
  91. void _FARFUNC _farfreef(void far *block, unsigned flags)
  92. {
  93.     if (block)
  94.     {
  95.     unsigned OldWinAllocFlags = _WinAllocFlag;
  96.     _WinAllocFlag = flags;
  97.  
  98.     _ES = FP_SEG(block);
  99.     _DI = FP_OFF(block);
  100.  
  101.     asm push si
  102.     _RLSMEM();
  103.     asm pop si
  104.  
  105.     block = 0L;
  106.  
  107.     _WinAllocFlag = OldWinAllocFlags;
  108.     }
  109. }
  110.  
  111.  
  112. void far * _FARFUNC _farreallocf(void far *block, unsigned long size,
  113.                                  unsigned flags)
  114. {
  115.     if (block == 0L)
  116.         return _farmallocf(size, flags);
  117.     else
  118.     {
  119.        if (size == 0L)
  120.           _farfreef(block, flags);       // falls through to return NULL
  121.        else
  122.        {
  123. #if defined(_Windows)
  124.       // Check for a non-suballocated allocation.  The offset
  125.       // of blocks allocated by Windows are currently always 0.
  126.       // We can use this fact to let Windows do the reallocation.
  127.       if ((unsigned short)block == 0)   // Windows allocated
  128.       {
  129.           HANDLE h;
  130.           if ((h = (HANDLE)GlobalHandle(FP_SEG(block))) != 0)
  131.           {
  132.               GlobalUnlock(h);
  133.               if ((h = GlobalReAlloc(h, size,
  134.                   GMEM_MOVEABLE | _WinAllocFlag)) != 0)
  135.                    return GlobalLock(h);
  136.           }
  137.       }
  138.       else
  139. #endif
  140.       {
  141.           // a quick and dirty realloc
  142.           void far *TempPtr = _farmallocf(size, flags);
  143.           unsigned long TempSize;
  144.  
  145.           if (TempPtr)
  146.           {
  147.               _ES = FP_SEG(block);
  148.               _DI = FP_OFF(block);
  149.  
  150.               asm push si
  151.               _SIZMEM();
  152.               asm pop si
  153.  
  154.               TempSize = (unsigned long)((void _seg *)(_DX)+(void near *)(_AX));
  155.               _fmemcpy(TempPtr, block,
  156.                   (size_t) (size > TempSize ? TempSize : size));
  157.               _farfreef(block, flags);
  158.               return TempPtr;
  159.           }
  160.       }
  161.        }
  162.     }
  163.     return NULL;        // catch all.
  164. }
  165.