ReactOS  0.4.14-dev-583-g2a1ba2c
mm.h File Reference
#include <pshpack1.h>
#include <poppack.h>
Include dependency graph for mm.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _FREELDR_MEMORY_DESCRIPTOR
 
struct  PAGE_LOOKUP_TABLE_ITEM
 

Macros

#define FREELDR_SECTION_COUNT   3
 
#define HEAP_PAGES   0x400
 
#define STACK_PAGES   0x00
 
#define DUMP_MEM_MAP_ON_VERIFY   0
 
#define DEFAULT_HEAP_SIZE   (1024 * 1024)
 
#define TEMP_HEAP_SIZE   (32 * 1024 * 1024)
 

Typedefs

typedef struct _FREELDR_MEMORY_DESCRIPTOR FREELDR_MEMORY_DESCRIPTOR
 
typedef struct _FREELDR_MEMORY_DESCRIPTORPFREELDR_MEMORY_DESCRIPTOR
 
typedef struct PAGE_LOOKUP_TABLE_ITEMPPAGE_LOOKUP_TABLE_ITEM
 

Functions

PFN_NUMBER MmGetPageNumberFromAddress (PVOID Address)
 
PFN_NUMBER MmGetAddressablePageCountIncludingHoles (VOID)
 
PVOID MmFindLocationForPageLookupTable (PFN_NUMBER TotalPageCount)
 
