Merge branch 'temp' into 'main'
This commit is contained in:
		| @@ -17,6 +17,7 @@ struct arena { | ||||
|   u8 *buf; | ||||
|   u8 *offset; | ||||
|   u64 capacity; | ||||
|   bool committed; | ||||
| }; | ||||
|  | ||||
| // PUBLIC API | ||||
| @@ -38,7 +39,7 @@ bool wapp_mem_arena_init(Arena **arena, u64 base_capacity, MemAllocFlags flags, | ||||
|                            : ARENA_MINIMUM_CAPACITY; | ||||
|  | ||||
|   arena_ptr->buf = (u8 *)wapp_mem_util_alloc( | ||||
|       arena_capacity, WAPP_MEM_ACCESS_READ_WRITE, flags, | ||||
|       NULL, arena_capacity, WAPP_MEM_ACCESS_READ_WRITE, flags, | ||||
|       zero_buffer ? WAPP_MEM_INIT_INITIALISED : WAPP_MEM_INIT_UNINITIALISED); | ||||
|  | ||||
|   if (!(arena_ptr->buf)) { | ||||
| @@ -48,6 +49,8 @@ bool wapp_mem_arena_init(Arena **arena, u64 base_capacity, MemAllocFlags flags, | ||||
|  | ||||
|   arena_ptr->capacity = arena_capacity; | ||||
|   arena_ptr->offset = arena_ptr->buf; | ||||
|   arena_ptr->committed = | ||||
|       (flags & WAPP_MEM_ALLOC_COMMIT) == WAPP_MEM_ALLOC_COMMIT; | ||||
|  | ||||
|   return true; | ||||
| } | ||||
| @@ -70,6 +73,14 @@ void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) { | ||||
|  | ||||
|   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); | ||||
|  | ||||
|   return (void *)output; | ||||
|   | ||||
| @@ -11,6 +11,18 @@ extern "C" { | ||||
|  | ||||
| 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 zero_buffer); | ||||
| void *wapp_mem_arena_alloc(Arena *arena, u64 size); | ||||
|   | ||||
| @@ -19,7 +19,7 @@ internal const i32 access_types[] = { | ||||
|     [WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PAGE_EXECUTE_READWRITE, | ||||
| }; | ||||
|  | ||||
| internal inline void *alloc_windows(u64 size, MemAccess access, | ||||
| internal inline void *alloc_windows(void *addr, u64 size, MemAccess access, | ||||
|                                     MemAllocFlags flags); | ||||
| #elif defined(WAPP_PLATFORM_POSIX) | ||||
| #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, | ||||
| }; | ||||
|  | ||||
| internal inline void *alloc_posix(u64 size, MemAccess access, | ||||
| internal inline void *alloc_posix(void *addr, u64 size, MemAccess access, | ||||
|                                   MemAllocFlags flags); | ||||
| #else | ||||
| #error "Unrecognised platform" | ||||
| @@ -62,17 +62,17 @@ void *wapp_mem_util_align_forward(void *ptr, u64 alignment) { | ||||
|   return (void *)p; | ||||
| } | ||||
|  | ||||
| void *wapp_mem_util_alloc(u64 size, MemAccess access, MemAllocFlags flags, | ||||
|                           MemInitType type) { | ||||
| void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access, | ||||
|                           MemAllocFlags flags, MemInitType type) { | ||||
| #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(addr, size, access, flags); | ||||
| #elif defined(WAPP_PLATFORM_POSIX) | ||||
|   void *output = alloc_posix(size, access, flags); | ||||
|   void *output = alloc_posix(addr, size, access, flags); | ||||
| #else | ||||
|   return NULL; | ||||
| #endif | ||||
| @@ -93,14 +93,14 @@ void wapp_mem_util_free(void *ptr, u64 size) { | ||||
| } | ||||
|  | ||||
| #ifdef WAPP_PLATFORM_WINDOWS | ||||
| internal inline void *alloc_windows(u64 size, MemAccess access, | ||||
| internal inline void *alloc_windows(void *addr, u64 size, MemAccess access, | ||||
|                                     MemAllocFlags flags) { | ||||
|   return VirtualAlloc2(NULL, NULL, (SIZE_T)size, flags, access_types[access]); | ||||
|   return VirtualAlloc2(addr, NULL, (SIZE_T)size, flags, access_types[access]); | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #if defined(WAPP_PLATFORM_POSIX) | ||||
| internal inline void *alloc_posix(u64 size, MemAccess access, | ||||
| internal inline void *alloc_posix(void *addr, u64 size, MemAccess access, | ||||
|                                   MemAllocFlags flags) { | ||||
|   i32 alloc_flags = flags | MAP_ANON | MAP_PRIVATE; | ||||
|  | ||||
| @@ -109,6 +109,6 @@ internal inline void *alloc_posix(u64 size, MemAccess access, | ||||
|   alloc_flags |= MAP_NORESERVE; | ||||
| #endif | ||||
|  | ||||
|   return mmap(NULL, size, access_types[access], alloc_flags, -1, 0); | ||||
|   return mmap(addr, size, access_types[access], alloc_flags, -1, 0); | ||||
| } | ||||
| #endif | ||||
|   | ||||
| @@ -49,8 +49,8 @@ typedef enum mem_init_type { | ||||
| } MemInitType; | ||||
|  | ||||
| void *wapp_mem_util_align_forward(void *ptr, u64 alignment); | ||||
| void *wapp_mem_util_alloc(u64 size, MemAccess access, MemAllocFlags flags, | ||||
|                           MemInitType type); | ||||
| void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access, | ||||
|                           MemAllocFlags flags, MemInitType type); | ||||
| void wapp_mem_util_free(void *ptr, u64 size); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| #include "test_arena.h" | ||||
| #include "aliases.h" | ||||
| #include "mem_arena.h" | ||||
| #include "mem_utils.h" | ||||
| #include "tester.h" | ||||
| #include <stdbool.h> | ||||
| #include <stdlib.h> | ||||
| @@ -13,8 +12,7 @@ internal u64 count = 20; | ||||
| internal i32 *array = NULL; | ||||
|  | ||||
| TestFuncResult test_arena_init(void) { | ||||
|   bool result = wapp_mem_arena_init(&arena, ARENA_CAPACITY, | ||||
|                                     WAPP_MEM_ALLOC_RESERVE, false); | ||||
|   bool result = wapp_mem_arena_init_default(&arena, ARENA_CAPACITY); | ||||
|  | ||||
|   return wapp_tester_result(result); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user