mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-25 02:40:49 +00:00
Kernel: Allow kmalloc(..) / kmalloc_aligned(..) to return nullptr
Now that we have a significant amount of code paths handling OOM, lets enable kmalloc and friends to actually return nullptr. This way we can start stressing these paths and validating all of they work as expected.
This commit is contained in:
committed by
Andreas Kling
parent
27111cfc6c
commit
e7fb70b05c
@@ -255,9 +255,6 @@ void* kmalloc(size_t size)
|
||||
}
|
||||
|
||||
void* ptr = g_kmalloc_global->m_heap.allocate(size);
|
||||
if (!ptr) {
|
||||
PANIC("kmalloc: Out of memory (requested size: {})", size);
|
||||
}
|
||||
|
||||
Thread* current_thread = Thread::current();
|
||||
if (!current_thread)
|
||||
|
||||
@@ -75,14 +75,16 @@ void operator delete(void* ptr, size_t, std::align_val_t) noexcept;
|
||||
void operator delete[](void* ptrs) noexcept DISALLOW("All deletes in the kernel should have a known size.");
|
||||
void operator delete[](void* ptr, size_t) noexcept;
|
||||
|
||||
[[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] void* kmalloc(size_t);
|
||||
[[gnu::malloc, gnu::alloc_size(1)]] void* kmalloc(size_t);
|
||||
|
||||
template<size_t ALIGNMENT>
|
||||
[[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] inline void* kmalloc_aligned(size_t size)
|
||||
[[gnu::malloc, gnu::alloc_size(1)]] inline void* kmalloc_aligned(size_t size)
|
||||
{
|
||||
static_assert(ALIGNMENT > sizeof(ptrdiff_t));
|
||||
static_assert(ALIGNMENT <= 4096);
|
||||
void* ptr = kmalloc(size + ALIGNMENT + sizeof(ptrdiff_t));
|
||||
if (ptr == nullptr)
|
||||
return ptr;
|
||||
size_t max_addr = (size_t)ptr + ALIGNMENT;
|
||||
void* aligned_ptr = (void*)(max_addr - (max_addr % ALIGNMENT));
|
||||
((ptrdiff_t*)aligned_ptr)[-1] = (ptrdiff_t)((u8*)aligned_ptr - (u8*)ptr);
|
||||
|
||||
Reference in New Issue
Block a user