Skip to content
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
61 changes: 52 additions & 9 deletions compiler/src/dmd/root/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import core.stdc.stdlib : _compare_fp_t;
import core.stdc.string;

import dmd.root.rmem;
import dmd.root.string;
import dmd.root.string : toDString;

// `qsort` is only `nothrow` since 2.081.0
private extern(C) void qsort(scope void* base, size_t nmemb, size_t size, _compare_fp_t compar) nothrow @nogc;
Expand Down Expand Up @@ -51,10 +51,26 @@ public:

~this() pure nothrow
{
debug (stomp) memset(data.ptr, 0xFF, data.length);
if (data.ptr != &smallarray[0])
debug (stomp)
{
if (data.ptr)
memset(data.ptr, 0xFF, data.length * T.sizeof);
}
if (data.ptr && data.ptr != &smallarray[0])
mem.xfree(data.ptr);
}

// this is using a template constraint because of ambiguity with this(size_t) when T is
// int, and c++ header generation doesn't accept wrapping this in static if
extern(D) this()(T[] elems ...) pure nothrow if (is(T == struct) || is(T == class))
{
this(elems.length);
foreach(i; 0 .. elems.length)
{
this[i] = elems[i];
}
}

///returns elements comma separated in []
extern(D) const(char)[] toString() const
{
Expand Down Expand Up @@ -189,22 +205,33 @@ public:
memcpy(p, data.ptr, length * T.sizeof);
memset(data.ptr, 0xFF, data.length * T.sizeof);
mem.xfree(data.ptr);
data = p[0 .. allocdim];
}
else
{
auto p = cast(T*)mem.xrealloc(data.ptr, allocdim * T.sizeof);
data = p[0 .. allocdim];
data = p[0 .. allocdim];
}
}

debug (stomp)
{
if (length < data.length)
memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof);
if (data.ptr)
{
if (length < data.length)
memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof);
}
}
else
{
if (mem.isGCEnabled)
if (length < data.length)
memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof);
{
if (data.ptr)
{
if (length < data.length)
memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof);
}
}
}
}

Expand Down Expand Up @@ -896,7 +923,7 @@ bool equal(Range1, Range2)(Range1 range1, Range2 range2)

else
{
static if (hasLength!Range1 && hasLength!Range2 && is(typeof(r1.length == r2.length)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamdruppe fix dead code in block

static if (hasLength!Range1 && hasLength!Range2 && is(typeof(range1.length == range2.length)))
{
if (range1.length != range2.length)
return false;
Expand Down Expand Up @@ -1181,3 +1208,19 @@ pure nothrow @nogc @safe unittest
b.popFront();
assert(b == expected[]);
}


/// Test Array array constructor
pure nothrow unittest
{
//check to make sure that this works with the aliases in arraytypes.d
import dmd.rootobject;
alias Objects = Array!RootObject;

auto ro1 = new RootObject();
auto ro2 = new RootObject();

auto aoo = new Objects(ro1, ro2);
assert((*aoo)[0] is ro1);
assert((*aoo)[1] is ro2);
}