Implement operator new/delete

This commit is contained in:
BlackMark 2022-01-10 02:51:39 +01:00
parent 81b3ae244c
commit bc462433ed
2 changed files with 60 additions and 0 deletions

42
new.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "new.hpp"
#include <stdlib.h>
[[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 {}

18
new.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <stddef.h>
[[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;