Reorganise memory utilities

This commit is contained in:
Abdelrahman Said 2025-02-24 00:21:14 +00:00
parent 6119cf5c5f
commit 4520f2269d
17 changed files with 104 additions and 82 deletions

View File

@ -1,7 +1,7 @@
#include "mem_arena.h"
#include "aliases.h"
#include "misc_utils.h"
#include "mem_utils.h"
#include "mem_os.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

View File

@ -2,7 +2,7 @@
#define MEM_ARENA_H
#include "aliases.h"
#include "mem_utils.h"
#include "mem_os.h"
#include <stdbool.h>
#ifdef __cplusplus

View File

@ -2,7 +2,7 @@
#define MEM_ARENA_ALLOCATOR_H
#include "aliases.h"
#include "mem_utils.h"
#include "mem_os.h"
#include "mem_allocator.h"
#include <stdbool.h>

View File

@ -0,0 +1,28 @@
#include "aliases.h"
#include "mem_utils.h"
#include <stdbool.h>
#include <stddef.h>
#include <assert.h>
internal bool is_power_of_two(u64 num) { return (num & (num - 1)) == 0; }
void *wapp_mem_util_align_forward(void *ptr, u64 alignment) {
if (!ptr) {
return NULL;
}
assert(is_power_of_two(alignment));
uptr p = (uptr)ptr;
uptr align = (uptr)alignment;
// Similar to p % align, but it's a faster implementation that works fine
// because align is guaranteed to be a power of 2
uptr modulo = p & (align - 1);
if (modulo != 0) {
p += align - modulo;
}
return (void *)p;
}

View File

@ -0,0 +1,16 @@
#ifndef MEM_UTILS_H
#define MEM_UTILS_H
#include "aliases.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // __cplusplus
void *wapp_mem_util_align_forward(void *ptr, u64 alignment);
#ifdef __cplusplus
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_UTILS_H

View File

@ -2,6 +2,7 @@
#define WAPP_CORE_C
#include "wapp_core.h"
#include "mem_utils.c"
#include "mem_arena.c"
#include "mem_arena_allocator.c"
#include "mem_allocator.c"

View File

@ -3,6 +3,7 @@
#include "mem_arena_allocator.h"
#include "mem_arena.h"
#include "mem_utils.h"
#include "mem_allocator.h"
#include "str8.h"
#include "wapp_common.h"

29
src/os/mem/mem_os.c Normal file
View File

@ -0,0 +1,29 @@
#include "mem_os.h"
#include "mem_os_ops.h"
#include "aliases.h"
#include "platform.h"
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#if defined(WAPP_PLATFORM_WINDOWS)
#include "mem_os_win.h"
#elif defined(WAPP_PLATFORM_POSIX)
#include "mem_os_posix.h"
#else
#error "Unrecognised platform"
#endif
void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type) {
void *output = mem_util_allocate(addr, size, access, flags, type);
if (type == WAPP_MEM_INIT_INITIALISED) {
memset(output, 0, size);
}
return output;
}
void wapp_mem_util_free(void *ptr, u64 size) {
mem_util_free(ptr, size);
}

View File

@ -1,5 +1,5 @@
#ifndef MEM_UTILS_H
#define MEM_UTILS_H
#ifndef MEM_OS_H
#define MEM_OS_H
#include "aliases.h"
#include "platform.h"
@ -8,12 +8,12 @@
BEGIN_C_LINKAGE
#endif // __cplusplus
#include "mem_utils_ops.h"
#include "mem_os_ops.h"
#if defined(WAPP_PLATFORM_WINDOWS)
#include "mem_utils_win.h"
#include "mem_os_win.h"
#elif defined(WAPP_PLATFORM_POSIX)
#include "mem_utils_posix.h"
#include "mem_os_posix.h"
#else
#error "Unrecognised platform"
#endif
@ -29,4 +29,4 @@ external void mem_util_free(void *ptr, u64 size);
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_UTILS_H
#endif // !MEM_OS_H

View File

