home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l430 / 1.ddi / SOURCE.ZIP / BASED.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-09  |  3.4 KB  |  136 lines

  1. /*
  2.     BASED.C -- based pointer routines for Windows
  3.         
  4.     See also WINHEAP.C
  5.  
  6.     Copyright (c) Andrew Schulman, 1990-1992.
  7.     All Rights Reserved.
  8.         
  9.     Contact:  Andrew Schulman (CompuServe 76320,302)
  10.     
  11.     From "Undocumented Windows" (Addison-Wesley 1992)
  12.     by Andrew Schulman, Dave Maxey and Matt Pietrek
  13.         
  14.     NOTE:  This is for Microsoft C only.  Would require some
  15.     tweaking to make work with Borland.
  16. */
  17.  
  18. #include <windows.h>
  19. #include <malloc.h>
  20. #include "winio.h"
  21.  
  22. #ifdef __BORLANDC__
  23. #include "based.h"
  24. #endif
  25.  
  26. /* Allocate a based heap, and return a segment value that should be used
  27.    by subsequent based-heap functions */
  28. _segment _bheapseg(size_t size)
  29. {
  30.     WORD blk;
  31.     blk = GlobalAlloc(GMEM_FIXED | GMEM_NODISCARD | GMEM_ZEROINIT, size);
  32.     if (blk == 0)
  33.         return -1;
  34.     if (LocalInit(blk, 0, size-1) == 0) /* calls GlobalLock */
  35.         return -1;
  36.     /* succeeded */
  37.     return blk;
  38. }
  39.  
  40. /* Free a based heap */
  41. int _bfreeseg(_segment seg)
  42. {
  43.     GlobalUnlock(seg);
  44.     return (GlobalFree(seg) == NULL) ? 0 : -1;
  45. }
  46.  
  47. /* Allocate a block of memory from a heap; the segment value must have
  48.    been previously returned by _bheapseg */
  49. void _based(void) *_bmalloc(_segment segm, size_t size)
  50. {
  51.     unsigned ret;
  52.     if (segm)   // if (segm==0) use default data segment
  53.     {
  54.         _asm push ds
  55.         _asm mov ax, segm
  56.         _asm mov ds, ax
  57.     }
  58.     if (ret = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, size))
  59.         ret = (unsigned) LocalLock(ret);
  60.     if (segm)
  61.         _asm pop ds
  62.     return (void _based(void) *) ret;
  63. }
  64.  
  65. /* Allocate storage for an array, zero-initialize the elements of the
  66.    array, and return a based pointer to the array */
  67. void _based(void) *_bcalloc(_segment segm, size_t num, size_t size)
  68. {
  69.     return _bmalloc(segm, num * size);
  70. }
  71.  
  72. /* Free an allocated block */
  73. void _bfree(_segment segm, void _based(void) *memblock)
  74. {
  75.     if (((unsigned) memblock) == 0xFFFF)
  76.         return;
  77.     if (segm)
  78.     {
  79.         _asm push ds
  80.         _asm mov ax, segm
  81.         _asm mov ds, ax
  82.     }
  83.     LocalUnlock((WORD) memblock);
  84.     LocalFree((WORD) memblock);
  85.     if (segm)
  86.         _asm pop ds
  87. }
  88.  
  89. /* Helper function used by _bexpand and _brealloc */
  90. static void _based(void) *_bresize(_segment segm, 
  91.     void _based(void) *memblock, size_t size, WORD wFlags)
  92. {
  93.     unsigned ret;
  94.     if (segm)
  95.     {
  96.         _asm push ds
  97.         _asm mov ax, segm
  98.         _asm mov ds, ax
  99.     }
  100.     ret = (unsigned) LocalReAlloc((char near *) memblock, size, 
  101.         wFlags | LMEM_ZEROINIT);
  102.     if (segm)
  103.         _asm pop ds
  104.     return (void _based(void) *) ret;
  105. }
  106.  
  107. /* Expand or shrink an allocated block in place */
  108. void _based(void) *_bexpand(_segment segm, 
  109.     void _based(void) *memblock, size_t size)
  110. {
  111.     return _bresize(segm, memblock, size, LMEM_FIXED);
  112. }
  113.  
  114. /* Expand or shrink an allocated block, possibly moving it */
  115. void _based(void) *_brealloc(_segment segm, 
  116.     void _based(void) *memblock, size_t size)
  117. {
  118.     return _bresize(segm, memblock, size, LMEM_MOVEABLE);
  119. }
  120.  
  121. /* Return the size of a previously allocated block */
  122. size_t _bmsize(_segment segm, void _based(void) *memblock)
  123. {
  124.     WORD ret;
  125.     if (segm)
  126.     {
  127.         _asm push ds
  128.         _asm mov ax, segm
  129.         _asm mov ds, ax
  130.     }
  131.     ret = LocalSize((WORD) memblock);
  132.     if (segm)
  133.         _asm pop ds
  134.     return ret;
  135. }
  136.