ReactOS  0.4.15-dev-439-g292f67a
heapuser.c
Go to the documentation of this file.
1 /*
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
7  */
8 
9 /* INCLUDES *****************************************************************/
10 
11 #include <rtl.h>
12 #include <heap.h>
13 
14 #define NDEBUG
15 #include <debug.h>
16 
18 
19 
20 /* Usermode only! */
21 VOID
22 NTAPI
24 {
25  PPEB Peb;
26 
27  /* Get PEB */
29 
30  /* Acquire the lock */
32 
33  //_SEH2_TRY {
34  /* Check if max number of heaps reached */
36  {
37  // TODO: Handle this case
38  ASSERT(FALSE);
39  }
40 
41  /* Add the heap to the process heaps */
43  Peb->NumberOfHeaps++;
45  // } _SEH2_FINALLY {
46 
47  /* Release the lock */
49 
50  // } _SEH2_END
51 }
52 
53 /* Usermode only! */
54 VOID
55 NTAPI
57 {
58  PPEB Peb;
59  PHEAP *Current, *Next;
60  ULONG Count;
61 
62  /* Get PEB */
64 
65  /* Acquire the lock */
67 
68  /* Check if we don't need anything to do */
69  if ((Heap->ProcessHeapsListIndex == 0) ||
71  (Peb->NumberOfHeaps == 0))
72  {
73  /* Release the lock */
75 
76  return;
77  }
78 
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 */
82 
83  Current = (PHEAP *)&Peb->ProcessHeaps[Heap->ProcessHeapsListIndex - 1];
84  Next = Current + 1;
85 
86  /* How many items we need to shift to the left */
88 
89  /* Move them all in a loop */
90  while (--Count)
91  {
92  /* Copy it and advance next pointer */
93  *Current = *Next;
94 
95  /* Update its index */
96  (*Current)->ProcessHeapsListIndex -= 1;
97 
98  /* Advance pointers */
99  Current++;
100  Next++;
101  }
102 
103  /* Decrease total number of heaps */
104  Peb->NumberOfHeaps--;
105 
106  /* Zero last unused item */
108  Heap->ProcessHeapsListIndex = 0;
109 
110  /* Release the lock */
112 }
113 
114 VOID
115 NTAPI
117 {
118  PPEB Peb;
119 
120  /* Get PEB */
121  Peb = RtlGetCurrentPeb();
122 
123  /* Initialize heap-related fields of PEB */
124  Peb->NumberOfHeaps = 0;
125 
126  /* Initialize the process heaps list protecting lock */
128 }
129 
PPEB Peb
Definition: dllmain.c:27
RTL_CRITICAL_SECTION RtlpProcessHeapsListLock
Definition: heapuser.c:17
VOID NTAPI RtlpRemoveHeapFromProcessList(PHEAP Heap)
Definition: heapuser.c:56
NTSYSAPI NTSTATUS NTAPI RtlEnterCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
NTSYSAPI NTSTATUS NTAPI RtlLeaveCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
smooth NULL
Definition: ftsmooth.c:416
ULONG NumberOfHeaps
Definition: ntddk_ex.h:286
PVOID * ProcessHeaps
Definition: ntddk_ex.h:288
ULONG MaximumNumberOfHeaps
Definition: ntddk_ex.h:287
NTSYSAPI NTSTATUS NTAPI RtlInitializeCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
PPEB NTAPI RtlGetCurrentPeb(VOID)
Definition: libsupp.c:63
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
VOID NTAPI RtlpAddHeapToProcessList(PHEAP Heap)
Definition: heapuser.c:23
unsigned short USHORT
Definition: pedump.c:61
USHORT ProcessHeapsListIndex
Definition: heap.h:242
unsigned int ULONG
Definition: retypes.h:1
VOID NTAPI RtlInitializeHeapManager(VOID)
Definition: heapuser.c:116
Definition: heap.c:51