Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.6 KiB

  1. /***
  2. *findaddr.c - Find a heap entry
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines the _heap_findaddr routine
  8. *
  9. *Revision History:
  10. * 07-06-89 JCR Module created.
  11. * 07-18-89 JCR Return -1 if nothing in heap
  12. * 11-13-89 GJF Fixed copyright
  13. * 12-04-89 GJF Renamed header file (now heap.h). Also, some tuning.
  14. * 12-18-89 GJF Added explicit _cdecl to function definition
  15. * 03-09-90 GJF Replaced _cdecl with _CALLTYPE1, added #include
  16. * <cruntime.h> and removed #include <register.h>.
  17. * 09-27-90 GJF New-style function declarator.
  18. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  19. * 05-01-95 GJF Made conditional on WINHEAP.
  20. *
  21. *******************************************************************************/
  22. #ifndef WINHEAP
  23. #include <cruntime.h>
  24. #include <heap.h>
  25. #include <stddef.h>
  26. #define TRUE 1
  27. /***
  28. *int _heap_findaddr() - Find a heap entry
  29. *
  30. *Purpose:
  31. * Given an address, find the corresponding heap entry.
  32. *
  33. *Entry:
  34. * void * address = address to look for
  35. * PBLKDESC * ppdesc = pointer to pointer to heap descriptor to be
  36. * filled in by this routine.
  37. *
  38. *Exit:
  39. *
  40. * _HEAPFIND_EXACT = 0 = exact fit, pdesc holds heap descriptor address
  41. * _HEAPFIND_WITHIN = 1 = not exact fit, pdesc holds previous heap
  42. * descriptor address
  43. *
  44. * _HEAPFIND_BEFORE = -1 = address is before the heap (pdesc NOT filled in)
  45. * _HEAPFIND_AFTER = -2 = address is after the heap (pdesc NOT filled in)
  46. * _HEAPFIND_EMPTY = -3 = no memory in heap (pdesc NOT filled in)
  47. *
  48. * [If return is negative, supplied pdesc is NOT filled in.]
  49. *
  50. *Uses:
  51. *
  52. *Exceptions:
  53. *
  54. *******************************************************************************/
  55. int __cdecl _heap_findaddr (
  56. void * address,
  57. _PBLKDESC * ppdesc
  58. )
  59. {
  60. REG1 _PBLKDESC pcurr;
  61. /*
  62. * See if heap there's anything in the heap
  63. */
  64. if (_heap_desc.pfirstdesc == &_heap_desc.sentinel)
  65. return(_HEAPFIND_EMPTY);
  66. /*
  67. * See if entry is in the heap or not
  68. */
  69. if (_ADDRESS(_heap_desc.pfirstdesc) > address)
  70. return(_HEAPFIND_BEFORE);
  71. if (_ADDRESS(&_heap_desc.sentinel) <= address)
  72. return(_HEAPFIND_AFTER);
  73. /*
  74. * Find the entry
  75. */
  76. #ifdef DEBUG
  77. for (pcurr = _heap_desc.pfirstdesc; pcurr != &_heap_desc.sentinel;
  78. pcurr = pcurr->pnextdesc) {
  79. #else
  80. for (pcurr = _heap_desc.pfirstdesc; TRUE; pcurr = pcurr->pnextdesc) {
  81. #endif
  82. if ( _ADDRESS(pcurr->pnextdesc) > address ) {
  83. /* Address is contained in this entry */
  84. *ppdesc = pcurr;
  85. /* Check for an exact fit */
  86. if ( _ADDRESS(pcurr) == address)
  87. return(_HEAPFIND_EXACT);
  88. else
  89. return(_HEAPFIND_WITHIN);
  90. }
  91. }
  92. #ifdef DEBUG
  93. /* Should never reach here! */
  94. _heap_abort();
  95. #endif
  96. }
  97. #endif /* WINHEAP */