28 lines
782 B
C
28 lines
782 B
C
// vim:fileencoding=utf-8:foldmethod=marker
|
|
|
|
#include "mem_utils.h"
|
|
#include "../../../common/aliases/aliases.h"
|
|
#include "../../../common/assert/assert.h"
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
|
|
internal bool is_power_of_two(u64 num) { return (num & (num - 1)) == 0; }
|
|
|
|
void *wapp_mem_util_align_forward(void *ptr, u64 alignment) {
|
|
wapp_debug_assert(ptr != NULL, "`ptr` should not be NULL");
|
|
wapp_runtime_assert(is_power_of_two(alignment), "`alignment` value is not a power of two");
|
|
|
|
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;
|
|
}
|