1#include <kassert.h>
2#include <math/align.h>
3#include <mem/alloc.h>
4#include <stddef.h>
5
6void *kmalloc_aligned_internal(size_t size, size_t align, enum alloc_flags f,
7 enum alloc_behavior b) {
8 uintptr_t raw = (uintptr_t) kmalloc(size + align + sizeof(uintptr_t), f, b);
9 if (!raw)
10 return NULL;
11
12 uintptr_t aligned = ALIGN_UP(raw + sizeof(uintptr_t), align);
13 ((uintptr_t *) aligned)[-1] = raw;
14
15 kassert(IS_ALIGNED(aligned, align));
16 return (void *) aligned;
17}
18
19void kfree_aligned_internal(void *ptr, enum alloc_behavior b) {
20 if (!ptr)
21 return;
22 uintptr_t raw = ((uintptr_t *) ptr)[-1];
23 kfree((void *) raw, b);
24}
25