8cb2a7391e477bdf9fcf58e1c5fc793a5e3597c5
2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/include/baseheap.h
5 * PURPOSE: Base Heap Structures
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
10 // Some important implementation notes.
12 // Firstly, the Global* APIs in Win32 are largely similar to the Local* APIs,
13 // but there are a number of small differences (for example, there is no such
14 // thing as DDE/Shared Local memory, and the re-allocation semantics are also
15 // simpler, because you cannot force a move).
17 // Note something VERY IMPORTANT: This implementation *depends* on the fact
18 // that all heap pointers are, at the very least, 8 byte aligned, and that heap
19 // handles are actually pointers inside our HandleEntry->Object, which happens
20 // to be at offset 0x4, which means testing with bit 3 tells us if the handle
21 // is a pointer or a real "handle". On 64-bit, heap pointers should be 16-byte
22 // aligned, and our offset should be 0x8, so the trick works anyways, but using
25 // Apart from MSDN, a wonderful source of information about how this works is
26 // available on Raymond's blog, in a 4-parter series starting at:
27 // http://blogs.msdn.com/oldnewthing/archive/2004/11/04/252258.aspx.
29 // Finally, as Raymond points out, be aware that some applications depend on
30 // the way this implementation was done, since global memory handles are a
31 // straight-forward overlay on top of the RTL Handle implementation, and rogue
32 // applications can easily do the conversion manually without calling the right
33 // API for it (such as GlobalLock).
37 // Define _BASE_HANDLE_TRACE for Traces
39 #ifdef _BASE_HANDLE_TRACE_
40 #define BH_PRINT DbgPrint
42 #define BH_PRINT DPRINT
44 #define BASE_TRACE_ALLOC(x, y) \
45 BH_PRINT("[BASE_HEAP] %s : Allocating %lx bytes with flags: %lx\n", \
47 #define BASE_TRACE_ALLOC2(x) \
48 BH_PRINT("[BASE_HEAP] %s : Allocated %p\n", \
50 #define BASE_TRACE_PTR(x, y) \
51 BH_PRINT("[BASE_HEAP] %s : Using handle: %lx for pointer: %p\n", \
53 #define BASE_TRACE_HANDLE(x, y) \
54 BH_PRINT("[BASE_HEAP] %s : Using handle: %lx for block: %p\n", \
56 #define BASE_TRACE_DEALLOC(x) \
57 BH_PRINT("[BASE_HEAP] %s : Freeing %p\n", \
59 #define BASE_TRACE_FAILURE() \
60 BH_PRINT("[BASE_HEAP] %s : Failing %d\n", \
61 __FUNCTION__, __LINE__)
64 // The handle structure for global heap handles.
65 // Notice that it nicely overlays with RTL_HANDLE_ENTRY.
66 // KEEP IT THAT WAY! ;-)
68 typedef struct _BASE_HEAP_HANDLE_ENTRY
77 } BASE_HEAP_HANDLE_ENTRY
, *PBASE_HEAP_HANDLE_ENTRY
;
81 // Note that 0x0001 is the shared/generic RTL_HANDLE_VALID
83 #define BASE_HEAP_ENTRY_FLAG_MOVABLE 0x0002
84 #define BASE_HEAP_ENTRY_FLAG_REUSABLE 0x0004
85 #define BASE_HEAP_ENTRY_FLAG_REUSE 0x0008
86 #define BASE_HEAP_ENTRY_FLAG_DDESHARE 0x0010
89 // Easy way to check if the global handle is actually an entry in our table
91 #define BASE_HEAP_IS_HANDLE_ENTRY \
92 (ULONG_PTR)FIELD_OFFSET(BASE_HEAP_HANDLE_ENTRY, Object)
95 // Tags for the entire heap allocation for this global memory.
96 // They are set part of the User Flags of the RTL Heap.
98 #define BASE_HEAP_FLAG_MOVABLE HEAP_SETTABLE_USER_FLAG1
99 #define BASE_HEAP_FLAG_DDESHARE HEAP_SETTABLE_USER_FLAG2
102 // Internal Handle Functions
104 #define BaseHeapAllocEntry() \
105 (PBASE_HEAP_HANDLE_ENTRY)RtlAllocateHandle(&BaseHeapHandleTable, NULL)
107 #define BaseHeapGetEntry(h) \
108 (PBASE_HEAP_HANDLE_ENTRY) \
109 CONTAINING_RECORD(h, \
110 BASE_HEAP_HANDLE_ENTRY, \
113 #define BaseHeapValidateEntry(he) \
114 RtlIsValidHandle(&BaseHeapHandleTable, (PRTL_HANDLE_TABLE_ENTRY)he)
116 #define BaseHeapFreeEntry(he) \
117 RtlFreeHandle(&BaseHeapHandleTable, (PRTL_HANDLE_TABLE_ENTRY)he);