Skip to content

Refactored memset and memcpy calls to D slices in multiple files #21147

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 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 9 additions & 8 deletions compiler/src/dmd/common/outbuffer.d
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
{
FileMapping!ubyte model;
fileMapping = cast(FileMapping!ubyte*) malloc(model.sizeof);
memcpy(fileMapping, &model, model.sizeof);
(cast(ubyte*)fileMapping)[0 .. model.sizeof] = (cast(ubyte*)&model)[0 .. model.sizeof];
fileMapping.__ctor(filename);
//fileMapping = new FileMapping!ubyte(filename);
data = (*fileMapping)[];
Expand Down Expand Up @@ -179,7 +179,7 @@
{
auto p = cast(ubyte*) pureMalloc(size);
p || assert(0, "OutBuffer: out of memory.");
memcpy(p, data.ptr, offset);
(cast(ubyte*)p)[0 .. offset] = (cast(ubyte*)data.ptr)[0 .. offset];
memset(data.ptr, 0xFF, data.length); // stomp old location
pureFree(data.ptr);
memset(p + offset, 0xff, size - offset); // stomp unused data
Expand Down Expand Up @@ -226,7 +226,7 @@
@system nothrow
void writen(const void *b, size_t len)
{
memcpy(data.ptr + offset, b, len);
(cast(ubyte*)(data.ptr + offset))[0 .. len] = (cast(const(ubyte)*)b)[0 .. len];
offset += len;
}

Expand All @@ -240,7 +240,7 @@
if (doindent && !notlinehead)
indent();
reserve(buf.length);
memcpy(this.data.ptr + offset, buf.ptr, buf.length);
(cast(ubyte*)(data.ptr + offset))[0 .. buf.length] = (cast(const(ubyte)*)buf.ptr)[0 .. buf.length];
offset += buf.length;
}

Expand Down Expand Up @@ -334,7 +334,7 @@
size_t len = strlen(string);
reserve(len);
memmove(data.ptr + len, data.ptr, offset);
memcpy(data.ptr, string, len);
(cast(ubyte*)data.ptr)[0 .. len] = (cast(const(ubyte)*)string)[0 .. len];
offset += len;
}

Expand All @@ -361,7 +361,8 @@
void *writezeros(size_t n) nothrow
{
reserve(n);
auto result = memset(data.ptr + offset, 0, n);
void* result = data.ptr + offset;
(cast(ubyte*)result)[0 .. n] = 0;
offset += n;
return result;
}
Expand Down Expand Up @@ -507,15 +508,15 @@
if (buf)
{
reserve(buf.offset);
memcpy(data.ptr + offset, buf.data.ptr, buf.offset);
(cast(ubyte*)(data.ptr + offset))[0 .. buf.offset] = (cast(const(ubyte)*)buf.data.ptr)[0 .. buf.offset];
offset += buf.offset;
}
}

extern (C++) void fill0(size_t nbytes) pure nothrow @trusted
{
reserve(nbytes);
memset(data.ptr + offset, 0, nbytes);
(cast(ubyte*)(data.ptr + offset))[0 .. nbytes] = 0;

Check warning on line 519 in compiler/src/dmd/common/outbuffer.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/common/outbuffer.d#L519

Added line #L519 was not covered by tests
offset += nbytes;
}

Expand Down
9 changes: 4 additions & 5 deletions compiler/src/dmd/dcast.d
Original file line number Diff line number Diff line change
Expand Up @@ -2405,8 +2405,8 @@ Expression castTo(Expression e, Scope* sc, Type t, Type att = null)
void* s = mem.xmalloc(fullSize);
const srcSize = e.len * e.sz;
const data = se.peekData();
memcpy(s, data.ptr, srcSize);
memset(s + srcSize, 0, fullSize - srcSize);
(cast(ubyte*)s)[0 .. srcSize] = (cast(const(ubyte)*)data.ptr)[0 .. srcSize];
(cast(ubyte*)s)[srcSize .. fullSize] = 0;
se.setData(s, se.len, se.sz);
}
return se;
Expand Down Expand Up @@ -2582,9 +2582,8 @@ Expression castTo(Expression e, Scope* sc, Type t, Type att = null)
const newsz = se.sz;
const d = (dim2 < se.len) ? dim2 : se.len;
void* s = mem.xmalloc((dim2 + 1) * newsz);
memcpy(s, se.peekData().ptr, d * newsz);
// Extend with 0, add terminating 0
memset(s + d * newsz, 0, (dim2 + 1 - d) * newsz);
(cast(ubyte*)s)[0 .. d * newsz] = (cast(const(ubyte)*)se.peekData().ptr)[0 .. d * newsz];
(cast(ubyte*)s)[d * newsz .. (dim2 + 1) * newsz] = 0;
se.setData(s, dim2, newsz);
}
}
Expand Down
23 changes: 11 additions & 12 deletions compiler/src/dmd/root/array.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Dynamic array implementation.
*
Expand Down Expand Up @@ -51,7 +50,7 @@ public:

