xref: /reactos/win32ss/user/ntuser/usrheap.h (revision 3e1f4074)
1 #pragma once
2 
3 typedef struct _WIN32HEAP WIN32HEAP, *PWIN32HEAP;
4 
5 /*
6 typedef struct _W32HEAP_USER_MAPPING
7 {
8     struct _W32HEAP_USER_MAPPING* Next;
9     PVOID KernelMapping;
10     PVOID UserMapping;
11     ULONG_PTR Limit;
12     ULONG Count;
13 } W32HEAP_USER_MAPPING, *PW32HEAP_USER_MAPPING;
14 */
15 
16 /* User heap */
17 extern HANDLE GlobalUserHeap;
18 extern PVOID GlobalUserHeapSection;
19 
20 PWIN32HEAP
21 UserCreateHeap(OUT PVOID *SectionObject,
22                IN OUT PVOID *SystemBase,
23                IN SIZE_T HeapSize);
24 
25 NTSTATUS
26 UnmapGlobalUserHeap(IN PEPROCESS Process);
27 
28 NTSTATUS
29 MapGlobalUserHeap(IN  PEPROCESS Process,
30                   OUT PVOID* KernelMapping,
31                   OUT PVOID* UserMapping);
32 
33 static __inline PVOID
34 UserHeapAlloc(SIZE_T Bytes)
35 {
36     /* User heap has no lock, using global user lock instead. */
37     ASSERT(UserIsEnteredExclusive());
38     return RtlAllocateHeap(GlobalUserHeap,
39                            HEAP_NO_SERIALIZE,
40                            Bytes);
41 }
42 
43 static __inline BOOL
44 UserHeapFree(PVOID lpMem)
45 {
46     /* User heap has no lock, using global user lock instead. */
47     ASSERT(UserIsEnteredExclusive());
48     return RtlFreeHeap(GlobalUserHeap,
49                        HEAP_NO_SERIALIZE,
50                        lpMem);
51 }
52 
53 static __inline PVOID
54 UserHeapReAlloc(PVOID lpMem,
55                 SIZE_T Bytes)
56 {
57 #if 0
58     /* NOTE: ntoskrnl doesn't export RtlReAllocateHeap... */
59     return RtlReAllocateHeap(GlobalUserHeap,
60                              HEAP_NO_SERIALIZE,
61                              lpMem,
62                              Bytes);
63 #else
64     SIZE_T PrevSize;
65     PVOID pNew;
66 
67     /* User heap has no lock, using global user lock instead. */
68     ASSERT(UserIsEnteredExclusive());
69 
70     PrevSize = RtlSizeHeap(GlobalUserHeap,
71                            HEAP_NO_SERIALIZE,
72                            lpMem);
73 
74     if (PrevSize == Bytes)
75         return lpMem;
76 
77     pNew = RtlAllocateHeap(GlobalUserHeap,
78                            HEAP_NO_SERIALIZE,
79                            Bytes);
80     if (pNew != NULL)
81     {
82         if (PrevSize < Bytes)
83             Bytes = PrevSize;
84 
85         RtlCopyMemory(pNew,
86                       lpMem,
87                       Bytes);
88 
89         RtlFreeHeap(GlobalUserHeap,
90                     HEAP_NO_SERIALIZE,
91                     lpMem);
92     }
93 
94     return pNew;
95 #endif
96 }
97 
98 static __inline PVOID
99 UserHeapAddressToUser(PVOID lpMem)
100 {
101     PPROCESSINFO W32Process = PsGetCurrentProcessWin32Process();
102 
103     /* The first mapping entry is the global user heap mapping */
104     return (PVOID)(((ULONG_PTR)lpMem - (ULONG_PTR)GlobalUserHeap) +
105                    (ULONG_PTR)W32Process->HeapMappings.UserMapping);
106 }
107 
108 /* EOF */
109