From f380b4b95b59287ac27307e2ee67a0ca85db021e Mon Sep 17 00:00:00 2001 From: R-Goc Date: Thu, 21 Nov 2024 14:15:44 +0100 Subject: [PATCH] AK: Port BumpAllocator.h to Windows Done by forward declaring the required functions and defining the needed constants. The defines shouldn't collide as they are from memoryapi.h. This is done to avoid including windows.h. --- AK/BumpAllocator.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/AK/BumpAllocator.h b/AK/BumpAllocator.h index 21348a4df0c..48f325d766b 100644 --- a/AK/BumpAllocator.h +++ b/AK/BumpAllocator.h @@ -10,7 +10,15 @@ #include #include #include -#include +#if !defined(AK_OS_WINDOWS) +# include +#else +extern "C" __declspec(dllimport) void* __stdcall VirtualAlloc(size_t dwSize, u32 flAllocationType, u32 flProtect); +extern "C" __declspec(dllimport) bool __stdcall VirtualFree(void* lpAddress, size_t dwSize, u32 dwFreeType); +# define MEM_COMMIT 0x00001000 +# define PAGE_READWRITE 0x04 +# define MEM_RELEASE 0x00008000 +#endif namespace AK { @@ -73,7 +81,11 @@ public: } if constexpr (use_mmap) { +#if defined(AK_OS_WINDOWS) + VirtualFree((LPVOID)chunk, m_chunk_size, MEM_RELEASE); +#else munmap((void*)chunk, m_chunk_size); +#endif } else { kfree_sized((void*)chunk, m_chunk_size); } @@ -105,11 +117,18 @@ protected: if constexpr (use_mmap) { #ifdef AK_OS_SERENITY new_chunk = serenity_mmap(nullptr, m_chunk_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_RANDOMIZED | MAP_PRIVATE, 0, 0, m_chunk_size, "BumpAllocator Chunk"); +#elif defined(AK_OS_WINDOWS) + new_chunk = VirtualAlloc(NULL, m_chunk_size, MEM_COMMIT, PAGE_READWRITE); #else new_chunk = mmap(nullptr, m_chunk_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); #endif +#if defined(AK_OS_WINDOWS) + if (new_chunk == NULL) + return false; +#else if (new_chunk == MAP_FAILED) return false; +#endif } else { new_chunk = kmalloc(m_chunk_size); if (!new_chunk)