Remove Init template parameter from memory.resize()

This commit is contained in:
Eugen Wissner 2017-03-22 08:51:00 +01:00
parent b90517580e
commit 7892c1a930
2 changed files with 18 additions and 27 deletions

View File

@ -315,10 +315,14 @@ struct Integer
{ {
if (summand.length > this.length) if (summand.length > this.length)
{ {
const delta = summand.size - this.size;
auto tmp = this.rep[0 .. this.size]; auto tmp = this.rep[0 .. this.size];
this.rep = allocator.resize!ubyte(null, summand.size).ptr; this.rep = allocator.resize!ubyte(null, summand.size).ptr;
tmp.copy(this.rep[summand.size - this.size .. summand.size]); tmp.copy(this.rep[delta .. summand.size]);
allocator.resize(tmp[0 .. this.size], 0); this.rep[0 .. delta].initializeAll();
allocator.deallocate(tmp[0 .. this.size]);
this.size = summand.size; this.size = summand.size;
} }
@ -533,7 +537,7 @@ struct Integer
return this; return this;
} }
private @nogc unittest private unittest
{ {
{ {
auto h1 = Integer(1019); auto h1 = Integer(1019);
@ -547,14 +551,14 @@ struct Integer
auto h2 = Integer(2_147_483_647); auto h2 = Integer(2_147_483_647);
ubyte[4] expected = [0x80, 0x00, 0x11, 0x03]; ubyte[4] expected = [0x80, 0x00, 0x11, 0x03];
h1 += h2; h1 += h2;
assert(h1.rep[0 .. h1.length] == expected); assert(h1.rep[0 .. h1.size] == expected);
} }
{ {
auto h1 = Integer(2147488003L); auto h1 = Integer(2147488003L);
auto h2 = Integer(2_147_483_647); auto h2 = Integer(2_147_483_647);
ubyte[5] expected = [0x01, 0x00, 0x00, 0x11, 0x02]; ubyte[5] expected = [0x01, 0x00, 0x00, 0x11, 0x02];
h1 += h2; h1 += h2;
assert(h1.rep[0 .. h1.length] == expected); assert(h1.rep[0 .. h1.size] == expected);
} }
} }

View File

@ -137,7 +137,8 @@ template stateSize(T)
* *
* Returns: Aligned size. * Returns: Aligned size.
*/ */
size_t alignedSize(in size_t size, in size_t alignment = 8) pure nothrow @safe @nogc size_t alignedSize(const size_t size, const size_t alignment = 8)
pure nothrow @safe @nogc
{ {
return (size - 1) / alignment * alignment + alignment; return (size - 1) / alignment * alignment + alignment;
} }
@ -145,31 +146,24 @@ size_t alignedSize(in size_t size, in size_t alignment = 8) pure nothrow @safe @
/** /**
* Internal function used to create, resize or destroy a dynamic array. It * Internal function used to create, resize or destroy a dynamic array. It
* may throw $(D_PSYMBOL OutOfMemoryError). The new * may throw $(D_PSYMBOL OutOfMemoryError). The new
* allocated part of the array is initialized only if $(D_PARAM Init) * allocated part of the array isn't initialized. This function can be trusted
* is set. This function can be trusted only in the data structures that * only in the data structures that can ensure that the array is
* can ensure that the array is allocated/rellocated/deallocated with the * allocated/rellocated/deallocated with the same allocator.
* same allocator.
* *
* Params: * Params:
* T = Element type of the array being created. * T = Element type of the array being created.
* Init = If should be initialized.
* allocator = The allocator used for getting memory. * allocator = The allocator used for getting memory.
* array = A reference to the array being changed. * array = A reference to the array being changed.
* length = New array length. * length = New array length.
* *
* Returns: $(D_PARAM array). * Returns: $(D_PARAM array).
*/ */
package(tanya) T[] resize(T, package(tanya) T[] resize(T)(shared Allocator allocator,
bool Init = true) auto ref T[] array,
(shared Allocator allocator, const size_t length) @trusted
auto ref T[] array,
const size_t length) @trusted
{ {
void[] buf = array; void[] buf = array;
static if (Init)
{
const oldLength = array.length;
}
if (!allocator.reallocate(buf, length * T.sizeof)) if (!allocator.reallocate(buf, length * T.sizeof))
{ {
onOutOfMemoryError; onOutOfMemoryError;
@ -177,13 +171,6 @@ package(tanya) T[] resize(T,
// Casting from void[] is unsafe, but we know we cast to the original type. // Casting from void[] is unsafe, but we know we cast to the original type.
array = cast(T[]) buf; array = cast(T[]) buf;
static if (Init)
{
if (oldLength < length)
{
array[oldLength .. $] = T.init;
}
}
return array; return array;
} }