Split the platform specific mem_utils implementations
This commit is contained in:
parent
f2f155744a
commit
1d1c3ca928
@ -1,4 +1,5 @@
|
|||||||
#include "mem_utils.h"
|
#include "mem_utils.h"
|
||||||
|
#include "mem_utils_ops.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@ -7,35 +8,9 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#if defined(WAPP_PLATFORM_WINDOWS)
|
#if defined(WAPP_PLATFORM_WINDOWS)
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#include "mem_utils_win.h"
|
||||||
#include <Windows.h>
|
|
||||||
#include <memoryapi.h>
|
|
||||||
|
|
||||||
internal const i32 access_types[] = {
|
|
||||||
[WAPP_MEM_ACCESS_NONE] = PAGE_NOACCESS,
|
|
||||||
[WAPP_MEM_ACCESS_READ_ONLY] = PAGE_READONLY,
|
|
||||||
[WAPP_MEM_ACCESS_EXEC_ONLY] = PAGE_EXECUTE,
|
|
||||||
[WAPP_MEM_ACCESS_READ_WRITE] = PAGE_READWRITE,
|
|
||||||
[WAPP_MEM_ACCESS_READ_EXEC] = PAGE_EXECUTE_READ,
|
|
||||||
[WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PAGE_EXECUTE_READWRITE,
|
|
||||||
};
|
|
||||||
|
|
||||||
internal inline void *alloc_windows(void *addr, u64 size, MemAccess access,
|
|
||||||
MemAllocFlags flags);
|
|
||||||
#elif defined(WAPP_PLATFORM_POSIX)
|
#elif defined(WAPP_PLATFORM_POSIX)
|
||||||
#include <sys/mman.h>
|
#include "mem_utils_posix.h"
|
||||||
|
|
||||||
internal const i32 access_types[] = {
|
|
||||||
[WAPP_MEM_ACCESS_NONE] = PROT_NONE,
|
|
||||||
[WAPP_MEM_ACCESS_READ_ONLY] = PROT_READ,
|
|
||||||
[WAPP_MEM_ACCESS_EXEC_ONLY] = PROT_EXEC,
|
|
||||||
[WAPP_MEM_ACCESS_READ_WRITE] = PROT_READ | PROT_WRITE,
|
|
||||||
[WAPP_MEM_ACCESS_READ_EXEC] = PROT_READ | PROT_EXEC,
|
|
||||||
[WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
||||||
};
|
|
||||||
|
|
||||||
internal inline void *alloc_posix(void *addr, u64 size, MemAccess access,
|
|
||||||
MemAllocFlags flags);
|
|
||||||
#else
|
#else
|
||||||
#error "Unrecognised platform"
|
#error "Unrecognised platform"
|
||||||
#endif
|
#endif
|
||||||
@ -63,53 +38,10 @@ void *wapp_mem_util_align_forward(void *ptr, u64 alignment) {
|
|||||||
return (void *)p;
|
return (void *)p;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access,
|
void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type) {
|
||||||
MemAllocFlags flags, MemInitType type) {
|
return mem_util_allocate(addr, size, access, flags);
|
||||||
#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(addr, size, access, flags);
|
|
||||||
#elif defined(WAPP_PLATFORM_POSIX)
|
|
||||||
void *output = alloc_posix(addr, size, access, flags);
|
|
||||||
#else
|
|
||||||
#error "Unrecognised platform"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (type == WAPP_MEM_INIT_INITIALISED) {
|
|
||||||
memset(output, 0, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_mem_util_free(void *ptr, u64 size) {
|
void wapp_mem_util_free(void *ptr, u64 size) {
|
||||||
#if defined(WAPP_PLATFORM_WINDOWS)
|
mem_util_free(ptr, size);
|
||||||
VirtualFree(ptr, size, MEM_RELEASE);
|
|
||||||
#elif defined(WAPP_PLATFORM_POSIX)
|
|
||||||
munmap(ptr, size);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
|
||||||
internal inline void *alloc_windows(void *addr, u64 size, MemAccess access,
|
|
||||||
MemAllocFlags flags) {
|
|
||||||
return VirtualAlloc(addr, (SIZE_T)size, flags, access_types[access]);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(WAPP_PLATFORM_POSIX)
|
|
||||||
internal inline void *alloc_posix(void *addr, u64 size, MemAccess access,
|
|
||||||
MemAllocFlags flags) {
|
|
||||||
i32 alloc_flags = flags | MAP_ANON | MAP_PRIVATE;
|
|
||||||
|
|
||||||
#if defined(WAPP_PLATFORM_LINUX) || defined(WAPP_PLATFORM_GNU) || \
|
|
||||||
defined(WAPP_PLATFORM_NET_BSD)
|
|
||||||
alloc_flags |= MAP_NORESERVE;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return mmap(addr, size, access_types[access], alloc_flags, -1, 0);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
@ -3,13 +3,12 @@
|
|||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
#include "mem_utils_ops.h"
|
||||||
|
|
||||||
#if defined(WAPP_PLATFORM_WINDOWS)
|
#if defined(WAPP_PLATFORM_WINDOWS)
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#include "mem_utils_win.h"
|
||||||
#include <Windows.h>
|
|
||||||
#include <memoryapi.h>
|
|
||||||
#elif defined(WAPP_PLATFORM_POSIX)
|
#elif defined(WAPP_PLATFORM_POSIX)
|
||||||
#include <sys/mman.h>
|
#include "mem_utils_posix.h"
|
||||||
#else
|
#else
|
||||||
#error "Unrecognised platform"
|
#error "Unrecognised platform"
|
||||||
#endif
|
#endif
|
||||||
@ -18,42 +17,13 @@
|
|||||||
BEGIN_C_LINKAGE
|
BEGIN_C_LINKAGE
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
|
||||||
typedef enum mem_access {
|
|
||||||
WAPP_MEM_ACCESS_NONE,
|
|
||||||
WAPP_MEM_ACCESS_READ_ONLY,
|
|
||||||
WAPP_MEM_ACCESS_EXEC_ONLY,
|
|
||||||
WAPP_MEM_ACCESS_READ_WRITE,
|
|
||||||
WAPP_MEM_ACCESS_READ_EXEC,
|
|
||||||
WAPP_MEM_ACCESS_READ_WRITE_EXEC,
|
|
||||||
} MemAccess;
|
|
||||||
|
|
||||||
typedef enum mem_alloc_flags {
|
|
||||||
#if defined(WAPP_PLATFORM_WINDOWS)
|
|
||||||
WAPP_MEM_ALLOC_RESERVE = MEM_RESERVE,
|
|
||||||
WAPP_MEM_ALLOC_COMMIT = MEM_COMMIT,
|
|
||||||
#elif defined(WAPP_PLATFORM_LINUX) || defined(WAPP_PLATFORM_GNU)
|
|
||||||
WAPP_MEM_ALLOC_RESERVE = 0,
|
|
||||||
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
|
|
||||||
} MemAllocFlags;
|
|
||||||
|
|
||||||
typedef enum mem_init_type {
|
|
||||||
WAPP_MEM_INIT_UNINITIALISED,
|
|
||||||
WAPP_MEM_INIT_INITIALISED,
|
|
||||||
} MemInitType;
|
|
||||||
|
|
||||||
void *wapp_mem_util_align_forward(void *ptr, u64 alignment);
|
void *wapp_mem_util_align_forward(void *ptr, u64 alignment);
|
||||||
void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access,
|
void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type);
|
||||||
MemAllocFlags flags, MemInitType type);
|
|
||||||
void wapp_mem_util_free(void *ptr, u64 size);
|
void wapp_mem_util_free(void *ptr, u64 size);
|
||||||
|
|
||||||
|
external void *mem_util_allocate(void *addr, u64 size, MemAccess access, MemAllocFlags flags);
|
||||||
|
external void mem_util_free(void *ptr, u64 size);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
26
src/common/mem_utils/mem_utils_ops.h
Normal file
26
src/common/mem_utils/mem_utils_ops.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef MEM_UTILS_OPS_H
|
||||||
|
#define MEM_UTILS_OPS_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
BEGIN_C_LINKAGE
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
typedef enum mem_access {
|
||||||
|
WAPP_MEM_ACCESS_NONE,
|
||||||
|
WAPP_MEM_ACCESS_READ_ONLY,
|
||||||
|
WAPP_MEM_ACCESS_EXEC_ONLY,
|
||||||
|
WAPP_MEM_ACCESS_READ_WRITE,
|
||||||
|
WAPP_MEM_ACCESS_READ_EXEC,
|
||||||
|
WAPP_MEM_ACCESS_READ_WRITE_EXEC,
|
||||||
|
} MemAccess;
|
||||||
|
|
||||||
|
typedef enum mem_init_type {
|
||||||
|
WAPP_MEM_INIT_UNINITIALISED,
|
||||||
|
WAPP_MEM_INIT_INITIALISED,
|
||||||
|
} MemInitType;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
END_C_LINKAGE
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !MEM_UTILS_OPS_H
|
33
src/common/mem_utils/posix/mem_utils_posix.c
Normal file
33
src/common/mem_utils/posix/mem_utils_posix.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "aliases.h"
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
|
#ifdef WAPP_PLATFORM_POSIX
|
||||||
|
|
||||||
|
#include "mem_utils_ops.h"
|
||||||
|
#include "mem_utils_posix.h"
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
internal const i32 access_types[] = {
|
||||||
|
[WAPP_MEM_ACCESS_NONE] = PROT_NONE,
|
||||||
|
[WAPP_MEM_ACCESS_READ_ONLY] = PROT_READ,
|
||||||
|
[WAPP_MEM_ACCESS_EXEC_ONLY] = PROT_EXEC,
|
||||||
|
[WAPP_MEM_ACCESS_READ_WRITE] = PROT_READ | PROT_WRITE,
|
||||||
|
[WAPP_MEM_ACCESS_READ_EXEC] = PROT_READ | PROT_EXEC,
|
||||||
|
[WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PROT_READ | PROT_WRITE | PROT_EXEC,
|
||||||
|
};
|
||||||
|
|
||||||
|
void *mem_util_allocate(void *addr, u64 size, MemAccess access, MemAllocFlags flags) {
|
||||||
|
i32 alloc_flags = flags | MAP_ANON | MAP_PRIVATE;
|
||||||
|
|
||||||
|
#if defined(WAPP_PLATFORM_LINUX) || defined(WAPP_PLATFORM_GNU) || defined(WAPP_PLATFORM_NET_BSD)
|
||||||
|
alloc_flags |= MAP_NORESERVE;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return mmap(addr, size, access_types[access], alloc_flags, -1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mem_util_free(void *ptr, u64 size) {
|
||||||
|
munmap(ptr, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !WAPP_PLATFORM_POSIX
|
33
src/common/mem_utils/posix/mem_utils_posix.h
Normal file
33
src/common/mem_utils/posix/mem_utils_posix.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef MEM_UTILS_POSIX_H
|
||||||
|
#define MEM_UTILS_POSIX_H
|
||||||
|
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
BEGIN_C_LINKAGE
|
||||||
|
#endif // !__cplusplus
|
||||||
|
|
||||||
|
#ifdef WAPP_PLATFORM_POSIX
|
||||||
|
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
typedef enum mem_alloc_flags {
|
||||||
|
#if defined(WAPP_PLATFORM_LINUX) || defined(WAPP_PLATFORM_GNU)
|
||||||
|
WAPP_MEM_ALLOC_RESERVE = 0,
|
||||||
|
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
|
||||||
|
} MemAllocFlags;
|
||||||
|
|
||||||
|
#endif // !WAPP_PLATFORM_POSIX
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
END_C_LINKAGE
|
||||||
|
#endif // !__cplusplus
|
||||||
|
|
||||||
|
#endif // !MEM_UTILS_POSIX_H
|
30
src/common/mem_utils/win/mem_utils_win.c
Normal file
30
src/common/mem_utils/win/mem_utils_win.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include "aliases.h"
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
|
|
||||||
|
#include "mem_utils_ops.h"
|
||||||
|
#include "mem_utils_win.h"
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#include <Windows.h>
|
||||||
|
#include <memoryapi.h>
|
||||||
|
|
||||||
|
internal const i32 access_types[] = {
|
||||||
|
[WAPP_MEM_ACCESS_NONE] = PAGE_NOACCESS,
|
||||||
|
[WAPP_MEM_ACCESS_READ_ONLY] = PAGE_READONLY,
|
||||||
|
[WAPP_MEM_ACCESS_EXEC_ONLY] = PAGE_EXECUTE,
|
||||||
|
[WAPP_MEM_ACCESS_READ_WRITE] = PAGE_READWRITE,
|
||||||
|
[WAPP_MEM_ACCESS_READ_EXEC] = PAGE_EXECUTE_READ,
|
||||||
|
[WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PAGE_EXECUTE_READWRITE,
|
||||||
|
};
|
||||||
|
|
||||||
|
void *mem_util_allocate(void *addr, u64 size, MemAccess access, MemAllocFlags flags) {
|
||||||
|
return VirtualAlloc(addr, (SIZE_T)size, flags, access_types[access]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mem_util_free(void *ptr, u64 size) {
|
||||||
|
VirtualFree(ptr, size, MEM_RELEASE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !WAPP_PLATFORM_WINDOWS
|
27
src/common/mem_utils/win/mem_utils_win.h
Normal file
27
src/common/mem_utils/win/mem_utils_win.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef MEM_UTILS_WIN_H
|
||||||
|
#define MEM_UTILS_WIN_H
|
||||||
|
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
BEGIN_C_LINKAGE
|
||||||
|
#endif // !__cplusplus
|
||||||
|
|
||||||
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#include <Windows.h>
|
||||||
|
#include <memoryapi.h>
|
||||||
|
|
||||||
|
typedef enum mem_alloc_flags {
|
||||||
|
WAPP_MEM_ALLOC_RESERVE = MEM_RESERVE,
|
||||||
|
WAPP_MEM_ALLOC_COMMIT = MEM_COMMIT,
|
||||||
|
} MemAllocFlags;
|
||||||
|
|
||||||
|
#endif // !WAPP_PLATFORM_WINDOWS
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
END_C_LINKAGE
|
||||||
|
#endif // !__cplusplus
|
||||||
|
|
||||||
|
#endif // !MEM_UTILS_WIN_H
|
@ -8,7 +8,6 @@
|
|||||||
#define GB(SIZE) (MB(SIZE) * 1024)
|
#define GB(SIZE) (MB(SIZE) * 1024)
|
||||||
#define TB(SIZE) (GB(SIZE) * 1024)
|
#define TB(SIZE) (GB(SIZE) * 1024)
|
||||||
|
|
||||||
#define wapp_misc_utils_padding_size(SIZE) \
|
#define wapp_misc_utils_padding_size(SIZE) u8 reserved_padding[sizeof(void *) - ((SIZE) % sizeof(void *))]
|
||||||
u8 reserved_padding[sizeof(void *) - ((SIZE) % sizeof(void *))]
|
|
||||||
|
|
||||||
#endif // !MISC_UTILS_H
|
#endif // !MISC_UTILS_H
|
||||||
|
Loading…
Reference in New Issue
Block a user