From 8434b4a368b85bd99e6a1c791cc16e480ebf9cc9 Mon Sep 17 00:00:00 2001 From: Xphalnos Date: Mon, 6 Oct 2025 15:58:05 +0200 Subject: [PATCH] Windows: Use VirtualAlloc Signed-off-by: Xphalnos --- src/host/memory/arena.cpp | 21 ++++++++++++++++----- src/host/memory/arena_stl.h | 4 ++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/host/memory/arena.cpp b/src/host/memory/arena.cpp index 046ce4f..7c1d8e2 100644 --- a/src/host/memory/arena.cpp +++ b/src/host/memory/arena.cpp @@ -1,7 +1,10 @@ #include "arena.h" #include "common/passert.h" #include -#ifndef WIN32 + +#ifdef WIN32 +#include +#else #include #endif @@ -10,12 +13,15 @@ namespace pound::host::memory arena_t arena_init(size_t capacity) { - // TODO(GloriousTaco:memory): Replace malloc with a windows memory mapping API. #ifdef WIN32 - auto data = static_cast(malloc(sizeof(size_t) * capacity)); + void* const data = ::VirtualAlloc(nullptr, capacity, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + if (NULL == data) + { + return {0, 0, nullptr}; // Return invalid arena on failure + } #else - void* data = ::mmap(nullptr, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (data == MAP_FAILED) + void* const data = ::mmap(nullptr, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (MAP_FAILED == data) { return {0, 0, nullptr}; // Return invalid arena on failure } @@ -51,7 +57,12 @@ void arena_free(memory::arena_t* arena) PVM_ASSERT(arena != nullptr); arena->capacity = 0; arena->size = 0; + // TODO(GloriousTaco:memory): Replace free with a memory safe alternative. +#ifdef WIN32 + VirtualFree(arena->data, 0, MEM_RELEASE); +#else free(arena->data); +#endif } } // namespace pound::host::memory diff --git a/src/host/memory/arena_stl.h b/src/host/memory/arena_stl.h index e8fb59c..d6da5c6 100644 --- a/src/host/memory/arena_stl.h +++ b/src/host/memory/arena_stl.h @@ -59,13 +59,13 @@ struct arena_allocator }; template -inline bool operator==(const arena_allocator& a, const arena_allocator& b) +inline constexpr bool operator==(const arena_allocator& a, const arena_allocator& b) { return a.arena == b.arena; } template -inline bool operator!=(const arena_allocator& a, const arena_allocator& b) +inline constexpr bool operator!=(const arena_allocator& a, const arena_allocator& b) { return a.arena != b.arena; }