From d759058b80717cc7a309a40724aebcc0578f5c14 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 16 Dec 2022 18:14:48 +0100 Subject: [PATCH] Introduce std::nothrow Let's make sure we explicitly tell new that we don't want exceptions --- kernel/src/memory/Heap.cpp | 10 ++++++++-- luna/include/luna/Alloc.h | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/kernel/src/memory/Heap.cpp b/kernel/src/memory/Heap.cpp index 10f558aa..5cf176e8 100644 --- a/kernel/src/memory/Heap.cpp +++ b/kernel/src/memory/Heap.cpp @@ -5,12 +5,18 @@ #include "memory/KernelVM.h" #include "memory/MemoryManager.h" #include +#include #include #include #include #include #include +namespace std +{ + const std::nothrow_t nothrow; +} + static constexpr int BLOCK_USED = 1 << 0; static constexpr int BLOCK_START_MEM = 1 << 1; static constexpr int BLOCK_END_MEM = 1 << 2; @@ -354,12 +360,12 @@ void dump_heap_usage() kdbgln("-- Heap memory in use by the kernel: %zu bytes", alloc_used); } -void* operator new(usize size) noexcept +void* operator new(usize size, const std::nothrow_t&) noexcept { return kmalloc(size).value_or(nullptr); } -void* operator new[](usize size) noexcept +void* operator new[](usize size, const std::nothrow_t&) noexcept { return kmalloc(size).value_or(nullptr); } diff --git a/luna/include/luna/Alloc.h b/luna/include/luna/Alloc.h index d76b7442..562ac483 100644 --- a/luna/include/luna/Alloc.h +++ b/luna/include/luna/Alloc.h @@ -1,19 +1,36 @@ #pragma once #include +namespace std +{ + struct nothrow_t + { + explicit nothrow_t() = default; + }; + + extern const nothrow_t nothrow; + + enum class align_val_t : usize + { + }; +}; + [[nodiscard]] void* raw_malloc(usize); void raw_free(void*); +void* operator new(usize size, const std::nothrow_t&) noexcept; +void* operator new[](usize size, const std::nothrow_t&) noexcept; + template [[nodiscard]] Result make(Args... args) { - T* const result = new T(args...); + T* const result = new (std::nothrow) T(args...); if (!result) return err(ENOMEM); return result; } template [[nodiscard]] Result make_array(usize count) { - T* const result = new T[count]; + T* const result = new (std::nothrow) T[count]; if (!result) return err(ENOMEM); return result; }