VOID MmInitPageLookupTable (PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
 
VOID MmMarkPagesInLookupTable (PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
 
VOID MmAllocatePagesInLookupTable (PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
 
PFN_NUMBER MmCountFreePagesInLookupTable (PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
 
PFN_NUMBER MmFindAvailablePages (PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
 
PFN_NUMBER MmFindAvailablePagesBeforePage (PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, PFN_NUMBER LastPage)
 
VOID MmUpdateLastFreePageHint (PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
 
BOOLEAN MmAreMemoryPagesAvailable (PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount)
 
VOID MmSetMemoryType (PVOID MemoryAddress, SIZE_T MemorySize, TYPE_OF_MEMORY NewType)
 
PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap (PFN_NUMBER *NoEntries)
 
BOOLEAN MmInitializeMemoryManager (VOID)
 
VOID MmInitializeHeap (PVOID PageLookupTable)
 
PVOID MmAllocateMemory (SIZE_T MemorySize)
 
PVOID MmAllocateMemoryWithType (SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
 
VOID MmFreeMemory (PVOID MemoryPointer)
 
PVOID MmAllocateMemoryAtAddress (SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
 
PVOID MmAllocateHighestMemoryBelowAddress (SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
 
PVOID FrLdrHeapCreate (SIZE_T MaximumSize, TYPE_OF_MEMORY MemoryType)
 
VOID FrLdrHeapDestroy (PVOID HeapHandle)
 
VOID FrLdrHeapRelease (PVOID HeapHandle)
 
VOID FrLdrHeapVerify (PVOID HeapHandle)
 
VOID FrLdrHeapCleanupAll (VOID)
 
PVOID FrLdrHeapAllocateEx (PVOID HeapHandle, SIZE_T ByteSize, ULONG Tag)
 
VOID FrLdrHeapFreeEx (PVOID HeapHandle, PVOID Pointer, ULONG Tag)
 
FORCEINLINE PVOID FrLdrHeapAlloc (SIZE_T MemorySize, ULONG Tag)
 
FORCEINLINE VOID FrLdrHeapFree (PVOID MemoryPointer, ULONG Tag)
 
FORCEINLINE PVOID FrLdrTempAlloc (_In_ SIZE_T Size, _In_ ULONG Tag)
 
FORCEINLINE VOID FrLdrTempFree (PVOID Allocation, ULONG Tag)
 

Variables

char __ImageBase
 
PVOID PageLookupTableAddress
 
PFN_NUMBER TotalPagesInLookupTable
 
PFN_NUMBER FreePagesInLookupTable
 
PFN_NUMBER LastFreePageHint
 
PVOID FrLdrDefaultHeap
 
PVOID FrLdrTempHeap
 
SIZE_T FrLdrImageSize
 

Macro Definition Documentation

◆ DEFAULT_HEAP_SIZE

#define DEFAULT_HEAP_SIZE   (1024 * 1024)

Definition at line 122 of file mm.h.

◆ DUMP_MEM_MAP_ON_VERIFY

#define DUMP_MEM_MAP_ON_VERIFY   0

Definition at line 85 of file mm.h.

◆ FREELDR_SECTION_COUNT

#define FREELDR_SECTION_COUNT   3

Definition at line 32 of file mm.h.

◆ HEAP_PAGES

#define HEAP_PAGES   0x400

Definition at line 69 of file mm.h.

◆ STACK_PAGES

#define STACK_PAGES   0x00

Definition at line 70 of file mm.h.

◆ TEMP_HEAP_SIZE

#define TEMP_HEAP_SIZE   (32 * 1024 * 1024)

Definition at line 123 of file mm.h.

Typedef Documentation

◆ FREELDR_MEMORY_DESCRIPTOR

◆ PFREELDR_MEMORY_DESCRIPTOR

◆ PPAGE_LOOKUP_TABLE_ITEM

Function Documentation

◆ FrLdrHeapAlloc()

◆ FrLdrHeapAllocateEx()

PVOID FrLdrHeapAllocateEx ( PVOID  HeapHandle,
SIZE_T  ByteSize,
ULONG  Tag 
)

Definition at line 320 of file heap.c.

324 {
325  PHEAP Heap = HeapHandle;
326  PHEAP_BLOCK Block, NextBlock;
327  USHORT BlockSize, Remaining;
328 #if DBG && !defined(_M_ARM)
329  ULONGLONG Time = __rdtsc();
330 #endif
331 
332 #ifdef FREELDR_HEAP_VERIFIER
333  /* Verify the heap */
334  FrLdrHeapVerify(HeapHandle);
335 
336  /* Add space for a size field and 2 redzones */
338 #endif
339 
340  /* Check if the allocation is too large */
341  if ((ByteSize + sizeof(HEAP_BLOCK)) > MAXUSHORT * sizeof(HEAP_BLOCK))
342  {
343  ERR("HEAP: Allocation of 0x%lx bytes too large\n", ByteSize);
344  return NULL;
345  }
346 
347  /* We need a proper tag */
348  if (Tag == 0) Tag = 'enoN';
349 
350  /* Calculate alloc size */
351  BlockSize = (USHORT)((ByteSize + sizeof(HEAP_BLOCK) - 1) / sizeof(HEAP_BLOCK));
352 
353  /* Walk the free block list */
354  Block = &Heap->Blocks + Heap->TerminatingBlock;
355  for (Block = &Heap->Blocks + Block->Data[0].Flink;
356  Block->Size != 0;
357  Block = &Heap->Blocks + Block->Data[0].Flink)
358  {
359  ASSERT(Block->Tag == 0);
360 
361  /* Continue, if its too small */
362  if (Block->Size < BlockSize) continue;
363 
364  /* This block is just fine, use it */
365  Block->Tag = Tag;
366 
367  /* Remove this entry from the free list */
368  FrLdrHeapRemoveFreeList(Heap, Block);
369 
370  /* Calculate the remaining size */
371  Remaining = Block->Size - BlockSize;
372 
373  /* Check if the remaining space is large enough for a new block */
374  if (Remaining > 1)
375  {
376  /* Make the allocated block as large as necessary */
377  Block->Size = BlockSize;
378 
379  /* Get pointer to the new block */
380  NextBlock = Block + 1 + BlockSize;
381 
382  /* Make it a free block */
383  NextBlock->Tag = 0;
384  NextBlock->Size = Remaining - 1;
385  NextBlock->PreviousSize = BlockSize;
386  BlockSize = NextBlock->Size;
387  FrLdrHeapInsertFreeList(Heap, NextBlock);
388 
389  /* Advance to the next block */
390  NextBlock = NextBlock + 1 + BlockSize;
391  }
392  else
393  {
394  /* Not enough left, use the full block */
395  BlockSize = Block->Size;
396 
397  /* Get the next block */
398  NextBlock = Block + 1 + BlockSize;
399  }
400 
401  /* Update the next blocks back link */
402  NextBlock->PreviousSize = BlockSize;
403 
404  /* Update heap usage */
405  Heap->NumAllocs++;
406  Heap->CurrentAllocBytes += Block->Size * sizeof(HEAP_BLOCK);
407  Heap->MaxAllocBytes = max(Heap->MaxAllocBytes, Heap->CurrentAllocBytes);
409  Block->Size * sizeof(HEAP_BLOCK));
410 #if DBG && !defined(_M_ARM)
411  Heap->AllocationTime += (__rdtsc() - Time);
412 #endif
413  TRACE("HeapAllocate(%p, %ld, %.4s) -> return %p\n",
414  HeapHandle, ByteSize, &Tag, Block->Data);
415 
416  /* HACK: zero out the allocation */
417  RtlZeroMemory(Block->Data, Block->Size * sizeof(HEAP_BLOCK));
418 
419 #ifdef FREELDR_HEAP_VERIFIER
420  /* Write size and redzones */
422  *REDZONE_LOW(Block) = REDZONE_MARK;
423  *REDZONE_HI(Block) = REDZONE_MARK;
424 
425  /* Allocation starts after size field and redzone */
426  return (PUCHAR)Block->Data + REDZONE_LOW_OFFSET;
427 #endif
428  /* Return pointer to the data */
429  return Block->Data;
430  }
431 
432  /* We found nothing */
433  WARN("HEAP: nothing suitable found for 0x%lx bytes\n", ByteSize);
434  return NULL;
435 }
SIZE_T MaxAllocBytes
Definition: heap.c:55
HEAP_BLOCK Blocks
Definition: heap.c:62
ULONGLONG AllocationTime
Definition: heap.c:59
#define max(a, b)
Definition: svc.c:63
ULONG_PTR Flink
Definition: heap.c:39
#define REDZONE_MARK
Definition: heap.c:27
unsigned char * PUCHAR
Definition: retypes.h:3
#define WARN(fmt,...)
Definition: debug.h:111
VOID FrLdrHeapVerify(PVOID HeapHandle)
Definition: heap.c:156
USHORT PreviousSize
Definition: heap.c:46
ULONG Tag
Definition: heap.c:47
PPC_QUAL unsigned long long __rdtsc(void)
Definition: intrin_ppc.h:688
USHORT Size
Definition: heap.c:45
#define REDZONE_SIZE(Block)
Definition: heap.c:30
struct _HEAP_BLOCK HEAP_BLOCK
smooth NULL
Definition: ftsmooth.c:416
ULONG NumAllocs
Definition: heap.c:56
#define REDZONE_ALLOCATION
Definition: heap.c:28
#define TRACE(s)
Definition: solgame.cpp:4
uint64_t ULONGLONG
Definition: typedefs.h:65
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define REDZONE_LOW_OFFSET
Definition: heap.c:29
static VOID FrLdrHeapInsertFreeList(PHEAP Heap, PHEAP_BLOCK FreeBlock)
Definition: heap.c:298
#define ERR(fmt,...)
Definition: debug.h:109
BLOCK_DATA Data[]
Definition: heap.c:48
unsigned short USHORT
Definition: pedump.c:61
#define MAXUSHORT
Definition: typedefs.h:81
SIZE_T CurrentAllocBytes
Definition: heap.c:54
IN ULONG IN ULONG Tag
Definition: evtlib.h:159
#define REDZONE_LOW(Block)
Definition: heap.c:31
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
ULONG_PTR TerminatingBlock
Definition: heap.c:61
#define REDZONE_HI(Block)
Definition: heap.c:32
_IRQL_requires_same_ _In_ CLONG ByteSize
Definition: rtltypes.h:389
SIZE_T LargestAllocation
Definition: heap.c:58
static PLARGE_INTEGER Time
Definition: time.c:105
Definition: heap.c:51
static VOID FrLdrHeapRemoveFreeList(PHEAP Heap, PHEAP_BLOCK Block)
Definition: heap.c:280

Referenced by ExAllocatePool(), ExAllocatePoolWithTag(), FrLdrHeapAlloc(), FrLdrTempAlloc(), RtlAllocateHeap(), and RtlpAllocateMemory().

◆ FrLdrHeapCleanupAll()

VOID FrLdrHeapCleanupAll ( VOID  )

Definition at line 248 of file heap.c.

249 {
250 #if DBG
251  PHEAP Heap;
252 
253  Heap = FrLdrDefaultHeap;
254  TRACE("Heap statistics for default heap:\n"
255  "CurrentAlloc=0x%lx, MaxAlloc=0x%lx, LargestAllocation=0x%lx\n"
256  "NumAllocs=%ld, NumFrees=%ld\n",
258  Heap->NumAllocs, Heap->NumFrees);
259  TRACE("AllocTime = %I64d, FreeTime = %I64d, sum = %I64d\n",
260  Heap->AllocationTime, Heap->FreeTime, Heap->AllocationTime + Heap->FreeTime);
261 #endif
262 
263  /* Release free pages from the default heap */
265 
266 #if DBG
267  Heap = FrLdrTempHeap;
268  TRACE("Heap statistics for temp heap:\n"
269  "CurrentAlloc=0x%lx, MaxAlloc=0x%lx, LargestAllocation=0x%lx\n"
270  "NumAllocs=%ld, NumFrees=%ld\n",
272  Heap->NumAllocs, Heap->NumFrees);
273 #endif
274 
275  /* Destroy the temp heap */
277 }
SIZE_T MaxAllocBytes
Definition: heap.c:55
ULONGLONG AllocationTime
Definition: heap.c:59
VOID FrLdrHeapRelease(PVOID HeapHandle)
Definition: heap.c:181
VOID FrLdrHeapDestroy(PVOID HeapHandle)
Definition: heap.c:137
ULONG NumAllocs
Definition: heap.c:56
#define TRACE(s)
Definition: solgame.cpp:4
ULONGLONG FreeTime
Definition: heap.c:60
PVOID FrLdrTempHeap
Definition: heap.c:35
ULONG NumFrees
Definition: heap.c:57
SIZE_T CurrentAllocBytes
Definition: heap.c:54
SIZE_T LargestAllocation
Definition: heap.c:58
PVOID FrLdrDefaultHeap
Definition: heap.c:34
Definition: heap.c:51

Referenced by WinLdrSetupMemoryLayout().

◆ FrLdrHeapCreate()

PVOID FrLdrHeapCreate ( SIZE_T  MaximumSize,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 66 of file heap.c.

69 {
70  PHEAP Heap;
71  PHEAP_BLOCK Block;
72  SIZE_T Remaining;
73  USHORT PreviousSize;
74  TRACE("HeapCreate(MemoryType=%ld)\n", MemoryType);
75 
76  /* Allocate some memory for the heap */
77  MaximumSize = ALIGN_UP_BY(MaximumSize, MM_PAGE_SIZE);
78  Heap = MmAllocateMemoryWithType(MaximumSize, MemoryType);
79  if (!Heap)
80  {
81  ERR("HEAP: Failed to allocate heap of size 0x%lx, Type\n",
82  MaximumSize, MemoryType);
83  return NULL;
84  }
85 
86  /* Initialize the heap header */
87  Heap->MaximumSize = MaximumSize;
88  Heap->CurrentAllocBytes = 0;
89  Heap->MaxAllocBytes = 0;
90  Heap->NumAllocs = 0;
91  Heap->NumFrees = 0;
92  Heap->LargestAllocation = 0;
93 
94  /* Calculate what's left to process */
95  Remaining = (MaximumSize - sizeof(HEAP)) / sizeof(HEAP_BLOCK);
96  TRACE("Remaining = %ld\n", Remaining);
97 
98  /* Substract 2 for the terminating entry (header + free entry) */
99  Remaining -= 2;
100 
101  Block = &Heap->Blocks;
102  PreviousSize = 0;
103 
104  /* Create free blocks */
105  while (Remaining > 1)
106  {
107  /* Initialize this free block */
108  Block->Size = (USHORT)min(MAXUSHORT, Remaining - 1);
109  Block->PreviousSize = PreviousSize;
110  Block->Tag = 0;
111  Block->Data[0].Flink = (Block - &Heap->Blocks) + Block->Size + 1;
112  Block->Data[0].Blink = (Block - &Heap->Blocks) - 1 - PreviousSize;
113 
114  /* Substract current block size from remainder */
115  Remaining -= (Block->Size + 1);
116 
117  /* Go to next block */
118  PreviousSize = Block->Size;
119  Block = Block + Block->Size + 1;
120 
121  TRACE("Remaining = %ld\n", Remaining);
122  }
123 
124  /* Now finish with a terminating block */
125  Heap->TerminatingBlock = Block - &Heap->Blocks;
126  Block->Size = 0;
127  Block->PreviousSize = PreviousSize;
128  Block->Tag = 'dnE#';
129  Block->Data[0].Flink = 0;
130  Block->Data[0].Blink = (Block - &Heap->Blocks) - 1 - PreviousSize;
131  Heap->Blocks.Data[0].Blink = Heap->TerminatingBlock;
132 
133  return Heap;
134 }
SIZE_T MaxAllocBytes
Definition: heap.c:55
HEAP_BLOCK Blocks
Definition: heap.c:62
ULONG_PTR Flink
Definition: heap.c:39
USHORT PreviousSize
Definition: heap.c:46
ULONG Tag
Definition: heap.c:47
USHORT Size
Definition: heap.c:45
struct _HEAP_BLOCK HEAP_BLOCK
ULONG_PTR Blink
Definition: heap.c:40
smooth NULL
Definition: ftsmooth.c:416
ULONG NumAllocs
Definition: heap.c:56
#define TRACE(s)
Definition: solgame.cpp:4
PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:31
#define ERR(fmt,...)
Definition: debug.h:109
ULONG_PTR SIZE_T
Definition: typedefs.h:78
BLOCK_DATA Data[]
Definition: heap.c:48
unsigned short USHORT
Definition: pedump.c:61
ULONG NumFrees
Definition: heap.c:57
struct _HEAP HEAP
#define min(a, b)
Definition: monoChain.cc:55
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:360
#define MAXUSHORT
Definition: typedefs.h:81
SIZE_T CurrentAllocBytes
Definition: heap.c:54
ULONG_PTR TerminatingBlock
Definition: heap.c:61
#define ALIGN_UP_BY(size, align)
SIZE_T LargestAllocation
Definition: heap.c:58
SIZE_T MaximumSize
Definition: heap.c:53
Definition: heap.c:51

Referenced by MmInitializeHeap().

◆ FrLdrHeapDestroy()

VOID FrLdrHeapDestroy ( PVOID  HeapHandle)

Definition at line 137 of file heap.c.

139 {
140  PHEAP Heap = HeapHandle;
141 
142  /* Mark all pages as firmware temporary, so they are free for the kernel */
144  (ULONG_PTR)Heap / MM_PAGE_SIZE,
145  (PFN_COUNT)(Heap->MaximumSize / MM_PAGE_SIZE),
147 
148 #if DBG
149  /* Make sure everything is dead */
150  RtlFillMemory(Heap, Heap->MaximumSize, 0xCCCCCCCC);
151 #endif
152 }
ULONG PFN_COUNT
Definition: mmtypes.h:102
uint32_t ULONG_PTR
Definition: typedefs.h:63
PVOID PageLookupTableAddress
Definition: meminit.c:26
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
Definition: meminit.c:504
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:593
SIZE_T MaximumSize
Definition: heap.c:53
Definition: heap.c:51

Referenced by FrLdrHeapCleanupAll().

◆ FrLdrHeapFree()

FORCEINLINE VOID FrLdrHeapFree ( PVOID  MemoryPointer,
ULONG  Tag 
)

Definition at line 170 of file mm.h.

171 {
172  FrLdrHeapFreeEx(FrLdrDefaultHeap, MemoryPointer, Tag);
173 }
PVOID FrLdrDefaultHeap
Definition: heap.c:34
VOID FrLdrHeapFreeEx(PVOID HeapHandle, PVOID Pointer, ULONG Tag)
Definition: heap.c:438
IN ULONG IN ULONG Tag
Definition: evtlib.h:159

Referenced by LoadOperatingSystem(), PcGetHarddiskConfigurationData(), PeLdrAllocateDataTableEntry(), WinLdrAddDriverToList(), WinLdrScanRegistry(), and XboxGetHarddiskConfigurationData().

◆ FrLdrHeapFreeEx()

VOID FrLdrHeapFreeEx ( PVOID  HeapHandle,
PVOID  Pointer,
ULONG  Tag 
)

Definition at line 438 of file heap.c.

442 {
443  PHEAP Heap = HeapHandle;
444  PHEAP_BLOCK Block, PrevBlock, NextBlock;
445 #if DBG && !defined(_M_ARM)
446  ULONGLONG Time = __rdtsc();
447 #endif
448  TRACE("HeapFree(%p, %p)\n", HeapHandle, Pointer);
449  ASSERT(Tag != 'dnE#');
450 
451 #ifdef FREELDR_HEAP_VERIFIER
452  /* Verify the heap */
453  FrLdrHeapVerify(HeapHandle);
454 #endif
455 
456  /* Check if the block is really inside this heap */
457  if ((Pointer < (PVOID)(Heap + 1)) ||
458  (Pointer > (PVOID)((PUCHAR)Heap + Heap->MaximumSize)))
459  {
460  ERR("HEAP: trying to free %p outside of heap %p\n", Pointer, Heap);
461  ASSERT(FALSE);
462  }
463 
464  Block = ((PHEAP_BLOCK)Pointer) - 1;
465 #ifdef FREELDR_HEAP_VERIFIER
466  Block = (PHEAP_BLOCK)((PUCHAR)Block - REDZONE_LOW_OFFSET);
467 
468  /* Verify size and redzones */
469  ASSERT(*REDZONE_SIZE(Block) <= Block->Size * sizeof(HEAP_BLOCK));
470  ASSERT(*REDZONE_LOW(Block) == REDZONE_MARK);
471  ASSERT(*REDZONE_HI(Block) == REDZONE_MARK);
472 #endif
473 
474  /* Check if the tag matches */
475  if ((Tag && (Block->Tag != Tag)) || (Block->Tag == 0))
476  {
477  ERR("HEAP: Bad tag! Pointer=%p: block tag '%.4s', requested '%.4s', size=0x%lx\n",
478  Pointer, &Block->Tag, &Tag, Block->Size);
479  ASSERT(FALSE);
480  }
481 
482  /* Mark as free */
483  Block->Tag = 0;
484 
485 #if DBG
486  /* Erase contents */
487  RtlFillMemory(Block->Data, Block->Size * sizeof(HEAP_BLOCK), 0xCCCCCCCC);
488 #endif
489 
490  /* Update heap usage */
491  Heap->NumFrees++;
492  Heap->CurrentAllocBytes -= Block->Size * sizeof(HEAP_BLOCK);
493 
494  /* Get pointers to the next and previous block */
495  PrevBlock = Block - Block->PreviousSize - 1;
496  NextBlock = Block + Block->Size + 1;
497 
498  /* Check if next block is free */
499  if ((NextBlock->Tag == 0) &&
500  ((Block->Size + NextBlock->Size + 1) <= MAXUSHORT))
501  {
502  /* Merge next block into current */
503  Block->Size += NextBlock->Size + 1;
504  FrLdrHeapRemoveFreeList(Heap, NextBlock);
505 
506  NextBlock = Block + Block->Size + 1;
507  }
508 
509  /* Check if there is a block before and it's free */
510  if ((Block->PreviousSize != 0) && (PrevBlock->Tag == 0) &&
511  ((PrevBlock->Size + Block->Size + 1) <= MAXUSHORT))
512  {
513  /* Merge current block into previous */
514  PrevBlock->Size += Block->Size + 1;
515  Block = PrevBlock;
516  }
517  else
518  {
519  /* Insert the entry into the free list */
520  FrLdrHeapInsertFreeList(Heap, Block);
521  }
522 
523  /* Update the next block's back link */
524  NextBlock->PreviousSize = Block->Size;
525 #if DBG && !defined(_M_ARM)
526  Heap->FreeTime += (__rdtsc() - Time);
527 #endif
528 }
#define REDZONE_MARK
Definition: heap.c:27
unsigned char * PUCHAR
Definition: retypes.h:3
VOID FrLdrHeapVerify(PVOID HeapHandle)
Definition: heap.c:156
USHORT PreviousSize
Definition: heap.c:46
ULONG Tag
Definition: heap.c:47
PPC_QUAL unsigned long long __rdtsc(void)
Definition: intrin_ppc.h:688
USHORT Size
Definition: heap.c:45
#define REDZONE_SIZE(Block)
Definition: heap.c:30
struct _HEAP_BLOCK HEAP_BLOCK
struct _HEAP_BLOCK * PHEAP_BLOCK
#define TRACE(s)
Definition: solgame.cpp:4
uint64_t ULONGLONG
Definition: typedefs.h:65
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
ULONGLONG FreeTime
Definition: heap.c:60
#define REDZONE_LOW_OFFSET
Definition: heap.c:29
static VOID FrLdrHeapInsertFreeList(PHEAP Heap, PHEAP_BLOCK FreeBlock)
Definition: heap.c:298
#define ERR(fmt,...)
Definition: debug.h:109
BLOCK_DATA Data[]
Definition: heap.c:48
ULONG NumFrees
Definition: heap.c:57
#define MAXUSHORT
Definition: typedefs.h:81
SIZE_T CurrentAllocBytes
Definition: heap.c:54
IN ULONG IN ULONG Tag
Definition: evtlib.h:159
#define REDZONE_LOW(Block)
Definition: heap.c:31
#define REDZONE_HI(Block)
Definition: heap.c:32
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:593
SIZE_T MaximumSize
Definition: heap.c:53
static PLARGE_INTEGER Time
Definition: time.c:105
Definition: heap.c:51
static VOID FrLdrHeapRemoveFreeList(PHEAP Heap, PHEAP_BLOCK Block)
Definition: heap.c:280

Referenced by ExFreePool(), ExFreePoolWithTag(), FrLdrHeapFree(), FrLdrTempFree(), RtlFreeHeap(), and RtlpFreeMemory().

◆ FrLdrHeapRelease()

VOID FrLdrHeapRelease ( PVOID  HeapHandle)

Definition at line 181 of file heap.c.

183 {
184  PHEAP Heap = HeapHandle;
185  PHEAP_BLOCK Block;
186  PUCHAR StartAddress, EndAddress;
187  PFN_COUNT FreePages, AllFreePages = 0;
188 
189  TRACE("HeapRelease(%p)\n", HeapHandle);
190 
191  /* Loop all heap chunks */
192  for (Block = &Heap->Blocks;
193  Block->Size != 0;
194  Block = Block + 1 + Block->Size)
195  {
196  /* Continue, if its not free */
197  if (Block->Tag != 0)
198  {
199 #ifdef FREELDR_HEAP_VERIFIER
200  /* Verify size and redzones */
201  ASSERT(*REDZONE_SIZE(Block) <= Block->Size * sizeof(HEAP_BLOCK));
202  ASSERT(*REDZONE_LOW(Block) == REDZONE_MARK);
203  ASSERT(*REDZONE_HI(Block) == REDZONE_MARK);
204 #endif
205  continue;
206  }
207 
208  /* Calculate page aligned start address of the free region */
209  StartAddress = ALIGN_UP_POINTER_BY(Block->Data, PAGE_SIZE);
210 
211  /* Walk over adjacent free blocks */
212  while (Block->Tag == 0) Block = Block + Block->Size + 1;
213 
214  /* Check if this was the last block */
215  if (Block->Size == 0)
216  {
217  /* Align the end address up to cover the end of the heap */
218  EndAddress = ALIGN_UP_POINTER_BY(Block->Data, PAGE_SIZE);
219  }
220  else
221  {
222  /* Align the end address down to not cover any allocations */
223  EndAddress = ALIGN_DOWN_POINTER_BY(Block->Data, PAGE_SIZE);
224  }
225 
226  /* Check if we have free pages */
227  if (EndAddress > StartAddress)
228  {
229  /* Calculate the size of the free region in pages */
230  FreePages = (PFN_COUNT)((EndAddress - StartAddress) / MM_PAGE_SIZE);
231  AllFreePages += FreePages;
232 
233  /* Now mark the pages free */
235  (ULONG_PTR)StartAddress / MM_PAGE_SIZE,
236  FreePages,
237  LoaderFree);
238  }
239 
240  /* bail out, if it was the last block */
241  if (Block->Size == 0) break;
242  }
243 
244  TRACE("HeapRelease() done, freed %lu of %lu pages\n", AllFreePages, Heap->MaximumSize / MM_PAGE_SIZE);
245 }
HEAP_BLOCK Blocks
Definition: heap.c:62
ULONG PFN_COUNT
Definition: mmtypes.h:102
#define REDZONE_MARK
Definition: heap.c:27
unsigned char * PUCHAR
Definition: retypes.h:3
ULONG Tag
Definition: heap.c:47
USHORT Size
Definition: heap.c:45
#define REDZONE_SIZE(Block)
Definition: heap.c:30
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define TRACE(s)
Definition: solgame.cpp:4
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ALIGN_UP_POINTER_BY(ptr, align)
Definition: umtypes.h:85
#define PAGE_SIZE
Definition: env_spec_w32.h:49
PVOID PageLookupTableAddress
Definition: meminit.c:26
BLOCK_DATA Data[]
Definition: heap.c:48
#define REDZONE_LOW(Block)
Definition: heap.c:31
#define REDZONE_HI(Block)
Definition: heap.c:32
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
Definition: meminit.c:504
#define ALIGN_DOWN_POINTER_BY(ptr, align)
Definition: umtypes.h:82
SIZE_T MaximumSize
Definition: heap.c:53
Definition: heap.c:51

Referenced by FrLdrHeapCleanupAll().

◆ FrLdrHeapVerify()

VOID FrLdrHeapVerify ( PVOID  HeapHandle)

Definition at line 156 of file heap.c.

158 {
159  PHEAP Heap = HeapHandle;
160  PHEAP_BLOCK Block;
161 
162  /* Loop all heap chunks */
163  for (Block = &Heap->Blocks;
164  Block->Size != 0;
165  Block = Block + 1 + Block->Size)
166  {
167  /* Continue, if its not free */
168  if (Block->Tag != 0)
169  {
170  /* Verify size and redzones */
171  ASSERT(*REDZONE_SIZE(Block) <= Block->Size * sizeof(HEAP_BLOCK));
172  ASSERT(*REDZONE_LOW(Block) == REDZONE_MARK);
173  ASSERT(*REDZONE_HI(Block) == REDZONE_MARK);
174  continue;
175  }
176  }
177 }
HEAP_BLOCK Blocks
Definition: heap.c:62
#define REDZONE_MARK
Definition: heap.c:27
ULONG Tag
Definition: heap.c:47
USHORT Size
Definition: heap.c:45
#define REDZONE_SIZE(Block)
Definition: heap.c:30
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define REDZONE_LOW(Block)
Definition: heap.c:31
#define REDZONE_HI(Block)
Definition: heap.c:32
Definition: heap.c:51

Referenced by FrLdrHeapAllocateEx(), and FrLdrHeapFreeEx().

◆ FrLdrTempAlloc()

FORCEINLINE PVOID FrLdrTempAlloc ( _In_ SIZE_T  Size,
_In_ ULONG  Tag 
)

Definition at line 177 of file mm.h.

180 {
182 }
PVOID FrLdrHeapAllocateEx(PVOID HeapHandle, SIZE_T ByteSize, ULONG Tag)
Definition: heap.c:320
PVOID FrLdrTempHeap
Definition: heap.c:35
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
IN ULONG IN ULONG Tag
Definition: evtlib.h:159

Referenced by ArcOpen(), btrfs_read_extent_reg(), btrfs_readlink(), BtrFsMount(), BtrFsOpen(), CacheInternalAddBlockToCache(), CmpAllocate(), DiskOpen(), Ext2CopyDoubleIndirectBlockPointers(), Ext2CopyIndirectBlockPointers(), Ext2CopyTripleIndirectBlockPointers(), Ext2Mount(), Ext2OpenFile(), Ext2ReadBlockPointerList(), Ext2ReadDirectory(), Ext2ReadGroupDescriptors(), Ext2ReadPartialBlock(), Ext2ReadSuperBlock(), FatBufferDirectory(), FatMount(), FatOpen(), FatOpenVolume(), FatReadPartialCluster(), FrLdrMapModule(), FsRegisterDevice(), InfOpenFile(), InfpAddFieldToLine(), InfpAddKeyToLine(), InfpCacheAddLine(), InfpCacheAddSection(), IniAddSection(), IniAddSettingValueToSection(), IniFileInitialize(), IniModifySettingValue(), IniParseFile(), init_path(), insert_chunk_item(), IsoBufferDirectory(), IsoOpen(), LoadReactOSSetup(), NtfsFindAttributeHelper(), NtfsFindMftRecord(), NtfsMount(), NtfsOpen(), NtfsPrepareAttributeContext(), PxeOpen(), RunLoader(), ScsiPortInitialize(), TuiEditBox(), TuiFadeInBackdrop(), TuiFadeOut(), TuiMessageBox(), UiShowMessageBoxesInArgv(), UiShowMessageBoxesInSection(), and VideoFadeIn().

◆ FrLdrTempFree()

FORCEINLINE VOID FrLdrTempFree ( PVOID  Allocation,
ULONG  Tag 
)

◆ MmAllocateHighestMemoryBelowAddress()

PVOID MmAllocateHighestMemoryBelowAddress ( SIZE_T  MemorySize,
PVOID  DesiredAddress,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 160 of file mm.c.

161 {
162  PFN_NUMBER PagesNeeded;
163  PFN_NUMBER FirstFreePageFromEnd;
164  PFN_NUMBER DesiredAddressPageNumber;
165  PVOID MemPointer;
166 
167  if (MemorySize == 0)
168  {
169  WARN("MmAllocateHighestMemoryBelowAddress() called for 0 bytes. Returning NULL.\n");
170  UiMessageBoxCritical("Memory allocation failed: MmAllocateHighestMemoryBelowAddress() called for 0 bytes.");
171  return NULL;
172  }
173 
174  // Find out how many blocks it will take to
175  // satisfy this allocation
176  PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
177 
178  // Get the page number for their desired address
179  DesiredAddressPageNumber = (ULONG_PTR)DesiredAddress / MM_PAGE_SIZE;
180 
181  // If we don't have enough available mem
182  // then return NULL
183  if (FreePagesInLookupTable < PagesNeeded)
184  {
185  ERR("Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize);
186  UiMessageBoxCritical("Memory allocation failed: out of memory.");
187  return NULL;
188  }
189 
190  FirstFreePageFromEnd = MmFindAvailablePagesBeforePage(PageLookupTableAddress, TotalPagesInLookupTable, PagesNeeded, DesiredAddressPageNumber);
191 
192  if (FirstFreePageFromEnd == 0)
193  {
194  ERR("Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes.\n", MemorySize);
195  UiMessageBoxCritical("Memory allocation failed: out of memory.");
196  return NULL;
197  }
198 
199  MmAllocatePagesInLookupTable(PageLookupTableAddress, FirstFreePageFromEnd, PagesNeeded, MemoryType);
200 
201  FreePagesInLookupTable -= PagesNeeded;
202  MemPointer = (PVOID)((ULONG_PTR)FirstFreePageFromEnd * MM_PAGE_SIZE);
203 
204  TRACE("Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, FirstFreePageFromEnd);
205  TRACE("Memory allocation pointer: 0x%x\n", MemPointer);
206 
207  // Update LoaderPagesSpanned count
208  if ((((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT) > LoaderPagesSpanned)
209  LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize) >> PAGE_SHIFT);
210 
211  // Now return the pointer
212  return MemPointer;
213 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
#define WARN(fmt,...)
Definition: debug.h:111
PFN_NUMBER MmFindAvailablePagesBeforePage(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, PFN_NUMBER LastPage)
Definition: meminit.c:624
VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
Definition: meminit.c:534
uint32_t ULONG_PTR
Definition: typedefs.h:63
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:416
PFN_NUMBER FreePagesInLookupTable
Definition: meminit.c:28
void * PVOID
Definition: retypes.h:9
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER LoaderPagesSpanned
Definition: mm.c:29
static CONST DWORD MemorySize[]
Definition: svga.c:32
PVOID PageLookupTableAddress
Definition: meminit.c:26
#define ERR(fmt,...)
Definition: debug.h:109
#define ULONG_PTR
Definition: config.h:101
VOID UiMessageBoxCritical(PCSTR MessageText)
Definition: ui.c:331

◆ MmAllocateMemory()

PVOID MmAllocateMemory ( SIZE_T  MemorySize)

◆ MmAllocateMemoryAtAddress()

PVOID MmAllocateMemoryAtAddress ( SIZE_T  MemorySize,
PVOID  DesiredAddress,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 85 of file mm.c.

86 {
87  PFN_NUMBER PagesNeeded;
88  PFN_NUMBER StartPageNumber;
89  PVOID MemPointer;
90 
91  if (MemorySize == 0)
92  {
93  WARN("MmAllocateMemoryAtAddress() called for 0 bytes. Returning NULL.\n");
94  UiMessageBoxCritical("Memory allocation failed: MmAllocateMemoryAtAddress() called for 0 bytes.");
95  return NULL;
96  }
97 
98  // Find out how many blocks it will take to
99  // satisfy this allocation
100  PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
101 
102  // Get the starting page number
103  StartPageNumber = MmGetPageNumberFromAddress(DesiredAddress);
104 
105  // If we don't have enough available mem
106  // then return NULL
107  if (FreePagesInLookupTable < PagesNeeded)
108  {
109  ERR("Memory allocation failed in MmAllocateMemoryAtAddress(). "
110  "Not enough free memory to allocate %d bytes (requesting %d pages but have only %d). "
111  "\n", MemorySize, PagesNeeded, FreePagesInLookupTable);
112  UiMessageBoxCritical("Memory allocation failed: out of memory.");
113  return NULL;
114  }
115 
117  {
118  WARN("Memory allocation failed in MmAllocateMemoryAtAddress(). "
119  "Not enough free memory to allocate %d bytes at address %p.\n",
120  MemorySize, DesiredAddress);
121 
122  // Don't tell this to user since caller should try to alloc this memory
123  // at a different address
124  //UiMessageBoxCritical("Memory allocation failed: out of memory.");
125  return NULL;
126  }
127 
128  MmAllocatePagesInLookupTable(PageLookupTableAddress, StartPageNumber, PagesNeeded, MemoryType);
129 
130  FreePagesInLookupTable -= PagesNeeded;
131  MemPointer = (PVOID)((ULONG_PTR)StartPageNumber * MM_PAGE_SIZE);
132 
133  TRACE("Allocated %d bytes (%d pages) of memory starting at page %d.\n", MemorySize, PagesNeeded, StartPageNumber);
134  TRACE("Memory allocation pointer: 0x%x\n", MemPointer);
135 
136  // Update LoaderPagesSpanned count
137  if ((((ULONG_PTR)MemPointer + MemorySize + PAGE_SIZE - 1) >> PAGE_SHIFT) > LoaderPagesSpanned)
138  LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize + PAGE_SIZE - 1) >> PAGE_SHIFT);
139 
140  // Now return the pointer
141  return MemPointer;
142 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
#define WARN(fmt,...)
Definition: debug.h:111
VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
Definition: meminit.c:534
uint32_t ULONG_PTR
Definition: typedefs.h:63
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:416
PFN_NUMBER FreePagesInLookupTable
Definition: meminit.c:28
void * PVOID
Definition: retypes.h:9
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER LoaderPagesSpanned
Definition: mm.c:29
static CONST DWORD MemorySize[]
Definition: svga.c:32
#define PAGE_SIZE
Definition: env_spec_w32.h:49
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:369
PVOID PageLookupTableAddress
Definition: meminit.c:26
#define ERR(fmt,...)
Definition: debug.h:109
BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount)
Definition: meminit.c:672
#define ULONG_PTR
Definition: config.h:101
VOID UiMessageBoxCritical(PCSTR MessageText)
Definition: ui.c:331

Referenced by MempAllocatePageTables(), and PeLdrLoadImage().

◆ MmAllocateMemoryWithType()

PVOID MmAllocateMemoryWithType ( SIZE_T  MemorySize,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 31 of file mm.c.

32 {
33  PFN_NUMBER PagesNeeded;
34  PFN_NUMBER FirstFreePageFromEnd;
35  PVOID MemPointer;
36 
37  if (MemorySize == 0)
38  {
39  WARN("MmAllocateMemory() called for 0 bytes. Returning NULL.\n");
40  UiMessageBoxCritical("Memory allocation failed: MmAllocateMemory() called for 0 bytes.");
41  return NULL;
42  }
43 
45 
46  // Find out how many blocks it will take to
47  // satisfy this allocation
48  PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
49 
50  // If we don't have enough available mem
51  // then return NULL
52  if (FreePagesInLookupTable < PagesNeeded)
53  {
54  ERR("Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize);
55  UiMessageBoxCritical("Memory allocation failed: out of memory.");
56  return NULL;
57  }
58 
59  FirstFreePageFromEnd = MmFindAvailablePages(PageLookupTableAddress, TotalPagesInLookupTable, PagesNeeded, FALSE);
60 
61  if (FirstFreePageFromEnd == 0)
62  {
63  ERR("Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes.\n", MemorySize);
64  UiMessageBoxCritical("Memory allocation failed: out of memory.");
65  return NULL;
66  }
67 
68  MmAllocatePagesInLookupTable(PageLookupTableAddress, FirstFreePageFromEnd, PagesNeeded, MemoryType);
69 
70  FreePagesInLookupTable -= PagesNeeded;
71  MemPointer = (PVOID)((ULONG_PTR)FirstFreePageFromEnd * MM_PAGE_SIZE);
72 
73  TRACE("Allocated %d bytes (%d pages) of memory (type %ld) starting at page 0x%lx.\n",
74  MemorySize, PagesNeeded, MemoryType, FirstFreePageFromEnd);
75  TRACE("Memory allocation pointer: 0x%x\n", MemPointer);
76 
77  // Update LoaderPagesSpanned count
78  if ((((ULONG_PTR)MemPointer + MemorySize + PAGE_SIZE - 1) >> PAGE_SHIFT) > LoaderPagesSpanned)
79  LoaderPagesSpanned = (((ULONG_PTR)MemPointer + MemorySize + PAGE_SIZE - 1) >> PAGE_SHIFT);
80 
81  // Now return the pointer
82  return MemPointer;
83 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
#define WARN(fmt,...)
Definition: debug.h:111
VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
Definition: meminit.c:534
uint32_t ULONG_PTR
Definition: typedefs.h:63
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:416
PFN_NUMBER FreePagesInLookupTable
Definition: meminit.c:28
void * PVOID
Definition: retypes.h:9
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER LoaderPagesSpanned
Definition: mm.c:29
static CONST DWORD MemorySize[]
Definition: svga.c:32
#define PAGE_SIZE
Definition: env_spec_w32.h:49
PVOID PageLookupTableAddress
Definition: meminit.c:26
#define ERR(fmt,...)
Definition: debug.h:109
PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
Definition: meminit.c:565
#define ULONG_PTR
Definition: config.h:101
VOID UiMessageBoxCritical(PCSTR MessageText)
Definition: ui.c:331

Referenced by AllocateAndInitLPB(), FrLdrHeapCreate(), MempAllocatePageTables(), MempGetOrCreatePageDir(), PeLdrLoadImage(), RamDiskLoadVirtualFile(), VideoAllocateOffScreenBuffer(), WinLdrLoadModule(), WinLdrLoadNLSData(), WinLdrLoadSystemHive(), WinLdrSetupMachineDependent(), and WinLdrSetupMemoryLayout().

◆ MmAllocatePagesInLookupTable()

VOID MmAllocatePagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  StartPage,
PFN_NUMBER  PageCount,
TYPE_OF_MEMORY  MemoryType 
)

Definition at line 534 of file meminit.c.

535 {
536  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
538 
540  for (Index=StartPage; Index<(StartPage+PageCount); Index++)
541  {
542  RealPageLookupTable[Index].PageAllocated = MemoryType;
543  RealPageLookupTable[Index].PageAllocationLength = (Index == StartPage) ? PageCount : 0;
544  }
545 }
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
int WINAPI StartPage(_In_ HDC)
ULONG PFN_NUMBER
Definition: ke.h:8
PFN_NUMBER PageAllocationLength
Definition: mm.h:76
TYPE_OF_MEMORY PageAllocated
Definition: mm.h:75
static const UCHAR Index[8]
Definition: usbohci.c:18
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Referenced by MmAllocateHighestMemoryBelowAddress(), MmAllocateMemoryAtAddress(), MmAllocateMemoryWithType(), and MmSetMemoryType().

◆ MmAreMemoryPagesAvailable()

BOOLEAN MmAreMemoryPagesAvailable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PVOID  PageAddress,
PFN_NUMBER  PageCount 
)

Definition at line 672 of file meminit.c.

673 {
674  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
677 
679 
680  if (StartPage < MmLowestPhysicalPage) return FALSE;
681 
683 
684  // Make sure they aren't trying to go past the
685  // end of available memory
686  if ((StartPage + PageCount) > TotalPageCount)
687  {
688  return FALSE;
689  }
690 
691  for (Index = StartPage; Index < (StartPage + PageCount); Index++)
692  {
693  // If this page is allocated then there obviously isn't
694  // memory available so return FALSE
695  if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
696  {
697  return FALSE;
698  }
699  }
700 
701  return TRUE;
702 }
#define TRUE
Definition: types.h:120
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
_Must_inspect_result_ _In_ SIZE_T _In_ PVOID PageAddress
Definition: mmfuncs.h:472
int WINAPI StartPage(_In_ HDC)
ULONG PFN_NUMBER
Definition: ke.h:8
static const UCHAR Index[8]
Definition: usbohci.c:18
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:369
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Referenced by MmAllocateMemoryAtAddress().

◆ MmCountFreePagesInLookupTable()

PFN_NUMBER MmCountFreePagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 547 of file meminit.c.

548 {
549  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
551  PFN_NUMBER FreePageCount;
552 
553  FreePageCount = 0;
554  for (Index=0; Index<TotalPageCount; Index++)
555  {
556  if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
557  {
558  FreePageCount++;
559  }
560  }
561 
562  return FreePageCount;
563 }
ULONG PFN_NUMBER
Definition: ke.h:8
static const UCHAR Index[8]
Definition: usbohci.c:18
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Referenced by MmInitializeMemoryManager().

◆ MmFindAvailablePages()

PFN_NUMBER MmFindAvailablePages ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PFN_NUMBER  PagesNeeded,
BOOLEAN  FromEnd 
)

Definition at line 565 of file meminit.c.

566 {
567  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
568  PFN_NUMBER AvailablePagesSoFar;
570 
571  if (LastFreePageHint > TotalPageCount)
572  {
573  LastFreePageHint = TotalPageCount;
574  }
575 
576  AvailablePagesSoFar = 0;
577  if (FromEnd)
578  {
579  /* Allocate "high" (from end) pages */
580  for (Index=LastFreePageHint-1; Index>0; Index--)
581  {
582  if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
583  {
584  AvailablePagesSoFar = 0;
585  continue;
586  }
587  else
588  {
589  AvailablePagesSoFar++;
590  }
591 
592  if (AvailablePagesSoFar >= PagesNeeded)
593  {
594  return Index + MmLowestPhysicalPage;
595  }
596  }
597  }
598  else
599  {
600  TRACE("Alloc low memory, LastFreePageHint 0x%x, TPC 0x%x\n", LastFreePageHint, TotalPageCount);
601  /* Allocate "low" pages */
602  for (Index=1; Index < LastFreePageHint; Index++)
603  {
604  if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
605  {
606  AvailablePagesSoFar = 0;
607  continue;
608  }
609  else
610  {
611  AvailablePagesSoFar++;
612  }
613 
614  if (AvailablePagesSoFar >= PagesNeeded)
615  {
616  return Index - AvailablePagesSoFar + 1 + MmLowestPhysicalPage;
617  }
618  }
619  }
620 
621  return 0;
622 }
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
ULONG PFN_NUMBER
Definition: ke.h:8
#define TRACE(s)
Definition: solgame.cpp:4
static const UCHAR Index[8]
Definition: usbohci.c:18
PFN_NUMBER LastFreePageHint
Definition: meminit.c:29
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Referenced by MmAllocateMemoryWithType(), and MmFindAvailablePagesBeforePage().

◆ MmFindAvailablePagesBeforePage()

PFN_NUMBER MmFindAvailablePagesBeforePage ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount,
PFN_NUMBER  PagesNeeded,
PFN_NUMBER  LastPage 
)

Definition at line 624 of file meminit.c.

625 {
626  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
627  PFN_NUMBER AvailablePagesSoFar;
629 
630  if (LastPage > TotalPageCount)
631  {
632  return MmFindAvailablePages(PageLookupTable, TotalPageCount, PagesNeeded, TRUE);
633  }
634 
635  AvailablePagesSoFar = 0;
636  for (Index=LastPage-1; Index>0; Index--)
637  {
638  if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
639  {
640  AvailablePagesSoFar = 0;
641  continue;
642  }
643  else
644  {
645  AvailablePagesSoFar++;
646  }
647 
648  if (AvailablePagesSoFar >= PagesNeeded)
649  {
650  return Index + MmLowestPhysicalPage;
651  }
652  }
653 
654  return 0;
655 }
#define TRUE
Definition: types.h:120
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
ULONG PFN_NUMBER
Definition: ke.h:8
static const UCHAR Index[8]
Definition: usbohci.c:18
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
Definition: meminit.c:565

Referenced by MmAllocateHighestMemoryBelowAddress().

◆ MmFindLocationForPageLookupTable()

PVOID MmFindLocationForPageLookupTable ( PFN_NUMBER  TotalPageCount)

Definition at line 414 of file meminit.c.

415 {
417  SIZE_T PageLookupTableSize;
418  PFN_NUMBER RequiredPages;
419  PFN_NUMBER CandidateBasePage = 0;
420  PFN_NUMBER CandidatePageCount;
421  PFN_NUMBER PageLookupTableEndPage;
422  PVOID PageLookupTableMemAddress = NULL;
423 
424  // Calculate how much pages we need to keep the page lookup table
425  PageLookupTableSize = TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM);
426  RequiredPages = PageLookupTableSize / MM_PAGE_SIZE;
427 
428  // Search the highest memory block big enough to contain lookup table
430  {
431  // Continue, if memory is not free
432  if (MemoryDescriptor->MemoryType != LoaderFree) continue;
433 
434  // Continue, if the block is not big enough
435  if (MemoryDescriptor->PageCount < RequiredPages) continue;
436 
437  // Continue, if it is not at a higher address than previous address
438  if (MemoryDescriptor->BasePage < CandidateBasePage) continue;
439 
440  // Continue, if the address is too high
441  if (MemoryDescriptor->BasePage + RequiredPages >= MM_MAX_PAGE) continue;
442 
443  // Memory block is more suitable than the previous one
444  CandidateBasePage = MemoryDescriptor->BasePage;
445  CandidatePageCount = MemoryDescriptor->PageCount;
446  }
447 
448  // Calculate the end address for the lookup table
449  PageLookupTableEndPage = min(CandidateBasePage + CandidatePageCount,
450  MM_MAX_PAGE);
451 
452  // Calculate the virtual address
453  PageLookupTableMemAddress = (PVOID)((PageLookupTableEndPage * PAGE_SIZE)
454  - PageLookupTableSize);
455 
456  TRACE("MmFindLocationForPageLookupTable() returning 0x%x\n", PageLookupTableMemAddress);
457 
458  return PageLookupTableMemAddress;
459 }
_Out_ PNDIS_BUFFER _In_ NDIS_HANDLE _In_ PVOID MemoryDescriptor
Definition: ndis.h:3270
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:223
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:416
void * PVOID
Definition: retypes.h:9
#define TRACE(s)
Definition: solgame.cpp:4
#define PAGE_SIZE
Definition: env_spec_w32.h:49
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define min(a, b)
Definition: monoChain.cc:55

Referenced by MmInitializeMemoryManager().

◆ MmFreeMemory()

VOID MmFreeMemory ( PVOID  MemoryPointer)

Definition at line 215 of file mm.c.

216 {
217 }

Referenced by FrLdrAddPageMapping(), RamDiskInitialize(), RamDiskLoadVirtualFile(), and VideoAllocateOffScreenBuffer().

◆ MmGetAddressablePageCountIncludingHoles()

PFN_NUMBER MmGetAddressablePageCountIncludingHoles ( VOID  )

Definition at line 374 of file meminit.c.

375 {
377  PFN_NUMBER PageCount;
378 
379  //
380  // Go through the whole memory map to get max address
381  //
383  {
384  //
385  // Check if we got a higher end page address
386  //
387  if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount > MmHighestPhysicalPage)
388  {
389  //
390  // Yes, remember it if this is real memory
391  //
392  if (MemoryDescriptor->MemoryType == LoaderFree)
394  }
395 
396  //
397  // Check if we got a higher (usable) start page address
398  //
399  if (MemoryDescriptor->BasePage < MmLowestPhysicalPage)
400  {
401  //
402  // Yes, remember it if this is real memory
403  //
405  }
406  }
407 
408  TRACE("lo/hi %lx %lx\n", MmLowestPhysicalPage, MmHighestPhysicalPage);
410  TRACE("MmGetAddressablePageCountIncludingHoles() returning 0x%x\n", PageCount);
411  return PageCount;
412 }
_Out_ PNDIS_BUFFER _In_ NDIS_HANDLE _In_ PVOID MemoryDescriptor
Definition: ndis.h:3270
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:223
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:416
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER MmHighestPhysicalPage
Definition: meminit.c:31

Referenced by MmInitializeMemoryManager().

◆ MmGetMemoryMap()

PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap ( PFN_NUMBER NoEntries)

Definition at line 297 of file mm.c.

298 {
300 
301  *NoEntries = TotalPagesInLookupTable;
302 
303  return RealPageLookupTable;
304 }
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
PVOID PageLookupTableAddress
Definition: meminit.c:26
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Referenced by FrLdrStartup(), and WinLdrSetupMemoryLayout().

◆ MmGetPageNumberFromAddress()

PFN_NUMBER MmGetPageNumberFromAddress ( PVOID  Address)

Definition at line 369 of file meminit.c.

370 {
371  return ((ULONG_PTR)Address) / MM_PAGE_SIZE;
372 }
uint32_t ULONG_PTR
Definition: typedefs.h:63
static WCHAR Address[46]
Definition: ping.c:68

Referenced by MmAllocateMemoryAtAddress(), MmAreMemoryPagesAvailable(), MmInitPageLookupTable(), and MmSetMemoryType().

◆ MmInitializeHeap()

VOID MmInitializeHeap ( PVOID  PageLookupTable)

Definition at line 534 of file heap.c.

535 {
536  TRACE("MmInitializeHeap()\n");
537 
538  /* Create the default heap */
541 
542  /* Create a temporary heap */
545 
546  TRACE("MmInitializeHeap() done, default heap %p, temp heap %p\n",
548 }
#define TEMP_HEAP_SIZE
Definition: mm.h:123
#define DEFAULT_HEAP_SIZE
Definition: mm.h:122
#define TRACE(s)
Definition: solgame.cpp:4
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
PVOID FrLdrTempHeap
Definition: heap.c:35
PVOID FrLdrHeapCreate(SIZE_T MaximumSize, TYPE_OF_MEMORY MemoryType)
Definition: heap.c:66
PVOID FrLdrDefaultHeap
Definition: heap.c:34

Referenced by MmInitializeMemoryManager().

◆ MmInitializeMemoryManager()

BOOLEAN MmInitializeMemoryManager ( VOID  )

Definition at line 313 of file meminit.c.

314 {
315 #if DBG
317 #endif
318 
319  TRACE("Initializing Memory Manager.\n");
320 
321  /* Check the freeldr binary */
323 
325 
326 #if DBG
327  // Dump the system memory map
328  TRACE("System Memory Map (Base Address, Length, Type):\n");
330  {
331  TRACE("%x\t %x\t %s\n",
332  MemoryDescriptor->BasePage * MM_PAGE_SIZE,
333  MemoryDescriptor->PageCount * MM_PAGE_SIZE,
334  MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
335  }
336 #endif
337 
338  // Find address for the page lookup table
342 
343  if (PageLookupTableAddress == 0)
344  {
345  // If we get here then we probably couldn't
346  // find a contiguous chunk of memory big
347  // enough to hold the page lookup table
348  printf("Error initializing memory manager!\n");
349  return FALSE;
350  }
351 
352  // Initialize the page lookup table
354 
356 
359 
361 
362  TRACE("Memory Manager initialized. 0x%x pages available.\n", FreePagesInLookupTable);
363 
364 
365  return TRUE;
366 }
PVOID MmFindLocationForPageLookupTable(PFN_NUMBER TotalPageCount)
Definition: meminit.c:414
PFREELDR_MEMORY_DESCRIPTOR(* GetMemoryMap)(PULONG MaxMemoryMapSize)
Definition: machine.h:63
#define TRUE
Definition: types.h:120
PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:547
_Out_ PNDIS_BUFFER _In_ NDIS_HANDLE _In_ PVOID MemoryDescriptor
Definition: ndis.h:3270
PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID)
Definition: meminit.c:374
PFREELDR_MEMORY_DESCRIPTOR BiosMemoryMap
Definition: meminit.c:33
ULONG BiosMemoryMapEntryCount
Definition: meminit.c:34
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:223
VOID MmInitializeHeap(PVOID PageLookupTable)
Definition: heap.c:534
smooth NULL
Definition: ftsmooth.c:416
VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:657
static VOID MmCheckFreeldrImageFile(VOID)
Definition: meminit.c:239
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
PFN_NUMBER LastFreePageHint
Definition: meminit.c:29
MACHVTBL MachVtbl
Definition: arcemul.c:21
VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:461
PVOID PageLookupTableAddress
Definition: meminit.c:26
PFN_NUMBER MmHighestPhysicalPage
Definition: meminit.c:31
PFN_NUMBER FreePagesInLookupTable
Definition: meminit.c:28
#define printf
Definition: config.h:203

Referenced by BootMain().

◆ MmInitPageLookupTable()

VOID MmInitPageLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 461 of file meminit.c.

462 {
464  PFN_NUMBER PageLookupTableStartPage;
465  PFN_NUMBER PageLookupTablePageCount;
466 
467  TRACE("MmInitPageLookupTable()\n");
468 
469  // Mark every page as allocated initially
470  // We will go through and mark pages again according to the memory map
471  // But this will mark any holes not described in the map as allocated
472  MmMarkPagesInLookupTable(PageLookupTable, MmLowestPhysicalPage, TotalPageCount, LoaderFirmwarePermanent);
473 
474  // Parse the whole memory map
476  {
477  // Mark used pages in the lookup table
478 
479  if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount <= TotalPageCount)
480  {
481  TRACE("Marking pages 0x%lx-0x%lx as type %s\n",
482  MemoryDescriptor->BasePage,
483  MemoryDescriptor->BasePage + MemoryDescriptor->PageCount,
484  MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
485  MmMarkPagesInLookupTable(PageLookupTable,
486  MemoryDescriptor->BasePage,
487  MemoryDescriptor->PageCount,
488  MemoryDescriptor->MemoryType);
489  }
490  else
491  TRACE("Ignoring pages 0x%lx-0x%lx (%s)\n",
492  MemoryDescriptor->BasePage,
493  MemoryDescriptor->BasePage + MemoryDescriptor->PageCount,
494  MmGetSystemMemoryMapTypeString(MemoryDescriptor->MemoryType));
495  }
496 
497  // Mark the pages that the lookup table occupies as reserved
498  PageLookupTableStartPage = MmGetPageNumberFromAddress(PageLookupTable);
499  PageLookupTablePageCount = MmGetPageNumberFromAddress((PVOID)((ULONG_PTR)PageLookupTable + ROUND_UP(TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM), MM_PAGE_SIZE))) - PageLookupTableStartPage;
500  TRACE("Marking the page lookup table pages as reserved StartPage: 0x%x PageCount: 0x%x\n", PageLookupTableStartPage, PageLookupTablePageCount);
501  MmMarkPagesInLookupTable(PageLookupTable, PageLookupTableStartPage, PageLookupTablePageCount, LoaderFirmwareTemporary);
502 }
_Out_ PNDIS_BUFFER _In_ NDIS_HANDLE _In_ PVOID MemoryDescriptor
Definition: ndis.h:3270
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
const FREELDR_MEMORY_DESCRIPTOR * ArcGetMemoryDescriptor(const FREELDR_MEMORY_DESCRIPTOR *Current)
Definition: meminit.c:223
uint32_t ULONG_PTR
Definition: typedefs.h:63
ULONG PFN_NUMBER
Definition: ke.h:8
smooth NULL
Definition: ftsmooth.c:416
#define TRACE(s)
Definition: solgame.cpp:4
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:369
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
Definition: meminit.c:504

Referenced by MmInitializeMemoryManager().

◆ MmMarkPagesInLookupTable()

VOID MmMarkPagesInLookupTable ( PVOID  PageLookupTable,
PFN_NUMBER  StartPage,
PFN_NUMBER  PageCount,
TYPE_OF_MEMORY  PageAllocated 
)

Definition at line 504 of file meminit.c.

505 {
506  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
508  TRACE("MmMarkPagesInLookupTable()\n");
509 
510  /* Validate the range */
512  ((StartPage + PageCount - 1) > MmHighestPhysicalPage))
513  {
514  ERR("Memory (0x%lx:0x%lx) outside of lookup table! Valid range: 0x%lx-0x%lx.\n",
516  return;
517  }
518 
520  for (Index=StartPage; Index<(StartPage+PageCount); Index++)
521  {
522 #if 0
523  if ((Index <= (StartPage + 16)) || (Index >= (StartPage+PageCount-16)))
524  {
525  TRACE("Index = 0x%x StartPage = 0x%x PageCount = 0x%x\n", Index, StartPage, PageCount);
526  }
527 #endif
528  RealPageLookupTable[Index].PageAllocated = PageAllocated;
529  RealPageLookupTable[Index].PageAllocationLength = (PageAllocated != LoaderFree) ? 1 : 0;
530  }
531  TRACE("MmMarkPagesInLookupTable() Done\n");
532 }
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
int WINAPI StartPage(_In_ HDC)
ULONG PFN_NUMBER
Definition: ke.h:8
PFN_NUMBER PageAllocationLength
Definition: mm.h:76
TYPE_OF_MEMORY PageAllocated
Definition: mm.h:75
#define TRACE(s)
Definition: solgame.cpp:4
static const UCHAR Index[8]
Definition: usbohci.c:18
#define ERR(fmt,...)
Definition: debug.h:109
PFN_NUMBER MmHighestPhysicalPage
Definition: meminit.c:31
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Referenced by FrLdrHeapDestroy(), FrLdrHeapRelease(), and MmInitPageLookupTable().

◆ MmSetMemoryType()

VOID MmSetMemoryType ( PVOID  MemoryAddress,
SIZE_T  MemorySize,
TYPE_OF_MEMORY  NewType 
)

Definition at line 144 of file mm.c.

145 {
146  PFN_NUMBER PagesNeeded;
147  PFN_NUMBER StartPageNumber;
148 
149  // Find out how many blocks it will take to
150  // satisfy this allocation
151  PagesNeeded = ROUND_UP(MemorySize, MM_PAGE_SIZE) / MM_PAGE_SIZE;
152 
153  // Get the starting page number
154  StartPageNumber = MmGetPageNumberFromAddress(MemoryAddress);
155 
156  // Set new type for these pages
157  MmAllocatePagesInLookupTable(PageLookupTableAddress, StartPageNumber, PagesNeeded, NewType);
158 }
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
Definition: meminit.c:534
ULONG PFN_NUMBER
Definition: ke.h:8
static CONST DWORD MemorySize[]
Definition: svga.c:32
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:369
PVOID PageLookupTableAddress
Definition: meminit.c:26

Referenced by MempAllocatePageTables().

◆ MmUpdateLastFreePageHint()

VOID MmUpdateLastFreePageHint ( PVOID  PageLookupTable,
PFN_NUMBER  TotalPageCount 
)

Definition at line 657 of file meminit.c.

658 {
659  PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
661 
662  for (Index=TotalPageCount-1; Index>0; Index--)
663  {
664  if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
665  {
667  break;
668  }
669  }
670 }
PFN_NUMBER MmLowestPhysicalPage
Definition: meminit.c:30
ULONG PFN_NUMBER
Definition: ke.h:8
static const UCHAR Index[8]
Definition: usbohci.c:18
PFN_NUMBER LastFreePageHint
Definition: meminit.c:29
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM

Referenced by MmInitializeMemoryManager().

Variable Documentation

◆ __ImageBase

char __ImageBase

Definition at line 17 of file mstscax.cpp.

◆ FreePagesInLookupTable

◆ FrLdrDefaultHeap

◆ FrLdrImageSize

SIZE_T FrLdrImageSize

◆ FrLdrTempHeap

PVOID FrLdrTempHeap

Definition at line 35 of file heap.c.

Referenced by FrLdrHeapCleanupAll(), FrLdrTempAlloc(), FrLdrTempFree(), and MmInitializeHeap().

◆ LastFreePageHint

PFN_NUMBER LastFreePageHint

◆ PageLookupTableAddress

◆ TotalPagesInLookupTable