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

Commit

Permalink
Add D wrapper.
Browse files Browse the repository at this point in the history
Need to make it a template, otherwise druntime wants to link the CRT.
  • Loading branch information
TurkeyMan committed Jan 9, 2019
1 parent bf7e5bb commit 94a1d7f
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/core/stdcpp/new_.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import core.stdcpp.xutility : __cpp_sized_deallocation, __cpp_aligned_new;

@nogc:


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


extern (C++, "std")
{
///
Expand All @@ -28,6 +30,88 @@ extern (C++, "std")
}


/// D binding for ::operator new
void[] cpp_new(_ = void)(size_t count)
{
return __cpp_new(count)[0 .. count];
}

/// D binding for ::operator new
void[] cpp_new(_ = void)(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)(void* ptr)
{
__cpp_delete(ptr);
}

/// D binding for ::operator delete
void cpp_delete(_ = void)(void* ptr) nothrow
{
__cpp_delete_nothrow(ptr);
}

/// D binding for ::operator delete
void cpp_delete(_ = void)(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)(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(_ = void)(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(_ = void)(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)(void* ptr, size_t alignment)
{
__cpp_delete_aligned(ptr, cast(align_val_t)alignment);
}

/// D binding for ::operator delete
void cpp_delete(_ = void)(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)(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)(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++):

Expand Down

0 comments on commit 94a1d7f

Please sign in to comment.