~this() pure nothrow
{
debug (stomp) memset(data.ptr, 0xFF, data.length);
debug (stomp) (cast(ubyte*)data.ptr)[0 .. data.length] = 0xFF;
if (data.ptr && data.ptr != &smallarray[0])
mem.xfree(data.ptr);
}
Expand Down Expand Up @@ -136,7 +135,7 @@ public:
{
const oldLength = length;
setDim(oldLength + a.length);
memcpy(data.ptr + oldLength, a.ptr, a.length * T.sizeof);
(cast(ubyte*)data.ptr)[oldLength * T.sizeof .. (oldLength + a.length) * T.sizeof] = (cast(const(ubyte)*)a.ptr)[0 .. a.length * T.sizeof];
return this;
}

Expand Down Expand Up @@ -171,7 +170,7 @@ public:
{
const allocdim = length + nentries;
auto p = cast(T*)mem.xmalloc(allocdim * T.sizeof);
memcpy(p, smallarray.ptr, length * T.sizeof);
(cast(ubyte*)p)[0 .. length * T.sizeof] = (cast(const(ubyte)*)smallarray.ptr)[0 .. length * T.sizeof];
data = p[0 .. allocdim];
}
else
Expand All @@ -186,8 +185,8 @@ public:
{
// always move using allocate-copy-stomp-free
auto p = cast(T*)mem.xmalloc(allocdim * T.sizeof);
memcpy(p, data.ptr, length * T.sizeof);
memset(data.ptr, 0xFF, data.length * T.sizeof);
(cast(ubyte*)p)[0 .. length * T.sizeof] = (cast(const(ubyte)*)data.ptr)[0 .. length * T.sizeof];
(cast(ubyte*)data.ptr)[0 .. data.length * T.sizeof] = 0xFF;
mem.xfree(data.ptr);
}
else
Expand All @@ -198,13 +197,13 @@ public:
debug (stomp)
{
if (length < data.length)
memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof);
(cast(ubyte*)data.ptr)[(length * T.sizeof) .. (data.length * T.sizeof)] = 0xFF;
}
else
{
if (mem.isGCEnabled)
if (length < data.length)
memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof);
(cast(ubyte*)data.ptr)[(length * T.sizeof) .. (data.length * T.sizeof)] = 0xFF;
}
}

Expand All @@ -217,7 +216,7 @@ public:
if (length - i - 1)
memmove(data.ptr + i, data.ptr + i + 1, (length - i - 1) * T.sizeof);
length--;
debug (stomp) memset(data.ptr + length, 0xFF, T.sizeof);
debug (stomp) (cast(ubyte*)data.ptr)[length * T.sizeof .. (length + 1) * T.sizeof] = 0xFF;
}

void insert(size_t index, typeof(this)* a) pure nothrow
Expand All @@ -228,7 +227,7 @@ public:
reserve(d);
if (length != index)
memmove(data.ptr + index + d, data.ptr + index, (length - index) * T.sizeof);
memcpy(data.ptr + index, a.data.ptr, d * T.sizeof);
(cast(ubyte*)(data.ptr + index))[0 .. d * T.sizeof] = (cast(const(ubyte)*)a.data.ptr)[0 .. d * T.sizeof];
length += d;
}
}
Expand All @@ -239,7 +238,7 @@ public:
reserve(d);
if (length != index)
memmove(data.ptr + index + d, data.ptr + index, (length - index) * T.sizeof);
memcpy(data.ptr + index, a.ptr, d * T.sizeof);
(cast(ubyte*)(data.ptr + index))[0 .. d * T.sizeof] = (cast(const(ubyte)*)a.ptr)[0 .. d * T.sizeof];
length += d;
}

Expand Down Expand Up @@ -303,7 +302,7 @@ public:
{
auto a = new Array!T();
a.setDim(length);
memcpy(a.data.ptr, data.ptr, length * T.sizeof);
(cast(ubyte*)a.data.ptr)[0 .. length * T.sizeof] = (cast(const(ubyte)*)data.ptr)[0 .. length * T.sizeof];
return a;
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/root/bitarray.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct BitArray
if (!len)
length(b.len);
assert(len == b.len);
memcpy(ptr, b.ptr, bytes(len));
(cast(ubyte*)ptr)[0 .. bytes(len)] = (cast(const(ubyte)*)b.ptr)[0 .. bytes(len)];
}

bool opIndex(size_t idx) const @nogc nothrow pure
Expand Down Expand Up @@ -77,7 +77,7 @@ struct BitArray

void zero() @nogc nothrow pure
{
memset(ptr, 0, bytes(len));
(cast(ubyte*)ptr)[0 .. bytes(len)] = 0;
}

/******
Expand Down
Loading