ReactOS 0.4.15-dev-8344-g0574987
memory.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Drivers
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: drivers/sac/driver/memory.c
5 * PURPOSE: Driver for the Server Administration Console (SAC) for EMS
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9/* INCLUDES *******************************************************************/
10
11#include "sacdrv.h"
12
13/* GLOBALS ********************************************************************/
14
18
19/* FUNCTIONS ******************************************************************/
20
24{
26 SAC_DBG(SAC_DBG_ENTRY_EXIT, "Entering\n");
27
28 /* Allocate a nonpaged heap for us to use */
34 {
35 /* Initialize a lock for it */
37
38 /* Initialize the head of the list */
43
44 /* Initialize the first free entry */
46 Entry->Signature = LOCAL_MEMORY_SIGNATURE;
47 Entry->Tag = FREE_POOL_TAG;
49
50 /* All done */
51 SAC_DBG(SAC_DBG_ENTRY_EXIT, "Exiting with TRUE.\n");
52 return TRUE;
53 }
54
55 /* No pool available to manage our heap */
56 SAC_DBG(SAC_DBG_ENTRY_EXIT, "Exiting with FALSE. No pool.\n");
57 return FALSE;
58}
59
60VOID
63{
66 SAC_DBG(SAC_DBG_ENTRY_EXIT, "Entering\n");
67
68 /* Acquire the memory lock while freeing the list(s) */
70 while (GlobalMemoryList)
71 {
73
74 /* While outside of the lock, save the next list and free this one */
76 Next = GlobalMemoryList->Next;
78
79 /* Reacquire the lock and see if there was another list to free */
81 GlobalMemoryList = Next;
82 }
83
84 /* All done */
86 SAC_DBG(SAC_DBG_ENTRY_EXIT, "Exiting\n");
87}
88
92 IN ULONG Tag,
95{
96 PVOID p;
97 p = ExAllocatePoolWithTag(NonPagedPool, PoolSize, 'HACK');
98 RtlZeroMemory(p, PoolSize);
99 SAC_DBG(SAC_DBG_MM, "Returning block 0x%X.\n", p);
100 return p;
101#if 0
103 PSAC_MEMORY_LIST GlobalDescriptor, NewDescriptor;
104 PSAC_MEMORY_ENTRY LocalDescriptor, NextDescriptor;
105 ULONG GlobalSize, ActualSize;
107
109 SAC_DBG(SAC_DBG_MM, "Entering.\n");
110
111 /* Acquire the memory allocation lock and align the size request */
113 PoolSize = ALIGN_UP(PoolSize, ULONGLONG);
114
115#if _USE_SAC_HEAP_ALLOCATOR_
116 GlobalDescriptor = GlobalMemoryList;
118 while (GlobalDescriptor)
119 {
121
122 LocalDescriptor = GlobalDescriptor->LocalDescriptor;
123
124 GlobalSize = GlobalDescriptor->Size;
125 while (GlobalSize)
126 {
127 ASSERT(LocalDescriptor->Signature == LOCAL_MEMORY_SIGNATURE);
128
129 if ((LocalDescriptor->Tag == FREE_POOL_TAG) &&
130 (LocalDescriptor->Size >= PoolSize))
131 {
132 break;
133 }
134
135 GlobalSize -= (LocalDescriptor->Size + sizeof(SAC_MEMORY_ENTRY));
136
137 LocalDescriptor =
138 (PSAC_MEMORY_ENTRY)((ULONG_PTR)LocalDescriptor +
139 LocalDescriptor->Size +
140 sizeof(SAC_MEMORY_ENTRY));
141 }
142
143 GlobalDescriptor = GlobalDescriptor->Next;
144 }
145
146 if (!GlobalDescriptor)
147 {
149
150 ActualSize = min(
151 PAGE_SIZE,
152 PoolSize + sizeof(SAC_MEMORY_ENTRY) + sizeof(SAC_MEMORY_LIST));
153
154 SAC_DBG(SAC_DBG_MM, "Allocating new space.\n");
155
157 ActualSize,
160 if (!NewDescriptor)
161 {
162 SAC_DBG(SAC_DBG_MM, "No more memory, returning NULL.\n");
163 return NULL;
164 }
165
167
169 NewDescriptor->LocalDescriptor = (PSAC_MEMORY_ENTRY)(NewDescriptor + 1);
170 NewDescriptor->Size = ActualSize - 16;
172
174
175 LocalDescriptor = NewDescriptor->LocalDescriptor;
176 LocalDescriptor->Signature = LOCAL_MEMORY_SIGNATURE;
177 LocalDescriptor->Tag = FREE_POOL_TAG;
178 LocalDescriptor->Size =
180 }
181
182 SAC_DBG(SAC_DBG_MM, "Found a good sized block.\n");
183 ASSERT(LocalDescriptor->Tag == FREE_POOL_TAG);
184 ASSERT(LocalDescriptor->Signature == LOCAL_MEMORY_SIGNATURE);
185
186 if (LocalDescriptor->Size > (PoolSize + sizeof(SAC_MEMORY_ENTRY)))
187 {
188 NextDescriptor =
189 (PSAC_MEMORY_ENTRY)((ULONG_PTR)LocalDescriptor +
190 PoolSize +
191 sizeof(SAC_MEMORY_ENTRY));
192 if (NextDescriptor->Tag == FREE_POOL_TAG)
193 {
194 NextDescriptor->Tag = FREE_POOL_TAG;
195 NextDescriptor->Signature = LOCAL_MEMORY_SIGNATURE;
196 NextDescriptor->Size =
197 (LocalDescriptor->Size - PoolSize - sizeof(SAC_MEMORY_ENTRY));
198
199 LocalDescriptor->Size = PoolSize;
200 }
201 }
202#else
203 /* Shut the compiler up */
204 NewDescriptor = GlobalDescriptor = NULL;
206 ActualSize = GlobalSize;
207 NextDescriptor = (PVOID)ActualSize;
208 NewDescriptor = (PVOID)NextDescriptor;
209
210 /* Use the NT pool allocator */
211 LocalDescriptor = ExAllocatePoolWithTag(NonPagedPool,
212 PoolSize + sizeof(*LocalDescriptor),
213 Tag);
214 LocalDescriptor->Size = PoolSize;
215#endif
216 /* Set the tag, and release the lock */
217 LocalDescriptor->Tag = Tag;
219
220 /* Update our performance counters */
223
224 /* Return the buffer and zero it */
225 SAC_DBG(SAC_DBG_MM, "Returning block 0x%X.\n", LocalDescriptor);
226 Buffer = LocalDescriptor + 1;
227 RtlZeroMemory(Buffer, PoolSize);
228 return Buffer;
229#endif
230}
231
232VOID
233NTAPI
235{
236#if 0
237 PSAC_MEMORY_ENTRY NextDescriptor;
238 PSAC_MEMORY_ENTRY ThisDescriptor, FoundDescriptor;
239 PSAC_MEMORY_ENTRY LocalDescriptor = (PVOID)((ULONG_PTR)(*Block) - sizeof(SAC_MEMORY_ENTRY));
241 PSAC_MEMORY_LIST GlobalDescriptor;
243 SAC_DBG(SAC_DBG_MM, "Entering with block 0x%X.\n", LocalDescriptor);
244
245 /* Make sure this was a valid entry */
246 ASSERT(LocalDescriptor->Size > 0);
247 ASSERT(LocalDescriptor->Signature == LOCAL_MEMORY_SIGNATURE);
248
249 /* Update performance counters */
251 InterlockedExchangeAdd(&TotalBytesFreed, LocalDescriptor->Size);
252
253 /* Acquire the memory allocation lock */
254 GlobalDescriptor = GlobalMemoryList;
256
257#if _USE_SAC_HEAP_ALLOCATOR_
258 while (GlobalDescriptor)
259 {
261
262 FoundDescriptor = NULL;
263
264 ThisDescriptor = GlobalDescriptor->LocalDescriptor;
265
266 GlobalSize = GlobalDescriptor->Size;
267 while (GlobalSize)
268 {
269 ASSERT(ThisDescriptor->Signature == LOCAL_MEMORY_SIGNATURE);
270
271 if (ThisDescriptor == LocalDescriptor) break;
272
273 GlobalSize -= (ThisDescriptor->Size + sizeof(SAC_MEMORY_ENTRY));
274
275 ThisDescriptor =
276 (PSAC_MEMORY_ENTRY)((ULONG_PTR)ThisDescriptor +
277 ThisDescriptor->Size +
278 sizeof(SAC_MEMORY_ENTRY));
279 }
280
281 if (ThisDescriptor == LocalDescriptor) break;
282
283 GlobalDescriptor = GlobalDescriptor->Next;
284 }
285
286 if (!GlobalDescriptor)
287 {
289 SAC_DBG(SAC_DBG_MM, "Could not find block.\n");
290 return;
291 }
292
293 ASSERT(ThisDescriptor->Signature == LOCAL_MEMORY_SIGNATURE);
294
295 if (LocalDescriptor->Tag == FREE_POOL_TAG)
296 {
298 SAC_DBG(SAC_DBG_MM, "Attempted to free something twice.\n");
299 return;
300 }
301
302 LocalSize = LocalDescriptor->Size;
303 LocalDescriptor->Tag = FREE_POOL_TAG;
304
305 if (GlobalSize > (LocalSize + sizeof(SAC_MEMORY_ENTRY)))
306 {
307 NextDescriptor =
308 (PSAC_MEMORY_ENTRY)((ULONG_PTR)LocalDescriptor +
309 LocalSize +
310 sizeof(SAC_MEMORY_ENTRY));
311 if (NextDescriptor->Tag == FREE_POOL_TAG)
312 {
313 NextDescriptor->Tag = 0;
314 NextDescriptor->Signature = 0;
315
316 LocalDescriptor->Size +=
317 (NextDescriptor->Size + sizeof(SAC_MEMORY_ENTRY));
318 }
319 }
320
321 if ((FoundDescriptor) && (FoundDescriptor->Tag == FREE_POOL_TAG))
322 {
323 LocalDescriptor->Signature = 0;
324 LocalDescriptor->Tag = 0;
325
326 FoundDescriptor->Size +=
327 (LocalDescriptor->Size + sizeof(SAC_MEMORY_ENTRY));
328 }
329#else
330 /* Shut the compiler up */
331 LocalSize = GlobalSize = 0;
332 ThisDescriptor = (PVOID)LocalSize;
333 NextDescriptor = (PVOID)GlobalSize;
334 GlobalDescriptor = (PVOID)ThisDescriptor;
335 FoundDescriptor = (PVOID)GlobalDescriptor;
336 GlobalDescriptor = (PVOID)NextDescriptor;
337 NextDescriptor = (PVOID)FoundDescriptor;
338
339 /* Use the NT pool allocator*/
340 ExFreePool(LocalDescriptor);
341#endif
342
343 /* Release the lock, delete the address, and return */
345#endif
346 SAC_DBG(SAC_DBG_MM, "exiting: 0x%p.\n", *Block);
347 ExFreePoolWithTag(*Block, 'HACK');
348 *Block = NULL;
349}
unsigned char BOOLEAN
#define InterlockedIncrement
Definition: armddk.h:53
Definition: bufpool.h:45
Definition: File.h:16
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
VOID NTAPI MyFreePool(IN PVOID *Block)
Definition: memory.c:234
VOID NTAPI FreeMemoryManagement(VOID)
Definition: memory.c:62
KSPIN_LOCK MemoryLock
Definition: memory.c:16
PSAC_MEMORY_LIST GlobalMemoryList
Definition: memory.c:17
BOOLEAN NTAPI InitializeMemoryManagement(VOID)
Definition: memory.c:23
LONG TotalFrees
Definition: memory.c:15
LONG TotalBytesAllocated
Definition: memory.c:15
PVOID NTAPI MyAllocatePool(IN SIZE_T PoolSize, IN ULONG Tag, IN PCHAR File, IN ULONG Line)
Definition: memory.c:91
LONG TotalAllocations
Definition: memory.c:15
LONG TotalBytesFreed
Definition: memory.c:15
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
UCHAR KIRQL
Definition: env_spec_w32.h:591
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define NonPagedPool
Definition: env_spec_w32.h:307
#define KeInitializeSpinLock(sl)
Definition: env_spec_w32.h:604
PVOID NTAPI ExAllocatePoolWithTagPriority(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes, IN ULONG Tag, IN EX_POOL_PRIORITY Priority)
Definition: expool.c:2963
GLfloat GLfloat p
Definition: glext.h:8902
SIZE_T NTAPI LocalSize(HLOCAL hMem)
Definition: heapmem.c:1794
SIZE_T NTAPI GlobalSize(HGLOBAL hMem)
Definition: heapmem.c:1090
#define InterlockedExchangeAdd
Definition: interlocked.h:181
#define ASSERT(a)
Definition: mode.c:44
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#define min(a, b)
Definition: monoChain.cc:55
long LONG
Definition: pedump.c:60
#define SAC_DBG(x,...)
Definition: sacdrv.h:37
#define SAC_DBG_MM
Definition: sacdrv.h:35
#define ALLOC_BLOCK_TAG
Definition: sacdrv.h:138
struct _SAC_MEMORY_LIST SAC_MEMORY_LIST
#define SAC_MEMORY_LIST_SIZE
Definition: sacdrv.h:150
#define SAC_DBG_ENTRY_EXIT
Definition: sacdrv.h:32
struct _SAC_MEMORY_ENTRY SAC_MEMORY_ENTRY
#define FREE_POOL_TAG
Definition: sacdrv.h:141
#define GLOBAL_MEMORY_SIGNATURE
Definition: sacdrv.h:145
#define LOCAL_MEMORY_SIGNATURE
Definition: sacdrv.h:144
struct _SAC_MEMORY_ENTRY * PSAC_MEMORY_ENTRY
#define INITIAL_BLOCK_TAG
Definition: sacdrv.h:139
base of all file and directory entries
Definition: entries.h:83
Definition: ncftp.h:79
Definition: sacdrv.h:192
ULONG Size
Definition: sacdrv.h:195
ULONG Tag
Definition: sacdrv.h:194
ULONG Signature
Definition: sacdrv.h:193
ULONG Signature
Definition: sacdrv.h:199
PSAC_MEMORY_ENTRY LocalDescriptor
Definition: sacdrv.h:200
struct _SAC_MEMORY_LIST * Next
Definition: sacdrv.h:202
#define NTAPI
Definition: typedefs.h:36
void * PVOID
Definition: typedefs.h:50
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
char * PCHAR
Definition: typedefs.h:51
#define ALIGN_UP(size, type)
Definition: umtypes.h:91
_Must_inspect_result_ _In_ WDFDEVICE _In_ BOOLEAN _In_opt_ PVOID Tag
Definition: wdfdevice.h:4065
@ HighPoolPriority
Definition: extypes.h:46
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778
_In_opt_ PSECURITY_DESCRIPTOR _Out_ PSECURITY_DESCRIPTOR * NewDescriptor
Definition: sefuncs.h:30