Reorganise memory utilities

This commit is contained in:
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"