Remove shared from the allocators
This commit is contained in:
parent
965ca0088e
commit
192ee20bf7
@ -119,17 +119,6 @@ enum Event : uint
|
|||||||
|
|
||||||
alias EventMask = BitFlags!Event;
|
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.
|
* Event loop.
|
||||||
*/
|
*/
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
* Copyright: Eugene Wissner 2016.
|
* Copyright: Eugene Wissner 2016.
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 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;
|
module tanya.container.buffer;
|
||||||
|
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
@ -131,7 +131,7 @@ class ReadBuffer : Buffer
|
|||||||
{
|
{
|
||||||
this.minAvailable = minAvailable;
|
this.minAvailable = minAvailable;
|
||||||
this.blockSize = size;
|
this.blockSize = size;
|
||||||
defaultAllocator.resizeArray!ubyte(buffer_, size);
|
theAllocator.resizeArray!ubyte(buffer_, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,17 +139,17 @@ class ReadBuffer : Buffer
|
|||||||
*/
|
*/
|
||||||
~this()
|
~this()
|
||||||
{
|
{
|
||||||
defaultAllocator.dispose(buffer_);
|
theAllocator.dispose(buffer_);
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto b = defaultAllocator.make!ReadBuffer;
|
auto b = theAllocator.make!ReadBuffer;
|
||||||
assert(b.capacity == 8192);
|
assert(b.capacity == 8192);
|
||||||
assert(b.length == 0);
|
assert(b.length == 0);
|
||||||
|
|
||||||
defaultAllocator.dispose(b);
|
theAllocator.dispose(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -190,7 +190,7 @@ class ReadBuffer : Buffer
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto b = defaultAllocator.make!ReadBuffer;
|
auto b = theAllocator.make!ReadBuffer;
|
||||||
size_t numberRead;
|
size_t numberRead;
|
||||||
|
|
||||||
// Fills the buffer with values 0..10
|
// Fills the buffer with values 0..10
|
||||||
@ -202,7 +202,7 @@ class ReadBuffer : Buffer
|
|||||||
b.clear();
|
b.clear();
|
||||||
assert(b.free == b.blockSize);
|
assert(b.free == b.blockSize);
|
||||||
|
|
||||||
defaultAllocator.dispose(b);
|
theAllocator.dispose(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -224,7 +224,7 @@ class ReadBuffer : Buffer
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto b = defaultAllocator.make!ReadBuffer;
|
auto b = theAllocator.make!ReadBuffer;
|
||||||
size_t numberRead;
|
size_t numberRead;
|
||||||
ubyte[] result;
|
ubyte[] result;
|
||||||
|
|
||||||
@ -252,7 +252,7 @@ class ReadBuffer : Buffer
|
|||||||
assert(result[10] == 20);
|
assert(result[10] == 20);
|
||||||
assert(result[14] == 24);
|
assert(result[14] == 24);
|
||||||
|
|
||||||
defaultAllocator.dispose(b);
|
theAllocator.dispose(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -294,7 +294,7 @@ class ReadBuffer : Buffer
|
|||||||
{
|
{
|
||||||
if (capacity - length < minAvailable)
|
if (capacity - length < minAvailable)
|
||||||
{
|
{
|
||||||
defaultAllocator.resizeArray!ubyte(buffer_, capacity + blockSize);
|
theAllocator.resizeArray!ubyte(buffer_, capacity + blockSize);
|
||||||
}
|
}
|
||||||
ring = length_;
|
ring = length_;
|
||||||
return buffer_[length_..$];
|
return buffer_[length_..$];
|
||||||
@ -304,7 +304,7 @@ class ReadBuffer : Buffer
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto b = defaultAllocator.make!ReadBuffer;
|
auto b = theAllocator.make!ReadBuffer;
|
||||||
size_t numberRead;
|
size_t numberRead;
|
||||||
ubyte[] result;
|
ubyte[] result;
|
||||||
|
|
||||||
@ -319,7 +319,7 @@ class ReadBuffer : Buffer
|
|||||||
b.clear();
|
b.clear();
|
||||||
assert(b.length == 0);
|
assert(b.length == 0);
|
||||||
|
|
||||||
defaultAllocator.dispose(b);
|
theAllocator.dispose(b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,7 +365,7 @@ class WriteBuffer : Buffer
|
|||||||
{
|
{
|
||||||
blockSize = size;
|
blockSize = size;
|
||||||
ring = size - 1;
|
ring = size - 1;
|
||||||
defaultAllocator.resizeArray!ubyte(buffer_, size);
|
theAllocator.resizeArray!ubyte(buffer_, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -373,7 +373,7 @@ class WriteBuffer : Buffer
|
|||||||
*/
|
*/
|
||||||
~this()
|
~this()
|
||||||
{
|
{
|
||||||
defaultAllocator.dispose(buffer_);
|
theAllocator.dispose(buffer_);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -415,7 +415,7 @@ class WriteBuffer : Buffer
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto b = defaultAllocator.make!WriteBuffer(4);
|
auto b = theAllocator.make!WriteBuffer(4);
|
||||||
ubyte[3] buf = [48, 23, 255];
|
ubyte[3] buf = [48, 23, 255];
|
||||||
|
|
||||||
b ~= buf;
|
b ~= buf;
|
||||||
@ -433,7 +433,7 @@ class WriteBuffer : Buffer
|
|||||||
b += b.length;
|
b += b.length;
|
||||||
assert(b.length == 0);
|
assert(b.length == 0);
|
||||||
|
|
||||||
defaultAllocator.dispose(b);
|
theAllocator.dispose(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -498,7 +498,7 @@ class WriteBuffer : Buffer
|
|||||||
{
|
{
|
||||||
auto newSize = end / blockSize * blockSize + blockSize;
|
auto newSize = end / blockSize * blockSize + blockSize;
|
||||||
|
|
||||||
defaultAllocator.resizeArray!ubyte(buffer_, newSize);
|
theAllocator.resizeArray!ubyte(buffer_, newSize);
|
||||||
}
|
}
|
||||||
buffer_[position..end] = buffer[start..$];
|
buffer_[position..end] = buffer[start..$];
|
||||||
position = end;
|
position = end;
|
||||||
@ -514,7 +514,7 @@ class WriteBuffer : Buffer
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto b = defaultAllocator.make!WriteBuffer(4);
|
auto b = theAllocator.make!WriteBuffer(4);
|
||||||
ubyte[3] buf = [48, 23, 255];
|
ubyte[3] buf = [48, 23, 255];
|
||||||
|
|
||||||
b ~= buf;
|
b ~= buf;
|
||||||
@ -533,9 +533,9 @@ class WriteBuffer : Buffer
|
|||||||
assert(b.buffer_[0] == 23 && b.buffer_[1] == 255
|
assert(b.buffer_[0] == 23 && b.buffer_[1] == 255
|
||||||
&& b.buffer_[2] == 48 && b.buffer_[3] == 23 && b.buffer_[4] == 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;
|
b ~= buf;
|
||||||
assert(b.start == 0);
|
assert(b.start == 0);
|
||||||
@ -543,7 +543,7 @@ class WriteBuffer : Buffer
|
|||||||
assert(b.ring == 3);
|
assert(b.ring == 3);
|
||||||
assert(b.position == 3);
|
assert(b.position == 3);
|
||||||
|
|
||||||
defaultAllocator.dispose(b);
|
theAllocator.dispose(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -620,7 +620,7 @@ class WriteBuffer : Buffer
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto b = defaultAllocator.make!WriteBuffer;
|
auto b = theAllocator.make!WriteBuffer;
|
||||||
ubyte[6] buf = [23, 23, 255, 128, 127, 9];
|
ubyte[6] buf = [23, 23, 255, 128, 127, 9];
|
||||||
|
|
||||||
b ~= buf;
|
b ~= buf;
|
||||||
@ -630,7 +630,7 @@ class WriteBuffer : Buffer
|
|||||||
b += 4;
|
b += 4;
|
||||||
assert(b.length == 0);
|
assert(b.length == 0);
|
||||||
|
|
||||||
defaultAllocator.dispose(b);
|
theAllocator.dispose(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -663,7 +663,7 @@ class WriteBuffer : Buffer
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto b = defaultAllocator.make!WriteBuffer(6);
|
auto b = theAllocator.make!WriteBuffer(6);
|
||||||
ubyte[6] buf = [23, 23, 255, 128, 127, 9];
|
ubyte[6] buf = [23, 23, 255, 128, 127, 9];
|
||||||
|
|
||||||
b ~= buf;
|
b ~= buf;
|
||||||
@ -679,7 +679,7 @@ class WriteBuffer : Buffer
|
|||||||
assert(b[0..$] == buf[0..6]);
|
assert(b[0..$] == buf[0..6]);
|
||||||
b += b.length;
|
b += b.length;
|
||||||
|
|
||||||
defaultAllocator.dispose(b);
|
theAllocator.dispose(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +27,7 @@ class SList(T)
|
|||||||
* allocator = The allocator should be used for the element
|
* allocator = The allocator should be used for the element
|
||||||
* allocations.
|
* allocations.
|
||||||
*/
|
*/
|
||||||
this(shared Allocator allocator = defaultAllocator)
|
this(IAllocator allocator = defaultAllocator)
|
||||||
{
|
{
|
||||||
this.allocator = allocator;
|
this.allocator = allocator;
|
||||||
reset();
|
reset();
|
||||||
@ -388,7 +388,7 @@ class SList(T)
|
|||||||
/// Current position in the list.
|
/// Current position in the list.
|
||||||
protected Entry* position;
|
protected Entry* position;
|
||||||
|
|
||||||
private shared Allocator allocator;
|
private IAllocator allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Stuff
|
interface Stuff
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.container;
|
module tanya.container;
|
||||||
|
|
||||||
|
public import tanya.container.bit;
|
||||||
public import tanya.container.buffer;
|
public import tanya.container.buffer;
|
||||||
public import tanya.container.list;
|
public import tanya.container.list;
|
||||||
public import tanya.container.vector;
|
public import tanya.container.vector;
|
||||||
|
@ -27,7 +27,7 @@ class Queue(T)
|
|||||||
* allocator = The allocator should be used for the element
|
* allocator = The allocator should be used for the element
|
||||||
* allocations.
|
* allocations.
|
||||||
*/
|
*/
|
||||||
this(shared Allocator allocator = defaultAllocator)
|
this(IAllocator allocator = defaultAllocator)
|
||||||
{
|
{
|
||||||
this.allocator = allocator;
|
this.allocator = allocator;
|
||||||
}
|
}
|
||||||
@ -206,7 +206,7 @@ class Queue(T)
|
|||||||
/// The last element of the list.
|
/// The last element of the list.
|
||||||
protected Entry* rear;
|
protected Entry* rear;
|
||||||
|
|
||||||
private shared Allocator allocator;
|
private IAllocator allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@ -40,14 +40,14 @@ class Vector(T)
|
|||||||
* allocator = The allocator should be used for the element
|
* allocator = The allocator should be used for the element
|
||||||
* allocations.
|
* allocations.
|
||||||
*/
|
*/
|
||||||
this(size_t length, shared Allocator allocator = defaultAllocator)
|
this(size_t length, IAllocator allocator = defaultAllocator)
|
||||||
{
|
{
|
||||||
this.allocator = allocator;
|
this.allocator = allocator;
|
||||||
vector = makeArray!T(allocator, length);
|
vector = makeArray!T(allocator, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
this(shared Allocator allocator = defaultAllocator)
|
this(IAllocator allocator = defaultAllocator)
|
||||||
{
|
{
|
||||||
this(0, allocator);
|
this(0, allocator);
|
||||||
}
|
}
|
||||||
@ -405,7 +405,7 @@ class Vector(T)
|
|||||||
/// Container.
|
/// Container.
|
||||||
protected T[] vector;
|
protected T[] vector;
|
||||||
|
|
||||||
private shared Allocator allocator;
|
private IAllocator allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@ -44,7 +44,7 @@ enum PaddingMode
|
|||||||
ubyte[] pad(ref ubyte[] input,
|
ubyte[] pad(ref ubyte[] input,
|
||||||
in PaddingMode mode,
|
in PaddingMode mode,
|
||||||
in ushort blockSize,
|
in ushort blockSize,
|
||||||
shared Allocator allocator = defaultAllocator)
|
IAllocator allocator = defaultAllocator)
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(blockSize > 0 && blockSize <= 256);
|
assert(blockSize > 0 && blockSize <= 256);
|
||||||
@ -204,7 +204,7 @@ unittest
|
|||||||
ref ubyte[] unpad(ref ubyte[] input,
|
ref ubyte[] unpad(ref ubyte[] input,
|
||||||
in PaddingMode mode,
|
in PaddingMode mode,
|
||||||
in ushort blockSize,
|
in ushort blockSize,
|
||||||
shared Allocator allocator = defaultAllocator)
|
IAllocator allocator = defaultAllocator)
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(input.length != 0);
|
assert(input.length != 0);
|
||||||
|
@ -12,6 +12,7 @@ module tanya.memory.allocator;
|
|||||||
|
|
||||||
import std.experimental.allocator;
|
import std.experimental.allocator;
|
||||||
import std.traits;
|
import std.traits;
|
||||||
|
import std.typecons : Ternary;
|
||||||
|
|
||||||
version (unittest)
|
version (unittest)
|
||||||
{
|
{
|
||||||
@ -21,43 +22,122 @@ version (unittest)
|
|||||||
/**
|
/**
|
||||||
* Allocator interface.
|
* 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:
|
* Params:
|
||||||
* size = Amount of memory to allocate.
|
* 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:
|
* 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:
|
* Params:
|
||||||
* p = A pointer to the memory block.
|
* n = Amount of memory to allocate.
|
||||||
* size = Size of the reallocated block.
|
* a = Alignment.
|
||||||
*
|
*
|
||||||
* Returns: Whether the reallocation was successful.
|
* Returns: $(D_KEYWORD false).
|
||||||
*/
|
*/
|
||||||
bool reallocate(ref void[] p, size_t size) shared;
|
bool alignedReallocate(ref void[] b, size_t size, uint alignment)
|
||||||
|
const @nogc @safe pure nothrow
|
||||||
/**
|
{
|
||||||
* Returns: The alignment offered.
|
return false;
|
||||||
*/
|
}
|
||||||
@property immutable(uint) alignment() shared const @safe pure nothrow;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -70,7 +150,7 @@ interface Allocator
|
|||||||
* Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could
|
* Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could
|
||||||
* not be reallocated. In the latter
|
* not be reallocated. In the latter
|
||||||
*/
|
*/
|
||||||
bool resizeArray(T)(shared Allocator allocator,
|
bool resizeArray(T)(IAllocator allocator,
|
||||||
ref T[] array,
|
ref T[] array,
|
||||||
in size_t length)
|
in size_t length)
|
||||||
{
|
{
|
||||||
|
@ -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_;
|
|
||||||
}
|
|
@ -83,7 +83,7 @@ class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: The pointer to the new allocated memory.
|
* 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)
|
if (!size)
|
||||||
{
|
{
|
||||||
@ -119,7 +119,7 @@ class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Data the block points to or $(D_KEYWORD null).
|
* 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;
|
Block block1;
|
||||||
RegionLoop: for (auto r = head; r !is null; r = r.next)
|
RegionLoop: for (auto r = head; r !is null; r = r.next)
|
||||||
@ -177,7 +177,7 @@ class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the deallocation was successful.
|
* 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)
|
if (p is null)
|
||||||
{
|
{
|
||||||
@ -233,7 +233,7 @@ class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the reallocation was successful.
|
* 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;
|
void[] reallocP;
|
||||||
|
|
||||||
@ -301,7 +301,7 @@ class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Global $(D_PSYMBOL MmapPool) instance.
|
* 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)
|
if (instance_ is null)
|
||||||
{
|
{
|
||||||
@ -312,7 +312,7 @@ class MmapPool : Allocator
|
|||||||
if (data !is null)
|
if (data !is null)
|
||||||
{
|
{
|
||||||
data[0..instanceSize] = typeid(MmapPool).initializer[];
|
data[0..instanceSize] = typeid(MmapPool).initializer[];
|
||||||
instance_ = cast(shared MmapPool) data;
|
instance_ = cast(MmapPool) data;
|
||||||
instance_.head = head;
|
instance_.head = head;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -334,7 +334,6 @@ class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: A pointer to the data.
|
* Returns: A pointer to the data.
|
||||||
*/
|
*/
|
||||||
pragma(inline)
|
|
||||||
private static void* initializeRegion(size_t size,
|
private static void* initializeRegion(size_t size,
|
||||||
ref Region head) @nogc nothrow
|
ref Region head) @nogc nothrow
|
||||||
{
|
{
|
||||||
@ -409,7 +408,7 @@ class MmapPool : Allocator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
private void* initializeRegion(size_t size) shared @nogc nothrow
|
private void* initializeRegion(size_t size) @nogc nothrow
|
||||||
{
|
{
|
||||||
return initializeRegion(size, head);
|
return initializeRegion(size, head);
|
||||||
}
|
}
|
||||||
@ -451,13 +450,13 @@ class MmapPool : Allocator
|
|||||||
return x / pageSize * pageSize + pageSize;
|
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_;
|
return alignment_;
|
||||||
}
|
}
|
||||||
private enum alignment_ = 8;
|
private enum alignment_ = 8;
|
||||||
|
|
||||||
private shared static MmapPool instance_;
|
private static MmapPool instance_;
|
||||||
|
|
||||||
private shared static immutable size_t pageSize;
|
private shared static immutable size_t pageSize;
|
||||||
|
|
||||||
|
@ -10,20 +10,10 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.memory;
|
module tanya.memory;
|
||||||
|
|
||||||
public
|
public import tanya.memory.allocator;
|
||||||
{
|
public import std.experimental.allocator;
|
||||||
import tanya.memory.allocator;
|
|
||||||
import std.experimental.allocator : make, dispose, shrinkArray, expandArray, makeArray, dispose;
|
|
||||||
}
|
|
||||||
|
|
||||||
shared Allocator allocator;
|
@property IAllocator defaultAllocator()
|
||||||
|
|
||||||
@property ref shared(Allocator) defaultAllocator()
|
|
||||||
{
|
{
|
||||||
import tanya.memory.mallocator;
|
return theAllocator;
|
||||||
if (allocator is null)
|
|
||||||
{
|
|
||||||
allocator = Mallocator.instance;
|
|
||||||
}
|
|
||||||
return allocator;
|
|
||||||
}
|
}
|
||||||
|
@ -164,7 +164,7 @@ class Entropy
|
|||||||
|
|
||||||
private ubyte sourceCount_;
|
private ubyte sourceCount_;
|
||||||
|
|
||||||
private shared Allocator allocator;
|
private IAllocator allocator;
|
||||||
|
|
||||||
/// Entropy accumulator.
|
/// Entropy accumulator.
|
||||||
protected SHA!(maxGather * 8, 512) accumulator;
|
protected SHA!(maxGather * 8, 512) accumulator;
|
||||||
@ -175,7 +175,7 @@ class Entropy
|
|||||||
* allocator = Allocator to allocate entropy sources available on the
|
* allocator = Allocator to allocate entropy sources available on the
|
||||||
* system.
|
* system.
|
||||||
*/
|
*/
|
||||||
this(size_t maxSources = 20, shared Allocator allocator = defaultAllocator)
|
this(size_t maxSources = 20, IAllocator allocator = defaultAllocator)
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(maxSources > 0 && maxSources <= ubyte.max);
|
assert(maxSources > 0 && maxSources <= ubyte.max);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user