Set allocation flags per platform

This commit is contained in:
Abdelrahman Said 2024-06-02 20:41:30 +01:00
parent 9807334ac9
commit 57de75c1f8
2 changed files with 15 additions and 2 deletions

View File

@ -65,6 +65,11 @@ void *wapp_mem_util_align_forward(void *ptr, u64 alignment) {
void *wapp_mem_util_alloc(u64 size, MemAccess access, MemAllocFlags flags, void *wapp_mem_util_alloc(u64 size, MemAccess access, MemAllocFlags flags,
MemInitType type) { MemInitType type) {
#if defined(WAPP_PLATFORM_WINDOWS) #if defined(WAPP_PLATFORM_WINDOWS)
// Ensure memory is committed if it's meant to be initialised
if (type == WAPP_MEM_INIT_INITIALISED) {
flags |= WAPP_MEM_ALLOC_COMMIT;
}
void *output = alloc_windows(size, access, flags); void *output = alloc_windows(size, access, flags);
#elif defined(WAPP_PLATFORM_POSIX) #elif defined(WAPP_PLATFORM_POSIX)
void *output = alloc_posix(size, access, flags); void *output = alloc_posix(size, access, flags);
@ -99,7 +104,8 @@ internal inline void *alloc_posix(u64 size, MemAccess access,
MemAllocFlags flags) { MemAllocFlags flags) {
i32 alloc_flags = flags | MAP_ANON | MAP_PRIVATE; i32 alloc_flags = flags | MAP_ANON | MAP_PRIVATE;
#ifdef WAPP_PLATFORM_LINUX #if defined(WAPP_PLATFORM_LINUX) || defined(WAPP_PLATFORM_GNU) || \
defined(WAPP_PLATFORM_NET_BSD)
alloc_flags |= MAP_NORESERVE; alloc_flags |= MAP_NORESERVE;
#endif #endif

View File

@ -30,9 +30,16 @@ typedef enum mem_alloc_flags {
#if defined(WAPP_PLATFORM_WINDOWS) #if defined(WAPP_PLATFORM_WINDOWS)
WAPP_MEM_ALLOC_RESERVE = MEM_RESERVE, WAPP_MEM_ALLOC_RESERVE = MEM_RESERVE,
WAPP_MEM_ALLOC_COMMIT = MEM_COMMIT, WAPP_MEM_ALLOC_COMMIT = MEM_COMMIT,
#elif defined(WAPP_PLATFORM_POSIX) #elif defined(WAPP_PLATFORM_LINUX) || defined(WAPP_PLATFORM_GNU)
WAPP_MEM_ALLOC_RESERVE = 0, WAPP_MEM_ALLOC_RESERVE = 0,
WAPP_MEM_ALLOC_COMMIT = MAP_POPULATE, WAPP_MEM_ALLOC_COMMIT = MAP_POPULATE,
#elif defined(WAPP_PLATFORM_FREE_BSD)
WAPP_MEM_ALLOC_RESERVE = 0,
WAPP_MEM_ALLOC_COMMIT = MAP_PREFAULT_READ,
#elif defined(WAPP_PLATFORM_BSD) || defined(WAPP_PLATFORM_UNIX) || \
defined(WAPP_PLATFORM_APPLE)
WAPP_MEM_ALLOC_RESERVE = 0,
WAPP_MEM_ALLOC_COMMIT = 0,
#endif #endif
} MemAllocFlags; } MemAllocFlags;