home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / ddeml / mem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  4.6 KB  |  215 lines

  1. #include "ddemlp.h"
  2.  
  3. /*
  4.  * Memory routines
  5.  *
  6.  *  These routines simplify and centralize memory handling to help reduce
  7.  *  selector usage overhead and ease transport to 32 bit memory models.
  8.  */
  9.  
  10.  
  11. /*
  12.  * Debugging routines --------------------------------------------------
  13.  */
  14.  
  15. PBYTE FarAllocMem(hheap, cb)
  16. HHEAP hheap;
  17. USHORT cb;
  18. {
  19.     register NPBYTE pb;
  20.  
  21.     pb = MyAllocMem(hheap, cb);
  22.     if (pb == NULL)
  23.         return(NULL);
  24.     return(MAKEP(SELECTOROF(hheap), pb));
  25. }
  26.  
  27.  
  28. #ifdef DEBUG
  29.  
  30. #define MAX_SQUIRL 400
  31. #define HPSIG 0xfedc
  32.  
  33. PUSHORT asqu[MAX_SQUIRL] = { NULL };
  34. USHORT cMaxSqu = 0;
  35. USHORT cCurSqu = 0;
  36.  
  37. VOID Walk(VOID);
  38. VOID Squirl(PBYTE);
  39. VOID Unsquirl(PBYTE);
  40.  
  41.  
  42. VOID Walk()
  43. {
  44.     static BOOL fRecurse = FALSE;
  45.     register USHORT i;
  46.  
  47.     if (fRecurse)
  48.         return;
  49.     fRecurse = TRUE;
  50.     for (i = 0; i < cMaxSqu; i++) {
  51.         if (OFFSETOF(asqu[i]) < CheckSel(SELECTOROF(asqu[i])))
  52.             if (!(*asqu[i] == HPSIG &&
  53.                     (*(PUSHORT)((PBYTE)asqu[i] +
  54.                     (*(asqu[i] - 1)) - 2) == HPSIG))) {
  55.                 AssertF(FALSE, "Heap Walk failure");
  56.                 asqu[i] = NULL;
  57.             }
  58.     }
  59.     fRecurse = FALSE;
  60. }
  61.  
  62. VOID Squirl(p)
  63. PBYTE p;
  64. {
  65.     register USHORT i;
  66.     
  67.     for (i = 0; i < cMaxSqu + 1; i++) {
  68.         if (asqu[i] == NULL) {
  69.             asqu[i] = (PUSHORT)p;
  70.             cCurSqu = min(cCurSqu + 1, MAX_SQUIRL - 1);
  71.             cMaxSqu = max(cCurSqu, cMaxSqu);
  72.             return;
  73.         }
  74.     }
  75. }
  76.  
  77. VOID Unsquirl(p)
  78. PBYTE p;
  79. {
  80.     register USHORT i;
  81.     
  82.     for (i = 0; i < cMaxSqu + 1; i++) {
  83.         if (asqu[i] == (PUSHORT)p) {
  84.             asqu[i] = NULL;
  85.             cCurSqu--;
  86.             return;
  87.         }
  88.     }
  89.     AssertF(cMaxSqu == (MAX_SQUIRL - 1), "Unsquirl error");
  90. }
  91.  
  92.  
  93. HHEAP DbgCreateHeap(
  94. SEL sel,
  95. USHORT cb,
  96. USHORT cbGrow,
  97. USHORT cbMinDed,
  98. USHORT cbMaxDed,
  99. USHORT fs)
  100. {
  101.     HHEAP hheap;
  102.     register USHORT i;
  103.  
  104.     hheap = WinCreateHeap(sel, cb, cbGrow, cbMinDed, cbMaxDed, fs);
  105.     sel = HIUSHORT(WinLockHeap(hheap));
  106.     /*
  107.      * remove all squirled pointers in this segment so we don't
  108.      * get errors later.
  109.      */
  110.     for (i = 0; i < cMaxSqu; i++) {
  111.         if (SELECTOROF(asqu[i]) == sel) {
  112.             asqu[i] = NULL;
  113.             cCurSqu--;
  114.         }
  115.     }
  116.     return(hheap);
  117. }
  118.  
  119.  
  120.  
  121. HHEAP DbgDestroyHeap(
  122. HHEAP hheap)
  123. {
  124.     /*
  125.      * purge squirl list of any of these pointers.
  126.      */
  127.     register USHORT i;
  128.     
  129.     for (i = 0; i < cMaxSqu; i++) {
  130.         if (SELECTOROF(asqu[i]) == SELECTOROF(hheap)) {
  131.             asqu[i] = NULL;
  132.             cCurSqu--;
  133.         }
  134.     }
  135.     return(WinDestroyHeap(hheap));
  136. }
  137.  
  138.  
  139.  
  140. NPBYTE DbgAllocMem(hheap, cb)
  141. HHEAP hheap;
  142. USHORT cb;
  143. {
  144.     register PBYTE p;
  145.  
  146.     Walk();
  147.     p = MAKEP(SELECTOROF(hheap), WinAllocMem(hheap, cb + 4));
  148.     if (LOUSHORT(p) == NULL) 
  149.         return(NULL);
  150.     Squirl(p);
  151.     *(PUSHORT)p = HPSIG;
  152.     *(PUSHORT)(p + cb + 2) = HPSIG;
  153.     return((NPBYTE)(SHORT)p + 2);
  154. }
  155.  
  156.  
  157. /*
  158.  * note that cbOld perameter includes the signatures since it was derived
  159.  * from the heap itself.  cbNew, however does not incude the signatures.
  160.  */
  161. NPBYTE DbgReallocMem(hheap, npMem, cbOld, cbNew)
  162. HHEAP hheap;
  163. NPBYTE npMem;
  164. USHORT cbOld;
  165. USHORT cbNew;
  166. {
  167.     register PBYTE p;
  168.  
  169.     Walk();
  170.     p = MAKEP(SELECTOROF(hheap), npMem - 2);
  171.     *(PUSHORT)p = 0;
  172.     *(PUSHORT)(p + cbOld - 2) = 0;
  173.     Unsquirl(p);
  174.     p = MAKEP(SELECTOROF(hheap), WinReallocMem(hheap, npMem - 2, cbOld, cbNew + 4));
  175.     if (LOUSHORT(p) == NULL) {
  176.         AssertF(FALSE, "Reallocation failure");
  177.         return(NULL);
  178.     }
  179.     Squirl(p);
  180.     *(PUSHORT)p = HPSIG;
  181.     *(PUSHORT)(p + cbNew + 2) = HPSIG;
  182.     return((NPBYTE)(SHORT)p + 2);
  183. }
  184.  
  185.  
  186.  
  187. /*
  188.  * note that cbMem perameter includes the signatures since it was derived
  189.  * from the heap itself.
  190.  */
  191. NPBYTE DbgFreeMem(hheap, npMem, cbMem)
  192. HHEAP hheap;
  193. NPBYTE npMem;
  194. USHORT cbMem;
  195. {
  196.     register PBYTE p;
  197.  
  198.     Walk();
  199.     p = MAKEP(SELECTOROF(hheap), npMem - 2);
  200.     Unsquirl(p);
  201.     cbMem += 4;
  202.     AssertF(cbMem == *(PUSHORT)(p - 2), "FreeMem:Incorrect cbMem perameter")
  203.         
  204.     if (*(PUSHORT)p != HPSIG || *(PUSHORT)(p + cbMem - 2) != HPSIG)
  205.         AssertF(FALSE, "Freed memory is corrupted");
  206.     *(PUSHORT)p = 0;
  207.     *(PUSHORT)(p + cbMem - 2) = 0;
  208.     p = (PBYTE)WinFreeMem(hheap, npMem - 2, cbMem);
  209.     if (LOUSHORT(p) != 0)
  210.         AssertF(FALSE, "Memory freeing failed");
  211.     return((NPBYTE)LOUSHORT(p));
  212. }
  213.  
  214. #endif /* DEBUG ------------------------------------------------------   */
  215.