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

  1. /***
  2. *expand.c - Win32 expand heap routine
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *******************************************************************************/
  9.  
  10. #ifdef WINHEAP
  11.  
  12. #include <cruntime.h>
  13. #include <malloc.h>
  14. #include <winheap.h>
  15. #include <windows.h>
  16. #include <mtdll.h>
  17. #include <dbgint.h>
  18.  
  19. /***
  20. *void *_expand(void *pblck, size_t newsize) - expand/contract a block of memory
  21. *       in the heap
  22. *
  23. *Purpose:
  24. *       Resizes a block in the heap to newsize bytes. newsize may be either
  25. *       greater (expansion) or less (contraction) than the original size of
  26. *       the block. The block is NOT moved.
  27. *
  28. *       NOTES:
  29. *
  30. *       (1) In this implementation, if the block cannot be grown to the
  31. *       desired size, the resulting block will NOT be grown to the max
  32. *       possible size.  (That is, either it works or it doesn't.)
  33. *
  34. *       (2) Unlike other implementations, you can NOT pass a previously
  35. *       freed block to this routine and expect it to work.
  36. *
  37. *Entry:
  38. *       void *pblck - pointer to block in the heap previously allocated
  39. *                 by a call to malloc(), realloc() or _expand().
  40. *
  41. *       size_t newsize  - requested size for the resized block
  42. *
  43. *Exit:
  44. *       Success:  Pointer to the resized memory block (i.e., pblck)
  45. *       Failure:  NULL
  46. *
  47. *Uses:
  48. *
  49. *Exceptions:
  50. *       If pblck does not point to a valid allocation block in the heap,
  51. *       _expand() will behave unpredictably and probably corrupt the heap.
  52. *
  53. *******************************************************************************/
  54.  
  55. void * __cdecl _expand_base (void * pBlock, size_t newsize)
  56. {
  57.     PHEADER     pHeader;
  58.     void *      pvReturn;
  59.  
  60.  
  61.     /* validate size */
  62.     if ( newsize > _HEAP_MAXREQ )
  63.         return NULL;
  64.  
  65.     _mlock(_HEAP_LOCK);
  66.  
  67.     //  if allocation block lies within the small-block heap,
  68.     //  try to resize it there
  69.     if ((pHeader = __sbh_find_block(pBlock)) != NULL)
  70.     {
  71.         pvReturn = NULL;
  72.         if (newsize <= __sbh_threshold &&
  73.                          __sbh_resize_block(pHeader, pBlock, newsize))
  74.             pvReturn = pBlock;
  75.  
  76.         _munlock(_HEAP_LOCK);
  77.         return pvReturn;
  78.     }
  79.  
  80.     _munlock(_HEAP_LOCK);
  81.  
  82.     //  force nonzero size and round up to next paragraph
  83.     if (newsize == 0)
  84.         newsize = 1;
  85.     newsize = (newsize + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1);
  86.  
  87.     return (HeapReAlloc(_crtheap, HEAP_REALLOC_IN_PLACE_ONLY,
  88.                                                     pBlock, newsize));
  89. }
  90.  
  91.  
  92. #endif  /* WINHEAP */
  93.