From 192ee20bf7e190e209655867c501103287dc5a24 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 30 Nov 2016 21:20:18 +0100 Subject: [PATCH] Remove shared from the allocators --- source/tanya/async/loop.d | 11 -- source/tanya/container/buffer.d | 52 +++++----- source/tanya/container/list.d | 4 +- source/tanya/container/package.d | 1 + source/tanya/container/queue.d | 4 +- source/tanya/container/vector.d | 6 +- source/tanya/crypto/mode.d | 4 +- source/tanya/memory/allocator.d | 118 +++++++++++++++++---- source/tanya/memory/mallocator.d | 173 ------------------------------- source/tanya/memory/mmappool.d | 19 ++-- source/tanya/memory/package.d | 18 +--- source/tanya/random.d | 4 +- 12 files changed, 150 insertions(+), 264 deletions(-) delete mode 100644 source/tanya/memory/mallocator.d diff --git a/source/tanya/async/loop.d b/source/tanya/async/loop.d index 17429d4..f34fedc 100644 --- a/source/tanya/async/loop.d +++ b/source/tanya/async/loop.d @@ -119,17 +119,6 @@ enum Event : uint alias EventMask = BitFlags!Event; -/** - * Tries to set $(D_PSYMBOL MmapPool) to the default allocator. - */ -shared static this() -{ - if (allocator is null) - { - allocator = MmapPool.instance; - } -} - /** * Event loop. */ diff --git a/source/tanya/container/buffer.d b/source/tanya/container/buffer.d index 338c0bb..ce7c476 100644 --- a/source/tanya/container/buffer.d +++ b/source/tanya/container/buffer.d @@ -6,8 +6,8 @@ * Copyright: Eugene Wissner 2016. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner) - */ + * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) + */ module tanya.container.buffer; import tanya.memory; @@ -131,7 +131,7 @@ class ReadBuffer : Buffer { this.minAvailable = minAvailable; this.blockSize = size; - defaultAllocator.resizeArray!ubyte(buffer_, size); + theAllocator.resizeArray!ubyte(buffer_, size); } /** @@ -139,17 +139,17 @@ class ReadBuffer : Buffer */ ~this() { - defaultAllocator.dispose(buffer_); + theAllocator.dispose(buffer_); } /// unittest { - auto b = defaultAllocator.make!ReadBuffer; + auto b = theAllocator.make!ReadBuffer; assert(b.capacity == 8192); assert(b.length == 0); - defaultAllocator.dispose(b); + theAllocator.dispose(b); } /** @@ -190,7 +190,7 @@ class ReadBuffer : Buffer /// unittest { - auto b = defaultAllocator.make!ReadBuffer; + auto b = theAllocator.make!ReadBuffer; size_t numberRead; // Fills the buffer with values 0..10 @@ -202,7 +202,7 @@ class ReadBuffer : Buffer b.clear(); assert(b.free == b.blockSize); - defaultAllocator.dispose(b); + theAllocator.dispose(b); } /** @@ -224,7 +224,7 @@ class ReadBuffer : Buffer /// unittest { - auto b = defaultAllocator.make!ReadBuffer; + auto b = theAllocator.make!ReadBuffer; size_t numberRead; ubyte[] result; @@ -252,7 +252,7 @@ class ReadBuffer : Buffer assert(result[10] == 20); assert(result[14] == 24); - defaultAllocator.dispose(b); + theAllocator.dispose(b); } /** @@ -294,7 +294,7 @@ class ReadBuffer : Buffer { if (capacity - length < minAvailable) { - defaultAllocator.resizeArray!ubyte(buffer_, capacity + blockSize); + theAllocator.resizeArray!ubyte(buffer_, capacity + blockSize); } ring = length_; return buffer_[length_..$]; @@ -304,7 +304,7 @@ class ReadBuffer : Buffer /// unittest { - auto b = defaultAllocator.make!ReadBuffer; + auto b = theAllocator.make!ReadBuffer; size_t numberRead; ubyte[] result; @@ -319,7 +319,7 @@ class ReadBuffer : Buffer b.clear(); assert(b.length == 0); - defaultAllocator.dispose(b); + theAllocator.dispose(b); } } @@ -365,7 +365,7 @@ class WriteBuffer : Buffer { blockSize = size; ring = size - 1; - defaultAllocator.resizeArray!ubyte(buffer_, size); + theAllocator.resizeArray!ubyte(buffer_, size); } /** @@ -373,7 +373,7 @@ class WriteBuffer : Buffer */ ~this() { - defaultAllocator.dispose(buffer_); + theAllocator.dispose(buffer_); } /** @@ -415,7 +415,7 @@ class WriteBuffer : Buffer /// unittest { - auto b = defaultAllocator.make!WriteBuffer(4); + auto b = theAllocator.make!WriteBuffer(4); ubyte[3] buf = [48, 23, 255]; b ~= buf; @@ -433,7 +433,7 @@ class WriteBuffer : Buffer b += b.length; assert(b.length == 0); - defaultAllocator.dispose(b); + theAllocator.dispose(b); } /** @@ -498,7 +498,7 @@ class WriteBuffer : Buffer { auto newSize = end / blockSize * blockSize + blockSize; - defaultAllocator.resizeArray!ubyte(buffer_, newSize); + theAllocator.resizeArray!ubyte(buffer_, newSize); } buffer_[position..end] = buffer[start..$]; position = end; @@ -514,7 +514,7 @@ class WriteBuffer : Buffer /// unittest { - auto b = defaultAllocator.make!WriteBuffer(4); + auto b = theAllocator.make!WriteBuffer(4); ubyte[3] buf = [48, 23, 255]; b ~= buf; @@ -533,9 +533,9 @@ class WriteBuffer : Buffer assert(b.buffer_[0] == 23 && b.buffer_[1] == 255 && b.buffer_[2] == 48 && b.buffer_[3] == 23 && b.buffer_[4] == 255); - defaultAllocator.dispose(b); + theAllocator.dispose(b); - b = make!WriteBuffer(defaultAllocator, 2); + b = make!WriteBuffer(theAllocator, 2); b ~= buf; assert(b.start == 0); @@ -543,7 +543,7 @@ class WriteBuffer : Buffer assert(b.ring == 3); assert(b.position == 3); - defaultAllocator.dispose(b); + theAllocator.dispose(b); } /** @@ -620,7 +620,7 @@ class WriteBuffer : Buffer /// unittest { - auto b = defaultAllocator.make!WriteBuffer; + auto b = theAllocator.make!WriteBuffer; ubyte[6] buf = [23, 23, 255, 128, 127, 9]; b ~= buf; @@ -630,7 +630,7 @@ class WriteBuffer : Buffer b += 4; assert(b.length == 0); - defaultAllocator.dispose(b); + theAllocator.dispose(b); } /** @@ -663,7 +663,7 @@ class WriteBuffer : Buffer /// unittest { - auto b = defaultAllocator.make!WriteBuffer(6); + auto b = theAllocator.make!WriteBuffer(6); ubyte[6] buf = [23, 23, 255, 128, 127, 9]; b ~= buf; @@ -679,7 +679,7 @@ class WriteBuffer : Buffer assert(b[0..$] == buf[0..6]); b += b.length; - defaultAllocator.dispose(b); + theAllocator.dispose(b); } /** diff --git a/source/tanya/container/list.d b/source/tanya/container/list.d index d9b40f2..ab59387 100644 --- a/source/tanya/container/list.d +++ b/source/tanya/container/list.d @@ -27,7 +27,7 @@ class SList(T) * allocator = The allocator should be used for the element * allocations. */ - this(shared Allocator allocator = defaultAllocator) + this(IAllocator allocator = defaultAllocator) { this.allocator = allocator; reset(); @@ -388,7 +388,7 @@ class SList(T) /// Current position in the list. protected Entry* position; - private shared Allocator allocator; + private IAllocator allocator; } interface Stuff diff --git a/source/tanya/container/package.d b/source/tanya/container/package.d index eba95a7..e1a5205 100644 --- a/source/tanya/container/package.d +++ b/source/tanya/container/package.d @@ -10,6 +10,7 @@ */ module tanya.container; +public import tanya.container.bit; public import tanya.container.buffer; public import tanya.container.list; public import tanya.container.vector; diff --git a/source/tanya/container/queue.d b/source/tanya/container/queue.d index 6c9b794..88405b4 100644 --- a/source/tanya/container/queue.d +++ b/source/tanya/container/queue.d @@ -27,7 +27,7 @@ class Queue(T) * allocator = The allocator should be used for the element * allocations. */ - this(shared Allocator allocator = defaultAllocator) + this(IAllocator allocator = defaultAllocator) { this.allocator = allocator; } @@ -206,7 +206,7 @@ class Queue(T) /// The last element of the list. protected Entry* rear; - private shared Allocator allocator; + private IAllocator allocator; } /// diff --git a/source/tanya/container/vector.d b/source/tanya/container/vector.d index f6bde1f..bf3721a 100644 --- a/source/tanya/container/vector.d +++ b/source/tanya/container/vector.d @@ -40,14 +40,14 @@ class Vector(T) * allocator = The allocator should be used for the element * allocations. */ - this(size_t length, shared Allocator allocator = defaultAllocator) + this(size_t length, IAllocator allocator = defaultAllocator) { this.allocator = allocator; vector = makeArray!T(allocator, length); } /// Ditto. - this(shared Allocator allocator = defaultAllocator) + this(IAllocator allocator = defaultAllocator) { this(0, allocator); } @@ -405,7 +405,7 @@ class Vector(T) /// Container. protected T[] vector; - private shared Allocator allocator; + private IAllocator allocator; } /// diff --git a/source/tanya/crypto/mode.d b/source/tanya/crypto/mode.d index 52c24e6..3bcda28 100644 --- a/source/tanya/crypto/mode.d +++ b/source/tanya/crypto/mode.d @@ -44,7 +44,7 @@ enum PaddingMode ubyte[] pad(ref ubyte[] input, in PaddingMode mode, in ushort blockSize, - shared Allocator allocator = defaultAllocator) + IAllocator allocator = defaultAllocator) in { assert(blockSize > 0 && blockSize <= 256); @@ -204,7 +204,7 @@ unittest ref ubyte[] unpad(ref ubyte[] input, in PaddingMode mode, in ushort blockSize, - shared Allocator allocator = defaultAllocator) + IAllocator allocator = defaultAllocator) in { assert(input.length != 0); diff --git a/source/tanya/memory/allocator.d b/source/tanya/memory/allocator.d index 711b1fd..f2ec28b 100644 --- a/source/tanya/memory/allocator.d +++ b/source/tanya/memory/allocator.d @@ -12,6 +12,7 @@ module tanya.memory.allocator; import std.experimental.allocator; import std.traits; +import std.typecons : Ternary; version (unittest) { @@ -21,43 +22,122 @@ version (unittest) /** * Allocator interface. */ -interface Allocator +abstract class Allocator : IAllocator { /** - * Allocates $(D_PARAM size) bytes of memory. + * Not supported. * + * Returns: $(D_KEYWORD false). + */ + bool deallocateAll() const @nogc @safe pure nothrow + { + return false; + } + + /** + * Not supported. + * + * Returns $(D_PSYMBOL Ternary.unknown). + */ + Ternary empty() const @nogc @safe pure nothrow + { + return Ternary.unknown; + } + + /** + * Not supported. + * + * Params: + * b = Memory block. + * + * Returns: $(D_PSYMBOL Ternary.unknown). + */ + Ternary owns(void[] b) const @nogc @safe pure nothrow + { + return Ternary.unknown; + } + + /** + * Not supported. + * + * Params: + * p = Pointer to a memory block. + * result = Full block allocated. + * + * Returns: $(D_PSYMBOL Ternary.unknown). + */ + Ternary resolveInternalPointer(void* p, ref void[] result) + const @nogc @safe pure nothrow + { + return Ternary.unknown; + } + + /** * Params: * size = Amount of memory to allocate. * - * Returns: The pointer to the new allocated memory. + * Returns: The good allocation size that guarantees zero internal + * fragmentation. */ - void[] allocate(size_t size) shared; + size_t goodAllocSize(size_t s) + { + auto rem = s % alignment; + return rem ? s + alignment - rem : s; + } /** - * Deallocates a memory block. + * Not supported. + * + * Returns: $(D_KEYWORD null). + * + */ + void[] allocateAll() const @nogc @safe pure nothrow + { + return null; + } + + /** + * Not supported. + * + * Params: + * b = Block to be expanded. + * s = New size. + * + * Returns: $(D_KEYWORD false). + */ + bool expand(ref void[] b, size_t s) const @nogc @safe pure nothrow + { + return false; + } + + /** + * Not supported. * * Params: - * p = A pointer to the memory block to be freed. + * n = Amount of memory to allocate. + * a = Alignment. * - * Returns: Whether the deallocation was successful. + * Returns: $(D_KEYWORD null). */ - bool deallocate(void[] p) shared; + void[] alignedAllocate(size_t n, uint a) const @nogc @safe pure nothrow + { + return null; + } /** - * Increases or decreases the size of a memory block. + * Not supported. * * Params: - * p = A pointer to the memory block. - * size = Size of the reallocated block. + * n = Amount of memory to allocate. + * a = Alignment. * - * Returns: Whether the reallocation was successful. + * Returns: $(D_KEYWORD false). */ - bool reallocate(ref void[] p, size_t size) shared; - - /** - * Returns: The alignment offered. - */ - @property immutable(uint) alignment() shared const @safe pure nothrow; + bool alignedReallocate(ref void[] b, size_t size, uint alignment) + const @nogc @safe pure nothrow + { + return false; + } } /** @@ -70,7 +150,7 @@ interface Allocator * Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could * not be reallocated. In the latter */ -bool resizeArray(T)(shared Allocator allocator, +bool resizeArray(T)(IAllocator allocator, ref T[] array, in size_t length) { diff --git a/source/tanya/memory/mallocator.d b/source/tanya/memory/mallocator.d deleted file mode 100644 index f878ae8..0000000 --- a/source/tanya/memory/mallocator.d +++ /dev/null @@ -1,173 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * Copyright: Eugene Wissner 2016. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) - */ -module tanya.memory.mallocator; - -import tanya.memory.allocator; -import core.exception; -import core.stdc.stdlib; -import std.algorithm.comparison; - -/** - * Wrapper for malloc/realloc/free from the C standard library. - */ -class Mallocator : Allocator -{ - /** - * Allocates $(D_PARAM size) bytes of memory. - * - * Params: - * size = Amount of memory to allocate. - * - * Returns: The pointer to the new allocated memory. - */ - void[] allocate(size_t size) shared @nogc nothrow - { - if (!size) - { - return null; - } - auto p = malloc(size + psize); - - if (!p) - { - onOutOfMemoryError(); - } - return p[psize.. psize + size]; - } - - /// - @nogc nothrow unittest - { - auto p = Mallocator.instance.allocate(20); - - assert(p.length == 20); - - Mallocator.instance.deallocate(p); - } - - /** - * Deallocates a memory block. - * - * Params: - * p = A pointer to the memory block to be freed. - * - * Returns: Whether the deallocation was successful. - */ - bool deallocate(void[] p) shared @nogc nothrow - { - if (p !is null) - { - free(p.ptr - psize); - } - return true; - } - - /// - @nogc nothrow unittest - { - void[] p; - assert(Mallocator.instance.deallocate(p)); - - p = Mallocator.instance.allocate(10); - assert(Mallocator.instance.deallocate(p)); - } - - /** - * Increases or decreases the size of a memory block. - * - * Params: - * p = A pointer to the memory block. - * size = Size of the reallocated block. - * - * Returns: Whether the reallocation was successful. - */ - bool reallocate(ref void[] p, size_t size) shared @nogc nothrow - { - if (!size) - { - deallocate(p); - p = null; - return true; - } - else if (p is null) - { - p = allocate(size); - return true; - } - auto r = realloc(p.ptr - psize, size + psize); - - if (!r) - { - onOutOfMemoryError(); - } - p = r[psize.. psize + size]; - - return true; - } - - /// - @nogc nothrow unittest - { - void[] p; - - Mallocator.instance.reallocate(p, 20); - assert(p.length == 20); - - Mallocator.instance.reallocate(p, 30); - assert(p.length == 30); - - Mallocator.instance.reallocate(p, 10); - assert(p.length == 10); - - Mallocator.instance.reallocate(p, 0); - assert(p is null); - } - - /** - * Returns: The alignment offered. - */ - @property immutable(uint) alignment() shared const @safe pure nothrow - { - return cast(uint) max(double.alignof, real.alignof); - } - - /** - * Static allocator instance and initializer. - * - * Returns: The global $(D_PSYMBOL Allocator) instance. - */ - static @property ref shared(Mallocator) instance() @nogc nothrow - { - if (instance_ is null) - { - immutable size = __traits(classInstanceSize, Mallocator) + psize; - void* p = malloc(size); - - if (p is null) - { - onOutOfMemoryError(); - } - p[psize..size] = typeid(Mallocator).initializer[]; - instance_ = cast(shared Mallocator) p[psize..size].ptr; - } - return instance_; - } - - /// - @nogc nothrow unittest - { - assert(instance is instance); - } - - private enum psize = 8; - - private shared static Mallocator instance_; -} diff --git a/source/tanya/memory/mmappool.d b/source/tanya/memory/mmappool.d index ed761ad..0e82af2 100644 --- a/source/tanya/memory/mmappool.d +++ b/source/tanya/memory/mmappool.d @@ -83,7 +83,7 @@ class MmapPool : Allocator * * Returns: The pointer to the new allocated memory. */ - void[] allocate(size_t size) shared @nogc @trusted nothrow + void[] allocate(size_t size, TypeInfo ti = null) @nogc @trusted nothrow { if (!size) { @@ -119,7 +119,7 @@ class MmapPool : Allocator * * Returns: Data the block points to or $(D_KEYWORD null). */ - private void* findBlock(size_t size) shared @nogc nothrow + private void* findBlock(size_t size) @nogc nothrow { Block block1; RegionLoop: for (auto r = head; r !is null; r = r.next) @@ -177,7 +177,7 @@ class MmapPool : Allocator * * Returns: Whether the deallocation was successful. */ - bool deallocate(void[] p) shared @nogc @trusted nothrow + bool deallocate(void[] p) @nogc @trusted nothrow { if (p is null) { @@ -233,7 +233,7 @@ class MmapPool : Allocator * * Returns: Whether the reallocation was successful. */ - bool reallocate(ref void[] p, size_t size) shared @nogc @trusted nothrow + bool reallocate(ref void[] p, size_t size) @nogc @trusted nothrow { void[] reallocP; @@ -301,7 +301,7 @@ class MmapPool : Allocator * * Returns: Global $(D_PSYMBOL MmapPool) instance. */ - static @property ref shared(MmapPool) instance() @nogc @trusted nothrow + static @property ref MmapPool instance() @nogc @trusted nothrow { if (instance_ is null) { @@ -312,7 +312,7 @@ class MmapPool : Allocator if (data !is null) { data[0..instanceSize] = typeid(MmapPool).initializer[]; - instance_ = cast(shared MmapPool) data; + instance_ = cast(MmapPool) data; instance_.head = head; } } @@ -334,7 +334,6 @@ class MmapPool : Allocator * * Returns: A pointer to the data. */ - pragma(inline) private static void* initializeRegion(size_t size, ref Region head) @nogc nothrow { @@ -409,7 +408,7 @@ class MmapPool : Allocator } /// Ditto. - private void* initializeRegion(size_t size) shared @nogc nothrow + private void* initializeRegion(size_t size) @nogc nothrow { return initializeRegion(size, head); } @@ -451,13 +450,13 @@ class MmapPool : Allocator return x / pageSize * pageSize + pageSize; } - @property immutable(uint) alignment() shared const @nogc @safe pure nothrow + @property uint alignment() const @nogc @safe pure nothrow { return alignment_; } private enum alignment_ = 8; - private shared static MmapPool instance_; + private static MmapPool instance_; private shared static immutable size_t pageSize; diff --git a/source/tanya/memory/package.d b/source/tanya/memory/package.d index 1585a87..dd30368 100644 --- a/source/tanya/memory/package.d +++ b/source/tanya/memory/package.d @@ -10,20 +10,10 @@ */ module tanya.memory; -public -{ - import tanya.memory.allocator; - import std.experimental.allocator : make, dispose, shrinkArray, expandArray, makeArray, dispose; -} +public import tanya.memory.allocator; +public import std.experimental.allocator; -shared Allocator allocator; - -@property ref shared(Allocator) defaultAllocator() +@property IAllocator defaultAllocator() { - import tanya.memory.mallocator; - if (allocator is null) - { - allocator = Mallocator.instance; - } - return allocator; + return theAllocator; } diff --git a/source/tanya/random.d b/source/tanya/random.d index 4e9197d..b42c6e7 100644 --- a/source/tanya/random.d +++ b/source/tanya/random.d @@ -164,7 +164,7 @@ class Entropy private ubyte sourceCount_; - private shared Allocator allocator; + private IAllocator allocator; /// Entropy accumulator. protected SHA!(maxGather * 8, 512) accumulator; @@ -175,7 +175,7 @@ class Entropy * allocator = Allocator to allocate entropy sources available on the * system. */ - this(size_t maxSources = 20, shared Allocator allocator = defaultAllocator) + this(size_t maxSources = 20, IAllocator allocator = defaultAllocator) in { assert(maxSources > 0 && maxSources <= ubyte.max);