/*
* FreeLoader
- * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
+ * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <freeldr.h>
#include <mm.h>
+#include "mem.h"
#include <rtl.h>
#include <debug.h>
#include <ui.h>
-//
-// Define this to 1 if you want the entire contents
-// of the memory allocation bitmap displayed
-// when a chunk is allocated or freed
-//
-#define DUMP_MEM_MAP_ON_VERIFY 0
-
-#define MEM_BLOCK_SIZE 256
-
-typedef struct
-{
- BOOL MemBlockAllocated; // Is this block allocated or free
- ULONG BlocksAllocated; // Block length, in multiples of 256 bytes
-} MEMBLOCK, *PMEMBLOCK;
-
-PVOID HeapBaseAddress = NULL;
-ULONG HeapLengthInBytes = 0;
-ULONG HeapMemBlockCount = 0;
-PMEMBLOCK HeapMemBlockArray = NULL;
-
#ifdef DEBUG
ULONG AllocationCount = 0;
VOID IncrementAllocationCount(VOID);
VOID DecrementAllocationCount(VOID);
VOID MemAllocTest(VOID);
-#endif DEBUG
-
-VOID InitMemoryManager(PVOID BaseAddress, ULONG Length)
-{
- ULONG MemBlocks;
-
- // Calculate how many memory blocks we have
- MemBlocks = (Length / MEM_BLOCK_SIZE);
-
- // Adjust the heap length so we can reserve
- // enough storage space for the MEMBLOCK array
- Length -= (MemBlocks * sizeof(MEMBLOCK));
-
- // Initialize our tracking variables
- HeapBaseAddress = BaseAddress;
- HeapLengthInBytes = Length;
- HeapMemBlockCount = (HeapLengthInBytes / MEM_BLOCK_SIZE);
- HeapMemBlockArray = (PMEMBLOCK)(HeapBaseAddress + HeapLengthInBytes);
-
- // Clear the memory
- RtlZeroMemory(HeapBaseAddress, HeapLengthInBytes);
- RtlZeroMemory(HeapMemBlockArray, (HeapMemBlockCount * sizeof(MEMBLOCK)));
-
-#ifdef DEBUG
- DbgPrint((DPRINT_MEMORY, "Memory Manager initialized. BaseAddress = 0x%x Length = 0x%x. %d blocks in heap.\n", BaseAddress, Length, HeapMemBlockCount));
- //MemAllocTest();
-#endif
-}
+#endif // DEBUG
PVOID AllocateMemory(ULONG NumberOfBytes)
{
#ifdef DEBUG
IncrementAllocationCount();
DbgPrint((DPRINT_MEMORY, "Allocated %d bytes (%d blocks) of memory starting at block %d. AllocCount: %d\n", NumberOfBytes, BlocksNeeded, Idx, AllocationCount));
+ DbgPrint((DPRINT_MEMORY, "Memory allocation pointer: 0x%x\n", MemPointer));
VerifyHeap();
-#endif DEBUG
+#endif // DEBUG
// Now return the pointer
return MemPointer;
{
BugCheck((DPRINT_MEMORY, "Bogus memory pointer (0x%x) passed to FreeMemory()\n", MemBlock));
}
-#endif DEBUG
+#endif // DEBUG
// Find out the block number if the first
// block of memory they allocated
DecrementAllocationCount();
DbgPrint((DPRINT_MEMORY, "Freed %d blocks of memory starting at block %d. AllocationCount: %d\n", BlockCount, BlockNumber, AllocationCount));
VerifyHeap();
-#endif DEBUG
+#endif // DEBUG
}
#ifdef DEBUG
{
if ((Idx % 32) == 0)
{
- DbgPrint((DPRINT_MEMORY, "\n%x:\t", (Idx * 256)));
+ DbgPrint((DPRINT_MEMORY, "\n"));
+ DbgPrint((DPRINT_MEMORY, "%x:\t", HeapBaseAddress + (Idx * 256)));
}
else if ((Idx % 4) == 0)
{
printf("MemPtr5: 0x%x\n", (int)MemPtr5);
getch();
}
-#endif DEBUG
+#endif // DEBUG
+
+// Returns the amount of total usuable memory available to the memory manager
+ULONG GetSystemMemorySize(VOID)
+{
+ return HeapLengthInBytes;
+}