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:
parent
27111cfc6c
commit
e7fb70b05c
Notes:
sideshowbarker
2024-07-18 07:02:20 +09:00
Author: https://github.com/bgianfo Commit: https://github.com/SerenityOS/serenity/commit/e7fb70b05c1 Pull-request: https://github.com/SerenityOS/serenity/pull/9374
2 changed files with 4 additions and 5 deletions
|
@ -255,9 +255,6 @@ void* kmalloc(size_t size)
|
||||||
}
|
}
|
||||||
|
|
||||||
void* ptr = g_kmalloc_global->m_heap.allocate(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();
|
Thread* current_thread = Thread::current();
|
||||||
if (!current_thread)
|
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* ptrs) noexcept DISALLOW("All deletes in the kernel should have a known size.");
|
||||||
void operator delete[](void* ptr, size_t) noexcept;
|
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>
|
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 > sizeof(ptrdiff_t));
|
||||||
static_assert(ALIGNMENT <= 4096);
|
static_assert(ALIGNMENT <= 4096);
|
||||||
void* ptr = kmalloc(size + ALIGNMENT + sizeof(ptrdiff_t));
|
void* ptr = kmalloc(size + ALIGNMENT + sizeof(ptrdiff_t));
|
||||||
|
if (ptr == nullptr)
|
||||||
|
return ptr;
|
||||||
size_t max_addr = (size_t)ptr + ALIGNMENT;
|
size_t max_addr = (size_t)ptr + ALIGNMENT;
|
||||||
void* aligned_ptr = (void*)(max_addr - (max_addr % ALIGNMENT));
|
void* aligned_ptr = (void*)(max_addr - (max_addr % ALIGNMENT));
|
||||||
((ptrdiff_t*)aligned_ptr)[-1] = (ptrdiff_t)((u8*)aligned_ptr - (u8*)ptr);
|
((ptrdiff_t*)aligned_ptr)[-1] = (ptrdiff_t)((u8*)aligned_ptr - (u8*)ptr);
|
||||||
|
|
Loading…
Add table
Reference in a new issue