diff options
author | Brian Gianforcaro <bgianf@serenityos.org> | 2021-08-13 01:32:48 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-13 11:09:25 +0200 |
commit | e7fb70b05c15166ff440e1d6f0bed3b5d555d146 (patch) | |
tree | 086d5fe0682fc8898ef21673f9ddbd643f9a7d0b /Kernel | |
parent | 27111cfc6cc60214269cc26ea75616667f5e9464 (diff) | |
download | serenity-e7fb70b05c15166ff440e1d6f0bed3b5d555d146.zip |
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.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Heap/kmalloc.cpp | 3 | ||||
-rw-r--r-- | Kernel/Heap/kmalloc.h | 6 |
2 files changed, 4 insertions, 5 deletions
diff --git a/Kernel/Heap/kmalloc.cpp b/Kernel/Heap/kmalloc.cpp index 538f791754..8ee983c321 100644 --- a/Kernel/Heap/kmalloc.cpp +++ b/Kernel/Heap/kmalloc.cpp @@ -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) diff --git a/Kernel/Heap/kmalloc.h b/Kernel/Heap/kmalloc.h index f05766ff21..f7af78577b 100644 --- a/Kernel/Heap/kmalloc.h +++ b/Kernel/Heap/kmalloc.h @@ -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); |