2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/rtl/heapuser.c
5 * PURPOSE: RTL Heap backend allocator (user mode only functions)
6 * PROGRAMMERS: Copyright 2010 Aleksey Bragin
9 /* INCLUDES *****************************************************************/
17 RTL_CRITICAL_SECTION RtlpProcessHeapsListLock
;
23 RtlpAddHeapToProcessList(PHEAP Heap
)
28 Peb
= RtlGetCurrentPeb();
30 /* Acquire the lock */
31 RtlEnterCriticalSection(&RtlpProcessHeapsListLock
);
34 /* Check if max number of heaps reached */
35 if (Peb
->NumberOfHeaps
== Peb
->MaximumNumberOfHeaps
)
37 // TODO: Handle this case
41 /* Add the heap to the process heaps */
42 Peb
->ProcessHeaps
[Peb
->NumberOfHeaps
] = Heap
;
44 Heap
->ProcessHeapsListIndex
= (USHORT
)Peb
->NumberOfHeaps
;
47 /* Release the lock */
48 RtlLeaveCriticalSection(&RtlpProcessHeapsListLock
);
56 RtlpRemoveHeapFromProcessList(PHEAP Heap
)
59 PHEAP
*Current
, *Next
;
63 Peb
= RtlGetCurrentPeb();
65 /* Acquire the lock */
66 RtlEnterCriticalSection(&RtlpProcessHeapsListLock
);
68 /* Check if we don't need anything to do */
69 if ((Heap
->ProcessHeapsListIndex
== 0) ||
70 (Heap
->ProcessHeapsListIndex
> Peb
->NumberOfHeaps
) ||
71 (Peb
->NumberOfHeaps
== 0))
73 /* Release the lock */
74 RtlLeaveCriticalSection(&RtlpProcessHeapsListLock
);
79 /* The process actually has more than one heap.
80 Use classic, lernt from university times algorithm for removing an entry
81 from a static array */
83 Current
= (PHEAP
*)&Peb
->ProcessHeaps
[Heap
->ProcessHeapsListIndex
- 1];
86 /* How many items we need to shift to the left */
87 Count
= Peb
->NumberOfHeaps
- (Heap
->ProcessHeapsListIndex
- 1);
89 /* Move them all in a loop */
92 /* Copy it and advance next pointer */
95 /* Update its index */
96 (*Current
)->ProcessHeapsListIndex
-= 1;
98 /* Advance pointers */
103 /* Decrease total number of heaps */
104 Peb
->NumberOfHeaps
--;
106 /* Zero last unused item */
107 Peb
->ProcessHeaps
[Peb
->NumberOfHeaps
] = NULL
;
108 Heap
->ProcessHeapsListIndex
= 0;
110 /* Release the lock */
111 RtlLeaveCriticalSection(&RtlpProcessHeapsListLock
);
116 RtlInitializeHeapManager(VOID
)
121 Peb
= RtlGetCurrentPeb();
123 /* Initialize heap-related fields of PEB */
124 Peb
->NumberOfHeaps
= 0;
126 /* Initialize the process heaps list protecting lock */
127 RtlInitializeCriticalSection(&RtlpProcessHeapsListLock
);