home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / free.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  4KB  |  183 lines

  1. /***
  2. *free.c - free an entry in the heap
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines the following functions:
  8. *           free()     - free a memory block in the heap
  9. *
  10. *******************************************************************************/
  11.  
  12. #ifdef WINHEAP
  13.  
  14. #include <cruntime.h>
  15. #include <malloc.h>
  16. #include <winheap.h>
  17. #include <windows.h>
  18. #include <internal.h>
  19. #include <mtdll.h>
  20. #include <dbgint.h>
  21.  
  22. /***
  23. *void free(pblock) - free a block in the heap
  24. *
  25. *Purpose:
  26. *       Free a memory block in the heap.
  27. *
  28. *       Special ANSI Requirements:
  29. *
  30. *       (1) free(NULL) is benign.
  31. *
  32. *Entry:
  33. *       void *pblock - pointer to a memory block in the heap
  34. *
  35. *Return:
  36. *       <void>
  37. *
  38. *******************************************************************************/
  39.  
  40. void __cdecl _free_base (void * pBlock)
  41. {
  42.         PHEADER     pHeader;
  43.  
  44.  
  45.         if (pBlock == NULL)
  46.             return;
  47.  
  48.         _mlock(_HEAP_LOCK);
  49.  
  50.         if ((pHeader = __sbh_find_block(pBlock)) != NULL)
  51.         {
  52.             __sbh_free_block(pHeader, pBlock);
  53.             _munlock(_HEAP_LOCK);
  54.         }
  55.         else
  56.         {
  57.             _munlock(_HEAP_LOCK);
  58.             HeapFree(_crtheap, 0, pBlock);
  59.         }
  60. }
  61.  
  62.  
  63. #else  /* WINHEAP */
  64.  
  65.  
  66. #include <cruntime.h>
  67. #include <heap.h>
  68. #include <malloc.h>
  69. #include <mtdll.h>
  70. #include <stdlib.h>
  71. #include <dbgint.h>
  72.  
  73. /***
  74. *void free(pblock) - free a block in the heap
  75. *
  76. *Purpose:
  77. *       Free a memory block in the heap.
  78. *
  79. *       Special Notes For Multi-thread: The non-multi-thread version is renamed
  80. *       to _free_lk(). The multi-thread free() simply locks the heap, calls
  81. *       _free_lk(), then unlocks the heap and returns.
  82. *
  83. *Entry:
  84. *       void *pblock - pointer to a memory block in the heap
  85. *
  86. *Return:
  87. *       <void>
  88. *
  89. *******************************************************************************/
  90.  
  91. #ifdef _MT
  92.  
  93. void __cdecl _free_base (
  94.         void *pblock
  95.         )
  96. {
  97.        /* lock the heap
  98.         */
  99.         _mlock(_HEAP_LOCK);
  100.  
  101.         /* free the block
  102.          */
  103.         _free_base_lk(pblock);
  104.  
  105.         /* unlock the heap
  106.          */
  107.         _munlock(_HEAP_LOCK);
  108. }
  109.  
  110.  
  111. /***
  112. *void _free_lk(pblock) - non-locking form of free
  113. *
  114. *Purpose:
  115. *       Same as free() except that no locking is performed
  116. *
  117. *Entry:
  118. *       See free
  119. *
  120. *Return:
  121. *
  122. *******************************************************************************/
  123.  
  124. void __cdecl _free_base_lk (
  125.  
  126. #else  /* _MT */
  127.  
  128. void __cdecl _free_base (
  129.  
  130. #endif  /* _MT */
  131.  
  132.         REG1 void *pblock
  133.         )
  134. {
  135.         REG2 _PBLKDESC pdesc;
  136.  
  137.  
  138.         /*
  139.          * If the pointer is NULL, just return [ANSI].
  140.          */
  141.  
  142.         if (pblock == NULL)
  143.             return;
  144.  
  145.         /*
  146.          * Point to block header and get the pointer back to the heap desc.
  147.          */
  148.  
  149.         pblock = (char *)pblock - _HDRSIZE;
  150.         pdesc = *(_PBLKDESC*)pblock;
  151.  
  152.         /*
  153.          * Validate the back pointer.
  154.          */
  155.  
  156.         if (_ADDRESS(pdesc) != pblock)
  157.             _heap_abort();
  158.  
  159.         /*
  160.          * Pointer is ok.  Mark block free.
  161.          */
  162.  
  163.         _SET_FREE(pdesc);
  164.  
  165.         /*
  166.          * Check for special conditions under which the rover is reset.
  167.          */
  168.         if ( (_heap_resetsize != 0xffffffff) &&
  169.              (_heap_desc.proverdesc->pblock > pdesc->pblock) &&
  170.              (_BLKSIZE(pdesc) >= _heap_resetsize) )
  171.         {
  172.             _heap_desc.proverdesc = pdesc;
  173.         }
  174. #if !defined (_M_MPPC) && !defined (_M_M68K)
  175.         else if ( _heap_desc.proverdesc == pdesc->pnextdesc )
  176.         {
  177.             _heap_desc.proverdesc = pdesc;
  178.         }
  179. #endif  /* !defined (_M_MPPC) && !defined (_M_M68K) */
  180. }
  181.  
  182. #endif  /* WINHEAP */
  183.