summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2017-03-22 08:51:00 +0100
committerEugen Wissner <belka@caraus.de>2017-03-22 08:51:00 +0100
commit7892c1a930f95ff113a8de2b00b808ac0f8c9218 (patch)
treee3116b1cc42e4f1581f8e4b2a8a9ad7463cc2bf6 /source
parentb90517580e8ef94524433719743b9c95fb1bc149 (diff)
downloadtanya-7892c1a930f95ff113a8de2b00b808ac0f8c9218.tar.gz
Remove Init template parameter from memory.resize()
Diffstat (limited to 'source')
-rw-r--r--source/tanya/math/mp.d14
-rw-r--r--source/tanya/memory/package.d31
2 files changed, 18 insertions, 27 deletions
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;
}