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

  1. /***
  2. *findaddr.c - Find a heap entry
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines the _heap_findaddr routine
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef WINHEAP
  12.  
  13. #include <cruntime.h>
  14. #include <heap.h>
  15. #include <stddef.h>
  16.  
  17. #define TRUE    1
  18.  
  19. /***
  20. *int _heap_findaddr() - Find a heap entry
  21. *
  22. *Purpose:
  23. *       Given an address, find the corresponding heap entry.
  24. *
  25. *Entry:
  26. *       void * address = address to look for
  27. *       PBLKDESC * ppdesc = pointer to pointer to heap descriptor to be
  28. *               filled in by this routine.
  29. *
  30. *Exit:
  31. *
  32. *       _HEAPFIND_EXACT  =  0 = exact fit, pdesc holds heap descriptor address
  33. *       _HEAPFIND_WITHIN =  1 = not exact fit, pdesc holds previous heap
  34. *                               descriptor address
  35. *
  36. *       _HEAPFIND_BEFORE = -1 = address is before the heap (pdesc NOT filled in)
  37. *       _HEAPFIND_AFTER  = -2 = address is after the heap (pdesc NOT filled in)
  38. *       _HEAPFIND_EMPTY  = -3 = no memory in heap (pdesc NOT filled in)
  39. *
  40. *       [If return is negative, supplied pdesc is NOT filled in.]
  41. *
  42. *Uses:
  43. *
  44. *Exceptions:
  45. *
  46. *******************************************************************************/
  47.  
  48. int  __cdecl _heap_findaddr (
  49.         void * address,
  50.         _PBLKDESC * ppdesc
  51.         )
  52. {
  53.         REG1 _PBLKDESC pcurr;
  54.  
  55.         /*
  56.          * See if heap there's anything in the heap
  57.          */
  58.  
  59.         if (_heap_desc.pfirstdesc == &_heap_desc.sentinel)
  60.                 return(_HEAPFIND_EMPTY);
  61.  
  62.         /*
  63.          * See if entry is in the heap or not
  64.          */
  65.  
  66.         if (_ADDRESS(_heap_desc.pfirstdesc) > address)
  67.                 return(_HEAPFIND_BEFORE);
  68.  
  69.         if (_ADDRESS(&_heap_desc.sentinel) <= address)
  70.                 return(_HEAPFIND_AFTER);
  71.  
  72.         /*
  73.          * Find the entry
  74.          */
  75.  
  76.         for (pcurr = _heap_desc.pfirstdesc; TRUE; pcurr = pcurr->pnextdesc) {
  77.  
  78.                 if ( _ADDRESS(pcurr->pnextdesc) > address ) {
  79.  
  80.                         /* Address is contained in this entry */
  81.                         *ppdesc = pcurr;
  82.  
  83.                         /* Check for an exact fit */
  84.                         if ( _ADDRESS(pcurr) == address)
  85.                                 return(_HEAPFIND_EXACT);
  86.                         else
  87.                                 return(_HEAPFIND_WITHIN);
  88.                 }
  89.         }
  90.  
  91. }
  92.  
  93. #endif  /* WINHEAP */
  94.