Skip to content

chore(allocator): make and makeArray should allocate aligned memory #8577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions std/experimental/allocator/package.d
Original file line number Diff line number Diff line change
@@ -1170,7 +1170,10 @@ auto make(T, Allocator, A...)(auto ref Allocator alloc, auto ref A args)
import std.algorithm.comparison : max;
static if (!is(T == class) && !is(T == interface) && A.length == 0
&& __traits(compiles, {T t;}) && __traits(isZeroInit, T)
&& is(typeof(alloc.allocateZeroed(size_t.max))))
&& is(typeof(alloc.allocateZeroed(size_t.max)))
&& (!is(typeof(alloc.alignedAllocate(size_t.max, uint.max))) ||
( is(typeof(alloc.alignedAllocate(size_t.max, uint.max))) && Allocator.alignment == T.alignof)
))
{
auto m = alloc.allocateZeroed(max(T.sizeof, 1));
return (() @trusted => cast(T*) m.ptr)();
@@ -1180,7 +1183,22 @@ auto make(T, Allocator, A...)(auto ref Allocator alloc, auto ref A args)
import core.internal.lifetime : emplaceRef;
import core.lifetime : emplace;

auto m = alloc.allocate(max(stateSize!T, 1));
static if (is(typeof(alloc.alignedAllocate(size_t.max, uint.max))))
{
static if (is(T == class))
{
import std.traits : classInstanceAlignment;
auto m = alloc.alignedAllocate(max(stateSize!T, 1), classInstanceAlignment!T);
}
else
{
auto m = alloc.alignedAllocate(max(stateSize!T, 1), T.alignof);
}
}
else
{
auto m = alloc.allocate(max(stateSize!T, 1));
}
if (!m.ptr) return null;

// make can only be @safe if emplace or emplaceRef is `pure`
@@ -1593,14 +1611,20 @@ T[] makeArray(T, Allocator)(auto ref Allocator alloc, size_t length)
if (overflow) return null;
}

static if (__traits(isZeroInit, T) && hasMember!(Allocator, "allocateZeroed"))
static if (__traits(isZeroInit, T)
&& hasMember!(Allocator, "allocateZeroed")
&& !hasMember!(Allocator, "alignedAllocate")
)
{
auto m = alloc.allocateZeroed(nAlloc);
return (() @trusted => cast(T[]) m)();
}
else
{
auto m = alloc.allocate(nAlloc);
static if (hasMember!(Allocator, "alignedAllocate"))
auto m = alloc.alignedAllocate(nAlloc, T.alignof);
else
auto m = alloc.allocate(nAlloc);
if (!m.ptr) return null;
alias U = Unqual!T;
return () @trusted { return cast(T[]) uninitializedFillDefault(cast(U[]) m); }();