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:
Brian Gianforcaro 2021-08-13 01:32:48 -07:00 committed by Andreas Kling
parent 27111cfc6c
commit e7fb70b05c
Notes: sideshowbarker 2024-07-18 07:02:20 +09:00
2 changed files with 4 additions and 5 deletions

View file

@ -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)

View file

@ -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);