summaryrefslogtreecommitdiffstats
path: root/private/crt32/heap/findaddr.c
blob: d7e9c699ca56dcb9205b66d5b0bd2b54a1f7ce4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/***
*findaddr.c - Find a heap entry
*
*	Copyright (c) 1989-1991, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	Defines the _heap_findaddr routine
*
*Revision History:
*	07-06-89   JCR	Module created.
*	07-18-89   JCR	Return -1 if nothing in heap
*	11-13-89   GJF	Fixed copyright
*	12-04-89   GJF	Renamed header file (now heap.h). Also, some tuning.
*	12-18-89   GJF	Added explicit _cdecl to function definition
*	03-09-90   GJF	Replaced _cdecl with _CALLTYPE1, added #include
*			<cruntime.h> and removed #include <register.h>.
*	09-27-90   GJF	New-style function declarator.
*
*******************************************************************************/

#include <cruntime.h>
#include <heap.h>
#include <stddef.h>

#define TRUE	1

/***
*int _heap_findaddr() - Find a heap entry
*
*Purpose:
*	Given an address, find the corresponding heap entry.
*
*Entry:
*	void * address = address to look for
*	PBLKDESC * ppdesc = pointer to pointer to heap descriptor to be
*		filled in by this routine.
*
*Exit:
*
*	_HEAPFIND_EXACT  =  0 = exact fit, pdesc holds heap descriptor address
*	_HEAPFIND_WITHIN =  1 = not exact fit, pdesc holds previous heap
*				descriptor address
*
*	_HEAPFIND_BEFORE = -1 = address is before the heap (pdesc NOT filled in)
*	_HEAPFIND_AFTER  = -2 = address is after the heap (pdesc NOT filled in)
*	_HEAPFIND_EMPTY  = -3 = no memory in heap (pdesc NOT filled in)
*
*	[If return is negative, supplied pdesc is NOT filled in.]
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/

int  _CALLTYPE1 _heap_findaddr (
	void * address,
	_PBLKDESC * ppdesc
	)
{
	REG1 _PBLKDESC pcurr;

	/*
	 * See if heap there's anything in the heap
	 */

	if (_heap_desc.pfirstdesc == &_heap_desc.sentinel)
		return(_HEAPFIND_EMPTY);

	/*
	 * See if entry is in the heap or not
	 */

	if (_ADDRESS(_heap_desc.pfirstdesc) > address)
		return(_HEAPFIND_BEFORE);

	if (_ADDRESS(&_heap_desc.sentinel) <= address)
		return(_HEAPFIND_AFTER);

	/*
	 * Find the entry
	 */

#ifdef	DEBUG
	for (pcurr = _heap_desc.pfirstdesc; pcurr != &_heap_desc.sentinel;
	     pcurr = pcurr->pnextdesc) {
#else
	for (pcurr = _heap_desc.pfirstdesc; TRUE; pcurr = pcurr->pnextdesc) {
#endif

		if ( _ADDRESS(pcurr->pnextdesc) > address ) {

			/* Address is contained in this entry */
			*ppdesc = pcurr;

			/* Check for an exact fit */
			if ( _ADDRESS(pcurr) == address)
				return(_HEAPFIND_EXACT);
			else
				return(_HEAPFIND_WITHIN);
		}
	}

#ifdef	DEBUG
	/* Should never reach here! */
	_heap_abort();
#endif
}