Skip to content

Commit

Permalink
core.internal.array: Replace _d_newarrayU with template in _dup (#…
Browse files Browse the repository at this point in the history
…16097)

- Only use the `pure nothrow` wrapper `_d_newarrayUPureNothrow` for the
`pure nothrow` `_dup`
- Unqualify the template type given to `_d_newarrayU`. The initial
template was relying on this unqualification being done by the compiler,
but now it's called from `_dup`
- Fix `TypeInfoSize` to only account for type `struct`s
- Cannot remove `_d_newarray{iT,T,U}` from `rt.lifetime` because they're used by `aaA.d`

Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
  • Loading branch information
teodutu committed Jan 27, 2024
1 parent 7d85d1b commit 1778ab8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
15 changes: 9 additions & 6 deletions druntime/src/core/internal/array/construction.d
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,20 @@ void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
* Returns:
* newly allocated array
*/
T[] _d_newarrayU(T)(size_t length, bool isShared=false) pure nothrow @nogc @trusted
T[] _d_newarrayUPureNothrow(T)(size_t length, bool isShared=false) pure nothrow @trusted
{
alias PureType = T[] function(size_t length, bool isShared) pure nothrow @nogc @trusted;
return (cast(PureType) &_d_newarrayUImpl!T)(length, isShared);
alias PureType = T[] function(size_t length, bool isShared) pure nothrow @trusted;
return (cast(PureType) &_d_newarrayU!T)(length, isShared);
}

T[] _d_newarrayUImpl(T)(size_t length, bool isShared=false) @trusted
T[] _d_newarrayU(T)(size_t length, bool isShared=false) @trusted
{
import core.exception : onOutOfMemoryError;
import core.internal.traits : Unqual;
import core.internal.array.utils : __arrayStart, __setArrayAllocLength, __arrayAlloc;

alias UnqT = Unqual!T;

size_t elemSize = T.sizeof;
size_t arraySize;

Expand Down Expand Up @@ -392,14 +395,14 @@ Loverflow:
assert(0);

Lcontinue:
auto info = __arrayAlloc!T(arraySize);
auto info = __arrayAlloc!UnqT(arraySize);
if (!info.base)
goto Loverflow;
debug(PRINTF) printf("p = %p\n", info.base);

auto arrstart = __arrayStart(info);

__setArrayAllocLength!T(info, arraySize, isShared);
__setArrayAllocLength!UnqT(info, arraySize, isShared);

return (cast(T*) arrstart)[0 .. length];
}
Expand Down
10 changes: 5 additions & 5 deletions druntime/src/core/internal/array/duplication.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ Source: $(DRUNTIMESRC core/internal/_array/_duplication.d)
*/
module core.internal.array.duplication;

private extern (C) void[] _d_newarrayU(const scope TypeInfo ti, size_t length) pure nothrow;

U[] _dup(T, U)(scope T[] a) pure nothrow @trusted if (__traits(isPOD, T))
{
if (__ctfe)
Expand All @@ -22,8 +20,9 @@ U[] _dup(T, U)(scope T[] a) pure nothrow @trusted if (__traits(isPOD, T))
else
{
import core.stdc.string : memcpy;
auto arr = _d_newarrayU(typeid(T[]), a.length);
memcpy(arr.ptr, cast(const(void)*) a.ptr, T.sizeof * a.length);
import core.internal.array.construction: _d_newarrayUPureNothrow;
auto arr = _d_newarrayUPureNothrow!T(a.length, is(T == shared));
memcpy(cast(void*) arr.ptr, cast(const(void)*) a.ptr, T.sizeof * a.length);
return *cast(U[]*) &arr;
}
}
Expand Down Expand Up @@ -55,8 +54,9 @@ U[] _dup(T, U)(T[] a) if (!__traits(isPOD, T))
else
{
import core.lifetime: copyEmplace;
import core.internal.array.construction: _d_newarrayU;
U[] res = () @trusted {
auto arr = cast(U*) _d_newarrayU(typeid(T[]), a.length);
auto arr = cast(U*) _d_newarrayU!T(a.length, is(T == shared));
size_t i;
scope (failure)
{
Expand Down
1 change: 0 additions & 1 deletion druntime/src/core/internal/array/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ private
auto gcStatsPure() nothrow pure
{
import core.memory : GC;

auto impureBypass = cast(GC.Stats function() pure nothrow)&GC.stats;
return impureBypass();
}
Expand Down
2 changes: 1 addition & 1 deletion druntime/src/core/lifetime.d
Original file line number Diff line number Diff line change
Expand Up @@ -3009,5 +3009,5 @@ version (D_ProfileGC)
template TypeInfoSize(T)
{
import core.internal.traits : hasElaborateDestructor;
enum TypeInfoSize = hasElaborateDestructor!T ? size_t.sizeof : 0;
enum TypeInfoSize = (is (T == struct) && hasElaborateDestructor!T) ? size_t.sizeof : 0;
}

0 comments on commit 1778ab8

Please sign in to comment.