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

@@ -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;
}