@ -1,5 +1,5 @@
#ifndef MEM_UTILS_OPS_H
#define MEM_UTILS_OPS_H
#ifndef MEM_OS_OPS_H
#define MEM_OS_OPS_H
#ifdef __cplusplus
BEGIN_C_LINKAGE
@ -23,4 +23,4 @@ typedef enum mem_init_type {
END_C_LINKAGE
#endif // __cplusplus
#endif // !MEM_UTILS_OPS_H
#endif // !MEM_OS_OPS_H

View File

@ -1,53 +0,0 @@
#include "mem_utils.h"
#include "mem_utils_ops.h"
#include "aliases.h"
#include "platform.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#if defined(WAPP_PLATFORM_WINDOWS)
#include "mem_utils_win.h"
#elif defined(WAPP_PLATFORM_POSIX)
#include "mem_utils_posix.h"
#else
#error "Unrecognised platform"
#endif
internal bool is_power_of_two(u64 num) { return (num & (num - 1)) == 0; }
void *wapp_mem_util_align_forward(void *ptr, u64 alignment) {
if (!ptr) {
return NULL;
}
assert(is_power_of_two(alignment));
uptr p = (uptr)ptr;
uptr align = (uptr)alignment;
// Similar to p % align, but it's a faster implementation that works fine
// because align is guaranteed to be a power of 2
uptr modulo = p & (align - 1);
if (modulo != 0) {
p += align - modulo;
}
return (void *)p;
}
void *wapp_mem_util_alloc(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type) {
void *output = mem_util_allocate(addr, size, access, flags, type);
if (type == WAPP_MEM_INIT_INITIALISED) {
memset(output, 0, size);
}
return output;
}
void wapp_mem_util_free(void *ptr, u64 size) {
mem_util_free(ptr, size);
}

View File

@ -3,8 +3,8 @@
#ifdef WAPP_PLATFORM_POSIX
#include "mem_utils_ops.h"
#include "mem_utils_posix.h"
#include "mem_os_ops.h"
#include "mem_os_posix.h"
#include <sys/mman.h>
internal const i32 access_types[] = {

View File

@ -1,5 +1,5 @@
#ifndef MEM_UTILS_POSIX_H
#define MEM_UTILS_POSIX_H
#ifndef MEM_OS_POSIX_H
#define MEM_OS_POSIX_H
#include "platform.h"
@ -30,4 +30,4 @@ typedef enum mem_alloc_flags {
END_C_LINKAGE
#endif // !__cplusplus
#endif // !MEM_UTILS_POSIX_H
#endif // !MEM_OS_POSIX_H

View File

@ -3,8 +3,8 @@
#ifdef WAPP_PLATFORM_WINDOWS
#include "mem_utils_ops.h"
#include "mem_utils_win.h"
#include "mem_os_ops.h"
#include "mem_os_win.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

View File

@ -1,5 +1,5 @@
#ifndef MEM_UTILS_WIN_H
#define MEM_UTILS_WIN_H
#ifndef MEM_OS_WIN_H
#define MEM_OS_WIN_H
#include "platform.h"
@ -24,4 +24,4 @@ typedef enum mem_alloc_flags {
END_C_LINKAGE
#endif // !__cplusplus
#endif // !MEM_UTILS_WIN_H
#endif // !MEM_OS_WIN_H

View File

@ -8,9 +8,9 @@
#include "commander_win.c"
#include "commander.c"
#include "commander_posix.c"
#include "mem_utils_win.c"
#include "mem_utils.c"
#include "mem_utils_posix.c"
#include "mem_os_win.c"
#include "mem_os.c"
#include "mem_os_posix.c"
#include "cpath.c"
#include "wapp_core.c"

View File

@ -6,10 +6,10 @@
#include "shell_utils.h"
#include "commander_output.h"
#include "commander.h"
#include "mem_utils_ops.h"
#include "mem_utils_win.h"
#include "mem_utils.h"
#include "mem_utils_posix.h"
#include "mem_os_ops.h"
#include "mem_os_win.h"
#include "mem_os.h"
#include "mem_os_posix.h"
#include "cpath.h"
#include "wapp_common.h"
#include "wapp_core.h"