Kernel: kmalloc requires alignment to be power of two

This commit is contained in:
Bananymous 2023-03-02 22:31:13 +02:00
parent 81cb342c12
commit f1f5e76636
1 changed files with 8 additions and 7 deletions

View File

@ -298,6 +298,13 @@ void* kmalloc(size_t size)
return res; return res;
} }
static bool is_power_of_two(size_t value)
{
if (value == 0)
return false;
return (value & (value - 1)) == 0;
}
void* kmalloc(size_t size, size_t align) void* kmalloc(size_t size, size_t align)
{ {
kmalloc_info& info = s_kmalloc_info; kmalloc_info& info = s_kmalloc_info;
@ -306,13 +313,7 @@ void* kmalloc(size_t size, size_t align)
return nullptr; return nullptr;
ASSERT(align); ASSERT(align);
ASSERT(is_power_of_two(align));
if (align % s_kmalloc_min_align)
{
size_t new_align = BAN::Math::lcm(align, s_kmalloc_min_align);
dwarnln("Asked to align to {}, aliging to {} instead", align, new_align);
align = new_align;
}
// if the size fits into fixed node, we will try to use that since it is faster // if the size fits into fixed node, we will try to use that since it is faster
if (align == s_kmalloc_min_align && size < sizeof(kmalloc_fixed_info::node::data)) if (align == s_kmalloc_min_align && size < sizeof(kmalloc_fixed_info::node::data))