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

  1. /***
  2. *msize.c - calculate the size of a memory block in the heap
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines the following function:
  8. *           _msize()    - calculate the size of a block in the heap
  9. *
  10. *******************************************************************************/
  11.  
  12.  
  13. #ifdef WINHEAP
  14.  
  15.  
  16. #include <cruntime.h>
  17. #include <malloc.h>
  18. #include <mtdll.h>
  19. #include <winheap.h>
  20. #include <windows.h>
  21. #include <dbgint.h>
  22.  
  23. /***
  24. *size_t _msize(pblock) - calculate the size of specified block in the heap
  25. *
  26. *Purpose:
  27. *       Calculates the size of memory block (in the heap) pointed to by
  28. *       pblock.
  29. *
  30. *Entry:
  31. *       void *pblock - pointer to a memory block in the heap
  32. *
  33. *Return:
  34. *       size of the block
  35. *
  36. *******************************************************************************/
  37.  
  38. size_t __cdecl _msize_base (void * pblock)
  39. {
  40.     size_t      retval;
  41.  
  42.  
  43.     _mlock(_HEAP_LOCK);
  44.  
  45.     if (__sbh_find_block(pblock) != NULL)
  46.     {
  47.         retval = (size_t)
  48.                  (((PENTRY)((char *)pblock - sizeof(int)))->sizeFront - 0x9);
  49.         _munlock(_HEAP_LOCK);
  50.     }
  51.     else
  52.     {
  53.         _munlock(_HEAP_LOCK);
  54.         retval = (size_t)HeapSize(_crtheap, 0, pblock);
  55.     }
  56.  
  57.     return retval;
  58. }
  59.  
  60. #else  /* WINHEAP */
  61.  
  62.  
  63. #include <cruntime.h>
  64. #include <heap.h>
  65. #include <malloc.h>
  66. #include <mtdll.h>
  67. #include <stdlib.h>
  68. #include <dbgint.h>
  69.  
  70. /***
  71. *size_t _msize(pblock) - calculate the size of specified block in the heap
  72. *
  73. *Purpose:
  74. *       Calculates the size of memory block (in the heap) pointed to by
  75. *       pblock.
  76. *
  77. *Entry:
  78. *       void *pblock - pointer to a memory block in the heap
  79. *
  80. *Return:
  81. *       size of the block
  82. *
  83. *******************************************************************************/
  84.  
  85. #ifdef _MT
  86.  
  87. size_t __cdecl _msize_base (
  88.         void *pblock
  89.         )
  90. {
  91.         size_t  retval;
  92.  
  93.         /* lock the heap
  94.          */
  95.         _mlock(_HEAP_LOCK);
  96.  
  97.         retval = _msize_lk(pblock);
  98.  
  99.         /* release the heap lock
  100.          */
  101.         _munlock(_HEAP_LOCK);
  102.  
  103.         return retval;
  104. }
  105.  
  106. size_t __cdecl _msize_lk (
  107.  
  108. #else  /* _MT */
  109.  
  110. size_t __cdecl _msize_base (
  111.  
  112. #endif  /* _MT */
  113.  
  114.         void *pblock
  115.         )
  116. {
  117.  
  118.  
  119.         return( (size_t) ((char *)_ADDRESS(_BACKPTR(pblock)->pnextdesc) -
  120.         (char *)pblock) );
  121. }
  122.  
  123.  
  124. #endif  /* WINHEAP */
  125.