mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:27:44 +00:00
Kernel: Allow kmalloc_aligned() alignment up to 4096
This allows us to get kmalloc() memory aligned to the VM page size.
This commit is contained in:
parent
a7b6282086
commit
40f2abf7c3
1 changed files with 5 additions and 5 deletions
|
@ -58,18 +58,18 @@ inline void* operator new[](size_t, void* p) { return p; }
|
|||
template<size_t ALIGNMENT>
|
||||
[[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] inline void* kmalloc_aligned(size_t size)
|
||||
{
|
||||
static_assert(ALIGNMENT > 1);
|
||||
static_assert(ALIGNMENT < 255);
|
||||
void* ptr = kmalloc(size + ALIGNMENT + sizeof(u8));
|
||||
static_assert(ALIGNMENT > sizeof(ptrdiff_t));
|
||||
static_assert(ALIGNMENT <= 4096);
|
||||
void* ptr = kmalloc(size + ALIGNMENT + sizeof(ptrdiff_t));
|
||||
size_t max_addr = (size_t)ptr + ALIGNMENT;
|
||||
void* aligned_ptr = (void*)(max_addr - (max_addr % ALIGNMENT));
|
||||
((u8*)aligned_ptr)[-1] = (u8)((u8*)aligned_ptr - (u8*)ptr);
|
||||
((ptrdiff_t*)aligned_ptr)[-1] = (ptrdiff_t)((u8*)aligned_ptr - (u8*)ptr);
|
||||
return aligned_ptr;
|
||||
}
|
||||
|
||||
inline void kfree_aligned(void* ptr)
|
||||
{
|
||||
kfree((u8*)ptr - ((u8*)ptr)[-1]);
|
||||
kfree((u8*)ptr - ((const ptrdiff_t*)ptr)[-1]);
|
||||
}
|
||||
|
||||
void kmalloc_enable_expand();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue