From 05b9724c85b333b6be9b59f8ab5dc5ee0c1a34fe Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 26 Aug 2021 15:55:09 +0100 Subject: [PATCH] Correct `platform_virtual_memory_init` on Unix --- src/common_memory.cpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/common_memory.cpp b/src/common_memory.cpp index 04fa874ca..cda3cc002 100644 --- a/src/common_memory.cpp +++ b/src/common_memory.cpp @@ -133,11 +133,24 @@ struct PlatformMemoryBlock { PlatformMemoryBlock *prev, *next; }; + +gb_global PlatformMemoryBlock global_platform_memory_block_sentinel; + PlatformMemoryBlock *platform_virtual_memory_alloc(isize total_size); void platform_virtual_memory_free(PlatformMemoryBlock *block); void platform_virtual_memory_protect(void *memory, isize size); #if defined(GB_SYSTEM_WINDOWS) + void platform_virtual_memory_init(void) { + global_platform_memory_block_sentinel.prev = &global_platform_memory_block_sentinel; + global_platform_memory_block_sentinel.next = &global_platform_memory_block_sentinel; + + SYSTEM_INFO sys_info = {}; + GetSystemInfo(&sys_info); + DEFAULT_PAGE_SIZE = gb_max(DEFAULT_PAGE_SIZE, cast(isize)sys_info.dwPageSize); + GB_ASSERT(gb_is_power_of_two(DEFAULT_PAGE_SIZE)); + } + PlatformMemoryBlock *platform_virtual_memory_alloc(isize total_size) { PlatformMemoryBlock *pmblock = (PlatformMemoryBlock *)VirtualAlloc(0, total_size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); GB_ASSERT_MSG(pmblock != nullptr, "Out of Virtual Memory, oh no..."); @@ -152,6 +165,14 @@ void platform_virtual_memory_protect(void *memory, isize size); GB_ASSERT(is_protected); } #else + void platform_virtual_memory_init(void) { + global_platform_memory_block_sentinel.prev = &global_platform_memory_block_sentinel; + global_platform_memory_block_sentinel.next = &global_platform_memory_block_sentinel; + + DEFAULT_PAGE_SIZE = gb_max(DEFAULT_PAGE_SIZE, cast(isize)sysconf(_SC_PAGE_SIZE)); + GB_ASSERT(gb_is_power_of_two(DEFAULT_PAGE_SIZE)); + } + PlatformMemoryBlock *platform_virtual_memory_alloc(isize total_size) { PlatformMemoryBlock *pmblock = (PlatformMemoryBlock *)mmap(nullptr, total_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); GB_ASSERT_MSG(pmblock != nullptr, "Out of Virtual Memory, oh no..."); @@ -167,18 +188,6 @@ void platform_virtual_memory_protect(void *memory, isize size); } #endif -gb_global PlatformMemoryBlock global_platform_memory_block_sentinel; - -void platform_virtual_memory_init(void) { - global_platform_memory_block_sentinel.prev = &global_platform_memory_block_sentinel; - global_platform_memory_block_sentinel.next = &global_platform_memory_block_sentinel; - - SYSTEM_INFO sys_info = {}; - GetSystemInfo(&sys_info); - DEFAULT_PAGE_SIZE = gb_max(DEFAULT_PAGE_SIZE, cast(isize)sys_info.dwPageSize); - GB_ASSERT(gb_is_power_of_two(DEFAULT_PAGE_SIZE)); -} - MemoryBlock *virtual_memory_alloc(isize size) { isize const page_size = DEFAULT_PAGE_SIZE;