// vim:fileencoding=utf-8:foldmethod=marker

#include "mem_utils.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/assert/assert.h"
#include "../../../common/misc/misc_utils.h"
#include <stddef.h>

void *wpMemUtilAlignForward(void *ptr, u64 alignment) {
	wpDebugAssert(ptr != NULL, "`ptr` should not be NULL");
	wpRuntimeAssert(wpMiscUtilsIsPowerOfTwo(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;
}
