Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 94a1d7f

Browse files
committed
Add D wrapper.
Need to make it a template, otherwise druntime wants to link the CRT.
1 parent bf7e5bb commit 94a1d7f

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

src/core/stdcpp/new_.d

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import core.stdcpp.xutility : __cpp_sized_deallocation, __cpp_aligned_new;
1515

1616
@nogc:
1717

18+
1819
// TODO: this really should come from __traits(getTargetInfo, "defaultNewAlignment")
1920
enum size_t __STDCPP_DEFAULT_NEW_ALIGNMENT__ = 16;
2021

22+
2123
extern (C++, "std")
2224
{
2325
///
@@ -28,6 +30,88 @@ extern (C++, "std")
2830
}
2931

3032

33+
/// D binding for ::operator new
34+
void[] cpp_new(_ = void)(size_t count)
35+
{
36+
return __cpp_new(count)[0 .. count];
37+
}
38+
39+
/// D binding for ::operator new
40+
void[] cpp_new(_ = void)(size_t count) nothrow @trusted
41+
{
42+
void* mem = __cpp_new_nothrow(count);
43+
return mem ? mem[0 .. count] : null;
44+
}
45+
46+
/// D binding for ::operator delete
47+
void cpp_delete(_ = void)(void* ptr)
48+
{
49+
__cpp_delete(ptr);
50+
}
51+
52+
/// D binding for ::operator delete
53+
void cpp_delete(_ = void)(void* ptr) nothrow
54+
{
55+
__cpp_delete_nothrow(ptr);
56+
}
57+
58+
/// D binding for ::operator delete
59+
void cpp_delete(_ = void)(void[] mem)
60+
{
61+
static if (__cpp_sized_deallocation)
62+
return __cpp_delete_size(mem.ptr, mem.length);
63+
else
64+
return __cpp_delete(mem.ptr);
65+
}
66+
67+
/// D binding for ::operator delete
68+
void cpp_delete(_ = void)(void[] mem) nothrow @trusted
69+
{
70+
// TODO: should we call the sized delete and catch instead `if (__cpp_sized_deallocation)`?
71+
__cpp_delete_nothrow(mem.ptr);
72+
}
73+
74+
static if (__cpp_aligned_new)
75+
{
76+
/// D binding for ::operator new
77+
void[] cpp_new(_ = void)(size_t count, size_t alignment)
78+
{
79+
return __cpp_new_aligned(count, cast(align_val_t)alignment)[0 .. count];
80+
}
81+
82+
/// D binding for ::operator new
83+
void[] cpp_new(_ = void)(size_t count, size_t alignment) nothrow @trusted
84+
{
85+
void* mem = __cpp_new_aligned_nothrow(count, cast(align_val_t)alignment);
86+
return mem ? mem[0 .. count] : null;
87+
}
88+
89+
/// D binding for ::operator delete
90+
void cpp_delete(_ = void)(void* ptr, size_t alignment)
91+
{
92+
__cpp_delete_aligned(ptr, cast(align_val_t)alignment);
93+
}
94+
95+
/// D binding for ::operator delete
96+
void cpp_delete(_ = void)(void* ptr, size_t alignment) nothrow
97+
{
98+
__cpp_delete_align_nothrow(ptr, cast(align_val_t)alignment);
99+
}
100+
101+
/// D binding for ::operator delete
102+
void cpp_delete(_ = void)(void[] mem, size_t alignment)
103+
{
104+
__cpp_delete_size_aligned(mem.ptr, mem.length, cast(align_val_t)alignment);
105+
}
106+
107+
/// D binding for ::operator delete
108+
void cpp_delete(_ = void)(void[] mem, size_t alignment) nothrow @trusted
109+
{
110+
// TODO: should we call the sized delete and catch instead?
111+
__cpp_delete_align_nothrow(mem.ptr, cast(align_val_t)alignment);
112+
}
113+
}
114+
31115
// raw C++ functions
32116
extern(C++):
33117

0 commit comments

Comments
 (0)