aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-08-26 15:55:09 +0100
committergingerBill <bill@gingerbill.org>2021-08-26 15:55:09 +0100
commit05b9724c85b333b6be9b59f8ab5dc5ee0c1a34fe (patch)
tree0d1971d34f9324f283abb1c25a51186002b59984
parent5053f0179c4c4ba035b93c3148edc1bf0b45c7c6 (diff)
Correct `platform_virtual_memory_init` on Unix
-rw-r--r--src/common_memory.cpp33
1 files 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;