Replace defaultAllocator with theAllocator

This commit is contained in:
Eugen Wissner 2016-11-30 21:54:31 +01:00
parent ed0eb4ac74
commit 9fdcef86e7
8 changed files with 106 additions and 117 deletions

View File

@ -27,7 +27,7 @@ class SList(T)
* allocator = The allocator should be used for the element
* allocations.
*/
this(IAllocator allocator = defaultAllocator)
this(IAllocator allocator = theAllocator)
{
this.allocator = allocator;
reset();
@ -79,7 +79,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
auto l = make!(SList!int)(theAllocator);
int[2] values = [8, 9];
l.front = values[0];
@ -87,7 +87,7 @@ class SList(T)
l.front = values[1];
assert(l.front == values[1]);
dispose(defaultAllocator, l);
dispose(theAllocator, l);
}
/**
@ -108,7 +108,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
auto l = make!(SList!int)(theAllocator);
int value = 5;
assert(l.empty);
@ -118,7 +118,7 @@ class SList(T)
assert(l.front == value);
assert(!l.empty);
dispose(defaultAllocator, l);
dispose(theAllocator, l);
}
/**
@ -153,7 +153,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
auto l = make!(SList!int)(theAllocator);
int[2] values = [8, 9];
l.front = values[0];
@ -162,7 +162,7 @@ class SList(T)
l.popFront();
assert(l.front == values[0]);
dispose(defaultAllocator, l);
dispose(theAllocator, l);
}
/**
@ -192,7 +192,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
auto l = make!(SList!int)(theAllocator);
int[3] values = [8, 5, 4];
l.front = values[0];
@ -203,7 +203,7 @@ class SList(T)
assert(l.remove() == 8);
assert(l.empty);
dispose(defaultAllocator, l);
dispose(theAllocator, l);
}
/**
@ -220,7 +220,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
auto l = make!(SList!int)(theAllocator);
int[2] values = [8, 5];
l.current = values[0];
@ -231,7 +231,7 @@ class SList(T)
l.reset();
assert(l.current == 5);
dispose(defaultAllocator, l);
dispose(theAllocator, l);
}
/**
@ -262,7 +262,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
auto l = make!(SList!int)(theAllocator);
int[3] values = [5, 4, 9];
l.front = values[0];
@ -275,7 +275,7 @@ class SList(T)
assert(i != 2 || e == values[0]);
}
dispose(defaultAllocator, l);
dispose(theAllocator, l);
}
/// Ditto.
@ -300,7 +300,7 @@ class SList(T)
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
auto l = make!(SList!int)(theAllocator);
int[3] values = [5, 4, 9];
size_t i;
@ -315,7 +315,7 @@ class SList(T)
++i;
}
dispose(defaultAllocator, l);
dispose(theAllocator, l);
}
/**
@ -398,7 +398,7 @@ interface Stuff
///
unittest
{
auto l = make!(SList!Stuff)(defaultAllocator);
auto l = make!(SList!Stuff)(theAllocator);
dispose(defaultAllocator, l);
dispose(theAllocator, l);
}

View File

@ -27,7 +27,7 @@ class Queue(T)
* allocator = The allocator should be used for the element
* allocations.
*/
this(IAllocator allocator = defaultAllocator)
this(IAllocator allocator = theAllocator)
{
this.allocator = allocator;
}
@ -91,7 +91,7 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
auto q = make!(Queue!int)(theAllocator);
int[2] values = [8, 9];
q.insertBack(values[0]);
@ -99,7 +99,7 @@ class Queue(T)
q.insertBack(values[1]);
assert(q.front is values[0]);
dispose(defaultAllocator, q);
dispose(theAllocator, q);
}
/**
@ -119,7 +119,7 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
auto q = make!(Queue!int)(theAllocator);
int value = 5;
assert(q.empty);
@ -129,7 +129,7 @@ class Queue(T)
assert(q.front == value);
assert(!q.empty);
dispose(defaultAllocator, q);
dispose(theAllocator, q);
}
/**
@ -143,14 +143,14 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
auto q = make!(Queue!int)(theAllocator);
int value = 7;
assert(q.empty);
q.insertBack(value);
assert(!q.empty);
dispose(defaultAllocator, q);
dispose(theAllocator, q);
}
/**
@ -176,7 +176,7 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
auto q = make!(Queue!int)(theAllocator);
int[2] values = [8, 9];
q.insertBack(values[0]);
@ -185,7 +185,7 @@ class Queue(T)
q.popFront();
assert(q.front is values[1]);
dispose(defaultAllocator, q);
dispose(theAllocator, q);
}
/**
@ -212,7 +212,7 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
auto q = make!(Queue!int)(theAllocator);
dispose(defaultAllocator, q);
dispose(theAllocator, q);
}

View File

@ -17,12 +17,12 @@ import tanya.memory;
*
* If you assign a value:
* ---
* auto v = make!(Vector!int)(defaultAllocator);
* auto v = make!(Vector!int)(theAllocator);
* int value = 5;
*
* v[1000] = value;
*
* dispose(defaultAllocator, v);
* dispose(theAllocator, v);
* ---
* it will allocate not only for one, but for 1000 elements. So this
* implementation is more suitable for sequential data with random access.
@ -40,14 +40,14 @@ class Vector(T)
* allocator = The allocator should be used for the element
* allocations.
*/
this(size_t length, IAllocator allocator = defaultAllocator)
this(size_t length, IAllocator allocator = theAllocator)
{
this.allocator = allocator;
vector = makeArray!T(allocator, length);
}
/// Ditto.
this(IAllocator allocator = defaultAllocator)
this(IAllocator allocator = theAllocator)
{
this(0, allocator);
}
@ -82,19 +82,18 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator);
auto v = make!(Vector!int)(theAllocator);
v.length = 5;
assert(v.length == 5);
// TODO
v.length = 7;
assert(v.length == 7);
v.length = 0;
assert(v.length == 0);
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}
/**
@ -140,7 +139,7 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator);
auto v = make!(Vector!int)(theAllocator);
int[2] values = [5, 15];
assert(v.length == 0);
@ -151,7 +150,7 @@ class Vector(T)
v[4] = values[1];
assert(v.length == 5);
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}
/**
@ -170,7 +169,7 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator);
auto v = make!(Vector!int)(theAllocator);
int[2] values = [5, 15];
v[1] = values[0];
@ -182,7 +181,7 @@ class Vector(T)
v[0] = values[1];
assert(v[0] is values[1]);
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}
/**
@ -227,7 +226,7 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator, 1);
auto v = make!(Vector!int)(theAllocator, 1);
int[3] values = [5, 15, 8];
v[0] = values[0];
@ -250,7 +249,7 @@ class Vector(T)
assert(j != 2 || e is values[2]);
}
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}
/**
@ -280,7 +279,7 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator, 1);
auto v = make!(Vector!int)(theAllocator, 1);
int[2] values = [5, 15];
v.front = values[0];
@ -289,7 +288,7 @@ class Vector(T)
v.front = values[1];
assert(v.front == 15);
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}
/**
@ -312,7 +311,7 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator, 1);
auto v = make!(Vector!int)(theAllocator, 1);
int[2] values = [5, 15];
v[0] = values[0];
@ -325,7 +324,7 @@ class Vector(T)
v.popFront();
assert(v.empty);
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}
/**
@ -355,7 +354,7 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator, 1);
auto v = make!(Vector!int)(theAllocator, 1);
int[2] values = [5, 15];
v.back = values[0];
@ -364,7 +363,7 @@ class Vector(T)
v.back = values[1];
assert(v.back == 15);
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}
/**
@ -386,7 +385,7 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator, 1);
auto v = make!(Vector!int)(theAllocator, 1);
int[2] values = [5, 15];
v[0] = values[0];
@ -399,7 +398,7 @@ class Vector(T)
v.popBack();
assert(v.empty);
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}
/// Container.
@ -411,7 +410,7 @@ class Vector(T)
///
unittest
{
auto v = make!(Vector!int)(defaultAllocator);
auto v = make!(Vector!int)(theAllocator);
dispose(defaultAllocator, v);
dispose(theAllocator, v);
}

View File

@ -44,7 +44,7 @@ enum PaddingMode
ubyte[] pad(ref ubyte[] input,
in PaddingMode mode,
in ushort blockSize,
IAllocator allocator = defaultAllocator)
IAllocator allocator = theAllocator)
in
{
assert(blockSize > 0 && blockSize <= 256);
@ -86,7 +86,7 @@ body
unittest
{
{ // Zeros
auto input = defaultAllocator.makeArray!ubyte(50);
auto input = theAllocator.makeArray!ubyte(50);
pad(input, PaddingMode.zero, 64);
assert(input.length == 64);
@ -95,10 +95,10 @@ unittest
assert(input.length == 64);
assert(input[63] == 0);
defaultAllocator.dispose(input);
theAllocator.dispose(input);
}
{ // PKCS#7
auto input = defaultAllocator.makeArray!ubyte(50);
auto input = theAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
@ -140,10 +140,10 @@ unittest
}
}
defaultAllocator.dispose(input);
theAllocator.dispose(input);
}
{ // ANSI X.923
auto input = defaultAllocator.makeArray!ubyte(50);
auto input = theAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
@ -185,7 +185,7 @@ unittest
}
}
defaultAllocator.dispose(input);
theAllocator.dispose(input);
}
}
@ -204,7 +204,7 @@ unittest
ref ubyte[] unpad(ref ubyte[] input,
in PaddingMode mode,
in ushort blockSize,
IAllocator allocator = defaultAllocator)
IAllocator allocator = theAllocator)
in
{
assert(input.length != 0);
@ -231,8 +231,8 @@ body
unittest
{
{ // Zeros
auto input = defaultAllocator.makeArray!ubyte(50);
auto inputDup = defaultAllocator.makeArray!ubyte(50);
auto input = theAllocator.makeArray!ubyte(50);
auto inputDup = theAllocator.makeArray!ubyte(50);
pad(input, PaddingMode.zero, 64);
pad(inputDup, PaddingMode.zero, 64);
@ -240,13 +240,13 @@ unittest
unpad(input, PaddingMode.zero, 64);
assert(input == inputDup);
defaultAllocator.dispose(input);
defaultAllocator.dispose(inputDup);
theAllocator.dispose(input);
theAllocator.dispose(inputDup);
}
{ // PKCS#7
auto input = defaultAllocator.makeArray!ubyte(50);
auto inputDup = defaultAllocator.makeArray!ubyte(50);
auto input = theAllocator.makeArray!ubyte(50);
auto inputDup = theAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
@ -257,12 +257,12 @@ unittest
unpad(input, PaddingMode.pkcs7, 64);
assert(input == inputDup);
defaultAllocator.dispose(input);
defaultAllocator.dispose(inputDup);
theAllocator.dispose(input);
theAllocator.dispose(inputDup);
}
{ // ANSI X.923
auto input = defaultAllocator.makeArray!ubyte(50);
auto inputDup = defaultAllocator.makeArray!ubyte(50);
auto input = theAllocator.makeArray!ubyte(50);
auto inputDup = theAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
@ -273,7 +273,7 @@ unittest
unpad(input, PaddingMode.pkcs7, 64);
assert(input == inputDup);
defaultAllocator.dispose(input);
defaultAllocator.dispose(inputDup);
theAllocator.dispose(input);
theAllocator.dispose(inputDup);
}
}

View File

@ -14,13 +14,8 @@ import std.experimental.allocator;
import std.traits;
import std.typecons : Ternary;
version (unittest)
{
import tanya.memory : defaultAllocator;
}
/**
* Allocator interface.
* Abstract class implementing a basic allocator.
*/
abstract class Allocator : IAllocator
{
@ -170,16 +165,16 @@ unittest
{
int[] p;
defaultAllocator.resizeArray(p, 20);
theAllocator.resizeArray(p, 20);
assert(p.length == 20);
defaultAllocator.resizeArray(p, 30);
theAllocator.resizeArray(p, 30);
assert(p.length == 30);
defaultAllocator.resizeArray(p, 10);
theAllocator.resizeArray(p, 10);
assert(p.length == 10);
defaultAllocator.resizeArray(p, 0);
theAllocator.resizeArray(p, 0);
assert(p is null);
}

View File

@ -12,8 +12,3 @@ module tanya.memory;
public import tanya.memory.allocator;
public import std.experimental.allocator;
@property IAllocator defaultAllocator()
{
return theAllocator;
}

View File

@ -252,7 +252,7 @@ else version (Windows)
if (result == SOCKET_ERROR && !wouldHaveBlocked)
{
throw defaultAllocator.make!SocketException("Unable to receive");
throw theAllocator.make!SocketException("Unable to receive");
}
return result == 0;
}
@ -282,7 +282,7 @@ else version (Windows)
if (result == FALSE && !wouldHaveBlocked)
{
disconnected_ = true;
throw defaultAllocator.make!SocketException("Unable to receive");
throw theAllocator.make!SocketException("Unable to receive");
}
if (lpNumber == 0)
{
@ -324,7 +324,7 @@ else version (Windows)
if (result == SOCKET_ERROR && !wouldHaveBlocked)
{
disconnected_ = true;
throw defaultAllocator.make!SocketException("Unable to send");
throw theAllocator.make!SocketException("Unable to send");
}
return result == 0;
}
@ -354,7 +354,7 @@ else version (Windows)
if (result == FALSE && !wouldHaveBlocked)
{
disconnected_ = true;
throw defaultAllocator.make!SocketException("Unable to receive");
throw theAllocator.make!SocketException("Unable to receive");
}
return lpNumber;
}
@ -396,7 +396,7 @@ else version (Windows)
NULL);
if (!result == SOCKET_ERROR)
{
throw defaultAllocator.make!SocketException("Unable to retrieve an accept extension function pointer");
throw theAllocator.make!SocketException("Unable to retrieve an accept extension function pointer");
}
}
@ -416,7 +416,7 @@ else version (Windows)
auto socket = cast(socket_t) socket(addressFamily, SOCK_STREAM, 0);
if (socket == socket_t.init)
{
throw defaultAllocator.make!SocketException("Unable to create socket");
throw theAllocator.make!SocketException("Unable to create socket");
}
scope (failure)
{
@ -426,7 +426,7 @@ else version (Windows)
overlapped.handle = cast(HANDLE) socket;
overlapped.event = OverlappedSocketEvent.accept;
overlapped.buffer.len = (sockaddr_in.sizeof + 16) * 2;
overlapped.buffer.buf = defaultAllocator.makeArray!char(overlapped.buffer.len).ptr;
overlapped.buffer.buf = theAllocator.makeArray!char(overlapped.buffer.len).ptr;
// We don't want to get any data now, but only start to accept the connections
BOOL result = acceptExtension(handle_,
@ -439,7 +439,7 @@ else version (Windows)
&overlapped.overlapped);
if (result == FALSE && !wouldHaveBlocked)
{
throw defaultAllocator.make!SocketException("Unable to accept socket connection");
throw theAllocator.make!SocketException("Unable to accept socket connection");
}
return result == TRUE;
}
@ -459,13 +459,13 @@ else version (Windows)
{
scope (exit)
{
defaultAllocator.dispose(overlapped.buffer.buf[0..overlapped.buffer.len]);
theAllocator.dispose(overlapped.buffer.buf[0..overlapped.buffer.len]);
}
auto socket = defaultAllocator.make!OverlappedConnectedSocket(cast(socket_t) overlapped.handle,
auto socket = theAllocator.make!OverlappedConnectedSocket(cast(socket_t) overlapped.handle,
addressFamily);
scope (failure)
{
defaultAllocator.dispose(socket);
theAllocator.dispose(socket);
}
socket.setOption(SocketOptionLevel.SOCKET,
cast(SocketOption) SO_UPDATE_ACCEPT_CONTEXT,
@ -737,7 +737,7 @@ abstract class Socket
result.ptr,
&length) == SOCKET_ERROR)
{
throw defaultAllocator.make!SocketException("Unable to get socket option");
throw theAllocator.make!SocketException("Unable to get socket option");
}
return length;
}
@ -797,7 +797,7 @@ abstract class Socket
value.ptr,
cast(uint) value.length) == SOCKET_ERROR)
{
throw defaultAllocator.make!SocketException("Unable to set socket option");
throw theAllocator.make!SocketException("Unable to set socket option");
}
}
@ -865,7 +865,7 @@ abstract class Socket
}
if (fl == SOCKET_ERROR)
{
throw defaultAllocator.make!SocketException("Unable to set socket blocking");
throw theAllocator.make!SocketException("Unable to set socket blocking");
}
}
else version (Windows)
@ -873,7 +873,7 @@ abstract class Socket
uint num = !yes;
if (ioctlsocket(handle_, FIONBIO, &num) == SOCKET_ERROR)
{
throw defaultAllocator.make!SocketException("Unable to set socket blocking");
throw theAllocator.make!SocketException("Unable to set socket blocking");
}
blocking_ = yes;
}
@ -942,7 +942,7 @@ abstract class Socket
{
if (.listen(handle_, backlog) == SOCKET_ERROR)
{
throw defaultAllocator.make!SocketException("Unable to listen on socket");
throw theAllocator.make!SocketException("Unable to listen on socket");
}
}
@ -992,7 +992,7 @@ class StreamSocket : Socket, ConnectionOrientedSocket
auto handle = cast(socket_t) socket(af, SOCK_STREAM, 0);
if (handle == socket_t.init)
{
throw defaultAllocator.make!SocketException("Unable to create socket");
throw theAllocator.make!SocketException("Unable to create socket");
}
super(handle, af);
}
@ -1009,7 +1009,7 @@ class StreamSocket : Socket, ConnectionOrientedSocket
{
if (.bind(handle_, address.name, address.length) == SOCKET_ERROR)
{
throw defaultAllocator.make!SocketException("Unable to bind socket");
throw theAllocator.make!SocketException("Unable to bind socket");
}
}
@ -1048,10 +1048,10 @@ class StreamSocket : Socket, ConnectionOrientedSocket
{
return null;
}
throw defaultAllocator.make!SocketException("Unable to accept socket connection");
throw theAllocator.make!SocketException("Unable to accept socket connection");
}
auto newSocket = defaultAllocator.make!ConnectedSocket(sock, addressFamily);
auto newSocket = theAllocator.make!ConnectedSocket(sock, addressFamily);
version (linux)
{ // Blocking mode already set
@ -1066,7 +1066,7 @@ class StreamSocket : Socket, ConnectionOrientedSocket
}
catch (SocketException e)
{
defaultAllocator.dispose(newSocket);
theAllocator.dispose(newSocket);
throw e;
}
}
@ -1162,7 +1162,7 @@ class ConnectedSocket : Socket, ConnectionOrientedSocket
return 0;
}
disconnected_ = true;
throw defaultAllocator.make!SocketException("Unable to receive");
throw theAllocator.make!SocketException("Unable to receive");
}
return ret;
}
@ -1199,7 +1199,7 @@ class ConnectedSocket : Socket, ConnectionOrientedSocket
{
return 0;
}
throw defaultAllocator.make!SocketException("Unable to send");
throw theAllocator.make!SocketException("Unable to send");
}
}
@ -1242,18 +1242,18 @@ class InternetAddress : Address
{
if (getaddrinfoPointer is null || freeaddrinfoPointer is null)
{
throw defaultAllocator.make!AddressException("Address info lookup is not available on this system");
throw theAllocator.make!AddressException("Address info lookup is not available on this system");
}
addrinfo* ai_res;
port_ = port;
// Make C-string from host.
char[] node = defaultAllocator.makeArray!char(host.length + 1);
char[] node = theAllocator.makeArray!char(host.length + 1);
node[0.. $ - 1] = host;
node[$ - 1] = '\0';
scope (exit)
{
defaultAllocator.dispose(node);
theAllocator.dispose(node);
}
// Convert port to a C-string.
@ -1278,7 +1278,7 @@ class InternetAddress : Address
auto ret = getaddrinfoPointer(node.ptr, servicePointer, null, &ai_res);
if (ret)
{
throw defaultAllocator.make!AddressException("Address info lookup failed");
throw theAllocator.make!AddressException("Address info lookup failed");
}
scope (exit)
{
@ -1292,7 +1292,7 @@ class InternetAddress : Address
}
if (ai_res.ai_family != AddressFamily.INET && ai_res.ai_family != AddressFamily.INET6)
{
throw defaultAllocator.make!AddressException("Wrong address family");
throw theAllocator.make!AddressException("Wrong address family");
}
}

View File

@ -148,13 +148,13 @@ version (linux)
/**
* Pseudorandom number generator.
* ---
* auto entropy = defaultAllocator.make!Entropy;
* auto entropy = theAllocator.make!Entropy;
*
* ubyte[blockSize] output;
*
* output = entropy.random;
*
* defaultAllocator.finalize(entropy);
* theAllocator.finalize(entropy);
* ---
*/
class Entropy
@ -175,7 +175,7 @@ class Entropy
* allocator = Allocator to allocate entropy sources available on the
* system.
*/
this(size_t maxSources = 20, IAllocator allocator = defaultAllocator)
this(size_t maxSources = 20, IAllocator allocator = theAllocator)
in
{
assert(maxSources > 0 && maxSources <= ubyte.max);