Make allocator shared and fix some RefCounted bugs

This commit is contained in:
2016-12-06 21:29:08 +01:00
parent b3fdd6fd4a
commit fa607141e4
19 changed files with 2682 additions and 2717 deletions

View File

@ -108,30 +108,36 @@ class ReadBuffer : Buffer
/// Size by which the buffer will grow.
protected immutable size_t blockSize;
/// Allocator.
protected shared Allocator allocator;
invariant
{
assert(length_ <= buffer_.length);
assert(blockSize > 0);
assert(minAvailable > 0);
assert(allocator !is null);
}
/**
* Creates a new read buffer.
*
* Params:
* size = Initial buffer size and the size by which the buffer
* will grow.
* minAvailable = minimal size should be always available to fill.
* So it will reallocate if $(D_INLINECODE
* $(D_PSYMBOL free) < $(D_PARAM minAvailable)
* ).
* size = Initial buffer size and the size by which the buffer
* will grow.
* minAvailable = minimal size should be always available to fill.
* So it will reallocate if $(D_INLINECODE
* $(D_PSYMBOL free) < $(D_PARAM minAvailable)).
* allocator = Allocator.
*/
this(size_t size = 8192,
size_t minAvailable = 1024)
size_t minAvailable = 1024,
shared Allocator allocator = defaultAllocator)
{
this.minAvailable = minAvailable;
this.blockSize = size;
theAllocator.resizeArray!ubyte(buffer_, size);
this.allocator = allocator;
allocator.resizeArray!ubyte(buffer_, size);
}
/**
@ -139,17 +145,17 @@ class ReadBuffer : Buffer
*/
~this()
{
theAllocator.dispose(buffer_);
allocator.dispose(buffer_);
}
///
unittest
{
auto b = theAllocator.make!ReadBuffer;
auto b = defaultAllocator.make!ReadBuffer;
assert(b.capacity == 8192);
assert(b.length == 0);
theAllocator.dispose(b);
defaultAllocator.dispose(b);
}
/**
@ -190,7 +196,7 @@ class ReadBuffer : Buffer
///
unittest
{
auto b = theAllocator.make!ReadBuffer;
auto b = defaultAllocator.make!ReadBuffer;
size_t numberRead;
// Fills the buffer with values 0..10
@ -202,7 +208,7 @@ class ReadBuffer : Buffer
b.clear();
assert(b.free == b.blockSize);
theAllocator.dispose(b);
defaultAllocator.dispose(b);
}
/**
@ -224,7 +230,7 @@ class ReadBuffer : Buffer
///
unittest
{
auto b = theAllocator.make!ReadBuffer;
auto b = defaultAllocator.make!ReadBuffer;
size_t numberRead;
ubyte[] result;
@ -252,7 +258,7 @@ class ReadBuffer : Buffer
assert(result[10] == 20);
assert(result[14] == 24);
theAllocator.dispose(b);
defaultAllocator.dispose(b);
}
/**
@ -294,7 +300,7 @@ class ReadBuffer : Buffer
{
if (capacity - length < minAvailable)
{
theAllocator.resizeArray!ubyte(buffer_, capacity + blockSize);
allocator.resizeArray!ubyte(buffer_, capacity + blockSize);
}
ring = length_;
return buffer_[length_..$];
@ -304,7 +310,7 @@ class ReadBuffer : Buffer
///
unittest
{
auto b = theAllocator.make!ReadBuffer;
auto b = defaultAllocator.make!ReadBuffer;
size_t numberRead;
ubyte[] result;
@ -319,7 +325,7 @@ class ReadBuffer : Buffer
b.clear();
assert(b.length == 0);
theAllocator.dispose(b);
defaultAllocator.dispose(b);
}
}
@ -349,23 +355,29 @@ class WriteBuffer : Buffer
/// The position of the free area in the buffer.
protected size_t position;
/// Allocator.
protected shared Allocator allocator;
invariant
{
assert(blockSize > 0);
// position can refer to an element outside the buffer if the buffer is full.
assert(position <= buffer_.length);
assert(allocator !is null);
}
/**
* Params:
* size = Initial buffer size and the size by which the buffer
* will grow.
* size = Initial buffer size and the size by which the buffer will
* grow.
* allocator = Allocator.
*/
this(size_t size = 8192)
this(size_t size = 8192, shared Allocator allocator = defaultAllocator)
{
this.allocator = allocator;
blockSize = size;
ring = size - 1;
theAllocator.resizeArray!ubyte(buffer_, size);
allocator.resizeArray!ubyte(buffer_, size);
}
/**
@ -373,7 +385,7 @@ class WriteBuffer : Buffer
*/
~this()
{
theAllocator.dispose(buffer_);
allocator.dispose(buffer_);
}
/**
@ -415,7 +427,7 @@ class WriteBuffer : Buffer
///
unittest
{
auto b = theAllocator.make!WriteBuffer(4);
auto b = defaultAllocator.make!WriteBuffer(4);
ubyte[3] buf = [48, 23, 255];
b ~= buf;
@ -433,7 +445,7 @@ class WriteBuffer : Buffer
b += b.length;
assert(b.length == 0);
theAllocator.dispose(b);
defaultAllocator.dispose(b);
}
/**
@ -498,7 +510,7 @@ class WriteBuffer : Buffer
{
auto newSize = end / blockSize * blockSize + blockSize;
theAllocator.resizeArray!ubyte(buffer_, newSize);
allocator.resizeArray!ubyte(buffer_, newSize);
}
buffer_[position..end] = buffer[start..$];
position = end;
@ -514,7 +526,7 @@ class WriteBuffer : Buffer
///
unittest
{
auto b = theAllocator.make!WriteBuffer(4);
auto b = defaultAllocator.make!WriteBuffer(4);
ubyte[3] buf = [48, 23, 255];
b ~= buf;
@ -533,9 +545,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);
theAllocator.dispose(b);
defaultAllocator.dispose(b);
b = make!WriteBuffer(theAllocator, 2);
b = make!WriteBuffer(defaultAllocator, 2);
b ~= buf;
assert(b.start == 0);
@ -543,7 +555,7 @@ class WriteBuffer : Buffer
assert(b.ring == 3);
assert(b.position == 3);
theAllocator.dispose(b);
defaultAllocator.dispose(b);
}
/**
@ -620,7 +632,7 @@ class WriteBuffer : Buffer
///
unittest
{
auto b = theAllocator.make!WriteBuffer;
auto b = defaultAllocator.make!WriteBuffer;
ubyte[6] buf = [23, 23, 255, 128, 127, 9];
b ~= buf;
@ -630,7 +642,7 @@ class WriteBuffer : Buffer
b += 4;
assert(b.length == 0);
theAllocator.dispose(b);
defaultAllocator.dispose(b);
}
/**
@ -663,7 +675,7 @@ class WriteBuffer : Buffer
///
unittest
{
auto b = theAllocator.make!WriteBuffer(6);
auto b = defaultAllocator.make!WriteBuffer(6);
ubyte[6] buf = [23, 23, 255, 128, 127, 9];
b ~= buf;
@ -679,7 +691,7 @@ class WriteBuffer : Buffer
assert(b[0..$] == buf[0..6]);
b += b.length;
theAllocator.dispose(b);
defaultAllocator.dispose(b);
}
/**

View File

@ -27,7 +27,7 @@ class SList(T)
* allocator = The allocator should be used for the element
* allocations.
*/
this(IAllocator allocator = theAllocator)
this(shared Allocator allocator = defaultAllocator)
{
this.allocator = allocator;
}
@ -54,14 +54,14 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(theAllocator);
auto l = make!(SList!int)(defaultAllocator);
l.insertFront(8);
l.insertFront(5);
l.clear();
assert(l.empty);
dispose(theAllocator, l);
dispose(defaultAllocator, l);
}
/**
@ -98,14 +98,14 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(theAllocator);
auto l = make!(SList!int)(defaultAllocator);
l.insertFront(8);
assert(l.front == 8);
l.insertFront(9);
assert(l.front == 9);
dispose(theAllocator, l);
dispose(defaultAllocator, l);
}
/**
@ -140,7 +140,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(theAllocator);
auto l = make!(SList!int)(defaultAllocator);
l.insertFront(8);
l.insertFront(9);
@ -148,7 +148,7 @@ class SList(T)
l.popFront();
assert(l.front == 8);
dispose(theAllocator, l);
dispose(defaultAllocator, l);
}
/**
@ -179,7 +179,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(theAllocator);
auto l = make!(SList!int)(defaultAllocator);
l.insertFront(8);
l.insertFront(5);
@ -189,7 +189,7 @@ class SList(T)
assert(l.removeFront(3) == 1);
assert(l.removeFront(3) == 0);
dispose(theAllocator, l);
dispose(defaultAllocator, l);
}
/**
@ -235,7 +235,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(theAllocator);
auto l = make!(SList!int)(defaultAllocator);
l.insertFront(5);
l.insertFront(4);
@ -246,7 +246,7 @@ class SList(T)
assert(i != 1 || e == 4);
assert(i != 2 || e == 5);
}
dispose(theAllocator, l);
dispose(defaultAllocator, l);
}
/**
@ -265,13 +265,13 @@ class SList(T)
protected Entry first;
/// Allocator.
protected IAllocator allocator;
protected shared Allocator allocator;
}
///
unittest
{
auto l = make!(SList!int)(theAllocator);
auto l = make!(SList!int)(defaultAllocator);
size_t i;
l.insertFront(5);
@ -286,7 +286,7 @@ unittest
}
assert(i == 3);
dispose(theAllocator, l);
dispose(defaultAllocator, l);
}
private unittest
@ -295,7 +295,7 @@ private unittest
{
}
auto l = make!(SList!Stuff)(theAllocator);
auto l = make!(SList!Stuff)(defaultAllocator);
dispose(theAllocator, l);
dispose(defaultAllocator, l);
}

View File

@ -27,7 +27,7 @@ class Queue(T)
* allocator = The allocator should be used for the element
* allocations.
*/
this(IAllocator allocator = theAllocator)
this(shared Allocator allocator = defaultAllocator)
{
this.allocator = allocator;
}
@ -54,7 +54,7 @@ class Queue(T)
///
unittest
{
auto q = theAllocator.make!(Queue!int);
auto q = defaultAllocator.make!(Queue!int);
assert(q.empty);
q.insertBack(8);
@ -62,7 +62,7 @@ class Queue(T)
q.clear();
assert(q.empty);
theAllocator.dispose(q);
defaultAllocator.dispose(q);
}
/**
@ -107,7 +107,7 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(theAllocator);
auto q = make!(Queue!int)(defaultAllocator);
assert(q.empty);
q.insertBack(8);
@ -115,7 +115,7 @@ class Queue(T)
q.insertBack(9);
assert(q.front == 8);
dispose(theAllocator, q);
dispose(defaultAllocator, q);
}
/**
@ -129,14 +129,14 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(theAllocator);
auto q = make!(Queue!int)(defaultAllocator);
int value = 7;
assert(q.empty);
q.insertBack(value);
assert(!q.empty);
dispose(theAllocator, q);
dispose(defaultAllocator, q);
}
/**
@ -158,7 +158,7 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(theAllocator);
auto q = make!(Queue!int)(defaultAllocator);
q.insertBack(8);
q.insertBack(9);
@ -166,7 +166,7 @@ class Queue(T)
q.popFront();
assert(q.front == 9);
dispose(theAllocator, q);
dispose(defaultAllocator, q);
}
/**
@ -210,7 +210,7 @@ class Queue(T)
///
unittest
{
auto q = theAllocator.make!(Queue!int);
auto q = defaultAllocator.make!(Queue!int);
size_t j;
q.insertBack(5);
@ -240,7 +240,7 @@ class Queue(T)
assert(j == 3);
assert(q.empty);
dispose(theAllocator, q);
dispose(defaultAllocator, q);
}
/**
@ -262,13 +262,13 @@ class Queue(T)
protected Entry* rear;
/// The allocator.
protected IAllocator allocator;
protected shared Allocator allocator;
}
///
unittest
{
auto q = theAllocator.make!(Queue!int);
auto q = defaultAllocator.make!(Queue!int);
q.insertBack(5);
assert(!q.empty);
@ -289,5 +289,5 @@ unittest
}
assert(q.empty);
theAllocator.dispose(q);
defaultAllocator.dispose(q);
}

View File

@ -200,7 +200,7 @@ class Vector(T)
* allocator = The allocator should be used for the element
* allocations.
*/
this(IAllocator allocator = theAllocator)
this(shared Allocator allocator = defaultAllocator)
{
this.allocator = allocator;
}
@ -211,18 +211,18 @@ class Vector(T)
* Params:
* U = Variadic template for the constructor parameters.
* params = Values to initialize the array with. The last parameter can
* be an allocator, if not, $(D_PSYMBOL theAllocator) is used.
* be an allocator, if not, $(D_PSYMBOL defaultAllocator) is used.
*/
this(U...)(U params)
{
static if (isImplicitlyConvertible!(typeof(params[$ - 1]), IAllocator))
static if (isImplicitlyConvertible!(typeof(params[$ - 1]), Allocator))
{
allocator = params[$ - 1];
auto values = params[0 .. $ - 1];
}
else
{
allocator = theAllocator;
allocator = defaultAllocator;
alias values = params;
}
@ -252,7 +252,7 @@ class Vector(T)
///
unittest
{
auto v = theAllocator.make!(Vector!int)(18, 20, 15);
auto v = defaultAllocator.make!(Vector!int)(18, 20, 15);
v.clear();
assert(v.length == 0);
@ -286,7 +286,7 @@ class Vector(T)
///
unittest
{
auto v = theAllocator.make!(Vector!int);
auto v = defaultAllocator.make!(Vector!int);
v.length = 5;
assert(v.length == 5);
@ -337,14 +337,14 @@ class Vector(T)
///
unittest
{
auto v = theAllocator.make!(Vector!int)(5, 18, 17);
auto v = defaultAllocator.make!(Vector!int)(5, 18, 17);
assert(v.removeBack(0) == 0);
assert(v.removeBack(2) == 2);
assert(v.removeBack(3) == 1);
assert(v.removeBack(3) == 0);
theAllocator.dispose(v);
defaultAllocator.dispose(v);
}
/**
@ -377,14 +377,14 @@ class Vector(T)
///
unittest
{
auto v1 = theAllocator.make!(Vector!int)(12, 1, 7);
auto v1 = defaultAllocator.make!(Vector!int)(12, 1, 7);
v1[] = 3;
assert(v1[0] == 3);
assert(v1[1] == 3);
assert(v1[2] == 3);
theAllocator.dispose(v1);
defaultAllocator.dispose(v1);
}
@ -406,14 +406,14 @@ class Vector(T)
///
unittest
{
auto v = theAllocator.make!(Vector!int)(6, 123, 34, 5);
auto v = defaultAllocator.make!(Vector!int)(6, 123, 34, 5);
assert(v[0] == 6);
assert(v[1] == 123);
assert(v[2] == 34);
assert(v[3] == 5);
theAllocator.dispose(v);
defaultAllocator.dispose(v);
}
/**
@ -435,8 +435,8 @@ class Vector(T)
///
unittest
{
auto v1 = theAllocator.make!(Vector!int);
auto v2 = theAllocator.make!(Vector!int);
auto v1 = defaultAllocator.make!(Vector!int);
auto v2 = defaultAllocator.make!(Vector!int);
assert(v1 == v2);
@ -453,8 +453,8 @@ class Vector(T)
v2[1] = 3;
assert(v1 == v2);
theAllocator.dispose(v1);
theAllocator.dispose(v2);
defaultAllocator.dispose(v1);
defaultAllocator.dispose(v2);
}
/**
@ -495,7 +495,7 @@ class Vector(T)
///
unittest
{
auto v = theAllocator.make!(Vector!int)(5, 15, 8);
auto v = defaultAllocator.make!(Vector!int)(5, 15, 8);
size_t i;
foreach (j, ref e; v)
@ -510,7 +510,7 @@ class Vector(T)
assert(j != 1 || e == 15);
assert(j != 2 || e == 8);
}
theAllocator.dispose(v);
defaultAllocator.dispose(v);
}
/**
@ -551,7 +551,7 @@ class Vector(T)
///
unittest
{
auto v = theAllocator.make!(Vector!int)(5, 15, 8);
auto v = defaultAllocator.make!(Vector!int)(5, 15, 8);
size_t i;
foreach_reverse (j, ref e; v)
@ -566,7 +566,7 @@ class Vector(T)
assert(j != 1 || e == 15);
assert(j != 0 || e == 5);
}
theAllocator.dispose(v);
defaultAllocator.dispose(v);
}
/**
@ -587,7 +587,7 @@ class Vector(T)
///
unittest
{
auto v = theAllocator.make!(Vector!int)(5);
auto v = defaultAllocator.make!(Vector!int)(5);
assert(v.front == 5);
@ -595,7 +595,7 @@ class Vector(T)
v[1] = 15;
assert(v.front == 5);
theAllocator.dispose(v);
defaultAllocator.dispose(v);
}
/**
@ -616,7 +616,7 @@ class Vector(T)
///
unittest
{
auto v = theAllocator.make!(Vector!int)(5);
auto v = defaultAllocator.make!(Vector!int)(5);
assert(v.back == 5);
@ -624,7 +624,7 @@ class Vector(T)
v[1] = 15;
assert(v.back == 15);
theAllocator.dispose(v);
defaultAllocator.dispose(v);
}
/**
@ -745,8 +745,8 @@ class Vector(T)
///
unittest
{
auto v1 = theAllocator.make!(Vector!int)(3, 3, 3);
auto v2 = theAllocator.make!(Vector!int)(1, 2);
auto v1 = defaultAllocator.make!(Vector!int)(3, 3, 3);
auto v2 = defaultAllocator.make!(Vector!int)(1, 2);
v1[0..2] = 286;
assert(v1[0] == 286);
@ -757,25 +757,25 @@ class Vector(T)
assert(v2[0] == 286);
assert(v2[1] == 3);
theAllocator.dispose(v2);
theAllocator.dispose(v1);
defaultAllocator.dispose(v2);
defaultAllocator.dispose(v1);
}
/// Internal representation.
protected T[] vector;
/// The allocator.
protected IAllocator allocator;
protected shared Allocator allocator;
}
///
unittest
{
auto v = theAllocator.make!(Vector!int)(5, 15, 8);
auto v = defaultAllocator.make!(Vector!int)(5, 15, 8);
assert(v.front == 5);
assert(v[1] == 15);
assert(v.back == 8);
theAllocator.dispose(v);
defaultAllocator.dispose(v);
}