diff --git a/source/tanya/math/mp.d b/source/tanya/math/mp.d index 50efb64..8c7c9a1 100644 --- a/source/tanya/math/mp.d +++ b/source/tanya/math/mp.d @@ -315,10 +315,14 @@ struct Integer { if (summand.length > this.length) { + const delta = summand.size - this.size; auto tmp = this.rep[0 .. this.size]; + this.rep = allocator.resize!ubyte(null, summand.size).ptr; - tmp.copy(this.rep[summand.size - this.size .. summand.size]); - allocator.resize(tmp[0 .. this.size], 0); + tmp.copy(this.rep[delta .. summand.size]); + this.rep[0 .. delta].initializeAll(); + + allocator.deallocate(tmp[0 .. this.size]); this.size = summand.size; } @@ -533,7 +537,7 @@ struct Integer return this; } - private @nogc unittest + private unittest { { auto h1 = Integer(1019); @@ -547,14 +551,14 @@ struct Integer auto h2 = Integer(2_147_483_647); ubyte[4] expected = [0x80, 0x00, 0x11, 0x03]; h1 += h2; - assert(h1.rep[0 .. h1.length] == expected); + assert(h1.rep[0 .. h1.size] == expected); } { auto h1 = Integer(2147488003L); auto h2 = Integer(2_147_483_647); ubyte[5] expected = [0x01, 0x00, 0x00, 0x11, 0x02]; h1 += h2; - assert(h1.rep[0 .. h1.length] == expected); + assert(h1.rep[0 .. h1.size] == expected); } } diff --git a/source/tanya/memory/package.d b/source/tanya/memory/package.d index 1959dbd..0595073 100644 --- a/source/tanya/memory/package.d +++ b/source/tanya/memory/package.d @@ -137,7 +137,8 @@ template stateSize(T) * * 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; } @@ -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 * may throw $(D_PSYMBOL OutOfMemoryError). The new - * allocated part of the array is initialized only if $(D_PARAM Init) - * is set. This function can be trusted only in the data structures that - * can ensure that the array is allocated/rellocated/deallocated with the - * same allocator. + * allocated part of the array isn't initialized. This function can be trusted + * only in the data structures that can ensure that the array is + * allocated/rellocated/deallocated with the same allocator. * * Params: * T = Element type of the array being created. - * Init = If should be initialized. * allocator = The allocator used for getting memory. * array = A reference to the array being changed. * length = New array length. * * Returns: $(D_PARAM array). */ -package(tanya) T[] resize(T, - bool Init = true) - (shared Allocator allocator, - auto ref T[] array, - const size_t length) @trusted +package(tanya) T[] resize(T)(shared Allocator allocator, + auto ref T[] array, + const size_t length) @trusted { void[] buf = array; - static if (Init) - { - const oldLength = array.length; - } + if (!allocator.reallocate(buf, length * T.sizeof)) { onOutOfMemoryError; @@ -177,13 +171,6 @@ package(tanya) T[] resize(T, // Casting from void[] is unsafe, but we know we cast to the original type. array = cast(T[]) buf; - static if (Init) - { - if (oldLength < length) - { - array[oldLength .. $] = T.init; - } - } return array; }