Compare commits

..

No commits in common. "dbdab01a2cb0e49cfe0633314fa53d07d209064a" and "8d89695fcc620127a452daf9494313bc4ca486b4" have entirely different histories.

5 changed files with 16 additions and 37 deletions

View File

@ -17,7 +17,6 @@ struct arena {
u8 *buf; u8 *buf;
u8 *offset; u8 *offset;
u64 capacity; u64 capacity;
bool committed;
}; };
// PUBLIC API // PUBLIC API
@ -39,7 +38,7 @@ bool wapp_mem_arena_init(Arena **arena, u64 base_capacity, MemAllocFlags flags,
: ARENA_MINIMUM_CAPACITY; : ARENA_MINIMUM_CAPACITY;
arena_ptr->buf = (u8 *)wapp_mem_util_alloc( arena_ptr->buf = (u8 *)wapp_mem_util_alloc(
NULL, arena_capacity, WAPP_MEM_ACCESS_READ_WRITE, flags, arena_capacity, WAPP_MEM_ACCESS_READ_WRITE, flags,
zero_buffer ? WAPP_MEM_INIT_INITIALISED : WAPP_MEM_INIT_UNINITIALISED); zero_buffer ? WAPP_MEM_INIT_INITIALISED : WAPP_MEM_INIT_UNINITIALISED);
if (!(arena_ptr->buf)) { if (!(arena_ptr->buf)) {
@ -49,8 +48,6 @@ bool wapp_mem_arena_init(Arena **arena, u64 base_capacity, MemAllocFlags flags,
arena_ptr->capacity = arena_capacity; arena_ptr->capacity = arena_capacity;
arena_ptr->offset = arena_ptr->buf; arena_ptr->offset = arena_ptr->buf;
arena_ptr->committed =
(flags & WAPP_MEM_ALLOC_COMMIT) == WAPP_MEM_ALLOC_COMMIT;
return true; return true;
} }
@ -73,14 +70,6 @@ void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
arena->offset = output + size; arena->offset = output + size;
#ifdef WAPP_PLATFORM_WINDOWS
if (!(arena->committed)) {
output = (u8 *)wapp_mem_util_alloc(output, size, WAPP_MEM_ACCESS_READ_WRITE,
WAPP_MEM_ALLOC_COMMIT,
WAPP_MEM_INIT_UNINITIALISED);
}
#endif // ifdef WAPP_PLATFORM_WINDOWS
memset(output, 0, size); memset(output, 0, size);
return (void *)output; return (void *)output;

View File

@ -11,18 +11,6 @@ extern "C" {
typedef struct arena Arena; typedef struct arena Arena;
#define wapp_mem_arena_init_default(arena_dptr, base_capacity) \
(wapp_mem_arena_init(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, \
false))
#define wapp_mem_arena_init_commit(arena_dptr, base_capacity) \
(wapp_mem_arena_init(arena_dptr, base_capacity, \
WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
#define wapp_mem_arena_init_zero(arena_dptr, base_capacity) \
(wapp_mem_arena_init(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
#define wapp_mem_arena_init_commit_and_zero(arena_dptr, base_capacity) \
(wapp_mem_arena_init(arena_dptr, base_capacity, \
WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
bool wapp_mem_arena_init(Arena **arena, u64 base_capacity, MemAllocFlags flags, bool wapp_mem_arena_init(Arena **arena, u64 base_capacity, MemAllocFlags flags,
bool zero_buffer); bool zero_buffer);
void *wapp_mem_arena_alloc(Arena *arena, u64 size); void *wapp_mem_arena_alloc(Arena *arena, u64 size);

View File

@ -19,7 +19,7 @@ internal const i32 access_types[] = {
[WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PAGE_EXECUTE_READWRITE, [WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PAGE_EXECUTE_READWRITE,
}; };
internal inline void *alloc_windows(void *addr, u64 size, MemAccess access, internal inline void *alloc_windows(u64 size, MemAccess access,
MemAllocFlags flags); MemAllocFlags flags);
#elif defined(WAPP_PLATFORM_POSIX) #elif defined(WAPP_PLATFORM_POSIX)
#include <sys/mman.h> #include <sys/mman.h>
@ -33,7 +33,7 @@ internal const i32 access_types[] = {
[WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PROT_READ | PROT_WRITE | 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, internal inline void *alloc_posix(u64 size, MemAccess access,
MemAllocFlags flags); MemAllocFlags flags);
#else #else
#error "Unrecognised platform" #error "Unrecognised platform"
@ -62,17 +62,17 @@ 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(u64 size, MemAccess access, MemAllocFlags flags,
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 // Ensure memory is committed if it's meant to be initialised
if (type == WAPP_MEM_INIT_INITIALISED) { if (type == WAPP_MEM_INIT_INITIALISED) {
flags |= WAPP_MEM_ALLOC_COMMIT; flags |= WAPP_MEM_ALLOC_COMMIT;
} }
void *output = alloc_windows(addr, size, access, flags); void *output = alloc_windows(size, access, flags);
#elif defined(WAPP_PLATFORM_POSIX) #elif defined(WAPP_PLATFORM_POSIX)
void *output = alloc_posix(addr, size, access, flags); void *output = alloc_posix(size, access, flags);
#else #else
return NULL; return NULL;
#endif #endif
@ -93,14 +93,14 @@ void wapp_mem_util_free(void *ptr, u64 size) {
} }
#ifdef WAPP_PLATFORM_WINDOWS #ifdef WAPP_PLATFORM_WINDOWS
internal inline void *alloc_windows(void *addr, u64 size, MemAccess access, internal inline void *alloc_windows(u64 size, MemAccess access,
MemAllocFlags flags) { MemAllocFlags flags) {
return VirtualAlloc2(addr, NULL, (SIZE_T)size, flags, access_types[access]); return VirtualAlloc2(NULL, NULL, (SIZE_T)size, flags, access_types[access]);
} }
#endif #endif
#if defined(WAPP_PLATFORM_POSIX) #if defined(WAPP_PLATFORM_POSIX)
internal inline void *alloc_posix(void *addr, u64 size, MemAccess access, 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;
@ -109,6 +109,6 @@ internal inline void *alloc_posix(void *addr, u64 size, MemAccess access,
alloc_flags |= MAP_NORESERVE; alloc_flags |= MAP_NORESERVE;
#endif #endif
return mmap(addr, size, access_types[access], alloc_flags, -1, 0); return mmap(NULL, size, access_types[access], alloc_flags, -1, 0);
} }
#endif #endif

View File

@ -49,8 +49,8 @@ typedef enum mem_init_type {
} MemInitType; } 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(u64 size, MemAccess access, MemAllocFlags flags,
MemAllocFlags flags, MemInitType type); MemInitType type);
void wapp_mem_util_free(void *ptr, u64 size); void wapp_mem_util_free(void *ptr, u64 size);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -1,6 +1,7 @@
#include "test_arena.h" #include "test_arena.h"
#include "aliases.h" #include "aliases.h"
#include "mem_arena.h" #include "mem_arena.h"
#include "mem_utils.h"
#include "tester.h" #include "tester.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
@ -12,7 +13,8 @@ internal u64 count = 20;
internal i32 *array = NULL; internal i32 *array = NULL;
TestFuncResult test_arena_init(void) { TestFuncResult test_arena_init(void) {
bool result = wapp_mem_arena_init_default(&arena, ARENA_CAPACITY); bool result = wapp_mem_arena_init(&arena, ARENA_CAPACITY,
WAPP_MEM_ALLOC_RESERVE, false);
return wapp_tester_result(result); return wapp_tester_result(result);
} }