From 90cd39b598ed40d2d6e14fb9582ae928b7facdf4 Mon Sep 17 00:00:00 2001 From: Manu Evans Date: Wed, 9 Jan 2019 14:36:49 -0800 Subject: [PATCH] Add D wrapper. --- src/core/stdcpp/new_.d | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/src/core/stdcpp/new_.d b/src/core/stdcpp/new_.d index ff0fd5ed09..a95ce6342a 100644 --- a/src/core/stdcpp/new_.d +++ b/src/core/stdcpp/new_.d @@ -16,9 +16,11 @@ import core.stdcpp.exception : exception; @nogc: + // TODO: this really should come from __traits(getTargetInfo, "defaultNewAlignment") enum size_t __STDCPP_DEFAULT_NEW_ALIGNMENT__ = 16; + extern (C++, "std") { /// @@ -37,6 +39,93 @@ extern (C++, "std") } +// D wrapping API to re-introduce the overloads +pragma(inline, true) +{ + /// D binding for ::operator new + void[] cpp_new(size_t count) + { + return __cpp_new(count)[0 .. count]; + } + + /// D binding for ::operator new + void[] cpp_new(size_t count) nothrow @trusted + { + void* mem = __cpp_new_nothrow(count); + return mem ? mem[0 .. count] : null; + } + + /// D binding for ::operator delete + void cpp_delete(void* ptr) + { + __cpp_delete(ptr); + } + + /// D binding for ::operator delete + void cpp_delete(void* ptr) nothrow + { + __cpp_delete_nothrow(ptr); + } + + /// D binding for ::operator delete + void cpp_delete(void[] mem) + { + static if (__cpp_sized_deallocation) + return __cpp_delete_size(mem.ptr, mem.length); + else + return __cpp_delete(mem.ptr); + } + + /// D binding for ::operator delete + void cpp_delete(void[] mem) nothrow @trusted + { + // TODO: should we call the sized delete and catch instead `if (__cpp_sized_deallocation)`? + __cpp_delete_nothrow(mem.ptr); + } + + static if (__cpp_aligned_new) + { + /// D binding for ::operator new + void[] cpp_new(size_t count, size_t alignment) + { + return __cpp_new_aligned(count, cast(align_val_t)alignment)[0 .. count]; + } + + /// D binding for ::operator new + void[] cpp_new(size_t count, size_t alignment) nothrow @trusted + { + void* mem = __cpp_new_aligned_nothrow(count, cast(align_val_t)alignment); + return mem ? mem[0 .. count] : null; + } + + /// D binding for ::operator delete + void cpp_delete(void* ptr, size_t alignment) + { + __cpp_delete_aligned(ptr, cast(align_val_t)alignment); + } + + /// D binding for ::operator delete + void cpp_delete(void* ptr, size_t alignment) nothrow + { + __cpp_delete_align_nothrow(ptr, cast(align_val_t)alignment); + } + + /// D binding for ::operator delete + void cpp_delete(void[] mem, size_t alignment) + { + __cpp_delete_size_aligned(mem.ptr, mem.length, cast(align_val_t)alignment); + } + + /// D binding for ::operator delete + void cpp_delete(void[] mem, size_t alignment) nothrow @trusted + { + // TODO: should we call the sized delete and catch instead? + __cpp_delete_align_nothrow(mem.ptr, cast(align_val_t)alignment); + } + } +} + + // raw C++ functions extern(C++):