xref: /reactos/win32ss/user/ntuser/usrheap.h (revision c2c66aff)
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     return RtlAllocateHeap(GlobalUserHeap,
37                            HEAP_NO_SERIALIZE,
38                            Bytes);
39 }
40 
41 static __inline BOOL
42 UserHeapFree(PVOID lpMem)
43 {
44     return RtlFreeHeap(GlobalUserHeap,
45                        HEAP_NO_SERIALIZE,
46                        lpMem);
47 }
48 
49 static __inline PVOID
50 UserHeapReAlloc(PVOID lpMem,
51                 SIZE_T Bytes)
52 {
53 #if 0
54     /* NOTE: ntoskrnl doesn't export RtlReAllocateHeap... */
55     return RtlReAllocateHeap(GlobalUserHeap,
56                              HEAP_NO_SERIALIZE,
57                              lpMem,
58                              Bytes);
59 #else
60     SIZE_T PrevSize;
61     PVOID pNew;
62 
63     PrevSize = RtlSizeHeap(GlobalUserHeap,
64                            HEAP_NO_SERIALIZE,
65                            lpMem);
66 
67     if (PrevSize == Bytes)
68         return lpMem;
69 
70     pNew = RtlAllocateHeap(GlobalUserHeap,
71                            HEAP_NO_SERIALIZE,
72                            Bytes);
73     if (pNew != NULL)
74     {
75         if (PrevSize < Bytes)
76             Bytes = PrevSize;
77 
78         RtlCopyMemory(pNew,
79                       lpMem,
80                       Bytes);
81 
82         RtlFreeHeap(GlobalUserHeap,
83                     HEAP_NO_SERIALIZE,
84                     lpMem);
85     }
86 
87     return pNew;
88 #endif
89 }
90 
91 static __inline PVOID
92 UserHeapAddressToUser(PVOID lpMem)
93 {
94     PPROCESSINFO W32Process = PsGetCurrentProcessWin32Process();
95 
96     /* The first mapping entry is the global user heap mapping */
97     return (PVOID)(((ULONG_PTR)lpMem - (ULONG_PTR)GlobalUserHeap) +
98                    (ULONG_PTR)W32Process->HeapMappings.UserMapping);
99 }
100 
101 /* EOF */
102