25#define FREELDR_HEAP_VERIFIER
27#define REDZONE_MARK 0xCCCCCCCCCCCCCCCCULL
28#define REDZONE_ALLOCATION 24
29#define REDZONE_LOW_OFFSET 16
30#define REDZONE_SIZE(Block) ((ULONG64*)Block->Data)
31#define REDZONE_LOW(Block) ((ULONG64*)Block->Data + 1)
32#define REDZONE_HI(Block) ((ULONG64*)((PUCHAR)Block->Data + 16 + *REDZONE_SIZE(Block)))
75 TRACE(
"HeapCreate(MemoryType=%ld)\n", MemoryType);
82 ERR(
"HEAP: Failed to allocate heap of size 0x%lx, Type %lu\n",
97 TRACE(
"Remaining = %ld\n", Remaining);
106 while (Remaining > 1)
116 Remaining -= (Block->
Size + 1);
119 PreviousSize = Block->
Size;
120 Block = Block + Block->
Size + 1;
122 TRACE(
"Remaining = %ld\n", Remaining);
141 PHEAP Heap = HeapHandle;
155#ifdef FREELDR_HEAP_VERIFIER
160 PHEAP Heap = HeapHandle;
164 for (Block = &Heap->
Blocks;
166 Block = Block + 1 + Block->
Size)
185 PHEAP Heap = HeapHandle;
187 PUCHAR StartAddress, EndAddress;
190 TRACE(
"HeapRelease(%p)\n", HeapHandle);
193 for (Block = &Heap->
Blocks;
195 Block = Block + 1 + Block->
Size)
200#ifdef FREELDR_HEAP_VERIFIER
213 while (Block->
Tag == 0) Block = Block + Block->
Size + 1;
216 if (Block->
Size == 0)
228 if (EndAddress > StartAddress)
231 FreePages = (
PFN_COUNT)((EndAddress - StartAddress) / MM_PAGE_SIZE);
232 AllFreePages += FreePages;
242 if (Block->
Size == 0)
break;
245 TRACE(
"HeapRelease() done, freed %lu of %lu pages\n", AllFreePages, Heap->
MaximumSize / MM_PAGE_SIZE);
255 TRACE(
"Heap statistics for default heap:\n"
256 "CurrentAlloc=0x%lx, MaxAlloc=0x%lx, LargestAllocation=0x%lx\n"
257 "NumAllocs=%ld, NumFrees=%ld\n",
260 TRACE(
"AllocTime = %I64d, FreeTime = %I64d, sum = %I64d\n",
269 TRACE(
"Heap statistics for temp heap:\n"
270 "CurrentAlloc=0x%lx, MaxAlloc=0x%lx, LargestAllocation=0x%lx\n"
271 "NumAllocs=%ld, NumFrees=%ld\n",
291 ASSERT((Previous->
Tag == 0) || (Previous->
Tag ==
'dnE#'));
310 NextBlock < FreeBlock;
326 PHEAP Heap = HeapHandle;
328 USHORT BlockSize, Remaining;
329#if DBG && !defined(_M_ARM)
333#ifdef FREELDR_HEAP_VERIFIER
344 ERR(
"HEAP: Allocation of 0x%lx bytes too large\n",
ByteSize);
349 if (
Tag == 0)
Tag =
'enoN';
363 if (Block->
Size < BlockSize)
continue;
372 Remaining = Block->
Size - BlockSize;
378 Block->
Size = BlockSize;
381 NextBlock = Block + 1 + BlockSize;
385 NextBlock->
Size = Remaining - 1;
387 BlockSize = NextBlock->
Size;
391 NextBlock = NextBlock + 1 + BlockSize;
396 BlockSize = Block->
Size;
399 NextBlock = Block + 1 + BlockSize;
411#if DBG && !defined(_M_ARM)
414 TRACE(
"HeapAllocate(%p, %ld, %.4s) -> return %p\n",
420#ifdef FREELDR_HEAP_VERIFIER
434 WARN(
"HEAP: nothing suitable found for 0x%lx bytes\n",
ByteSize);
444 PHEAP Heap = HeapHandle;
446#if DBG && !defined(_M_ARM)
450 TRACE(
"HeapFree(%p, %p)\n", HeapHandle, Pointer);
453#ifdef FREELDR_HEAP_VERIFIER
459 if ((Pointer < (
PVOID)(Heap + 1)) ||
462 ERR(
"HEAP: trying to free %p outside of heap %p\n", Pointer, Heap);
467#ifdef FREELDR_HEAP_VERIFIER
479 ERR(
"HEAP: Bad tag! Pointer=%p: block tag '%.4s', requested '%.4s', size=0x%lx\n",
498 NextBlock = Block + Block->
Size + 1;
501 if ((NextBlock->
Tag == 0) &&
508 NextBlock = Block + Block->
Size + 1;
527#if DBG && !defined(_M_ARM)
538 TRACE(
"MmInitializeHeap()\n");
548 TRACE(
"MmInitializeHeap() done, default heap %p, temp heap %p\n",
#define ALIGN_UP_BY(size, align)
#define DBG_DEFAULT_CHANNEL(ch)
PVOID PageLookupTableAddress
PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
#define DEFAULT_HEAP_SIZE
struct _HEAP_BLOCK HEAP_BLOCK
VOID FrLdrHeapDestroy(PVOID HeapHandle)
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
#define REDZONE_SIZE(Block)
#define REDZONE_ALLOCATION
VOID FrLdrHeapFreeEx(PVOID HeapHandle, PVOID Pointer, ULONG Tag)
static VOID FrLdrHeapInsertFreeList(PHEAP Heap, PHEAP_BLOCK FreeBlock)
#define REDZONE_LOW(Block)
struct _BLOCK_DATA BLOCK_DATA
struct _HEAP_BLOCK * PHEAP_BLOCK
static VOID FrLdrHeapRemoveFreeList(PHEAP Heap, PHEAP_BLOCK Block)
PVOID FrLdrHeapAllocateEx(PVOID HeapHandle, SIZE_T ByteSize, ULONG Tag)
VOID MmInitializeHeap(PVOID PageLookupTable)
VOID FrLdrHeapRelease(PVOID HeapHandle)
PVOID FrLdrHeapCreate(SIZE_T MaximumSize, TYPE_OF_MEMORY MemoryType)
#define REDZONE_LOW_OFFSET
VOID FrLdrHeapVerify(PVOID HeapHandle)
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
VOID FrLdrHeapCleanupAll(VOID)
struct _BLOCK_DATA * PBLOCK_DATA
#define REDZONE_HI(Block)
#define ExAllocatePoolWithTag(hernya, size, tag)
#define ExAllocatePool(type, size)
#define RtlFillMemory(Dest, Length, Fill)
PPC_QUAL unsigned long long __rdtsc(void)
#define ExFreePoolWithTag(_P, _T)
static PLARGE_INTEGER Time
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
@ LoaderFirmwareTemporary
enum _TYPE_OF_MEMORY TYPE_OF_MEMORY
ULONG_PTR TerminatingBlock
#define RtlZeroMemory(Destination, Length)
#define ALIGN_UP_POINTER_BY(ptr, align)
#define ALIGN_DOWN_POINTER_BY(ptr, align)
_Must_inspect_result_ _In_ WDFDEVICE _In_ BOOLEAN _In_opt_ PVOID Tag
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ _Strict_type_match_ POOL_TYPE PoolType
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
_Must_inspect_result_ _In_ ULONG Flags
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS _Inout_ PLARGE_INTEGER NumberOfBytes
_IRQL_requires_same_ _In_ CLONG ByteSize