public static void Init() { pageStates = new byte[((Multiboot.GetTotalMemory() * 1024 * 1024) / 4096) / 8]; for (uint i = 0; i < Multiboot.GetTotalMemory() * 1024 * 1024; i += 4096) { if (i <= Heap.heapPointer) { Set(i); } else { Clear(i); } } }
public static void Init() { directory = (DirectoryHeader *)PageManager.GetFreePage(totalPagesForDirectory); directory->location = (uint)directory; entryCount = (uint)((4096 * totalPagesForDirectory) - sizeof(DirectoryHeader)); for (int i = 0; i < entryCount; i++) { var entry = directory->GetEntry(i); entry->used = false; } Console.Write("Total System Memory "); Console.Write((int)Multiboot.GetTotalMemory()); Console.NewLine(); Console.Write("Useable Memory "); Console.Write(Multiboot.GetTotalMemory() - (heapPointer / 1024 / 1024)); Console.NewLine(); }
public static uint GetFreePage(int count) { uint startedAt = currentPage; bool skip = true; while (true) { for (; currentPage < Multiboot.GetTotalMemory() * 1024 * 1024; currentPage += 4096) { if (startedAt == currentPage) { if (skip) { skip = false; } else { return(0); } } if (!IsSet(currentPage)) { int f = 0; for (f = 0; f < count; f++) { if (IsSet(currentPage + (f * 4096))) { break; } } if (f >= count) { for (f = 0; f < count; f++) { Set(currentPage + (f * 4096)); } Utils.memset((byte *)(currentPage), 0, (uint)(count * 4096)); return(currentPage); } } } currentPage = 0; } }
private static uint internalAllocation(uint size, bool allign) { totalAllocations++; tryAgain :; uint returnable = 0; if (Settings.HeapUseBasic || entryCount == 0) { if (heapPointer == 0) { heapPointer = Utils.allign4K(Multiboot.GetEndOfKernel()) + 4096; } if (allign) { heapPointer = Utils.allign4K(heapPointer) + 4096; } uint tmp = heapPointer; heapPointer += size; returnable = tmp; goto allocationEnd; } mutex.Lock(); if (currentEntry >= entryCount) { currentEntry = 0; } var entry = directory->GetEntry(currentEntry); if (entry->used) { currentEntry++; goto tryAgain; } if (!allign && size < 4096) { if (entry->size <= size && entry->location != 0) { entry->used = true; returnable = entry->location; goto allocationEnd; } if (size > pageSpace) { pageSpace = 4096; heapPointer = PageManager.GetFreePage(); if (heapPointer == 0) { // need to to a GC scan and free some pages Console.WriteLine("RAN OUT OF RAM"); ACPI.Shutdown(); while (true) { } } } pageSpace -= size; entry->size = size; entry->location = heapPointer; heapPointer += size; entry->used = true; returnable = entry->location; goto allocationEnd; } else { int num = (int)(size / 4096); int remainder = (int)(size % 4096); if (remainder > 0) { num++; } entry->used = true; entry->location = PageManager.GetFreePage(num); entry->size = (uint)num * 4096; returnable = entry->location; goto allocationEnd; } allocationEnd :; Utils.memset((byte *)returnable, 0, size); mutex.Unlock(); return(returnable); }