From bc462433ede3778287b584a932b3923793bcc733 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Mon, 10 Jan 2022 02:51:39 +0100 Subject: [PATCH] Implement operator new/delete --- new.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ new.hpp | 18 ++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 new.cpp create mode 100644 new.hpp diff --git a/new.cpp b/new.cpp new file mode 100644 index 0000000..a070e4d --- /dev/null +++ b/new.cpp @@ -0,0 +1,42 @@ +#include "new.hpp" + +#include + +[[nodiscard]] void *operator new(size_t size) +{ + return malloc(size); +} + +void operator delete(void *ptr) noexcept +{ + free(ptr); +} +void operator delete(void *ptr, size_t) noexcept +{ + free(ptr); +} + +[[nodiscard]] void *operator new[](size_t size) +{ + return malloc(size); +} + +void operator delete[](void *ptr) noexcept +{ + free(ptr); +} +void operator delete[](void *ptr, size_t) noexcept +{ + free(ptr); +} + +[[nodiscard]] void *operator new(size_t, void *ptr) noexcept +{ + return ptr; +} +[[nodiscard]] void *operator new[](size_t, void *ptr) noexcept +{ + return ptr; +} +void operator delete(void *, void *)noexcept {} +void operator delete[](void *, void *) noexcept {} diff --git a/new.hpp b/new.hpp new file mode 100644 index 0000000..06c3e59 --- /dev/null +++ b/new.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +[[nodiscard]] void *operator new(size_t size); + +void operator delete(void *ptr) noexcept; +void operator delete(void *ptr, size_t size) noexcept; + +[[nodiscard]] void *operator new[](size_t size); + +void operator delete[](void *ptr) noexcept; +void operator delete[](void *ptr, size_t size) noexcept; + +[[nodiscard]] void *operator new(size_t size, void *ptr) noexcept; +[[nodiscard]] void *operator new[](size_t size, void *ptr) noexcept; +void operator delete(void *ptr, void *)noexcept; +void operator delete[](void *ptr, void *) noexcept;