From c504b5dc311edc3b3bd4cf63c7ae56f7f5fd848a Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Sat, 23 Oct 2021 18:36:31 +0800 Subject: [PATCH] memory: add all kinds of C++ new/delete operators --- memory/memory.hpp | 20 +++++++++++++++++++- memory/memory_cpp.cpp | 34 ++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/memory/memory.hpp b/memory/memory.hpp index f377e5a..de5fb5b 100644 --- a/memory/memory.hpp +++ b/memory/memory.hpp @@ -57,4 +57,22 @@ bool operator!=(const kAllocator &, const kAllocator &) { // operators new and delete only call kMalloc/kFree, so C++ code // must stay after paging setup void *operator new(std::size_t size); -void operator delete(void *ptr) noexcept; +void *operator new[](std::size_t size); + +#if __cplusplus >= 201703L +void *operator new(std::size_t size, std::align_val_t align); +void *operator new[](std::size_t size, std::align_val_t align); +#endif + +void operator delete(void *ptr) noexcept; +void operator delete[](void *ptr) noexcept; +#if __cplusplus >= 201402L +void operator delete(void *ptr, std::size_t size) noexcept; +void operator delete[](void *ptr, std::size_t size) noexcept; +#endif +#if __cplusplus >= 201703L +void operator delete(void *ptr, std::align_val_t align) noexcept; +void operator delete[](void *ptr, std::align_val_t align) noexcept; +void operator delete(void *ptr, std::size_t size, std::align_val_t align) noexcept; +void operator delete[](void *ptr, std::size_t size, std::align_val_t align) noexcept; +#endif diff --git a/memory/memory_cpp.cpp b/memory/memory_cpp.cpp index b35f4fa..4463c70 100644 --- a/memory/memory_cpp.cpp +++ b/memory/memory_cpp.cpp @@ -1,13 +1,27 @@ +#include "memory.h" #include "memory.hpp" -void *operator new(std::size_t size) { - void *data = kMalloc(size); - /*if (!data) { - throw helos::bad_alloc(); - }*/ - return data; -} +#include "../extlib/dlmalloc/malloc-2.8.6.h" -void operator delete(void *ptr) noexcept { - kFree(ptr); -} +void *operator new(std::size_t size) { return kMalloc(size); } +void *operator new[](std::size_t size) { return kMalloc(size); } + +void operator delete(void *ptr) noexcept { kFree(ptr); } +void operator delete[](void *ptr) noexcept { kFree(ptr); } + + +#if __cplusplus >= 201703L +void *operator new(std::size_t size, std::align_val_t align) { return dlmemalign((size_t)align, size); } +void *operator new[](std::size_t size, std::align_val_t align) { return dlmemalign((size_t)align, size); } +#endif + +#if __cplusplus >= 201402L +void operator delete(void *ptr, std::size_t size) noexcept { kFree(ptr); } +void operator delete[](void *ptr, std::size_t size) noexcept { kFree(ptr); } +#endif +#if __cplusplus >= 201703L +void operator delete(void *ptr, std::align_val_t align) noexcept { kFree(ptr); } +void operator delete[](void *ptr, std::align_val_t align) noexcept { kFree(ptr); } +void operator delete(void *ptr, std::size_t size, std::align_val_t align) noexcept { kFree(ptr); } +void operator delete[](void *ptr, std::size_t size, std::align_val_t align) noexcept { kFree(ptr); } +#endif