Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
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
25 changes: 13 additions & 12 deletions src/rt/arrayassign.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ extern (C) void[] _d_arrayassign(TypeInfo ti, void[] from, void[] to)

// Need a temporary buffer tmp[] big enough to hold one element
void[16] buf = void;
void* ptmp = (elementSize > buf.sizeof) ? alloca(elementSize) : buf.ptr;
void* ptmp = (elementSize > buf.sizeof) ? malloc(elementSize) : buf.ptr;
scope (exit)
{
if (ptmp != buf.ptr)
free(ptmp);
}
return _d_arrayassign_l(ti, from, to, ptmp);
}

Expand Down Expand Up @@ -206,24 +211,20 @@ extern (C) void* _d_arraysetassign(void* p, void* value, int count, TypeInfo ti)

auto element_size = ti.tsize;

//Need a temporary buffer tmp[] big enough to hold one element
void[16] buf = void;
void[] tmp;
if (element_size > buf.sizeof)
{
tmp = alloca(element_size)[0 .. element_size];
}
else
tmp = buf[];
// Need a temporary buffer tmp[] big enough to hold one element
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Need a temporary buffer tmp[] big enough to hold one element
// Need a temporary buffer big enough to hold one element.
// Stack allocate if element_size < 512.

immutable maxAllocaSize = 512;
void *ptmp = (element_size > maxAllocaSize) ? malloc(element_size) : alloca(element_size);

foreach (i; 0 .. count)
{
memcpy(tmp.ptr, p, element_size);
memcpy(ptmp, p, element_size);
memcpy(p, value, element_size);
ti.postblit(p);
ti.destroy(tmp.ptr);
ti.destroy(ptmp);
p += element_size;
}
if (element_size > maxAllocaSize)
free(ptmp);
return pstart;
}

Expand Down