Compare commits
54 Commits
Author | SHA1 | Date | |
---|---|---|---|
ab930657b6 | |||
291920b479 | |||
999c9bdb0f | |||
405b6d9f9f | |||
87b74b2542 | |||
d6514cb515 | |||
976eb4bfbc | |||
fb843e3473 | |||
f3d48234c0 | |||
254b881da6 | |||
8e0b742748 | |||
a35e04c049 | |||
4271c8583e | |||
e27d0fe58c | |||
67952dabdb | |||
b8d5d4c2bd | |||
b6413823cd | |||
48e355b87f | |||
b3f4ea572e | |||
c73e704421 | |||
0561e96f21 | |||
86d87430da | |||
0156c5a883 | |||
c966b42ac3 | |||
200fff3714 | |||
28755b4d01 | |||
8bd6a14988 | |||
b41dcc9f37 | |||
38addb7a5b | |||
f7fb89fed0 | |||
e32af2d09e | |||
f1bc4dc2e2 | |||
40857e69b7 | |||
c1fb89af99 | |||
061cd6264b | |||
f437dafa6b | |||
54d0597657 | |||
ab9f96e0c7 | |||
711855474c | |||
b20f367aa8 | |||
a2dadda511 | |||
77dca31261 | |||
b87aed4395 | |||
42bbb3b023 | |||
4309a30dfe | |||
9362287938 | |||
78bd901339 | |||
c8e6d44f7b | |||
f75433e0e6 | |||
fa607141e4 | |||
b3fdd6fd4a | |||
86c08e7af6 | |||
1c5796eb96 | |||
f7f92e7906 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -4,3 +4,7 @@
|
|||||||
# D
|
# D
|
||||||
.dub
|
.dub
|
||||||
__test__*__
|
__test__*__
|
||||||
|
__test__*__.core
|
||||||
|
|
||||||
|
/docs/
|
||||||
|
/docs.json
|
||||||
|
17
.travis.yml
Normal file
17
.travis.yml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
sudo: false
|
||||||
|
|
||||||
|
os:
|
||||||
|
- linux
|
||||||
|
|
||||||
|
language: d
|
||||||
|
|
||||||
|
d:
|
||||||
|
- dmd-2.072.2
|
||||||
|
- dmd-2.071.2
|
||||||
|
|
||||||
|
env:
|
||||||
|
matrix:
|
||||||
|
- ARCH=x86_64
|
||||||
|
|
||||||
|
script:
|
||||||
|
- dub test --arch=$ARCH
|
71
README.md
71
README.md
@ -1,4 +1,69 @@
|
|||||||
# tanya
|
# Tanya
|
||||||
|
|
||||||
tanya's mission is to provide a GC-free, general purpose library for D
|
[](https://travis-ci.org/caraus-ecms/tanya)
|
||||||
programming language for developing servers.
|
[](https://code.dlang.org/packages/tanya)
|
||||||
|
[](https://code.dlang.org/packages/tanya)
|
||||||
|
[](https://raw.githubusercontent.com/caraus-ecms/tanya/master/LICENSE)
|
||||||
|
|
||||||
|
Tanya is a general purpose library for D programming language.
|
||||||
|
|
||||||
|
Its aim is to simplify the manual memory management in D and to provide a
|
||||||
|
guarantee with @nogc attribute that there are no hidden allocations on the
|
||||||
|
Garbage Collector heap. Everything in the library is usable in @nogc code.
|
||||||
|
Tanya extends Phobos functionality and provides alternative implementations for
|
||||||
|
data structures and utilities that depend on the Garbage Collector in Phobos.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
Tanya consists of the following packages:
|
||||||
|
|
||||||
|
* `async`: Event loop (epoll, kqueue and IOCP).
|
||||||
|
* `container`: Queue, Vector, Singly linked list, buffers.
|
||||||
|
* `crypto`: Work in progress TLS implementation.
|
||||||
|
* `math`: Multiple precision integer and a set of functions.
|
||||||
|
* `memory`: Tools for manual memory management (allocator, reference counting,
|
||||||
|
helper functions).
|
||||||
|
* `network`: URL-Parsing, sockets.
|
||||||
|
|
||||||
|
### Supported compilers
|
||||||
|
|
||||||
|
* dmd 2.072.2
|
||||||
|
* dmd 2.071.2
|
||||||
|
|
||||||
|
### Current status
|
||||||
|
|
||||||
|
The library is currently under development, but some parts of it can already be
|
||||||
|
used.
|
||||||
|
|
||||||
|
`network` and `async` exist for quite some time and could be better tested than
|
||||||
|
other components.
|
||||||
|
|
||||||
|
Containers were newly reworked and the API won't change significantly, but will
|
||||||
|
be only extended. The same is true for the `memory` package.
|
||||||
|
|
||||||
|
`math` package contains an arbitrary precision integer implementation that has
|
||||||
|
a stable API (that mostly consists of operator overloads), but still needs
|
||||||
|
testing and work on its performance.
|
||||||
|
|
||||||
|
I'm currently mostly working on `crypto` that is not a complete cryptographic
|
||||||
|
suite, but contains (will contain) algorithm implementations required by TLS.
|
||||||
|
|
||||||
|
### Further characteristics
|
||||||
|
|
||||||
|
* Tanya is a native D library.
|
||||||
|
|
||||||
|
* Documentation and usage examples can be found in the source code.
|
||||||
|
Online documentation will be published soon.
|
||||||
|
|
||||||
|
* Tanya is cross-platform. The development happens on a 64-bit Linux, but it
|
||||||
|
is being tested on Windows and FreeBSD as well.
|
||||||
|
|
||||||
|
* The library isn't thread-safe. Thread-safity should be added later.
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Since I'm mostly busy writing new code and implementing new features I would
|
||||||
|
appreciate, if anyone uses the library. It would help me to improve the
|
||||||
|
codebase and fix issues.
|
||||||
|
|
||||||
|
Feel free to contact me if you have any questions.
|
||||||
|
@ -26,153 +26,162 @@ import core.sys.posix.unistd;
|
|||||||
import core.time;
|
import core.time;
|
||||||
import std.algorithm.comparison;
|
import std.algorithm.comparison;
|
||||||
|
|
||||||
|
extern (C) nothrow @nogc
|
||||||
|
{
|
||||||
|
int epoll_create1(int flags);
|
||||||
|
int epoll_ctl (int epfd, int op, int fd, epoll_event *event);
|
||||||
|
int epoll_wait (int epfd, epoll_event *events, int maxevents, int timeout);
|
||||||
|
}
|
||||||
|
|
||||||
class EpollLoop : SelectorLoop
|
class EpollLoop : SelectorLoop
|
||||||
{
|
{
|
||||||
protected int fd;
|
protected int fd;
|
||||||
private epoll_event[] events;
|
private epoll_event[] events;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the loop.
|
* Initializes the loop.
|
||||||
*/
|
*/
|
||||||
this()
|
this() @nogc
|
||||||
{
|
{
|
||||||
if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
|
if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
|
||||||
{
|
{
|
||||||
throw MmapPool.instance.make!BadLoopException("epoll initialization failed");
|
throw MmapPool.instance.make!BadLoopException("epoll initialization failed");
|
||||||
}
|
}
|
||||||
super();
|
super();
|
||||||
events = MmapPool.instance.makeArray!epoll_event(maxEvents);
|
MmapPool.instance.resizeArray(events, maxEvents);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Free loop internals.
|
* Free loop internals.
|
||||||
*/
|
*/
|
||||||
~this()
|
~this() @nogc
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(events);
|
MmapPool.instance.dispose(events);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be called if the backend configuration changes.
|
* Should be called if the backend configuration changes.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* watcher = Watcher.
|
* watcher = Watcher.
|
||||||
* oldEvents = The events were already set.
|
* oldEvents = The events were already set.
|
||||||
* events = The events should be set.
|
* events = The events should be set.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||||
*/
|
*/
|
||||||
protected override bool reify(ConnectionWatcher watcher, EventMask oldEvents, EventMask events)
|
protected override bool reify(ConnectionWatcher watcher,
|
||||||
in
|
EventMask oldEvents,
|
||||||
{
|
EventMask events) @nogc
|
||||||
assert(watcher !is null);
|
in
|
||||||
}
|
{
|
||||||
body
|
assert(watcher !is null);
|
||||||
{
|
}
|
||||||
int op = EPOLL_CTL_DEL;
|
body
|
||||||
epoll_event ev;
|
{
|
||||||
|
int op = EPOLL_CTL_DEL;
|
||||||
|
epoll_event ev;
|
||||||
|
|
||||||
if (events == oldEvents)
|
if (events == oldEvents)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (events && oldEvents)
|
if (events && oldEvents)
|
||||||
{
|
{
|
||||||
op = EPOLL_CTL_MOD;
|
op = EPOLL_CTL_MOD;
|
||||||
}
|
}
|
||||||
else if (events && !oldEvents)
|
else if (events && !oldEvents)
|
||||||
{
|
{
|
||||||
op = EPOLL_CTL_ADD;
|
op = EPOLL_CTL_ADD;
|
||||||
}
|
}
|
||||||
|
|
||||||
ev.data.fd = watcher.socket.handle;
|
ev.data.fd = watcher.socket.handle;
|
||||||
ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0)
|
ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0)
|
||||||
| (events & Event.write ? EPOLLOUT : 0)
|
| (events & Event.write ? EPOLLOUT : 0)
|
||||||
| EPOLLET;
|
| EPOLLET;
|
||||||
|
|
||||||
return epoll_ctl(fd, op, watcher.socket.handle, &ev) == 0;
|
return epoll_ctl(fd, op, watcher.socket.handle, &ev) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does the actual polling.
|
* Does the actual polling.
|
||||||
*/
|
*/
|
||||||
protected override void poll()
|
protected override void poll() @nogc
|
||||||
{
|
{
|
||||||
// Don't block
|
// Don't block
|
||||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
||||||
auto eventCount = epoll_wait(fd, events.ptr, maxEvents, timeout);
|
auto eventCount = epoll_wait(fd, events.ptr, maxEvents, timeout);
|
||||||
|
|
||||||
if (eventCount < 0)
|
if (eventCount < 0)
|
||||||
{
|
{
|
||||||
if (errno != EINTR)
|
if (errno != EINTR)
|
||||||
{
|
{
|
||||||
throw theAllocator.make!BadLoopException();
|
throw defaultAllocator.make!BadLoopException();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto i = 0; i < eventCount; ++i)
|
for (auto i = 0; i < eventCount; ++i)
|
||||||
{
|
{
|
||||||
auto io = cast(IOWatcher) connections[events[i].data.fd];
|
auto io = cast(IOWatcher) connections[events[i].data.fd];
|
||||||
|
|
||||||
if (io is null)
|
if (io is null)
|
||||||
{
|
{
|
||||||
acceptConnections(connections[events[i].data.fd]);
|
acceptConnections(connections[events[i].data.fd]);
|
||||||
}
|
}
|
||||||
else if (events[i].events & EPOLLERR)
|
else if (events[i].events & EPOLLERR)
|
||||||
{
|
{
|
||||||
kill(io, null);
|
kill(io, null);
|
||||||
}
|
}
|
||||||
else if (events[i].events & (EPOLLIN | EPOLLPRI | EPOLLHUP))
|
else if (events[i].events & (EPOLLIN | EPOLLPRI | EPOLLHUP))
|
||||||
{
|
{
|
||||||
auto transport = cast(SelectorStreamTransport) io.transport;
|
auto transport = cast(SelectorStreamTransport) io.transport;
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
|
|
||||||
SocketException exception;
|
SocketException exception;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ptrdiff_t received;
|
ptrdiff_t received;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
received = transport.socket.receive(io.output[]);
|
received = transport.socket.receive(io.output[]);
|
||||||
io.output += received;
|
io.output += received;
|
||||||
}
|
}
|
||||||
while (received);
|
while (received);
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
exception = e;
|
exception = e;
|
||||||
}
|
}
|
||||||
if (transport.socket.disconnected)
|
if (transport.socket.disconnected)
|
||||||
{
|
{
|
||||||
kill(io, exception);
|
kill(io, exception);
|
||||||
}
|
}
|
||||||
else if (io.output.length)
|
else if (io.output.length)
|
||||||
{
|
{
|
||||||
swapPendings.insertBack(io);
|
swapPendings.enqueue(io);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (events[i].events & EPOLLOUT)
|
else if (events[i].events & EPOLLOUT)
|
||||||
{
|
{
|
||||||
auto transport = cast(SelectorStreamTransport) io.transport;
|
auto transport = cast(SelectorStreamTransport) io.transport;
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
|
|
||||||
transport.writeReady = true;
|
transport.writeReady = true;
|
||||||
if (transport.input.length)
|
if (transport.input.length)
|
||||||
{
|
{
|
||||||
feed(transport);
|
feed(transport);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: The blocking time.
|
* Returns: The blocking time.
|
||||||
*/
|
*/
|
||||||
override protected @property inout(Duration) blockTime()
|
override protected @property inout(Duration) blockTime()
|
||||||
inout @safe pure nothrow
|
inout @safe pure nothrow
|
||||||
{
|
{
|
||||||
return min(super.blockTime, 1.dur!"seconds");
|
return min(super.blockTime, 1.dur!"seconds");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,267 +28,264 @@ import core.sys.windows.winsock2;
|
|||||||
|
|
||||||
class IOCPStreamTransport : StreamTransport
|
class IOCPStreamTransport : StreamTransport
|
||||||
{
|
{
|
||||||
private OverlappedConnectedSocket socket_;
|
private OverlappedConnectedSocket socket_;
|
||||||
|
|
||||||
private WriteBuffer input;
|
private WriteBuffer!ubyte input;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates new completion port transport.
|
* Creates new completion port transport.
|
||||||
* Params:
|
* Params:
|
||||||
* socket = Socket.
|
* socket = Socket.
|
||||||
*/
|
*/
|
||||||
this(OverlappedConnectedSocket socket)
|
this(OverlappedConnectedSocket socket) @nogc
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(socket !is null);
|
assert(socket !is null);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
socket_ = socket;
|
socket_ = socket;
|
||||||
input = MmapPool.instance.make!WriteBuffer();
|
input = WriteBuffer!ubyte(8192, MmapPool.instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
~this()
|
@property inout(OverlappedConnectedSocket) socket()
|
||||||
{
|
inout pure nothrow @safe @nogc
|
||||||
MmapPool.instance.dispose(input);
|
{
|
||||||
}
|
return socket_;
|
||||||
|
}
|
||||||
|
|
||||||
@property inout(OverlappedConnectedSocket) socket() inout pure nothrow @safe @nogc
|
/**
|
||||||
{
|
* Write some data to the transport.
|
||||||
return socket_;
|
*
|
||||||
}
|
* Params:
|
||||||
|
* data = Data to send.
|
||||||
/**
|
*/
|
||||||
* Write some data to the transport.
|
void write(ubyte[] data) @nogc
|
||||||
*
|
{
|
||||||
* Params:
|
immutable empty = input.length == 0;
|
||||||
* data = Data to send.
|
input ~= data;
|
||||||
*/
|
if (empty)
|
||||||
void write(ubyte[] data)
|
{
|
||||||
{
|
SocketState overlapped;
|
||||||
immutable empty = input.length == 0;
|
try
|
||||||
input ~= data;
|
{
|
||||||
if (empty)
|
overlapped = MmapPool.instance.make!SocketState;
|
||||||
{
|
socket.beginSend(input[], overlapped);
|
||||||
SocketState overlapped;
|
}
|
||||||
try
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
overlapped = MmapPool.instance.make!SocketState;
|
MmapPool.instance.dispose(overlapped);
|
||||||
socket.beginSend(input[], overlapped);
|
MmapPool.instance.dispose(e);
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
}
|
||||||
{
|
}
|
||||||
MmapPool.instance.dispose(overlapped);
|
|
||||||
MmapPool.instance.dispose(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class IOCPLoop : Loop
|
class IOCPLoop : Loop
|
||||||
{
|
{
|
||||||
protected HANDLE completionPort;
|
protected HANDLE completionPort;
|
||||||
|
|
||||||
protected OVERLAPPED overlap;
|
protected OVERLAPPED overlap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the loop.
|
* Initializes the loop.
|
||||||
*/
|
*/
|
||||||
this()
|
this() @nogc
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
|
|
||||||
completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
||||||
if (!completionPort)
|
if (!completionPort)
|
||||||
{
|
{
|
||||||
throw theAllocator.make!BadLoopException("Creating completion port failed");
|
throw make!BadLoopException(defaultAllocator,
|
||||||
}
|
"Creating completion port failed");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be called if the backend configuration changes.
|
* Should be called if the backend configuration changes.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* watcher = Watcher.
|
* watcher = Watcher.
|
||||||
* oldEvents = The events were already set.
|
* oldEvents = The events were already set.
|
||||||
* events = The events should be set.
|
* events = The events should be set.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||||
*/
|
*/
|
||||||
override protected bool reify(ConnectionWatcher watcher,
|
override protected bool reify(ConnectionWatcher watcher,
|
||||||
EventMask oldEvents,
|
EventMask oldEvents,
|
||||||
EventMask events)
|
EventMask events) @nogc
|
||||||
{
|
{
|
||||||
SocketState overlapped;
|
SocketState overlapped;
|
||||||
if (!(oldEvents & Event.accept) && (events & Event.accept))
|
if (!(oldEvents & Event.accept) && (events & Event.accept))
|
||||||
{
|
{
|
||||||
auto socket = cast(OverlappedStreamSocket) watcher.socket;
|
auto socket = cast(OverlappedStreamSocket) watcher.socket;
|
||||||
assert(socket !is null);
|
assert(socket !is null);
|
||||||
|
|
||||||
if (CreateIoCompletionPort(cast(HANDLE) socket.handle,
|
if (CreateIoCompletionPort(cast(HANDLE) socket.handle,
|
||||||
completionPort,
|
completionPort,
|
||||||
cast(ULONG_PTR) (cast(void*) watcher),
|
cast(ULONG_PTR) (cast(void*) watcher),
|
||||||
0) !is completionPort)
|
0) !is completionPort)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
overlapped = MmapPool.instance.make!SocketState;
|
overlapped = MmapPool.instance.make!SocketState;
|
||||||
socket.beginAccept(overlapped);
|
socket.beginAccept(overlapped);
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(overlapped);
|
MmapPool.instance.dispose(overlapped);
|
||||||
theAllocator.dispose(e);
|
defaultAllocator.dispose(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!(oldEvents & Event.read) && (events & Event.read)
|
if (!(oldEvents & Event.read) && (events & Event.read)
|
||||||
|| !(oldEvents & Event.write) && (events & Event.write))
|
|| !(oldEvents & Event.write) && (events & Event.write))
|
||||||
{
|
{
|
||||||
auto io = cast(IOWatcher) watcher;
|
auto io = cast(IOWatcher) watcher;
|
||||||
assert(io !is null);
|
assert(io !is null);
|
||||||
|
|
||||||
auto transport = cast(IOCPStreamTransport) io.transport;
|
auto transport = cast(IOCPStreamTransport) io.transport;
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
|
|
||||||
if (CreateIoCompletionPort(cast(HANDLE) transport.socket.handle,
|
if (CreateIoCompletionPort(cast(HANDLE) transport.socket.handle,
|
||||||
completionPort,
|
completionPort,
|
||||||
cast(ULONG_PTR) (cast(void*) watcher),
|
cast(ULONG_PTR) (cast(void*) watcher),
|
||||||
0) !is completionPort)
|
0) !is completionPort)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin to read
|
// Begin to read
|
||||||
if (!(oldEvents & Event.read) && (events & Event.read))
|
if (!(oldEvents & Event.read) && (events & Event.read))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
overlapped = MmapPool.instance.make!SocketState;
|
overlapped = MmapPool.instance.make!SocketState;
|
||||||
transport.socket.beginReceive(io.output[], overlapped);
|
transport.socket.beginReceive(io.output[], overlapped);
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(overlapped);
|
MmapPool.instance.dispose(overlapped);
|
||||||
theAllocator.dispose(e);
|
defaultAllocator.dispose(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does the actual polling.
|
* Does the actual polling.
|
||||||
*/
|
*/
|
||||||
override protected void poll()
|
override protected void poll() @nogc
|
||||||
{
|
{
|
||||||
DWORD lpNumberOfBytes;
|
DWORD lpNumberOfBytes;
|
||||||
ULONG_PTR key;
|
ULONG_PTR key;
|
||||||
LPOVERLAPPED overlap;
|
LPOVERLAPPED overlap;
|
||||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
||||||
|
|
||||||
auto result = GetQueuedCompletionStatus(completionPort,
|
auto result = GetQueuedCompletionStatus(completionPort,
|
||||||
&lpNumberOfBytes,
|
&lpNumberOfBytes,
|
||||||
&key,
|
&key,
|
||||||
&overlap,
|
&overlap,
|
||||||
timeout);
|
timeout);
|
||||||
if (result == FALSE && overlap == NULL)
|
if (result == FALSE && overlap == NULL)
|
||||||
{
|
{
|
||||||
return; // Timeout
|
return; // Timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
auto overlapped = (cast(SocketState) ((cast(void*) overlap) - 8));
|
auto overlapped = (cast(SocketState) ((cast(void*) overlap) - 8));
|
||||||
assert(overlapped !is null);
|
assert(overlapped !is null);
|
||||||
scope (failure)
|
scope (failure)
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(overlapped);
|
MmapPool.instance.dispose(overlapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (overlapped.event)
|
switch (overlapped.event)
|
||||||
{
|
{
|
||||||
case OverlappedSocketEvent.accept:
|
case OverlappedSocketEvent.accept:
|
||||||
auto connection = cast(ConnectionWatcher) (cast(void*) key);
|
auto connection = cast(ConnectionWatcher) (cast(void*) key);
|
||||||
assert(connection !is null);
|
assert(connection !is null);
|
||||||
|
|
||||||
auto listener = cast(OverlappedStreamSocket) connection.socket;
|
auto listener = cast(OverlappedStreamSocket) connection.socket;
|
||||||
assert(listener !is null);
|
assert(listener !is null);
|
||||||
|
|
||||||
auto socket = listener.endAccept(overlapped);
|
auto socket = listener.endAccept(overlapped);
|
||||||
auto transport = MmapPool.instance.make!IOCPStreamTransport(socket);
|
auto transport = MmapPool.instance.make!IOCPStreamTransport(socket);
|
||||||
auto io = MmapPool.instance.make!IOWatcher(transport, connection.protocol);
|
auto io = MmapPool.instance.make!IOWatcher(transport, connection.protocol);
|
||||||
|
|
||||||
connection.incoming.insertBack(io);
|
connection.incoming.enqueue(io);
|
||||||
|
|
||||||
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
|
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
|
||||||
|
|
||||||
swapPendings.insertBack(connection);
|
swapPendings.enqueue(connection);
|
||||||
listener.beginAccept(overlapped);
|
listener.beginAccept(overlapped);
|
||||||
break;
|
break;
|
||||||
case OverlappedSocketEvent.read:
|
case OverlappedSocketEvent.read:
|
||||||
auto io = cast(IOWatcher) (cast(void*) key);
|
auto io = cast(IOWatcher) (cast(void*) key);
|
||||||
assert(io !is null);
|
assert(io !is null);
|
||||||
if (!io.active)
|
if (!io.active)
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(io);
|
MmapPool.instance.dispose(io);
|
||||||
MmapPool.instance.dispose(overlapped);
|
MmapPool.instance.dispose(overlapped);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto transport = cast(IOCPStreamTransport) io.transport;
|
auto transport = cast(IOCPStreamTransport) io.transport;
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
|
|
||||||
int received;
|
int received;
|
||||||
SocketException exception;
|
SocketException exception;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
received = transport.socket.endReceive(overlapped);
|
received = transport.socket.endReceive(overlapped);
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
exception = e;
|
exception = e;
|
||||||
}
|
}
|
||||||
if (transport.socket.disconnected)
|
if (transport.socket.disconnected)
|
||||||
{
|
{
|
||||||
// We want to get one last notification to destroy the watcher
|
// We want to get one last notification to destroy the watcher
|
||||||
transport.socket.beginReceive(io.output[], overlapped);
|
transport.socket.beginReceive(io.output[], overlapped);
|
||||||
kill(io, exception);
|
kill(io, exception);
|
||||||
}
|
}
|
||||||
else if (received > 0)
|
else if (received > 0)
|
||||||
{
|
{
|
||||||
immutable full = io.output.free == received;
|
immutable full = io.output.free == received;
|
||||||
|
|
||||||
io.output += received;
|
io.output += received;
|
||||||
// Receive was interrupted because the buffer is full. We have to continue
|
// Receive was interrupted because the buffer is full. We have to continue
|
||||||
if (full)
|
if (full)
|
||||||
{
|
{
|
||||||
transport.socket.beginReceive(io.output[], overlapped);
|
transport.socket.beginReceive(io.output[], overlapped);
|
||||||
}
|
}
|
||||||
swapPendings.insertBack(io);
|
swapPendings.enqueue(io);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case OverlappedSocketEvent.write:
|
case OverlappedSocketEvent.write:
|
||||||
auto io = cast(IOWatcher) (cast(void*) key);
|
auto io = cast(IOWatcher) (cast(void*) key);
|
||||||
assert(io !is null);
|
assert(io !is null);
|
||||||
|
|
||||||
auto transport = cast(IOCPStreamTransport) io.transport;
|
auto transport = cast(IOCPStreamTransport) io.transport;
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
|
|
||||||
transport.input += transport.socket.endSend(overlapped);
|
transport.input += transport.socket.endSend(overlapped);
|
||||||
if (transport.input.length)
|
if (transport.input.length)
|
||||||
{
|
{
|
||||||
transport.socket.beginSend(transport.input[], overlapped);
|
transport.socket.beginSend(transport.input[], overlapped);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
transport.socket.beginReceive(io.output[], overlapped);
|
transport.socket.beginReceive(io.output[], overlapped);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(false, "Unknown event");
|
assert(false, "Unknown event");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,127 +6,102 @@
|
|||||||
* 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.async.event.kqueue;
|
module tanya.async.event.kqueue;
|
||||||
|
|
||||||
version (OSX)
|
version (OSX)
|
||||||
{
|
{
|
||||||
version = MissingKevent;
|
version = MacBSD;
|
||||||
}
|
}
|
||||||
else version (iOS)
|
else version (iOS)
|
||||||
{
|
{
|
||||||
version = MissingKevent;
|
version = MacBSD;
|
||||||
}
|
}
|
||||||
else version (TVOS)
|
else version (TVOS)
|
||||||
{
|
{
|
||||||
version = MissingKevent;
|
version = MacBSD;
|
||||||
}
|
}
|
||||||
else version (WatchOS)
|
else version (WatchOS)
|
||||||
{
|
{
|
||||||
version = MissingKevent;
|
version = MacBSD;
|
||||||
}
|
|
||||||
else version (OpenBSD)
|
|
||||||
{
|
|
||||||
version = MissingKevent;
|
|
||||||
}
|
|
||||||
else version (DragonFlyBSD)
|
|
||||||
{
|
|
||||||
version = MissingKevent;
|
|
||||||
}
|
|
||||||
|
|
||||||
version (MissingKevent)
|
|
||||||
{
|
|
||||||
extern (C):
|
|
||||||
nothrow:
|
|
||||||
@nogc:
|
|
||||||
|
|
||||||
import core.stdc.stdint; // intptr_t, uintptr_t
|
|
||||||
import core.sys.posix.time; // timespec
|
|
||||||
|
|
||||||
enum : short
|
|
||||||
{
|
|
||||||
EVFILT_READ = -1,
|
|
||||||
EVFILT_WRITE = -2,
|
|
||||||
EVFILT_AIO = -3, /* attached to aio requests */
|
|
||||||
EVFILT_VNODE = -4, /* attached to vnodes */
|
|
||||||
EVFILT_PROC = -5, /* attached to struct proc */
|
|
||||||
EVFILT_SIGNAL = -6, /* attached to struct proc */
|
|
||||||
EVFILT_TIMER = -7, /* timers */
|
|
||||||
EVFILT_MACHPORT = -8, /* Mach portsets */
|
|
||||||
EVFILT_FS = -9, /* filesystem events */
|
|
||||||
EVFILT_USER = -10, /* User events */
|
|
||||||
EVFILT_VM = -12, /* virtual memory events */
|
|
||||||
EVFILT_SYSCOUNT = 11
|
|
||||||
}
|
|
||||||
|
|
||||||
extern(D) void EV_SET(kevent_t* kevp, typeof(kevent_t.tupleof) args)
|
|
||||||
{
|
|
||||||
*kevp = kevent_t(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct kevent_t
|
|
||||||
{
|
|
||||||
uintptr_t ident; /* identifier for this event */
|
|
||||||
short filter; /* filter for event */
|
|
||||||
ushort flags;
|
|
||||||
uint fflags;
|
|
||||||
intptr_t data;
|
|
||||||
void *udata; /* opaque user data identifier */
|
|
||||||
}
|
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
/* actions */
|
|
||||||
EV_ADD = 0x0001, /* add event to kq (implies enable) */
|
|
||||||
EV_DELETE = 0x0002, /* delete event from kq */
|
|
||||||
EV_ENABLE = 0x0004, /* enable event */
|
|
||||||
EV_DISABLE = 0x0008, /* disable event (not reported) */
|
|
||||||
|
|
||||||
/* flags */
|
|
||||||
EV_ONESHOT = 0x0010, /* only report one occurrence */
|
|
||||||
EV_CLEAR = 0x0020, /* clear event state after reporting */
|
|
||||||
EV_RECEIPT = 0x0040, /* force EV_ERROR on success, data=0 */
|
|
||||||
EV_DISPATCH = 0x0080, /* disable event after reporting */
|
|
||||||
|
|
||||||
EV_SYSFLAGS = 0xF000, /* reserved by system */
|
|
||||||
EV_FLAG1 = 0x2000, /* filter-specific flag */
|
|
||||||
|
|
||||||
/* returned values */
|
|
||||||
EV_EOF = 0x8000, /* EOF detected */
|
|
||||||
EV_ERROR = 0x4000, /* error, data contains errno */
|
|
||||||
}
|
|
||||||
|
|
||||||
int kqueue();
|
|
||||||
int kevent(int kq, const kevent_t *changelist, int nchanges,
|
|
||||||
kevent_t *eventlist, int nevents,
|
|
||||||
const timespec *timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
version (OSX)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
|
||||||
else version (iOS)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
}
|
||||||
else version (FreeBSD)
|
else version (FreeBSD)
|
||||||
{
|
{
|
||||||
version = MacBSD;
|
version = MacBSD;
|
||||||
public import core.sys.freebsd.sys.event;
|
|
||||||
}
|
}
|
||||||
else version (OpenBSD)
|
else version (OpenBSD)
|
||||||
{
|
{
|
||||||
version = MacBSD;
|
version = MacBSD;
|
||||||
}
|
}
|
||||||
else version (DragonFlyBSD)
|
else version (DragonFlyBSD)
|
||||||
{
|
{
|
||||||
version = MacBSD;
|
version = MacBSD;
|
||||||
}
|
}
|
||||||
|
|
||||||
version (MacBSD):
|
version (MacBSD):
|
||||||
|
|
||||||
|
import core.stdc.stdint; // intptr_t, uintptr_t
|
||||||
|
import core.sys.posix.time; // timespec
|
||||||
|
|
||||||
|
void EV_SET(kevent_t* kevp, typeof(kevent_t.tupleof) args) pure nothrow @nogc
|
||||||
|
{
|
||||||
|
*kevp = kevent_t(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
enum : short
|
||||||
|
{
|
||||||
|
EVFILT_READ = -1,
|
||||||
|
EVFILT_WRITE = -2,
|
||||||
|
EVFILT_AIO = -3, /* attached to aio requests */
|
||||||
|
EVFILT_VNODE = -4, /* attached to vnodes */
|
||||||
|
EVFILT_PROC = -5, /* attached to struct proc */
|
||||||
|
EVFILT_SIGNAL = -6, /* attached to struct proc */
|
||||||
|
EVFILT_TIMER = -7, /* timers */
|
||||||
|
EVFILT_MACHPORT = -8, /* Mach portsets */
|
||||||
|
EVFILT_FS = -9, /* filesystem events */
|
||||||
|
EVFILT_USER = -10, /* User events */
|
||||||
|
EVFILT_VM = -12, /* virtual memory events */
|
||||||
|
EVFILT_SYSCOUNT = 11
|
||||||
|
}
|
||||||
|
|
||||||
|
struct kevent_t
|
||||||
|
{
|
||||||
|
uintptr_t ident; /* identifier for this event */
|
||||||
|
short filter; /* filter for event */
|
||||||
|
ushort flags;
|
||||||
|
uint fflags;
|
||||||
|
intptr_t data;
|
||||||
|
void *udata; /* opaque user data identifier */
|
||||||
|
}
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
/* actions */
|
||||||
|
EV_ADD = 0x0001, /* add event to kq (implies enable) */
|
||||||
|
EV_DELETE = 0x0002, /* delete event from kq */
|
||||||
|
EV_ENABLE = 0x0004, /* enable event */
|
||||||
|
EV_DISABLE = 0x0008, /* disable event (not reported) */
|
||||||
|
|
||||||
|
/* flags */
|
||||||
|
EV_ONESHOT = 0x0010, /* only report one occurrence */
|
||||||
|
EV_CLEAR = 0x0020, /* clear event state after reporting */
|
||||||
|
EV_RECEIPT = 0x0040, /* force EV_ERROR on success, data=0 */
|
||||||
|
EV_DISPATCH = 0x0080, /* disable event after reporting */
|
||||||
|
|
||||||
|
EV_SYSFLAGS = 0xF000, /* reserved by system */
|
||||||
|
EV_FLAG1 = 0x2000, /* filter-specific flag */
|
||||||
|
|
||||||
|
/* returned values */
|
||||||
|
EV_EOF = 0x8000, /* EOF detected */
|
||||||
|
EV_ERROR = 0x4000, /* error, data contains errno */
|
||||||
|
}
|
||||||
|
|
||||||
|
extern(C) int kqueue() nothrow @nogc;
|
||||||
|
extern(C) int kevent(int kq, const kevent_t *changelist, int nchanges,
|
||||||
|
kevent_t *eventlist, int nevents, const timespec *timeout)
|
||||||
|
nothrow @nogc;
|
||||||
|
|
||||||
import tanya.async.event.selector;
|
import tanya.async.event.selector;
|
||||||
import tanya.async.loop;
|
import tanya.async.loop;
|
||||||
import tanya.async.transport;
|
import tanya.async.transport;
|
||||||
@ -142,208 +117,210 @@ import std.algorithm.comparison;
|
|||||||
|
|
||||||
class KqueueLoop : SelectorLoop
|
class KqueueLoop : SelectorLoop
|
||||||
{
|
{
|
||||||
protected int fd;
|
protected int fd;
|
||||||
private kevent_t[] events;
|
private kevent_t[] events;
|
||||||
private kevent_t[] changes;
|
private kevent_t[] changes;
|
||||||
private size_t changeCount;
|
private size_t changeCount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Maximal event count can be got at a time
|
* Returns: Maximal event count can be got at a time
|
||||||
* (should be supported by the backend).
|
* (should be supported by the backend).
|
||||||
*/
|
*/
|
||||||
override protected @property inout(uint) maxEvents() inout const pure nothrow @safe @nogc
|
override protected @property inout(uint) maxEvents()
|
||||||
{
|
inout const pure nothrow @safe @nogc
|
||||||
return cast(uint) events.length;
|
{
|
||||||
}
|
return cast(uint) events.length;
|
||||||
|
}
|
||||||
|
|
||||||
this()
|
this() @nogc
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
|
|
||||||
if ((fd = kqueue()) == -1)
|
if ((fd = kqueue()) == -1)
|
||||||
{
|
{
|
||||||
throw MmapPool.instance.make!BadLoopException("epoll initialization failed");
|
throw MmapPool.instance.make!BadLoopException("epoll initialization failed");
|
||||||
}
|
}
|
||||||
events = MmapPool.instance.makeArray!kevent_t(64);
|
MmapPool.instance.resizeArray(events, 64);
|
||||||
changes = MmapPool.instance.makeArray!kevent_t(64);
|
MmapPool.instance.resizeArray(changes, 64);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Free loop internals.
|
* Free loop internals.
|
||||||
*/
|
*/
|
||||||
~this()
|
~this() @nogc
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(events);
|
MmapPool.instance.dispose(events);
|
||||||
MmapPool.instance.dispose(changes);
|
MmapPool.instance.dispose(changes);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void set(socket_t socket, short filter, ushort flags)
|
private void set(socket_t socket, short filter, ushort flags) @nogc
|
||||||
{
|
{
|
||||||
if (changes.length <= changeCount)
|
if (changes.length <= changeCount)
|
||||||
{
|
{
|
||||||
MmapPool.instance.resizeArray(changes, changeCount + maxEvents);
|
MmapPool.instance.resizeArray(changes, changeCount + maxEvents);
|
||||||
}
|
}
|
||||||
EV_SET(&changes[changeCount],
|
EV_SET(&changes[changeCount],
|
||||||
cast(ulong) socket,
|
cast(ulong) socket,
|
||||||
filter,
|
filter,
|
||||||
flags,
|
flags,
|
||||||
0U,
|
0U,
|
||||||
0L,
|
0L,
|
||||||
null);
|
null);
|
||||||
++changeCount;
|
++changeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be called if the backend configuration changes.
|
* Should be called if the backend configuration changes.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* watcher = Watcher.
|
* watcher = Watcher.
|
||||||
* oldEvents = The events were already set.
|
* oldEvents = The events were already set.
|
||||||
* events = The events should be set.
|
* events = The events should be set.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||||
*/
|
*/
|
||||||
override protected bool reify(ConnectionWatcher watcher,
|
override protected bool reify(ConnectionWatcher watcher,
|
||||||
EventMask oldEvents,
|
EventMask oldEvents,
|
||||||
EventMask events)
|
EventMask events) @nogc
|
||||||
{
|
{
|
||||||
if (events != oldEvents)
|
if (events != oldEvents)
|
||||||
{
|
{
|
||||||
if (oldEvents & Event.read || oldEvents & Event.accept)
|
if (oldEvents & Event.read || oldEvents & Event.accept)
|
||||||
{
|
{
|
||||||
set(watcher.socket.handle, EVFILT_READ, EV_DELETE);
|
set(watcher.socket.handle, EVFILT_READ, EV_DELETE);
|
||||||
}
|
}
|
||||||
if (oldEvents & Event.write)
|
if (oldEvents & Event.write)
|
||||||
{
|
{
|
||||||
set(watcher.socket.handle, EVFILT_WRITE, EV_DELETE);
|
set(watcher.socket.handle, EVFILT_WRITE, EV_DELETE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (events & (Event.read | events & Event.accept))
|
if (events & (Event.read | events & Event.accept))
|
||||||
{
|
{
|
||||||
set(watcher.socket.handle, EVFILT_READ, EV_ADD | EV_ENABLE);
|
set(watcher.socket.handle, EVFILT_READ, EV_ADD | EV_ENABLE);
|
||||||
}
|
}
|
||||||
if (events & Event.write)
|
if (events & Event.write)
|
||||||
{
|
{
|
||||||
set(watcher.socket.handle, EVFILT_WRITE, EV_ADD | EV_DISPATCH);
|
set(watcher.socket.handle, EVFILT_WRITE, EV_ADD | EV_DISPATCH);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does the actual polling.
|
* Does the actual polling.
|
||||||
*/
|
*/
|
||||||
protected override void poll()
|
protected override void poll() @nogc
|
||||||
{
|
{
|
||||||
timespec ts;
|
timespec ts;
|
||||||
blockTime.split!("seconds", "nsecs")(ts.tv_sec, ts.tv_nsec);
|
blockTime.split!("seconds", "nsecs")(ts.tv_sec, ts.tv_nsec);
|
||||||
|
|
||||||
if (changeCount > maxEvents)
|
if (changeCount > maxEvents)
|
||||||
{
|
{
|
||||||
MmapPool.instance.resizeArray(events, changes.length);
|
MmapPool.instance.resizeArray(events, changes.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto eventCount = kevent(fd, changes.ptr, cast(int) changeCount, events.ptr, maxEvents, &ts);
|
auto eventCount = kevent(fd, changes.ptr, cast(int) changeCount, events.ptr, maxEvents, &ts);
|
||||||
changeCount = 0;
|
changeCount = 0;
|
||||||
|
|
||||||
if (eventCount < 0)
|
if (eventCount < 0)
|
||||||
{
|
{
|
||||||
if (errno != EINTR)
|
if (errno != EINTR)
|
||||||
{
|
{
|
||||||
throw theAllocator.make!BadLoopException();
|
throw defaultAllocator.make!BadLoopException();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i; i < eventCount; ++i)
|
for (int i; i < eventCount; ++i)
|
||||||
{
|
{
|
||||||
assert(connections.length > events[i].ident);
|
assert(connections.length > events[i].ident);
|
||||||
|
|
||||||
IOWatcher io = cast(IOWatcher) connections[events[i].ident];
|
IOWatcher io = cast(IOWatcher) connections[events[i].ident];
|
||||||
// If it is a ConnectionWatcher. Accept connections.
|
// If it is a ConnectionWatcher. Accept connections.
|
||||||
if (io is null)
|
if (io is null)
|
||||||
{
|
{
|
||||||
acceptConnections(connections[events[i].ident]);
|
acceptConnections(connections[events[i].ident]);
|
||||||
}
|
}
|
||||||
else if (events[i].flags & EV_ERROR)
|
else if (events[i].flags & EV_ERROR)
|
||||||
{
|
{
|
||||||
kill(io, null);
|
kill(io, null);
|
||||||
}
|
}
|
||||||
else if (events[i].filter == EVFILT_READ)
|
else if (events[i].filter == EVFILT_READ)
|
||||||
{
|
{
|
||||||
auto transport = cast(SelectorStreamTransport) io.transport;
|
auto transport = cast(SelectorStreamTransport) io.transport;
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
|
|
||||||
SocketException exception;
|
SocketException exception;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ptrdiff_t received;
|
ptrdiff_t received;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
received = transport.socket.receive(io.output[]);
|
received = transport.socket.receive(io.output[]);
|
||||||
io.output += received;
|
io.output += received;
|
||||||
}
|
}
|
||||||
while (received);
|
while (received);
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
exception = e;
|
exception = e;
|
||||||
}
|
}
|
||||||
if (transport.socket.disconnected)
|
if (transport.socket.disconnected)
|
||||||
{
|
{
|
||||||
kill(io, exception);
|
kill(io, exception);
|
||||||
}
|
}
|
||||||
else if (io.output.length)
|
else if (io.output.length)
|
||||||
{
|
{
|
||||||
swapPendings.insertBack(io);
|
swapPendings.enqueue(io);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (events[i].filter == EVFILT_WRITE)
|
else if (events[i].filter == EVFILT_WRITE)
|
||||||
{
|
{
|
||||||
auto transport = cast(SelectorStreamTransport) io.transport;
|
auto transport = cast(SelectorStreamTransport) io.transport;
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
|
|
||||||
transport.writeReady = true;
|
transport.writeReady = true;
|
||||||
if (transport.input.length)
|
if (transport.input.length)
|
||||||
{
|
{
|
||||||
feed(transport);
|
feed(transport);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: The blocking time.
|
* Returns: The blocking time.
|
||||||
*/
|
*/
|
||||||
override protected @property inout(Duration) blockTime()
|
override protected @property inout(Duration) blockTime()
|
||||||
inout @safe pure nothrow
|
inout @nogc @safe pure nothrow
|
||||||
{
|
{
|
||||||
return min(super.blockTime, 1.dur!"seconds");
|
return min(super.blockTime, 1.dur!"seconds");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the transport couldn't send the data, the further sending should
|
* If the transport couldn't send the data, the further sending should
|
||||||
* be handled by the event loop.
|
* be handled by the event loop.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* transport = Transport.
|
* transport = Transport.
|
||||||
* exception = Exception thrown on sending.
|
* exception = Exception thrown on sending.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
||||||
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
||||||
* transport is be destroyed then).
|
* transport is be destroyed then).
|
||||||
*/
|
*/
|
||||||
protected override bool feed(SelectorStreamTransport transport, SocketException exception = null)
|
protected override bool feed(SelectorStreamTransport transport,
|
||||||
{
|
SocketException exception = null) @nogc
|
||||||
if (!super.feed(transport, exception))
|
{
|
||||||
{
|
if (!super.feed(transport, exception))
|
||||||
return false;
|
{
|
||||||
}
|
return false;
|
||||||
if (!transport.writeReady)
|
}
|
||||||
{
|
if (!transport.writeReady)
|
||||||
set(transport.socket.handle, EVFILT_WRITE, EV_DISPATCH);
|
{
|
||||||
return true;
|
set(transport.socket.handle, EVFILT_WRITE, EV_DISPATCH);
|
||||||
}
|
return true;
|
||||||
return false;
|
}
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,240 +27,233 @@ import core.stdc.errno;
|
|||||||
*/
|
*/
|
||||||
class SelectorStreamTransport : StreamTransport
|
class SelectorStreamTransport : StreamTransport
|
||||||
{
|
{
|
||||||
private ConnectedSocket socket_;
|
private ConnectedSocket socket_;
|
||||||
|
|
||||||
/// Input buffer.
|
/// Input buffer.
|
||||||
package WriteBuffer input;
|
package WriteBuffer!ubyte input;
|
||||||
|
|
||||||
private SelectorLoop loop;
|
private SelectorLoop loop;
|
||||||
|
|
||||||
/// Received notification that the underlying socket is write-ready.
|
/// Received notification that the underlying socket is write-ready.
|
||||||
package bool writeReady;
|
package bool writeReady;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Params:
|
* Params:
|
||||||
* loop = Event loop.
|
* loop = Event loop.
|
||||||
* socket = Socket.
|
* socket = Socket.
|
||||||
*/
|
*/
|
||||||
this(SelectorLoop loop, ConnectedSocket socket)
|
this(SelectorLoop loop, ConnectedSocket socket) @nogc
|
||||||
{
|
{
|
||||||
socket_ = socket;
|
socket_ = socket;
|
||||||
this.loop = loop;
|
this.loop = loop;
|
||||||
input = MmapPool.instance.make!WriteBuffer();
|
input = WriteBuffer!ubyte(8192, MmapPool.instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close the transport and deallocate the data buffers.
|
* Returns: Transport socket.
|
||||||
*/
|
*/
|
||||||
~this()
|
inout(ConnectedSocket) socket() inout pure nothrow @safe @nogc
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(input);
|
return socket_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Transport socket.
|
* Write some data to the transport.
|
||||||
*/
|
*
|
||||||
inout(ConnectedSocket) socket() inout pure nothrow @safe @nogc
|
* Params:
|
||||||
{
|
* data = Data to send.
|
||||||
return socket_;
|
*/
|
||||||
}
|
void write(ubyte[] data) @nogc
|
||||||
|
{
|
||||||
/**
|
if (!data.length)
|
||||||
* Write some data to the transport.
|
{
|
||||||
*
|
return;
|
||||||
* Params:
|
}
|
||||||
* data = Data to send.
|
// Try to write if the socket is write ready.
|
||||||
*/
|
if (writeReady)
|
||||||
void write(ubyte[] data)
|
{
|
||||||
{
|
ptrdiff_t sent;
|
||||||
if (!data.length)
|
SocketException exception;
|
||||||
{
|
try
|
||||||
return;
|
{
|
||||||
}
|
sent = socket.send(data);
|
||||||
// Try to write if the socket is write ready.
|
if (sent == 0)
|
||||||
if (writeReady)
|
{
|
||||||
{
|
writeReady = false;
|
||||||
ptrdiff_t sent;
|
}
|
||||||
SocketException exception;
|
}
|
||||||
try
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
sent = socket.send(data);
|
writeReady = false;
|
||||||
if (sent == 0)
|
exception = e;
|
||||||
{
|
}
|
||||||
writeReady = false;
|
if (sent < data.length)
|
||||||
}
|
{
|
||||||
}
|
input ~= data[sent..$];
|
||||||
catch (SocketException e)
|
loop.feed(this, exception);
|
||||||
{
|
}
|
||||||
writeReady = false;
|
}
|
||||||
exception = e;
|
else
|
||||||
}
|
{
|
||||||
if (sent < data.length)
|
input ~= data;
|
||||||
{
|
}
|
||||||
input ~= data[sent..$];
|
}
|
||||||
loop.feed(this, exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
input ~= data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class SelectorLoop : Loop
|
abstract class SelectorLoop : Loop
|
||||||
{
|
{
|
||||||
/// Pending connections.
|
/// Pending connections.
|
||||||
protected ConnectionWatcher[] connections;
|
protected ConnectionWatcher[] connections;
|
||||||
|
|
||||||
this()
|
this() @nogc
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
connections = MmapPool.instance.makeArray!ConnectionWatcher(maxEvents);
|
MmapPool.instance.resizeArray(connections, maxEvents);
|
||||||
}
|
}
|
||||||
|
|
||||||
~this()
|
~this() @nogc
|
||||||
{
|
{
|
||||||
foreach (ref connection; connections)
|
foreach (ref connection; connections)
|
||||||
{
|
{
|
||||||
// We want to free only IOWatchers. ConnectionWatcher are created by the
|
// We want to free only IOWatchers. ConnectionWatcher are created by the
|
||||||
// user and should be freed by himself.
|
// user and should be freed by himself.
|
||||||
auto io = cast(IOWatcher) connection;
|
auto io = cast(IOWatcher) connection;
|
||||||
if (io !is null)
|
if (io !is null)
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(io);
|
MmapPool.instance.dispose(io);
|
||||||
connection = null;
|
connection = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MmapPool.instance.dispose(connections);
|
MmapPool.instance.dispose(connections);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the transport couldn't send the data, the further sending should
|
* If the transport couldn't send the data, the further sending should
|
||||||
* be handled by the event loop.
|
* be handled by the event loop.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* transport = Transport.
|
* transport = Transport.
|
||||||
* exception = Exception thrown on sending.
|
* exception = Exception thrown on sending.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
||||||
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
||||||
* transport will be destroyed then).
|
* transport will be destroyed then).
|
||||||
*/
|
*/
|
||||||
protected bool feed(SelectorStreamTransport transport, SocketException exception = null)
|
protected bool feed(SelectorStreamTransport transport,
|
||||||
{
|
SocketException exception = null) @nogc
|
||||||
while (transport.input.length && transport.writeReady)
|
{
|
||||||
{
|
while (transport.input.length && transport.writeReady)
|
||||||
try
|
{
|
||||||
{
|
try
|
||||||
ptrdiff_t sent = transport.socket.send(transport.input[]);
|
{
|
||||||
if (sent == 0)
|
ptrdiff_t sent = transport.socket.send(transport.input[]);
|
||||||
{
|
if (sent == 0)
|
||||||
transport.writeReady = false;
|
{
|
||||||
}
|
transport.writeReady = false;
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
transport.input += sent;
|
{
|
||||||
}
|
transport.input += sent;
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
}
|
||||||
{
|
catch (SocketException e)
|
||||||
exception = e;
|
{
|
||||||
transport.writeReady = false;
|
exception = e;
|
||||||
}
|
transport.writeReady = false;
|
||||||
}
|
}
|
||||||
if (exception !is null)
|
}
|
||||||
{
|
if (exception !is null)
|
||||||
auto watcher = cast(IOWatcher) connections[transport.socket.handle];
|
{
|
||||||
assert(watcher !is null);
|
auto watcher = cast(IOWatcher) connections[transport.socket.handle];
|
||||||
|
assert(watcher !is null);
|
||||||
|
|
||||||
kill(watcher, exception);
|
kill(watcher, exception);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start watching.
|
* Start watching.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* watcher = Watcher.
|
* watcher = Watcher.
|
||||||
*/
|
*/
|
||||||
override void start(ConnectionWatcher watcher)
|
override void start(ConnectionWatcher watcher) @nogc
|
||||||
{
|
{
|
||||||
if (watcher.active)
|
if (watcher.active)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connections.length <= watcher.socket)
|
if (connections.length <= watcher.socket)
|
||||||
{
|
{
|
||||||
MmapPool.instance.resizeArray(connections, watcher.socket.handle + maxEvents / 2);
|
MmapPool.instance.resizeArray(connections, watcher.socket.handle + maxEvents / 2);
|
||||||
}
|
}
|
||||||
connections[watcher.socket.handle] = watcher;
|
connections[watcher.socket.handle] = watcher;
|
||||||
|
|
||||||
super.start(watcher);
|
super.start(watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accept incoming connections.
|
* Accept incoming connections.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* connection = Connection watcher ready to accept.
|
* connection = Connection watcher ready to accept.
|
||||||
*/
|
*/
|
||||||
package void acceptConnections(ConnectionWatcher connection)
|
package void acceptConnections(ConnectionWatcher connection) @nogc
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(connection !is null);
|
assert(connection !is null);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
ConnectedSocket client;
|
ConnectedSocket client;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client = (cast(StreamSocket) connection.socket).accept();
|
client = (cast(StreamSocket) connection.socket).accept();
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
theAllocator.dispose(e);
|
defaultAllocator.dispose(e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (client is null)
|
if (client is null)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
IOWatcher io;
|
IOWatcher io;
|
||||||
auto transport = MmapPool.instance.make!SelectorStreamTransport(this, client);
|
auto transport = MmapPool.instance.make!SelectorStreamTransport(this, client);
|
||||||
|
|
||||||
if (connections.length > client.handle)
|
if (connections.length > client.handle)
|
||||||
{
|
{
|
||||||
io = cast(IOWatcher) connections[client.handle];
|
io = cast(IOWatcher) connections[client.handle];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MmapPool.instance.resizeArray(connections, client.handle + maxEvents / 2);
|
MmapPool.instance.resizeArray(connections, client.handle + maxEvents / 2);
|
||||||
}
|
}
|
||||||
if (io is null)
|
if (io is null)
|
||||||
{
|
{
|
||||||
io = MmapPool.instance.make!IOWatcher(transport,
|
io = MmapPool.instance.make!IOWatcher(transport,
|
||||||
connection.protocol);
|
connection.protocol);
|
||||||
connections[client.handle] = io;
|
connections[client.handle] = io;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
io(transport, connection.protocol);
|
io(transport, connection.protocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
|
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
|
||||||
connection.incoming.insertBack(io);
|
connection.incoming.enqueue(io);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!connection.incoming.empty)
|
if (!connection.incoming.empty)
|
||||||
{
|
{
|
||||||
swapPendings.insertBack(connection);
|
swapPendings.enqueue(connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,12 +21,12 @@ import core.sys.windows.windef;
|
|||||||
*/
|
*/
|
||||||
class State
|
class State
|
||||||
{
|
{
|
||||||
/// For internal use by Windows API.
|
/// For internal use by Windows API.
|
||||||
align(1) OVERLAPPED overlapped;
|
align(1) OVERLAPPED overlapped;
|
||||||
|
|
||||||
/// File/socket handle.
|
/// File/socket handle.
|
||||||
HANDLE handle;
|
HANDLE handle;
|
||||||
|
|
||||||
/// For keeping events or event masks.
|
/// For keeping events or event masks.
|
||||||
int event;
|
int event;
|
||||||
}
|
}
|
||||||
|
@ -10,100 +10,105 @@
|
|||||||
*
|
*
|
||||||
* ---
|
* ---
|
||||||
* import tanya.async;
|
* import tanya.async;
|
||||||
|
* import tanya.memory;
|
||||||
* import tanya.network.socket;
|
* import tanya.network.socket;
|
||||||
*
|
*
|
||||||
* class EchoProtocol : TransmissionControlProtocol
|
* class EchoProtocol : TransmissionControlProtocol
|
||||||
* {
|
* {
|
||||||
* private DuplexTransport transport;
|
* private DuplexTransport transport;
|
||||||
*
|
*
|
||||||
* void received(ubyte[] data)
|
* void received(ubyte[] data) @nogc
|
||||||
* {
|
* {
|
||||||
* transport.write(data);
|
* transport.write(data);
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* void connected(DuplexTransport transport)
|
* void connected(DuplexTransport transport) @nogc
|
||||||
* {
|
* {
|
||||||
* this.transport = transport;
|
* this.transport = transport;
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* void disconnected(SocketException e = null)
|
* void disconnected(SocketException e = null) @nogc
|
||||||
* {
|
* {
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* void main()
|
* void main()
|
||||||
* {
|
* {
|
||||||
* auto address = new InternetAddress("127.0.0.1", cast(ushort) 8192);
|
* auto address = defaultAllocator.make!InternetAddress("127.0.0.1", cast(ushort) 8192);
|
||||||
*
|
*
|
||||||
* version (Windows)
|
* version (Windows)
|
||||||
* {
|
* {
|
||||||
* auto sock = new OverlappedStreamSocket(AddressFamily.INET);
|
* auto sock = defaultAllocator.make!OverlappedStreamSocket(AddressFamily.INET);
|
||||||
* }
|
* }
|
||||||
* else
|
* else
|
||||||
* {
|
* {
|
||||||
* auto sock = new StreamSocket(AddressFamily.INET);
|
* auto sock = defaultAllocator.make!StreamSocket(AddressFamily.INET);
|
||||||
* sock.blocking = false;
|
* sock.blocking = false;
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* sock.bind(address);
|
* sock.bind(address);
|
||||||
* sock.listen(5);
|
* sock.listen(5);
|
||||||
*
|
*
|
||||||
* auto io = new ConnectionWatcher(sock);
|
* auto io = defaultAllocator.make!ConnectionWatcher(sock);
|
||||||
* io.setProtocol!EchoProtocol;
|
* io.setProtocol!EchoProtocol;
|
||||||
*
|
*
|
||||||
* defaultLoop.start(io);
|
* defaultLoop.start(io);
|
||||||
* defaultLoop.run();
|
* defaultLoop.run();
|
||||||
*
|
*
|
||||||
* sock.shutdown();
|
* sock.shutdown();
|
||||||
|
* defaultAllocator.dispose(io);
|
||||||
|
* defaultAllocator.dispose(sock);
|
||||||
|
* defaultAllocator.dispose(address);
|
||||||
* }
|
* }
|
||||||
* ---
|
* ---
|
||||||
*/
|
*/
|
||||||
module tanya.async.loop;
|
module tanya.async.loop;
|
||||||
|
|
||||||
import tanya.async.protocol;
|
|
||||||
import tanya.async.transport;
|
|
||||||
import tanya.async.watcher;
|
|
||||||
import tanya.container.buffer;
|
|
||||||
import tanya.memory;
|
|
||||||
import tanya.memory.mmappool;
|
|
||||||
import tanya.network.socket;
|
|
||||||
import core.time;
|
import core.time;
|
||||||
import std.algorithm.iteration;
|
import std.algorithm.iteration;
|
||||||
import std.algorithm.mutation;
|
import std.algorithm.mutation;
|
||||||
import std.typecons;
|
import std.typecons;
|
||||||
|
import tanya.async.protocol;
|
||||||
|
import tanya.async.transport;
|
||||||
|
import tanya.async.watcher;
|
||||||
|
import tanya.container.buffer;
|
||||||
|
import tanya.container.queue;
|
||||||
|
import tanya.memory;
|
||||||
|
import tanya.memory.mmappool;
|
||||||
|
import tanya.network.socket;
|
||||||
|
|
||||||
version (DisableBackends)
|
version (DisableBackends)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
else version (linux)
|
else version (linux)
|
||||||
{
|
{
|
||||||
import tanya.async.event.epoll;
|
import tanya.async.event.epoll;
|
||||||
version = Epoll;
|
version = Epoll;
|
||||||
}
|
}
|
||||||
else version (Windows)
|
else version (Windows)
|
||||||
{
|
{
|
||||||
import tanya.async.event.iocp;
|
import tanya.async.event.iocp;
|
||||||
version = IOCP;
|
version = IOCP;
|
||||||
}
|
}
|
||||||
else version (OSX)
|
else version (OSX)
|
||||||
{
|
{
|
||||||
version = Kqueue;
|
version = Kqueue;
|
||||||
}
|
}
|
||||||
else version (iOS)
|
else version (iOS)
|
||||||
{
|
{
|
||||||
version = Kqueue;
|
version = Kqueue;
|
||||||
}
|
}
|
||||||
else version (FreeBSD)
|
else version (FreeBSD)
|
||||||
{
|
{
|
||||||
version = Kqueue;
|
version = Kqueue;
|
||||||
}
|
}
|
||||||
else version (OpenBSD)
|
else version (OpenBSD)
|
||||||
{
|
{
|
||||||
version = Kqueue;
|
version = Kqueue;
|
||||||
}
|
}
|
||||||
else version (DragonFlyBSD)
|
else version (DragonFlyBSD)
|
||||||
{
|
{
|
||||||
version = Kqueue;
|
version = Kqueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,11 +116,11 @@ else version (DragonFlyBSD)
|
|||||||
*/
|
*/
|
||||||
enum Event : uint
|
enum Event : uint
|
||||||
{
|
{
|
||||||
none = 0x00, /// No events.
|
none = 0x00, /// No events.
|
||||||
read = 0x01, /// Non-blocking read call.
|
read = 0x01, /// Non-blocking read call.
|
||||||
write = 0x02, /// Non-blocking write call.
|
write = 0x02, /// Non-blocking write call.
|
||||||
accept = 0x04, /// Connection made.
|
accept = 0x04, /// Connection made.
|
||||||
error = 0x80000000, /// Sent when an error occurs.
|
error = 0x80000000, /// Sent when an error occurs.
|
||||||
}
|
}
|
||||||
|
|
||||||
alias EventMask = BitFlags!Event;
|
alias EventMask = BitFlags!Event;
|
||||||
@ -125,161 +130,174 @@ alias EventMask = BitFlags!Event;
|
|||||||
*/
|
*/
|
||||||
abstract class Loop
|
abstract class Loop
|
||||||
{
|
{
|
||||||
/// Pending watchers.
|
/// Pending watchers.
|
||||||
protected PendingQueue!Watcher pendings;
|
protected Queue!Watcher* pendings;
|
||||||
|
|
||||||
protected PendingQueue!Watcher swapPendings;
|
/// Ditto.
|
||||||
|
protected Queue!Watcher* swapPendings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Maximal event count can be got at a time
|
* Returns: Maximal event count can be got at a time
|
||||||
* (should be supported by the backend).
|
* (should be supported by the backend).
|
||||||
*/
|
*/
|
||||||
protected @property inout(uint) maxEvents() inout const pure nothrow @safe @nogc
|
protected @property inout(uint) maxEvents()
|
||||||
{
|
inout const pure nothrow @safe @nogc
|
||||||
return 128U;
|
{
|
||||||
}
|
return 128U;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the loop.
|
* Initializes the loop.
|
||||||
*/
|
*/
|
||||||
this()
|
this() @nogc
|
||||||
{
|
{
|
||||||
pendings = MmapPool.instance.make!(PendingQueue!Watcher);
|
pendings = MmapPool.instance.make!(Queue!Watcher)(MmapPool.instance);
|
||||||
swapPendings = MmapPool.instance.make!(PendingQueue!Watcher);
|
swapPendings = MmapPool.instance.make!(Queue!Watcher)(MmapPool.instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Frees loop internals.
|
* Frees loop internals.
|
||||||
*/
|
*/
|
||||||
~this()
|
~this() @nogc
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(pendings);
|
foreach (w; *pendings)
|
||||||
MmapPool.instance.dispose(swapPendings);
|
{
|
||||||
}
|
MmapPool.instance.dispose(w);
|
||||||
|
}
|
||||||
|
MmapPool.instance.dispose(pendings);
|
||||||
|
|
||||||
/**
|
foreach (w; *swapPendings)
|
||||||
* Starts the loop.
|
{
|
||||||
*/
|
MmapPool.instance.dispose(w);
|
||||||
void run()
|
}
|
||||||
{
|
MmapPool.instance.dispose(swapPendings);
|
||||||
done_ = false;
|
}
|
||||||
do
|
|
||||||
{
|
|
||||||
poll();
|
|
||||||
|
|
||||||
// Invoke pendings
|
/**
|
||||||
swapPendings.each!((ref p) => p.invoke());
|
* Starts the loop.
|
||||||
|
*/
|
||||||
|
void run() @nogc
|
||||||
|
{
|
||||||
|
done_ = false;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
poll();
|
||||||
|
|
||||||
swap(pendings, swapPendings);
|
// Invoke pendings
|
||||||
}
|
foreach (ref w; *swapPendings)
|
||||||
while (!done_);
|
{
|
||||||
}
|
w.invoke();
|
||||||
|
}
|
||||||
|
swap(pendings, swapPendings);
|
||||||
|
}
|
||||||
|
while (!done_);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Break out of the loop.
|
* Break out of the loop.
|
||||||
*/
|
*/
|
||||||
void unloop() @safe pure nothrow
|
void unloop() @safe pure nothrow @nogc
|
||||||
{
|
{
|
||||||
done_ = true;
|
done_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start watching.
|
* Start watching.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* watcher = Watcher.
|
* watcher = Watcher.
|
||||||
*/
|
*/
|
||||||
void start(ConnectionWatcher watcher)
|
void start(ConnectionWatcher watcher) @nogc
|
||||||
{
|
{
|
||||||
if (watcher.active)
|
if (watcher.active)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
watcher.active = true;
|
watcher.active = true;
|
||||||
reify(watcher, EventMask(Event.none), EventMask(Event.accept));
|
reify(watcher, EventMask(Event.none), EventMask(Event.accept));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop watching.
|
* Stop watching.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* watcher = Watcher.
|
* watcher = Watcher.
|
||||||
*/
|
*/
|
||||||
void stop(ConnectionWatcher watcher)
|
void stop(ConnectionWatcher watcher) @nogc
|
||||||
{
|
{
|
||||||
if (!watcher.active)
|
if (!watcher.active)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
watcher.active = false;
|
watcher.active = false;
|
||||||
|
|
||||||
reify(watcher, EventMask(Event.accept), EventMask(Event.none));
|
reify(watcher, EventMask(Event.accept), EventMask(Event.none));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be called if the backend configuration changes.
|
* Should be called if the backend configuration changes.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* watcher = Watcher.
|
* watcher = Watcher.
|
||||||
* oldEvents = The events were already set.
|
* oldEvents = The events were already set.
|
||||||
* events = The events should be set.
|
* events = The events should be set.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||||
*/
|
*/
|
||||||
abstract protected bool reify(ConnectionWatcher watcher,
|
abstract protected bool reify(ConnectionWatcher watcher,
|
||||||
EventMask oldEvents,
|
EventMask oldEvents,
|
||||||
EventMask events);
|
EventMask events) @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: The blocking time.
|
* Returns: The blocking time.
|
||||||
*/
|
*/
|
||||||
protected @property inout(Duration) blockTime()
|
protected @property inout(Duration) blockTime()
|
||||||
inout @safe pure nothrow
|
inout @safe pure nothrow @nogc
|
||||||
{
|
{
|
||||||
// Don't block if we have to do.
|
// Don't block if we have to do.
|
||||||
return swapPendings.empty ? blockTime_ : Duration.zero;
|
return swapPendings.empty ? blockTime_ : Duration.zero;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the blocking time for IO watchers.
|
* Sets the blocking time for IO watchers.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* blockTime = The blocking time. Cannot be larger than
|
* blockTime = The blocking time. Cannot be larger than
|
||||||
* $(D_PSYMBOL maxBlockTime).
|
* $(D_PSYMBOL maxBlockTime).
|
||||||
*/
|
*/
|
||||||
protected @property void blockTime(in Duration blockTime) @safe pure nothrow
|
protected @property void blockTime(in Duration blockTime) @safe pure nothrow @nogc
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(blockTime <= 1.dur!"hours", "Too long to wait.");
|
assert(blockTime <= 1.dur!"hours", "Too long to wait.");
|
||||||
assert(!blockTime.isNegative);
|
assert(!blockTime.isNegative);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
blockTime_ = blockTime;
|
blockTime_ = blockTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Kills the watcher and closes the connection.
|
* Kills the watcher and closes the connection.
|
||||||
*/
|
*/
|
||||||
protected void kill(IOWatcher watcher, SocketException exception)
|
protected void kill(IOWatcher watcher, SocketException exception) @nogc
|
||||||
{
|
{
|
||||||
watcher.socket.shutdown();
|
watcher.socket.shutdown();
|
||||||
theAllocator.dispose(watcher.socket);
|
defaultAllocator.dispose(watcher.socket);
|
||||||
MmapPool.instance.dispose(watcher.transport);
|
MmapPool.instance.dispose(watcher.transport);
|
||||||
watcher.exception = exception;
|
watcher.exception = exception;
|
||||||
swapPendings.insertBack(watcher);
|
swapPendings.enqueue(watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does the actual polling.
|
* Does the actual polling.
|
||||||
*/
|
*/
|
||||||
abstract protected void poll();
|
abstract protected void poll() @nogc;
|
||||||
|
|
||||||
/// Whether the event loop should be stopped.
|
/// Whether the event loop should be stopped.
|
||||||
private bool done_;
|
private bool done_;
|
||||||
|
|
||||||
/// Maximal block time.
|
/// Maximal block time.
|
||||||
protected Duration blockTime_ = 1.dur!"minutes";
|
protected Duration blockTime_ = 1.dur!"minutes";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -287,18 +305,17 @@ abstract class Loop
|
|||||||
*/
|
*/
|
||||||
class BadLoopException : Exception
|
class BadLoopException : Exception
|
||||||
{
|
{
|
||||||
@nogc:
|
/**
|
||||||
/**
|
* Params:
|
||||||
* Params:
|
* file = The file where the exception occurred.
|
||||||
* file = The file where the exception occurred.
|
* line = The line number where the exception occurred.
|
||||||
* line = The line number where the exception occurred.
|
* next = The previous exception in the chain of exceptions, if any.
|
||||||
* next = The previous exception in the chain of exceptions, if any.
|
*/
|
||||||
*/
|
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null)
|
||||||
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null)
|
pure nothrow const @safe @nogc
|
||||||
pure @safe nothrow const
|
{
|
||||||
{
|
super("Event loop cannot be initialized.", file, line, next);
|
||||||
super("Event loop cannot be initialized.", file, line, next);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -308,26 +325,26 @@ class BadLoopException : Exception
|
|||||||
*
|
*
|
||||||
* Returns: The default event loop.
|
* Returns: The default event loop.
|
||||||
*/
|
*/
|
||||||
@property Loop defaultLoop()
|
@property Loop defaultLoop() @nogc
|
||||||
{
|
{
|
||||||
if (defaultLoop_ !is null)
|
if (defaultLoop_ !is null)
|
||||||
{
|
{
|
||||||
return defaultLoop_;
|
return defaultLoop_;
|
||||||
}
|
}
|
||||||
version (Epoll)
|
version (Epoll)
|
||||||
{
|
{
|
||||||
defaultLoop_ = MmapPool.instance.make!EpollLoop;
|
defaultLoop_ = MmapPool.instance.make!EpollLoop;
|
||||||
}
|
}
|
||||||
else version (IOCP)
|
else version (IOCP)
|
||||||
{
|
{
|
||||||
defaultLoop_ = MmapPool.instance.make!IOCPLoop;
|
defaultLoop_ = MmapPool.instance.make!IOCPLoop;
|
||||||
}
|
}
|
||||||
else version (Kqueue)
|
else version (Kqueue)
|
||||||
{
|
{
|
||||||
import tanya.async.event.kqueue;
|
import tanya.async.event.kqueue;
|
||||||
defaultLoop_ = MmapPool.instance.make!KqueueLoop;
|
defaultLoop_ = MmapPool.instance.make!KqueueLoop;
|
||||||
}
|
}
|
||||||
return defaultLoop_;
|
return defaultLoop_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -339,145 +356,16 @@ class BadLoopException : Exception
|
|||||||
* your implementation to this property.
|
* your implementation to this property.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* loop = The event loop.
|
* loop = The event loop.
|
||||||
*/
|
*/
|
||||||
@property void defaultLoop(Loop loop)
|
@property void defaultLoop(Loop loop) @nogc
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(loop !is null);
|
assert(loop !is null);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
defaultLoop_ = loop;
|
defaultLoop_ = loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Loop defaultLoop_;
|
private Loop defaultLoop_;
|
||||||
|
|
||||||
/**
|
|
||||||
* Queue.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* T = Content type.
|
|
||||||
*/
|
|
||||||
class PendingQueue(T)
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Creates a new $(D_PSYMBOL Queue).
|
|
||||||
*/
|
|
||||||
this()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes all elements from the queue.
|
|
||||||
*/
|
|
||||||
~this()
|
|
||||||
{
|
|
||||||
foreach (e; this)
|
|
||||||
{
|
|
||||||
MmapPool.instance.dispose(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: First element.
|
|
||||||
*/
|
|
||||||
@property ref T front()
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(!empty);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
return first.next.content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inserts a new element.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* x = New element.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD this).
|
|
||||||
*/
|
|
||||||
typeof(this) insertBack(T x)
|
|
||||||
{
|
|
||||||
Entry* temp = MmapPool.instance.make!Entry;
|
|
||||||
|
|
||||||
temp.content = x;
|
|
||||||
|
|
||||||
if (empty)
|
|
||||||
{
|
|
||||||
first.next = rear = temp;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rear.next = temp;
|
|
||||||
rear = rear.next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
alias insert = insertBack;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inserts a new element.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* x = New element.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD this).
|
|
||||||
*/
|
|
||||||
typeof(this) opOpAssign(string Op)(ref T x)
|
|
||||||
if (Op == "~")
|
|
||||||
{
|
|
||||||
return insertBack(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: $(D_KEYWORD true) if the queue is empty.
|
|
||||||
*/
|
|
||||||
@property bool empty() const @safe pure nothrow
|
|
||||||
{
|
|
||||||
return first.next is null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move position to the next element.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD this).
|
|
||||||
*/
|
|
||||||
typeof(this) popFront()
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(!empty);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
auto n = first.next.next;
|
|
||||||
|
|
||||||
MmapPool.instance.dispose(first.next);
|
|
||||||
first.next = n;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Queue entry.
|
|
||||||
*/
|
|
||||||
protected struct Entry
|
|
||||||
{
|
|
||||||
/// Queue item content.
|
|
||||||
T content;
|
|
||||||
|
|
||||||
/// Next list item.
|
|
||||||
Entry* next;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The first element of the list.
|
|
||||||
protected Entry first;
|
|
||||||
|
|
||||||
/// The last element of the list.
|
|
||||||
protected Entry* rear;
|
|
||||||
}
|
|
||||||
|
@ -18,28 +18,28 @@ import tanya.async.transport;
|
|||||||
*/
|
*/
|
||||||
interface Protocol
|
interface Protocol
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Params:
|
* Params:
|
||||||
* data = Read data.
|
* data = Read data.
|
||||||
*/
|
*/
|
||||||
void received(ubyte[] data);
|
void received(ubyte[] data) @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a connection is made.
|
* Called when a connection is made.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* transport = Protocol transport.
|
* transport = Protocol transport.
|
||||||
*/
|
*/
|
||||||
void connected(DuplexTransport transport);
|
void connected(DuplexTransport transport) @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a connection is lost.
|
* Called when a connection is lost.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* exception = $(D_PSYMBOL Exception) if an error caused
|
* exception = $(D_PSYMBOL Exception) if an error caused
|
||||||
* the disconnect, $(D_KEYWORD null) otherwise.
|
* the disconnect, $(D_KEYWORD null) otherwise.
|
||||||
*/
|
*/
|
||||||
void disconnected(SocketException exception = null);
|
void disconnected(SocketException exception = null) @nogc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,13 +31,13 @@ interface ReadTransport : Transport
|
|||||||
*/
|
*/
|
||||||
interface WriteTransport : Transport
|
interface WriteTransport : Transport
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Write some data to the transport.
|
* Write some data to the transport.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* data = Data to send.
|
* data = Data to send.
|
||||||
*/
|
*/
|
||||||
void write(ubyte[] data);
|
void write(ubyte[] data) @nogc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,7 +52,7 @@ interface DuplexTransport : ReadTransport, WriteTransport
|
|||||||
*/
|
*/
|
||||||
interface SocketTransport : Transport
|
interface SocketTransport : Transport
|
||||||
{
|
{
|
||||||
@property inout(Socket) socket() inout pure nothrow @safe @nogc;
|
@property inout(Socket) socket() inout pure nothrow @safe @nogc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,23 +10,24 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.async.watcher;
|
module tanya.async.watcher;
|
||||||
|
|
||||||
|
import std.functional;
|
||||||
|
import std.exception;
|
||||||
import tanya.async.loop;
|
import tanya.async.loop;
|
||||||
import tanya.async.protocol;
|
import tanya.async.protocol;
|
||||||
import tanya.async.transport;
|
import tanya.async.transport;
|
||||||
import tanya.container.buffer;
|
import tanya.container.buffer;
|
||||||
|
import tanya.container.queue;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
import tanya.memory.mmappool;
|
import tanya.memory.mmappool;
|
||||||
import tanya.network.socket;
|
import tanya.network.socket;
|
||||||
import std.functional;
|
|
||||||
import std.exception;
|
|
||||||
|
|
||||||
version (Windows)
|
version (Windows)
|
||||||
{
|
{
|
||||||
import core.sys.windows.basetyps;
|
import core.sys.windows.basetyps;
|
||||||
import core.sys.windows.mswsock;
|
import core.sys.windows.mswsock;
|
||||||
import core.sys.windows.winbase;
|
import core.sys.windows.winbase;
|
||||||
import core.sys.windows.windef;
|
import core.sys.windows.windef;
|
||||||
import core.sys.windows.winsock2;
|
import core.sys.windows.winsock2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,85 +36,87 @@ version (Windows)
|
|||||||
*/
|
*/
|
||||||
abstract class Watcher
|
abstract class Watcher
|
||||||
{
|
{
|
||||||
/// Whether the watcher is active.
|
/// Whether the watcher is active.
|
||||||
bool active;
|
bool active;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoke some action on event.
|
* Invoke some action on event.
|
||||||
*/
|
*/
|
||||||
void invoke();
|
void invoke() @nogc;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConnectionWatcher : Watcher
|
class ConnectionWatcher : Watcher
|
||||||
{
|
{
|
||||||
/// Watched socket.
|
/// Watched socket.
|
||||||
private Socket socket_;
|
private Socket socket_;
|
||||||
|
|
||||||
/// Protocol factory.
|
/// Protocol factory.
|
||||||
protected Protocol delegate() protocolFactory;
|
protected Protocol delegate() @nogc protocolFactory;
|
||||||
|
|
||||||
package PendingQueue!IOWatcher incoming;
|
package Queue!IOWatcher incoming;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Params:
|
* Params:
|
||||||
* socket = Socket.
|
* socket = Socket.
|
||||||
*/
|
*/
|
||||||
this(Socket socket)
|
this(Socket socket) @nogc
|
||||||
{
|
{
|
||||||
socket_ = socket;
|
socket_ = socket;
|
||||||
incoming = MmapPool.instance.make!(PendingQueue!IOWatcher);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
protected this()
|
protected this() pure nothrow @safe @nogc
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
~this()
|
~this() @nogc
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(incoming);
|
foreach (w; incoming)
|
||||||
}
|
{
|
||||||
|
MmapPool.instance.dispose(w);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Params:
|
* Params:
|
||||||
* P = Protocol should be used.
|
* P = Protocol should be used.
|
||||||
*/
|
*/
|
||||||
void setProtocol(P : Protocol)()
|
void setProtocol(P : Protocol)() @nogc
|
||||||
{
|
{
|
||||||
this.protocolFactory = () => cast(Protocol) MmapPool.instance.make!P;
|
this.protocolFactory = () @nogc => cast(Protocol) MmapPool.instance.make!P;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Socket.
|
* Returns: Socket.
|
||||||
*/
|
*/
|
||||||
@property inout(Socket) socket() inout pure nothrow @nogc
|
@property inout(Socket) socket() inout pure nothrow @nogc
|
||||||
{
|
{
|
||||||
return socket_;
|
return socket_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: New protocol instance.
|
* Returns: New protocol instance.
|
||||||
*/
|
*/
|
||||||
@property Protocol protocol()
|
@property Protocol protocol() @nogc
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(protocolFactory !is null, "Protocol isn't set.");
|
assert(protocolFactory !is null, "Protocol isn't set.");
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
return protocolFactory();
|
return protocolFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes new connection callback.
|
* Invokes new connection callback.
|
||||||
*/
|
*/
|
||||||
override void invoke()
|
override void invoke() @nogc
|
||||||
{
|
{
|
||||||
foreach (io; incoming)
|
foreach (io; incoming)
|
||||||
{
|
{
|
||||||
io.protocol.connected(cast(DuplexTransport) io.transport);
|
io.protocol.connected(cast(DuplexTransport) io.transport);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -122,121 +125,121 @@ class ConnectionWatcher : Watcher
|
|||||||
*/
|
*/
|
||||||
class IOWatcher : ConnectionWatcher
|
class IOWatcher : ConnectionWatcher
|
||||||
{
|
{
|
||||||
/// If an exception was thrown the transport should be already invalid.
|
/// If an exception was thrown the transport should be already invalid.
|
||||||
private union
|
private union
|
||||||
{
|
{
|
||||||
StreamTransport transport_;
|
StreamTransport transport_;
|
||||||
SocketException exception_;
|
SocketException exception_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Protocol protocol_;
|
private Protocol protocol_;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Underlying output buffer.
|
* Returns: Underlying output buffer.
|
||||||
*/
|
*/
|
||||||
package ReadBuffer output;
|
package ReadBuffer!ubyte output;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Params:
|
* Params:
|
||||||
* transport = Transport.
|
* transport = Transport.
|
||||||
* protocol = New instance of the application protocol.
|
* protocol = New instance of the application protocol.
|
||||||
*/
|
*/
|
||||||
this(StreamTransport transport, Protocol protocol)
|
this(StreamTransport transport, Protocol protocol) @nogc
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
assert(protocol !is null);
|
assert(protocol !is null);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
transport_ = transport;
|
transport_ = transport;
|
||||||
protocol_ = protocol;
|
protocol_ = protocol;
|
||||||
output = MmapPool.instance.make!ReadBuffer();
|
output = ReadBuffer!ubyte(8192, 1024, MmapPool.instance);
|
||||||
active = true;
|
active = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroys the watcher.
|
* Destroys the watcher.
|
||||||
*/
|
*/
|
||||||
protected ~this()
|
~this() @nogc
|
||||||
{
|
{
|
||||||
MmapPool.instance.dispose(output);
|
MmapPool.instance.dispose(protocol_);
|
||||||
MmapPool.instance.dispose(protocol_);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assigns a transport.
|
* Assigns a transport.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* transport = Transport.
|
* transport = Transport.
|
||||||
* protocol = Application protocol.
|
* protocol = Application protocol.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD this).
|
* Returns: $(D_KEYWORD this).
|
||||||
*/
|
*/
|
||||||
IOWatcher opCall(StreamTransport transport, Protocol protocol) pure nothrow @nogc
|
IOWatcher opCall(StreamTransport transport, Protocol protocol)
|
||||||
in
|
pure nothrow @nogc
|
||||||
{
|
in
|
||||||
assert(transport !is null);
|
{
|
||||||
assert(protocol !is null);
|
assert(transport !is null);
|
||||||
}
|
assert(protocol !is null);
|
||||||
body
|
}
|
||||||
{
|
body
|
||||||
transport_ = transport;
|
{
|
||||||
protocol_ = protocol;
|
transport_ = transport;
|
||||||
active = true;
|
protocol_ = protocol;
|
||||||
return this;
|
active = true;
|
||||||
}
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Transport used by this watcher.
|
* Returns: Transport used by this watcher.
|
||||||
*/
|
*/
|
||||||
@property inout(StreamTransport) transport() inout pure nothrow @nogc
|
@property inout(StreamTransport) transport() inout pure nothrow @nogc
|
||||||
{
|
{
|
||||||
return transport_;
|
return transport_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets an exception occurred during a read/write operation.
|
* Sets an exception occurred during a read/write operation.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* exception = Thrown exception.
|
* exception = Thrown exception.
|
||||||
*/
|
*/
|
||||||
@property void exception(SocketException exception) pure nothrow @nogc
|
@property void exception(SocketException exception) pure nothrow @nogc
|
||||||
{
|
{
|
||||||
exception_ = exception;
|
exception_ = exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Application protocol.
|
* Returns: Application protocol.
|
||||||
*/
|
*/
|
||||||
override @property Protocol protocol() pure nothrow @safe @nogc
|
override @property Protocol protocol() pure nothrow @safe @nogc
|
||||||
{
|
{
|
||||||
return protocol_;
|
return protocol_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Socket.
|
* Returns: Socket.
|
||||||
*/
|
*/
|
||||||
override @property inout(Socket) socket() inout pure nothrow @nogc
|
override @property inout(Socket) socket() inout pure nothrow @nogc
|
||||||
{
|
{
|
||||||
return transport.socket;
|
return transport.socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes the watcher callback.
|
* Invokes the watcher callback.
|
||||||
*/
|
*/
|
||||||
override void invoke()
|
override void invoke() @nogc
|
||||||
{
|
{
|
||||||
if (output.length)
|
if (output.length)
|
||||||
{
|
{
|
||||||
protocol.received(output[0..$]);
|
protocol.received(output[0..$]);
|
||||||
output.clear();
|
output.clear();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
protocol.disconnected(exception_);
|
protocol.disconnected(exception_);
|
||||||
active = false;
|
active = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
45
source/tanya/container/entry.d
Normal file
45
source/tanya/container/entry.d
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Internal package used by containers that rely on entries/nodes.
|
||||||
|
*
|
||||||
|
* 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.container.entry;
|
||||||
|
|
||||||
|
version (unittest)
|
||||||
|
{
|
||||||
|
package struct ConstEqualsStruct
|
||||||
|
{
|
||||||
|
int opEquals(typeof(this) that) const @nogc
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
package struct MutableEqualsStruct
|
||||||
|
{
|
||||||
|
int opEquals(typeof(this) that) @nogc
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
package struct NoEqualsStruct
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
package struct Entry(T)
|
||||||
|
{
|
||||||
|
/// Item content.
|
||||||
|
T content;
|
||||||
|
|
||||||
|
/// Next item.
|
||||||
|
Entry* next;
|
||||||
|
}
|
@ -10,28 +10,17 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.container.list;
|
module tanya.container.list;
|
||||||
|
|
||||||
|
import tanya.container.entry;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Singly linked list.
|
* Singly-linked list.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* T = Content type.
|
* T = Content type.
|
||||||
*/
|
*/
|
||||||
class SList(T)
|
struct SList(T)
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Creates a new $(D_PSYMBOL SList).
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* allocator = The allocator should be used for the element
|
|
||||||
* allocations.
|
|
||||||
*/
|
|
||||||
this(IAllocator allocator = theAllocator)
|
|
||||||
{
|
|
||||||
this.allocator = allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all elements from the list.
|
* Removes all elements from the list.
|
||||||
*/
|
*/
|
||||||
@ -41,7 +30,7 @@ class SList(T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all contents from the $(D_PSYMBOL SList).
|
* Removes all contents from the list.
|
||||||
*/
|
*/
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
@ -54,14 +43,12 @@ class SList(T)
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto l = make!(SList!int)(theAllocator);
|
SList!int l;
|
||||||
|
|
||||||
l.insertFront(8);
|
l.insertFront(8);
|
||||||
l.insertFront(5);
|
l.insertFront(5);
|
||||||
l.clear();
|
l.clear();
|
||||||
assert(l.empty);
|
assert(l.empty);
|
||||||
|
|
||||||
dispose(theAllocator, l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,35 +70,39 @@ class SList(T)
|
|||||||
* Params:
|
* Params:
|
||||||
* x = New element.
|
* x = New element.
|
||||||
*/
|
*/
|
||||||
void insertFront(T x)
|
void insertFront(ref T x)
|
||||||
{
|
{
|
||||||
Entry* temp = make!Entry(allocator);
|
auto temp = allocator.make!(Entry!T);
|
||||||
|
|
||||||
temp.content = x;
|
temp.content = x;
|
||||||
temp.next = first.next;
|
temp.next = first.next;
|
||||||
first.next = temp;
|
first.next = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
void insertFront(T x)
|
||||||
|
{
|
||||||
|
insertFront(x);
|
||||||
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
alias insert = insertFront;
|
alias insert = insertFront;
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto l = make!(SList!int)(theAllocator);
|
SList!int l;
|
||||||
|
|
||||||
l.insertFront(8);
|
l.insertFront(8);
|
||||||
assert(l.front == 8);
|
assert(l.front == 8);
|
||||||
l.insertFront(9);
|
l.insertFront(9);
|
||||||
assert(l.front == 9);
|
assert(l.front == 9);
|
||||||
|
|
||||||
dispose(theAllocator, l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: $(D_KEYWORD true) if the list is empty.
|
* Returns: $(D_KEYWORD true) if the list is empty.
|
||||||
*/
|
*/
|
||||||
@property bool empty() inout const
|
@property bool empty() const
|
||||||
{
|
{
|
||||||
return first.next is null;
|
return first.next is null;
|
||||||
}
|
}
|
||||||
@ -121,7 +112,7 @@ class SList(T)
|
|||||||
*
|
*
|
||||||
* Returns: The first element.
|
* Returns: The first element.
|
||||||
*/
|
*/
|
||||||
T popFront()
|
void popFront()
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(!empty);
|
assert(!empty);
|
||||||
@ -129,26 +120,21 @@ class SList(T)
|
|||||||
body
|
body
|
||||||
{
|
{
|
||||||
auto n = first.next.next;
|
auto n = first.next.next;
|
||||||
auto content = first.next.content;
|
|
||||||
|
|
||||||
dispose(allocator, first.next);
|
allocator.dispose(first.next);
|
||||||
first.next = n;
|
first.next = n;
|
||||||
|
|
||||||
return content;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto l = make!(SList!int)(theAllocator);
|
SList!int l;
|
||||||
|
|
||||||
l.insertFront(8);
|
l.insertFront(8);
|
||||||
l.insertFront(9);
|
l.insertFront(9);
|
||||||
assert(l.front == 9);
|
assert(l.front == 9);
|
||||||
l.popFront();
|
l.popFront();
|
||||||
assert(l.front == 8);
|
assert(l.front == 8);
|
||||||
|
|
||||||
dispose(theAllocator, l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -179,7 +165,7 @@ class SList(T)
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto l = make!(SList!int)(theAllocator);
|
SList!int l;
|
||||||
|
|
||||||
l.insertFront(8);
|
l.insertFront(8);
|
||||||
l.insertFront(5);
|
l.insertFront(5);
|
||||||
@ -188,8 +174,6 @@ class SList(T)
|
|||||||
assert(l.removeFront(2) == 2);
|
assert(l.removeFront(2) == 2);
|
||||||
assert(l.removeFront(3) == 1);
|
assert(l.removeFront(3) == 1);
|
||||||
assert(l.removeFront(3) == 0);
|
assert(l.removeFront(3) == 0);
|
||||||
|
|
||||||
dispose(theAllocator, l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -235,7 +219,7 @@ class SList(T)
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto l = make!(SList!int)(theAllocator);
|
SList!int l;
|
||||||
|
|
||||||
l.insertFront(5);
|
l.insertFront(5);
|
||||||
l.insertFront(4);
|
l.insertFront(4);
|
||||||
@ -246,32 +230,18 @@ class SList(T)
|
|||||||
assert(i != 1 || e == 4);
|
assert(i != 1 || e == 4);
|
||||||
assert(i != 2 || e == 5);
|
assert(i != 2 || e == 5);
|
||||||
}
|
}
|
||||||
dispose(theAllocator, l);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List entry.
|
|
||||||
*/
|
|
||||||
protected struct Entry
|
|
||||||
{
|
|
||||||
/// List item content.
|
|
||||||
T content;
|
|
||||||
|
|
||||||
/// Next list item.
|
|
||||||
Entry* next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 0th element of the list.
|
/// 0th element of the list.
|
||||||
protected Entry first;
|
private Entry!T first;
|
||||||
|
|
||||||
/// Allocator.
|
mixin DefaultAllocator;
|
||||||
protected IAllocator allocator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto l = make!(SList!int)(theAllocator);
|
SList!int l;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
l.insertFront(5);
|
l.insertFront(5);
|
||||||
@ -285,8 +255,6 @@ unittest
|
|||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
assert(i == 3);
|
assert(i == 3);
|
||||||
|
|
||||||
dispose(theAllocator, l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private unittest
|
private unittest
|
||||||
@ -294,8 +262,5 @@ private unittest
|
|||||||
interface Stuff
|
interface Stuff
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
static assert(is(SList!Stuff));
|
||||||
auto l = make!(SList!Stuff)(theAllocator);
|
|
||||||
|
|
||||||
dispose(theAllocator, l);
|
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
*/
|
*/
|
||||||
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;
|
||||||
|
@ -6,68 +6,153 @@
|
|||||||
* 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.queue;
|
module tanya.container.queue;
|
||||||
|
|
||||||
|
import std.traits;
|
||||||
|
import std.algorithm.mutation;
|
||||||
|
import tanya.container.entry;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue.
|
* FIFO queue.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* T = Content type.
|
* T = Content type.
|
||||||
*/
|
*/
|
||||||
class Queue(T)
|
struct Queue(T)
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Creates a new $(D_PSYMBOL Queue).
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* allocator = The allocator should be used for the element
|
|
||||||
* allocations.
|
|
||||||
*/
|
|
||||||
this(IAllocator allocator = theAllocator)
|
|
||||||
{
|
|
||||||
this.allocator = allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all elements from the queue.
|
* Removes all elements from the queue.
|
||||||
*/
|
*/
|
||||||
~this()
|
~this()
|
||||||
{
|
{
|
||||||
clear();
|
while (!empty)
|
||||||
|
{
|
||||||
|
dequeue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all elements from the queue.
|
* Removes all elements from the queue.
|
||||||
*/
|
*/
|
||||||
|
deprecated
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
while (!empty)
|
while (!empty)
|
||||||
{
|
{
|
||||||
popFront();
|
dequeue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns how many elements are in the queue. It iterates through the queue
|
||||||
|
* to count the elements.
|
||||||
|
*
|
||||||
|
* Returns: How many elements are in the queue.
|
||||||
|
*/
|
||||||
|
size_t length() const
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
for (const(Entry!T)* i = first.next; i !is null; i = i.next)
|
||||||
|
{
|
||||||
|
++len;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto q = theAllocator.make!(Queue!int);
|
Queue!int q;
|
||||||
|
|
||||||
assert(q.empty);
|
assert(q.length == 0);
|
||||||
q.insertBack(8);
|
q.enqueue(5);
|
||||||
q.insertBack(9);
|
assert(q.length == 1);
|
||||||
q.clear();
|
q.enqueue(4);
|
||||||
assert(q.empty);
|
assert(q.length == 2);
|
||||||
|
q.enqueue(9);
|
||||||
|
assert(q.length == 3);
|
||||||
|
|
||||||
theAllocator.dispose(q);
|
q.dequeue();
|
||||||
|
assert(q.length == 2);
|
||||||
|
q.dequeue();
|
||||||
|
assert(q.length == 1);
|
||||||
|
q.dequeue();
|
||||||
|
assert(q.length == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
version (D_Ddoc)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Compares two queues. Checks if all elements of the both queues are equal.
|
||||||
|
*
|
||||||
|
* Returns: Whether $(D_KEYWORD this) and $(D_PARAM that) are equal.
|
||||||
|
*/
|
||||||
|
deprecated
|
||||||
|
int opEquals(ref typeof(this) that);
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
deprecated
|
||||||
|
int opEquals(typeof(this) that);
|
||||||
|
}
|
||||||
|
else static if (!hasMember!(T, "opEquals")
|
||||||
|
|| (functionAttributes!(T.opEquals) & FunctionAttribute.const_))
|
||||||
|
{
|
||||||
|
deprecated
|
||||||
|
bool opEquals(in ref typeof(this) that) const
|
||||||
|
{
|
||||||
|
const(Entry!T)* i = first.next;
|
||||||
|
const(Entry!T)* j = that.first.next;
|
||||||
|
while (i !is null && j !is null)
|
||||||
|
{
|
||||||
|
if (i.content != j.content)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i = i.next;
|
||||||
|
j = j.next;
|
||||||
|
}
|
||||||
|
return i is null && j is null;
|
||||||
|
}
|
||||||
|
|
||||||
|
deprecated
|
||||||
|
bool opEquals(in typeof(this) that) const
|
||||||
|
{
|
||||||
|
return opEquals(that);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
deprecated
|
||||||
|
bool opEquals(ref typeof(this) that)
|
||||||
|
{
|
||||||
|
Entry!T* i = first.next;
|
||||||
|
Entry!T* j = that.first.next;
|
||||||
|
while (i !is null && j !is null)
|
||||||
|
{
|
||||||
|
if (i.content != j.content)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i = i.next;
|
||||||
|
j = j.next;
|
||||||
|
}
|
||||||
|
return i is null && j is null;
|
||||||
|
}
|
||||||
|
|
||||||
|
deprecated
|
||||||
|
bool opEquals(typeof(this) that)
|
||||||
|
{
|
||||||
|
return opEquals(that);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: First element.
|
* Returns: First element.
|
||||||
*/
|
*/
|
||||||
|
deprecated("Use dequeue instead.")
|
||||||
@property ref inout(T) front() inout
|
@property ref inout(T) front() inout
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
@ -83,13 +168,12 @@ class Queue(T)
|
|||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* x = New element.
|
* x = New element.
|
||||||
|
*
|
||||||
|
* Returns: $(D_KEYWORD this).
|
||||||
*/
|
*/
|
||||||
void insertBack(T x)
|
ref typeof(this) enqueue(ref T x)
|
||||||
{
|
{
|
||||||
Entry* temp = make!Entry(allocator);
|
auto temp = allocator.make!(Entry!T)(x);
|
||||||
|
|
||||||
temp.content = x;
|
|
||||||
|
|
||||||
if (empty)
|
if (empty)
|
||||||
{
|
{
|
||||||
first.next = rear = temp;
|
first.next = rear = temp;
|
||||||
@ -99,29 +183,36 @@ class Queue(T)
|
|||||||
rear.next = temp;
|
rear.next = temp;
|
||||||
rear = rear.next;
|
rear = rear.next;
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
alias insert = insertBack;
|
ref typeof(this) enqueue(T x)
|
||||||
|
{
|
||||||
|
return enqueue(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
deprecated("Use enqueue instead.")
|
||||||
|
alias insert = enqueue;
|
||||||
|
|
||||||
|
deprecated("Use enqueue instead.")
|
||||||
|
alias insertBack = enqueue;
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto q = make!(Queue!int)(theAllocator);
|
Queue!int q;
|
||||||
|
|
||||||
assert(q.empty);
|
assert(q.empty);
|
||||||
q.insertBack(8);
|
q.enqueue(8).enqueue(9);
|
||||||
assert(q.front == 8);
|
assert(q.dequeue() == 8);
|
||||||
q.insertBack(9);
|
assert(q.dequeue() == 9);
|
||||||
assert(q.front == 8);
|
|
||||||
|
|
||||||
dispose(theAllocator, q);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: $(D_KEYWORD true) if the queue is empty.
|
* Returns: $(D_KEYWORD true) if the queue is empty.
|
||||||
*/
|
*/
|
||||||
@property bool empty() inout const
|
@property bool empty() const
|
||||||
{
|
{
|
||||||
return first.next is null;
|
return first.next is null;
|
||||||
}
|
}
|
||||||
@ -129,79 +220,82 @@ class Queue(T)
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto q = make!(Queue!int)(theAllocator);
|
Queue!int q;
|
||||||
int value = 7;
|
int value = 7;
|
||||||
|
|
||||||
assert(q.empty);
|
assert(q.empty);
|
||||||
q.insertBack(value);
|
q.enqueue(value);
|
||||||
assert(!q.empty);
|
assert(!q.empty);
|
||||||
|
|
||||||
dispose(theAllocator, q);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the position to the next element.
|
* Move the position to the next element.
|
||||||
|
*
|
||||||
|
* Returns: Dequeued element.
|
||||||
*/
|
*/
|
||||||
void popFront()
|
T dequeue()
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(!empty);
|
assert(!empty);
|
||||||
|
assert(allocator !is null);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
auto n = first.next.next;
|
auto n = first.next.next;
|
||||||
|
T ret = move(first.next.content);
|
||||||
|
|
||||||
dispose(allocator, first.next);
|
dispose(allocator, first.next);
|
||||||
first.next = n;
|
first.next = n;
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deprecated("Use dequeue instead.")
|
||||||
|
alias popFront = dequeue;
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto q = make!(Queue!int)(theAllocator);
|
Queue!int q;
|
||||||
|
|
||||||
q.insertBack(8);
|
q.enqueue(8).enqueue(9);
|
||||||
q.insertBack(9);
|
assert(q.dequeue() == 8);
|
||||||
assert(q.front == 8);
|
assert(q.dequeue() == 9);
|
||||||
q.popFront();
|
|
||||||
assert(q.front == 9);
|
|
||||||
|
|
||||||
dispose(theAllocator, q);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $(D_KEYWORD foreach) iteration.
|
* $(D_KEYWORD foreach) iteration. The elements will be automatically
|
||||||
|
* dequeued.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* dg = $(D_KEYWORD foreach) body.
|
* dg = $(D_KEYWORD foreach) body.
|
||||||
*/
|
*/
|
||||||
int opApply(scope int delegate(ref size_t i, ref T) dg)
|
int opApply(scope int delegate(ref size_t i, ref T) @nogc dg)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
for (size_t i = 0; !empty; ++i)
|
for (size_t i = 0; !empty; ++i)
|
||||||
{
|
{
|
||||||
if ((result = dg(i, front)) != 0)
|
auto e = dequeue();
|
||||||
|
if ((result = dg(i, e)) != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
popFront();
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
int opApply(scope int delegate(ref T) dg)
|
int opApply(scope int delegate(ref T) @nogc dg)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
while (!empty)
|
while (!empty)
|
||||||
{
|
{
|
||||||
if ((result = dg(front)) != 0)
|
auto e = dequeue();
|
||||||
|
if ((result = dg(e)) != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
popFront();
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -209,12 +303,10 @@ class Queue(T)
|
|||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto q = theAllocator.make!(Queue!int);
|
Queue!int q;
|
||||||
|
|
||||||
size_t j;
|
size_t j;
|
||||||
q.insertBack(5);
|
q.enqueue(5).enqueue(4).enqueue(9);
|
||||||
q.insertBack(4);
|
|
||||||
q.insertBack(9);
|
|
||||||
foreach (i, e; q)
|
foreach (i, e; q)
|
||||||
{
|
{
|
||||||
assert(i != 2 || e == 9);
|
assert(i != 2 || e == 9);
|
||||||
@ -226,9 +318,7 @@ class Queue(T)
|
|||||||
assert(q.empty);
|
assert(q.empty);
|
||||||
|
|
||||||
j = 0;
|
j = 0;
|
||||||
q.insertBack(5);
|
q.enqueue(5).enqueue(4).enqueue(9);
|
||||||
q.insertBack(4);
|
|
||||||
q.insertBack(9);
|
|
||||||
foreach (e; q)
|
foreach (e; q)
|
||||||
{
|
{
|
||||||
assert(j != 2 || e == 9);
|
assert(j != 2 || e == 9);
|
||||||
@ -238,48 +328,28 @@ class Queue(T)
|
|||||||
}
|
}
|
||||||
assert(j == 3);
|
assert(j == 3);
|
||||||
assert(q.empty);
|
assert(q.empty);
|
||||||
|
|
||||||
dispose(theAllocator, q);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Queue entry.
|
|
||||||
*/
|
|
||||||
protected struct Entry
|
|
||||||
{
|
|
||||||
/// Queue item content.
|
|
||||||
T content;
|
|
||||||
|
|
||||||
/// Next list item.
|
|
||||||
Entry* next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The first element of the list.
|
/// The first element of the list.
|
||||||
protected Entry first;
|
private Entry!T first;
|
||||||
|
|
||||||
/// The last element of the list.
|
/// The last element of the list.
|
||||||
protected Entry* rear;
|
private Entry!T* rear;
|
||||||
|
|
||||||
/// The allocator.
|
mixin DefaultAllocator;
|
||||||
protected IAllocator allocator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto q = theAllocator.make!(Queue!int);
|
Queue!int q;
|
||||||
|
|
||||||
q.insertBack(5);
|
q.enqueue(5);
|
||||||
assert(!q.empty);
|
assert(!q.empty);
|
||||||
|
|
||||||
q.insertBack(4);
|
q.enqueue(4).enqueue(9);
|
||||||
assert(q.front == 5);
|
|
||||||
|
|
||||||
q.insertBack(9);
|
assert(q.dequeue() == 5);
|
||||||
assert(q.front == 5);
|
|
||||||
|
|
||||||
q.popFront();
|
|
||||||
assert(q.front == 4);
|
|
||||||
|
|
||||||
foreach (i, ref e; q)
|
foreach (i, ref e; q)
|
||||||
{
|
{
|
||||||
@ -287,6 +357,4 @@ unittest
|
|||||||
assert(i != 1 || e == 9);
|
assert(i != 1 || e == 9);
|
||||||
}
|
}
|
||||||
assert(q.empty);
|
assert(q.empty);
|
||||||
|
|
||||||
theAllocator.dispose(q);
|
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -8,7 +8,7 @@
|
|||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
||||||
*/
|
*/
|
||||||
module tanya.container.bit;
|
module tanya.crypto.bit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper that allows bit manipulation on $(D_KEYWORD ubyte[]) array.
|
* Wrapper that allows bit manipulation on $(D_KEYWORD ubyte[]) array.
|
@ -10,7 +10,7 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.crypto.des;
|
module tanya.crypto.des;
|
||||||
|
|
||||||
import tanya.container.bit;
|
import tanya.crypto.bit;
|
||||||
import tanya.crypto.symmetric;
|
import tanya.crypto.symmetric;
|
||||||
|
|
||||||
/// Initial permutation table.
|
/// Initial permutation table.
|
||||||
|
@ -20,260 +20,260 @@ import std.typecons;
|
|||||||
* Supported padding mode.
|
* Supported padding mode.
|
||||||
*
|
*
|
||||||
* See_Also:
|
* See_Also:
|
||||||
* $(D_PSYMBOL pad)
|
* $(D_PSYMBOL pad)
|
||||||
*/
|
*/
|
||||||
enum PaddingMode
|
enum PaddingMode
|
||||||
{
|
{
|
||||||
zero,
|
zero,
|
||||||
pkcs7,
|
pkcs7,
|
||||||
ansiX923,
|
ansiX923,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Params:
|
* Params:
|
||||||
* input = Sequence that should be padded.
|
* input = Sequence that should be padded.
|
||||||
* mode = Padding mode.
|
* mode = Padding mode.
|
||||||
* blockSize = Block size.
|
* blockSize = Block size.
|
||||||
* allocator = Allocator was used to allocate $(D_PARAM input).
|
* allocator = Allocator was used to allocate $(D_PARAM input).
|
||||||
*
|
*
|
||||||
* Returns: The function modifies the initial array and returns it.
|
* Returns: The function modifies the initial array and returns it.
|
||||||
*
|
*
|
||||||
* See_Also:
|
* See_Also:
|
||||||
* $(D_PSYMBOL PaddingMode)
|
* $(D_PSYMBOL PaddingMode)
|
||||||
*/
|
*/
|
||||||
ubyte[] pad(ref ubyte[] input,
|
ubyte[] pad(ref ubyte[] input,
|
||||||
in PaddingMode mode,
|
in PaddingMode mode,
|
||||||
in ushort blockSize,
|
in ushort blockSize,
|
||||||
IAllocator allocator = theAllocator)
|
shared Allocator allocator = defaultAllocator)
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(blockSize > 0 && blockSize <= 256);
|
assert(blockSize > 0 && blockSize <= 256);
|
||||||
assert(blockSize % 64 == 0);
|
assert(blockSize % 64 == 0);
|
||||||
assert(input.length > 0);
|
assert(input.length > 0);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
immutable rest = cast(ubyte) input.length % blockSize;
|
immutable rest = cast(ubyte) input.length % blockSize;
|
||||||
immutable size_t lastBlock = input.length - (rest > 0 ? rest : blockSize);
|
immutable size_t lastBlock = input.length - (rest > 0 ? rest : blockSize);
|
||||||
immutable needed = cast(ubyte) (rest > 0 ? blockSize - rest : 0);
|
immutable needed = cast(ubyte) (rest > 0 ? blockSize - rest : 0);
|
||||||
|
|
||||||
final switch (mode) with (PaddingMode)
|
final switch (mode) with (PaddingMode)
|
||||||
{
|
{
|
||||||
case zero:
|
case zero:
|
||||||
allocator.expandArray(input, needed);
|
allocator.resizeArray(input, input.length + needed);
|
||||||
break;
|
break;
|
||||||
case pkcs7:
|
case pkcs7:
|
||||||
if (needed)
|
if (needed)
|
||||||
{
|
{
|
||||||
allocator.expandArray(input, needed);
|
allocator.resizeArray(input, input.length + needed);
|
||||||
input[input.length - needed ..$].each!((ref e) => e = needed);
|
input[input.length - needed ..$].each!((ref e) => e = needed);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
allocator.expandArray(input, blockSize);
|
allocator.resizeArray(input, input.length + blockSize);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ansiX923:
|
case ansiX923:
|
||||||
allocator.expandArray(input, needed ? needed : blockSize);
|
allocator.resizeArray(input, input.length + (needed ? needed : blockSize));
|
||||||
input[$ - 1] = needed;
|
input[$ - 1] = needed;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
{ // Zeros
|
{ // Zeros
|
||||||
auto input = theAllocator.makeArray!ubyte(50);
|
auto input = defaultAllocator.makeArray!ubyte(50);
|
||||||
|
|
||||||
pad(input, PaddingMode.zero, 64);
|
pad(input, PaddingMode.zero, 64);
|
||||||
assert(input.length == 64);
|
assert(input.length == 64);
|
||||||
|
|
||||||
pad(input, PaddingMode.zero, 64);
|
pad(input, PaddingMode.zero, 64);
|
||||||
assert(input.length == 64);
|
assert(input.length == 64);
|
||||||
assert(input[63] == 0);
|
assert(input[63] == 0);
|
||||||
|
|
||||||
theAllocator.dispose(input);
|
defaultAllocator.dispose(input);
|
||||||
}
|
}
|
||||||
{ // PKCS#7
|
{ // PKCS#7
|
||||||
auto input = theAllocator.makeArray!ubyte(50);
|
auto input = defaultAllocator.makeArray!ubyte(50);
|
||||||
for (ubyte i; i < 40; ++i)
|
for (ubyte i; i < 40; ++i)
|
||||||
{
|
{
|
||||||
input[i] = i;
|
input[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
pad(input, PaddingMode.pkcs7, 64);
|
pad(input, PaddingMode.pkcs7, 64);
|
||||||
assert(input.length == 64);
|
assert(input.length == 64);
|
||||||
for (ubyte i; i < 64; ++i)
|
for (ubyte i; i < 64; ++i)
|
||||||
{
|
{
|
||||||
if (i >= 40 && i < 50)
|
if (i >= 40 && i < 50)
|
||||||
{
|
{
|
||||||
assert(input[i] == 0);
|
assert(input[i] == 0);
|
||||||
}
|
}
|
||||||
else if (i >= 50)
|
else if (i >= 50)
|
||||||
{
|
{
|
||||||
assert(input[i] == 14);
|
assert(input[i] == 14);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
assert(input[i] == i);
|
assert(input[i] == i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pad(input, PaddingMode.pkcs7, 64);
|
pad(input, PaddingMode.pkcs7, 64);
|
||||||
assert(input.length == 128);
|
assert(input.length == 128);
|
||||||
for (ubyte i; i < 128; ++i)
|
for (ubyte i; i < 128; ++i)
|
||||||
{
|
{
|
||||||
if (i >= 64 || (i >= 40 && i < 50))
|
if (i >= 64 || (i >= 40 && i < 50))
|
||||||
{
|
{
|
||||||
assert(input[i] == 0);
|
assert(input[i] == 0);
|
||||||
}
|
}
|
||||||
else if (i >= 50 && i < 64)
|
else if (i >= 50 && i < 64)
|
||||||
{
|
{
|
||||||
assert(input[i] == 14);
|
assert(input[i] == 14);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
assert(input[i] == i);
|
assert(input[i] == i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
theAllocator.dispose(input);
|
defaultAllocator.dispose(input);
|
||||||
}
|
}
|
||||||
{ // ANSI X.923
|
{ // ANSI X.923
|
||||||
auto input = theAllocator.makeArray!ubyte(50);
|
auto input = defaultAllocator.makeArray!ubyte(50);
|
||||||
for (ubyte i; i < 40; ++i)
|
for (ubyte i; i < 40; ++i)
|
||||||
{
|
{
|
||||||
input[i] = i;
|
input[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
pad(input, PaddingMode.ansiX923, 64);
|
pad(input, PaddingMode.ansiX923, 64);
|
||||||
assert(input.length == 64);
|
assert(input.length == 64);
|
||||||
for (ubyte i; i < 64; ++i)
|
for (ubyte i; i < 64; ++i)
|
||||||
{
|
{
|
||||||
if (i < 40)
|
if (i < 40)
|
||||||
{
|
{
|
||||||
assert(input[i] == i);
|
assert(input[i] == i);
|
||||||
}
|
}
|
||||||
else if (i == 63)
|
else if (i == 63)
|
||||||
{
|
{
|
||||||
assert(input[i] == 14);
|
assert(input[i] == 14);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
assert(input[i] == 0);
|
assert(input[i] == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pad(input, PaddingMode.pkcs7, 64);
|
pad(input, PaddingMode.pkcs7, 64);
|
||||||
assert(input.length == 128);
|
assert(input.length == 128);
|
||||||
for (ubyte i = 0; i < 128; ++i)
|
for (ubyte i = 0; i < 128; ++i)
|
||||||
{
|
{
|
||||||
if (i < 40)
|
if (i < 40)
|
||||||
{
|
{
|
||||||
assert(input[i] == i);
|
assert(input[i] == i);
|
||||||
}
|
}
|
||||||
else if (i == 63)
|
else if (i == 63)
|
||||||
{
|
{
|
||||||
assert(input[i] == 14);
|
assert(input[i] == 14);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
assert(input[i] == 0);
|
assert(input[i] == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
theAllocator.dispose(input);
|
defaultAllocator.dispose(input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Params:
|
* Params:
|
||||||
* input = Sequence that should be padded.
|
* input = Sequence that should be padded.
|
||||||
* mode = Padding mode.
|
* mode = Padding mode.
|
||||||
* blockSize = Block size.
|
* blockSize = Block size.
|
||||||
* allocator = Allocator was used to allocate $(D_PARAM input).
|
* allocator = Allocator was used to allocate $(D_PARAM input).
|
||||||
*
|
*
|
||||||
* Returns: The function modifies the initial array and returns it.
|
* Returns: The function modifies the initial array and returns it.
|
||||||
*
|
*
|
||||||
* See_Also:
|
* See_Also:
|
||||||
* $(D_PSYMBOL pad)
|
* $(D_PSYMBOL pad)
|
||||||
*/
|
*/
|
||||||
ref ubyte[] unpad(ref ubyte[] input,
|
ref ubyte[] unpad(ref ubyte[] input,
|
||||||
in PaddingMode mode,
|
in PaddingMode mode,
|
||||||
in ushort blockSize,
|
in ushort blockSize,
|
||||||
IAllocator allocator = theAllocator)
|
shared Allocator allocator = defaultAllocator)
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(input.length != 0);
|
assert(input.length != 0);
|
||||||
assert(input.length % 64 == 0);
|
assert(input.length % 64 == 0);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
final switch (mode) with (PaddingMode)
|
final switch (mode) with (PaddingMode)
|
||||||
{
|
{
|
||||||
case zero:
|
case zero:
|
||||||
break;
|
break;
|
||||||
case pkcs7:
|
case pkcs7:
|
||||||
case ansiX923:
|
case ansiX923:
|
||||||
immutable last = input[$ - 1];
|
immutable last = input[$ - 1];
|
||||||
|
|
||||||
allocator.shrinkArray(input, last ? last : blockSize);
|
allocator.resizeArray(input, input.length - (last ? last : blockSize));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
{ // Zeros
|
{ // Zeros
|
||||||
auto input = theAllocator.makeArray!ubyte(50);
|
auto input = defaultAllocator.makeArray!ubyte(50);
|
||||||
auto inputDup = theAllocator.makeArray!ubyte(50);
|
auto inputDup = defaultAllocator.makeArray!ubyte(50);
|
||||||
|
|
||||||
pad(input, PaddingMode.zero, 64);
|
pad(input, PaddingMode.zero, 64);
|
||||||
pad(inputDup, PaddingMode.zero, 64);
|
pad(inputDup, PaddingMode.zero, 64);
|
||||||
|
|
||||||
unpad(input, PaddingMode.zero, 64);
|
unpad(input, PaddingMode.zero, 64);
|
||||||
assert(input == inputDup);
|
assert(input == inputDup);
|
||||||
|
|
||||||
theAllocator.dispose(input);
|
defaultAllocator.dispose(input);
|
||||||
theAllocator.dispose(inputDup);
|
defaultAllocator.dispose(inputDup);
|
||||||
|
|
||||||
}
|
}
|
||||||
{ // PKCS#7
|
{ // PKCS#7
|
||||||
auto input = theAllocator.makeArray!ubyte(50);
|
auto input = defaultAllocator.makeArray!ubyte(50);
|
||||||
auto inputDup = theAllocator.makeArray!ubyte(50);
|
auto inputDup = defaultAllocator.makeArray!ubyte(50);
|
||||||
for (ubyte i; i < 40; ++i)
|
for (ubyte i; i < 40; ++i)
|
||||||
{
|
{
|
||||||
input[i] = i;
|
input[i] = i;
|
||||||
inputDup[i] = i;
|
inputDup[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
pad(input, PaddingMode.pkcs7, 64);
|
pad(input, PaddingMode.pkcs7, 64);
|
||||||
unpad(input, PaddingMode.pkcs7, 64);
|
unpad(input, PaddingMode.pkcs7, 64);
|
||||||
assert(input == inputDup);
|
assert(input == inputDup);
|
||||||
|
|
||||||
theAllocator.dispose(input);
|
defaultAllocator.dispose(input);
|
||||||
theAllocator.dispose(inputDup);
|
defaultAllocator.dispose(inputDup);
|
||||||
}
|
}
|
||||||
{ // ANSI X.923
|
{ // ANSI X.923
|
||||||
auto input = theAllocator.makeArray!ubyte(50);
|
auto input = defaultAllocator.makeArray!ubyte(50);
|
||||||
auto inputDup = theAllocator.makeArray!ubyte(50);
|
auto inputDup = defaultAllocator.makeArray!ubyte(50);
|
||||||
for (ubyte i; i < 40; ++i)
|
for (ubyte i; i < 40; ++i)
|
||||||
{
|
{
|
||||||
input[i] = i;
|
input[i] = i;
|
||||||
inputDup[i] = i;
|
inputDup[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
pad(input, PaddingMode.pkcs7, 64);
|
pad(input, PaddingMode.pkcs7, 64);
|
||||||
unpad(input, PaddingMode.pkcs7, 64);
|
unpad(input, PaddingMode.pkcs7, 64);
|
||||||
assert(input == inputDup);
|
assert(input == inputDup);
|
||||||
|
|
||||||
theAllocator.dispose(input);
|
defaultAllocator.dispose(input);
|
||||||
theAllocator.dispose(inputDup);
|
defaultAllocator.dispose(inputDup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,7 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.crypto;
|
module tanya.crypto;
|
||||||
|
|
||||||
public
|
public import tanya.crypto.bit;
|
||||||
{
|
public import tanya.crypto.des;
|
||||||
import tanya.crypto.des;
|
public import tanya.crypto.mode;
|
||||||
import tanya.crypto.mode;
|
public import tanya.crypto.symmetric;
|
||||||
import tanya.crypto.symmetric;
|
|
||||||
}
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -10,7 +10,9 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.math;
|
module tanya.math;
|
||||||
|
|
||||||
|
import std.traits;
|
||||||
public import tanya.math.mp;
|
public import tanya.math.mp;
|
||||||
|
public import tanya.math.random;
|
||||||
|
|
||||||
version (unittest)
|
version (unittest)
|
||||||
{
|
{
|
||||||
@ -20,26 +22,32 @@ version (unittest)
|
|||||||
/**
|
/**
|
||||||
* Computes $(D_PARAM x) to the power $(D_PARAM y) modulo $(D_PARAM z).
|
* Computes $(D_PARAM x) to the power $(D_PARAM y) modulo $(D_PARAM z).
|
||||||
*
|
*
|
||||||
|
* If $(D_PARAM I) is an $(D_PSYMBOL Integer), the allocator of $(D_PARAM x)
|
||||||
|
* is used to allocate the result.
|
||||||
|
*
|
||||||
* Params:
|
* Params:
|
||||||
|
* I = Base type.
|
||||||
|
* G = Exponent type.
|
||||||
|
* H = Divisor type:
|
||||||
* x = Base.
|
* x = Base.
|
||||||
* y = Exponent.
|
* y = Exponent.
|
||||||
* z = Divisor.
|
* z = Divisor.
|
||||||
*
|
*
|
||||||
* Returns: Reminder of the division of $(D_PARAM x) to the power $(D_PARAM y)
|
* Returns: Reminder of the division of $(D_PARAM x) to the power $(D_PARAM y)
|
||||||
* by $(D_PARAM z).
|
* by $(D_PARAM z).
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE z > 0)
|
||||||
*/
|
*/
|
||||||
ulong pow(ulong x, ulong y, ulong z) nothrow pure @safe @nogc
|
H pow(I, G, H)(in auto ref I x, in auto ref G y, in auto ref H z)
|
||||||
|
if (isIntegral!I && isIntegral!G && isIntegral!H)
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(z > 0);
|
assert(z > 0, "Division by zero.");
|
||||||
}
|
|
||||||
out (result)
|
|
||||||
{
|
|
||||||
assert(result >= 0);
|
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
ulong mask = ulong.max / 2 + 1, result;
|
G mask = G.max / 2 + 1;
|
||||||
|
H result;
|
||||||
|
|
||||||
if (y == 0)
|
if (y == 0)
|
||||||
{
|
{
|
||||||
@ -49,40 +57,92 @@ body
|
|||||||
{
|
{
|
||||||
return x % z;
|
return x % z;
|
||||||
}
|
}
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
auto bit = y & mask;
|
immutable bit = y & mask;
|
||||||
if (!result && bit)
|
if (!result && bit)
|
||||||
{
|
{
|
||||||
result = x;
|
result = x;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
result *= result;
|
result *= result;
|
||||||
if (bit)
|
if (bit)
|
||||||
{
|
{
|
||||||
result *= x;
|
result *= x;
|
||||||
}
|
}
|
||||||
result %= z;
|
result %= z;
|
||||||
|
}
|
||||||
}
|
while (mask >>= 1);
|
||||||
while (mask >>= 1);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
I pow(I)(in auto ref I x, in auto ref I y, in auto ref I z)
|
||||||
|
if (is(I == Integer))
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(z.length > 0, "Division by zero.");
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
size_t i = y.length;
|
||||||
|
auto tmp2 = Integer(x.allocator), tmp1 = Integer(x, x.allocator);
|
||||||
|
Integer result = Integer(x.allocator);
|
||||||
|
|
||||||
|
if (x.length == 0 && i != 0)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = 1;
|
||||||
|
}
|
||||||
|
while (i)
|
||||||
|
{
|
||||||
|
--i;
|
||||||
|
for (ubyte mask = 0x01; mask; mask <<= 1)
|
||||||
|
{
|
||||||
|
if (y.rep[i] & mask)
|
||||||
|
{
|
||||||
|
result *= tmp1;
|
||||||
|
result %= z;
|
||||||
|
}
|
||||||
|
tmp2 = tmp1;
|
||||||
|
tmp1 *= tmp2;
|
||||||
|
tmp1 %= z;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
assert(pow(3, 5, 7) == 5);
|
||||||
|
assert(pow(2, 2, 1) == 0);
|
||||||
|
assert(pow(3, 3, 3) == 0);
|
||||||
|
assert(pow(7, 4, 2) == 1);
|
||||||
|
assert(pow(53, 0, 2) == 1);
|
||||||
|
assert(pow(53, 1, 3) == 2);
|
||||||
|
assert(pow(53, 2, 5) == 4);
|
||||||
|
assert(pow(0, 0, 5) == 1);
|
||||||
|
assert(pow(0, 5, 5) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
assert(pow(3, 5, 7) == 5);
|
assert(cast(long) pow(Integer(3), Integer(5), Integer(7)) == 5);
|
||||||
assert(pow(2, 2, 1) == 0);
|
assert(cast(long) pow(Integer(2), Integer(2), Integer(1)) == 0);
|
||||||
assert(pow(3, 3, 3) == 0);
|
assert(cast(long) pow(Integer(3), Integer(3), Integer(3)) == 0);
|
||||||
assert(pow(7, 4, 2) == 1);
|
assert(cast(long) pow(Integer(7), Integer(4), Integer(2)) == 1);
|
||||||
assert(pow(53, 0, 2) == 1);
|
assert(cast(long) pow(Integer(53), Integer(0), Integer(2)) == 1);
|
||||||
assert(pow(53, 1, 3) == 2);
|
assert(cast(long) pow(Integer(53), Integer(1), Integer(3)) == 2);
|
||||||
assert(pow(53, 2, 5) == 4);
|
assert(cast(long) pow(Integer(53), Integer(2), Integer(5)) == 4);
|
||||||
assert(pow(0, 0, 5) == 1);
|
assert(cast(long) pow(Integer(0), Integer(0), Integer(5)) == 1);
|
||||||
assert(pow(0, 5, 5) == 0);
|
assert(cast(long) pow(Integer(0), Integer(5), Integer(5)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -103,7 +163,7 @@ bool isPseudoprime(ulong x) nothrow pure @safe @nogc
|
|||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
uint[30] known = [74623, 74653, 74687, 74699, 74707, 74713, 74717, 74719,
|
uint[30] known = [74623, 74653, 74687, 74699, 74707, 74713, 74717, 74719,
|
||||||
74843, 74747, 74759, 74761, 74771, 74779, 74797, 74821,
|
74843, 74747, 74759, 74761, 74771, 74779, 74797, 74821,
|
||||||
74827, 9973, 104729, 15485867, 49979693, 104395303,
|
74827, 9973, 104729, 15485867, 49979693, 104395303,
|
||||||
593441861, 104729, 15485867, 49979693, 104395303,
|
593441861, 104729, 15485867, 49979693, 104395303,
|
||||||
593441861, 899809363, 982451653];
|
593441861, 899809363, 982451653];
|
||||||
|
@ -3,16 +3,18 @@
|
|||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Random number generator.
|
||||||
|
*
|
||||||
* 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:belka@caraus.de, Eugene Wissner)
|
||||||
*/
|
*/
|
||||||
module tanya.random;
|
module tanya.math.random;
|
||||||
|
|
||||||
import tanya.memory;
|
|
||||||
import std.digest.sha;
|
import std.digest.sha;
|
||||||
import std.typecons;
|
import std.typecons;
|
||||||
|
import tanya.memory;
|
||||||
|
|
||||||
/// Block size of entropy accumulator (SHA-512).
|
/// Block size of entropy accumulator (SHA-512).
|
||||||
enum blockSize = 64;
|
enum blockSize = 64;
|
||||||
@ -148,13 +150,13 @@ version (linux)
|
|||||||
/**
|
/**
|
||||||
* Pseudorandom number generator.
|
* Pseudorandom number generator.
|
||||||
* ---
|
* ---
|
||||||
* auto entropy = theAllocator.make!Entropy;
|
* auto entropy = defaultAllocator.make!Entropy();
|
||||||
*
|
*
|
||||||
* ubyte[blockSize] output;
|
* ubyte[blockSize] output;
|
||||||
*
|
*
|
||||||
* output = entropy.random;
|
* output = entropy.random;
|
||||||
*
|
*
|
||||||
* theAllocator.finalize(entropy);
|
* defaultAllocator.finalize(entropy);
|
||||||
* ---
|
* ---
|
||||||
*/
|
*/
|
||||||
class Entropy
|
class Entropy
|
||||||
@ -164,7 +166,7 @@ class Entropy
|
|||||||
|
|
||||||
private ubyte sourceCount_;
|
private ubyte sourceCount_;
|
||||||
|
|
||||||
private IAllocator allocator;
|
private shared Allocator allocator;
|
||||||
|
|
||||||
/// Entropy accumulator.
|
/// Entropy accumulator.
|
||||||
protected SHA!(maxGather * 8, 512) accumulator;
|
protected SHA!(maxGather * 8, 512) accumulator;
|
||||||
@ -175,7 +177,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, IAllocator allocator = theAllocator)
|
this(size_t maxSources = 20, shared Allocator allocator = defaultAllocator)
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(maxSources > 0 && maxSources <= ubyte.max);
|
assert(maxSources > 0 && maxSources <= ubyte.max);
|
@ -10,173 +10,118 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.memory.allocator;
|
module tanya.memory.allocator;
|
||||||
|
|
||||||
import std.experimental.allocator;
|
|
||||||
import std.traits;
|
|
||||||
import std.typecons;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class implementing a basic allocator.
|
* Abstract class implementing a basic allocator.
|
||||||
*/
|
*/
|
||||||
abstract class Allocator : IAllocator
|
interface Allocator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Not supported.
|
* Returns: Alignment offered.
|
||||||
*
|
*/
|
||||||
* Returns: $(D_KEYWORD false).
|
@property uint alignment() const shared pure nothrow @safe @nogc;
|
||||||
*/
|
|
||||||
bool deallocateAll() const @nogc @safe pure nothrow
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Not supported.
|
* Allocates $(D_PARAM size) bytes of memory.
|
||||||
*
|
*
|
||||||
* Returns $(D_PSYMBOL Ternary.unknown).
|
* Params:
|
||||||
*/
|
* size = Amount of memory to allocate.
|
||||||
Ternary empty() const @nogc @safe pure nothrow
|
*
|
||||||
{
|
* Returns: Pointer to the new allocated memory.
|
||||||
return Ternary.unknown;
|
*/
|
||||||
}
|
void[] allocate(in size_t size) shared nothrow @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Not supported.
|
* Deallocates a memory block.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* b = Memory block.
|
* p = A pointer to the memory block to be freed.
|
||||||
*
|
*
|
||||||
* Returns: $(D_PSYMBOL Ternary.unknown).
|
* Returns: Whether the deallocation was successful.
|
||||||
*/
|
*/
|
||||||
Ternary owns(void[] b) const @nogc @safe pure nothrow
|
bool deallocate(void[] p) shared nothrow @nogc;
|
||||||
{
|
|
||||||
return Ternary.unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Not supported.
|
* Increases or decreases the size of a memory block.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* p = Pointer to a memory block.
|
* p = A pointer to the memory block.
|
||||||
* result = Full block allocated.
|
* size = Size of the reallocated block.
|
||||||
*
|
*
|
||||||
* Returns: $(D_PSYMBOL Ternary.unknown).
|
* Returns: Pointer to the allocated memory.
|
||||||
*/
|
*/
|
||||||
Ternary resolveInternalPointer(void* p, ref void[] result)
|
bool reallocate(ref void[] p, in size_t size) shared nothrow @nogc;
|
||||||
const @nogc @safe pure nothrow
|
|
||||||
{
|
|
||||||
return Ternary.unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Params:
|
* Expands a memory block in place.
|
||||||
* size = Amount of memory to allocate.
|
*
|
||||||
*
|
* Params:
|
||||||
* Returns: The good allocation size that guarantees zero internal
|
* p = A pointer to the memory block.
|
||||||
* fragmentation.
|
* size = Size of the reallocated block.
|
||||||
*/
|
*
|
||||||
size_t goodAllocSize(size_t s)
|
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
||||||
{
|
*/
|
||||||
auto rem = s % alignment;
|
bool expand(ref void[] p, in size_t size) shared nothrow @nogc;
|
||||||
return rem ? s + alignment - rem : s;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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:
|
|
||||||
* n = Amount of memory to allocate.
|
|
||||||
* a = Alignment.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD null).
|
|
||||||
*/
|
|
||||||
void[] alignedAllocate(size_t n, uint a) const @nogc @safe pure nothrow
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Not supported.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* n = Amount of memory to allocate.
|
|
||||||
* a = Alignment.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD false).
|
|
||||||
*/
|
|
||||||
bool alignedReallocate(ref void[] b, size_t size, uint alignment)
|
|
||||||
const @nogc @safe pure nothrow
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Params:
|
* The mixin generates common methods for classes and structs using
|
||||||
* T = Element type of the array being created.
|
* allocators. It provides a protected member, constructor and a read-only property,
|
||||||
* allocator = The allocator used for getting memory.
|
* that checks if an allocator was already set and sets it to the default
|
||||||
* array = A reference to the array being changed.
|
* one, if not (useful for structs which don't have a default constructor).
|
||||||
* length = New array length.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could
|
|
||||||
* not be reallocated. In the latter
|
|
||||||
*/
|
*/
|
||||||
bool resizeArray(T)(IAllocator allocator,
|
mixin template DefaultAllocator()
|
||||||
ref T[] array,
|
|
||||||
in size_t length)
|
|
||||||
{
|
{
|
||||||
void[] buf = array;
|
/// Allocator.
|
||||||
|
protected shared Allocator allocator_;
|
||||||
|
|
||||||
if (!allocator.reallocate(buf, length * T.sizeof))
|
/**
|
||||||
{
|
* Params:
|
||||||
return false;
|
* allocator = The allocator should be used.
|
||||||
}
|
*/
|
||||||
array = cast(T[]) buf;
|
this(shared Allocator allocator)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(allocator !is null);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
this.allocator_ = allocator;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
/**
|
||||||
|
* This property checks if the allocator was set in the constructor
|
||||||
|
* and sets it to the default one, if not.
|
||||||
|
*
|
||||||
|
* Returns: Used allocator.
|
||||||
|
*
|
||||||
|
* Postcondition: $(D_INLINECODE allocator_ !is null)
|
||||||
|
*/
|
||||||
|
protected @property shared(Allocator) allocator() nothrow @safe @nogc
|
||||||
|
out (allocator)
|
||||||
|
{
|
||||||
|
assert(allocator !is null);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
if (allocator_ is null)
|
||||||
|
{
|
||||||
|
allocator_ = defaultAllocator;
|
||||||
|
}
|
||||||
|
return allocator_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
@property shared(Allocator) allocator() const nothrow @trusted @nogc
|
||||||
|
out (allocator)
|
||||||
|
{
|
||||||
|
assert(allocator !is null);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
if (allocator_ is null)
|
||||||
|
{
|
||||||
|
return defaultAllocator;
|
||||||
|
}
|
||||||
|
return cast(shared Allocator) allocator_;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
|
||||||
unittest
|
|
||||||
{
|
|
||||||
int[] p;
|
|
||||||
|
|
||||||
theAllocator.resizeArray(p, 20);
|
|
||||||
assert(p.length == 20);
|
|
||||||
|
|
||||||
theAllocator.resizeArray(p, 30);
|
|
||||||
assert(p.length == 30);
|
|
||||||
|
|
||||||
theAllocator.resizeArray(p, 10);
|
|
||||||
assert(p.length == 10);
|
|
||||||
|
|
||||||
theAllocator.resizeArray(p, 0);
|
|
||||||
assert(p is null);
|
|
||||||
}
|
|
||||||
|
|
||||||
enum bool isFinalizable(T) = is(T == class) || is(T == interface)
|
|
||||||
|| hasElaborateDestructor!T || isDynamicArray!T;
|
|
||||||
|
@ -10,24 +10,24 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.memory.mmappool;
|
module tanya.memory.mmappool;
|
||||||
|
|
||||||
|
import core.stdc.string;
|
||||||
|
import std.algorithm.comparison;
|
||||||
import tanya.memory.allocator;
|
import tanya.memory.allocator;
|
||||||
import core.atomic;
|
|
||||||
import core.exception;
|
|
||||||
|
|
||||||
version (Posix)
|
version (Posix)
|
||||||
{
|
{
|
||||||
import core.stdc.errno;
|
import core.stdc.errno;
|
||||||
import core.sys.posix.sys.mman;
|
import core.sys.posix.sys.mman;
|
||||||
import core.sys.posix.unistd;
|
import core.sys.posix.unistd;
|
||||||
}
|
}
|
||||||
else version (Windows)
|
else version (Windows)
|
||||||
{
|
{
|
||||||
import core.sys.windows.winbase;
|
import core.sys.windows.winbase;
|
||||||
import core.sys.windows.windows;
|
import core.sys.windows.windows;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This allocator allocates memory in regions (multiple of 4 KB for example).
|
* This allocator allocates memory in regions (multiple of 64 KB for example).
|
||||||
* Each region is then splitted in blocks. So it doesn't request the memory
|
* Each region is then splitted in blocks. So it doesn't request the memory
|
||||||
* from the operating system on each call, but only if there are no large
|
* from the operating system on each call, but only if there are no large
|
||||||
* enough free blocks in the available regions.
|
* enough free blocks in the available regions.
|
||||||
@ -36,7 +36,7 @@ else version (Windows)
|
|||||||
* block as free and only if all blocks in the region are free, the complete
|
* block as free and only if all blocks in the region are free, the complete
|
||||||
* region is deallocated.
|
* region is deallocated.
|
||||||
*
|
*
|
||||||
* ----------------------------------------------------------------------------
|
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
* | | | | | || | | |
|
* | | | | | || | | |
|
||||||
* | |prev <----------- | || | | |
|
* | |prev <----------- | || | | |
|
||||||
* | R | B | | B | || R | B | |
|
* | R | B | | B | || R | B | |
|
||||||
@ -46,440 +46,531 @@ else version (Windows)
|
|||||||
* | O | K | | K | prev O | K | |
|
* | O | K | | K | prev O | K | |
|
||||||
* | N | -----------> next| || N | | |
|
* | N | -----------> next| || N | | |
|
||||||
* | | | | | || | | |
|
* | | | | | || | | |
|
||||||
* --------------------------------------------------- ------------------------
|
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
*
|
|
||||||
* TODO:
|
|
||||||
* $(UL
|
|
||||||
* $(LI Thread safety (core.atomic.cas))
|
|
||||||
* $(LI If two neighbour blocks are free, they can be merged)
|
|
||||||
* $(LI Reallocation shoud check if there is enough free space in the
|
|
||||||
* next block instead of always moving the memory)
|
|
||||||
* $(LI Make 64 KB regions mininmal region size on Linux)
|
|
||||||
* )
|
|
||||||
*/
|
*/
|
||||||
class MmapPool : Allocator
|
final class MmapPool : Allocator
|
||||||
{
|
{
|
||||||
@disable this();
|
/**
|
||||||
|
* Allocates $(D_PARAM size) bytes of memory.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* size = Amount of memory to allocate.
|
||||||
|
*
|
||||||
|
* Returns: Pointer to the new allocated memory.
|
||||||
|
*/
|
||||||
|
void[] allocate(in size_t size) shared nothrow @nogc
|
||||||
|
{
|
||||||
|
if (!size)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
immutable dataSize = addAlignment(size);
|
||||||
|
|
||||||
shared static this()
|
void* data = findBlock(dataSize);
|
||||||
{
|
if (data is null)
|
||||||
version (Posix)
|
{
|
||||||
{
|
data = initializeRegion(dataSize);
|
||||||
pageSize = sysconf(_SC_PAGE_SIZE);
|
}
|
||||||
}
|
|
||||||
else version (Windows)
|
|
||||||
{
|
|
||||||
SYSTEM_INFO si;
|
|
||||||
GetSystemInfo(&si);
|
|
||||||
pageSize = si.dwPageSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return data is null ? null : data[0 .. size];
|
||||||
* 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, TypeInfo ti = null) @nogc @trusted nothrow
|
|
||||||
{
|
|
||||||
if (!size)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
immutable dataSize = addAlignment(size);
|
|
||||||
|
|
||||||
void* data = findBlock(dataSize);
|
///
|
||||||
if (data is null)
|
nothrow unittest
|
||||||
{
|
{
|
||||||
data = initializeRegion(dataSize);
|
auto p = MmapPool.instance.allocate(20);
|
||||||
}
|
|
||||||
|
|
||||||
return data is null ? null : data[0..size];
|
assert(p);
|
||||||
}
|
|
||||||
|
|
||||||
///
|
MmapPool.instance.deallocate(p);
|
||||||
@nogc @safe nothrow unittest
|
}
|
||||||
{
|
|
||||||
auto p = MmapPool.instance.allocate(20);
|
|
||||||
|
|
||||||
assert(p);
|
/**
|
||||||
|
* Search for a block large enough to keep $(D_PARAM size) and split it
|
||||||
|
* into two blocks if the block is too large.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* size = Minimum size the block should have (aligned).
|
||||||
|
*
|
||||||
|
* Returns: Data the block points to or $(D_KEYWORD null).
|
||||||
|
*/
|
||||||
|
private void* findBlock(in ref size_t size) shared nothrow @nogc
|
||||||
|
{
|
||||||
|
Block block1;
|
||||||
|
RegionLoop: for (auto r = head; r !is null; r = r.next)
|
||||||
|
{
|
||||||
|
block1 = cast(Block) (cast(void*) r + RegionEntry.sizeof);
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (block1.free && block1.size >= size)
|
||||||
|
{
|
||||||
|
break RegionLoop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while ((block1 = block1.next) !is null);
|
||||||
|
}
|
||||||
|
if (block1 is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else if (block1.size >= size + alignment_ + BlockEntry.sizeof)
|
||||||
|
{ // Split the block if needed
|
||||||
|
Block block2 = cast(Block) (cast(void*) block1 + BlockEntry.sizeof + size);
|
||||||
|
block2.prev = block1;
|
||||||
|
block2.next = block1.next;
|
||||||
|
block2.free = true;
|
||||||
|
block2.size = block1.size - BlockEntry.sizeof - size;
|
||||||
|
block2.region = block1.region;
|
||||||
|
|
||||||
MmapPool.instance.deallocate(p);
|
if (block1.next !is null)
|
||||||
}
|
{
|
||||||
|
block1.next.prev = block2;
|
||||||
|
}
|
||||||
|
block1.next = block2;
|
||||||
|
block1.size = size;
|
||||||
|
}
|
||||||
|
block1.free = false;
|
||||||
|
block1.region.blocks = block1.region.blocks + 1;
|
||||||
|
|
||||||
/**
|
return cast(void*) block1 + BlockEntry.sizeof;
|
||||||
* Search for a block large enough to keep $(D_PARAM size) and split it
|
}
|
||||||
* into two blocks if the block is too large.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* size = Minimum size the block should have.
|
|
||||||
*
|
|
||||||
* Returns: Data the block points to or $(D_KEYWORD null).
|
|
||||||
*/
|
|
||||||
private void* findBlock(size_t size) @nogc nothrow
|
|
||||||
{
|
|
||||||
Block block1;
|
|
||||||
RegionLoop: for (auto r = head; r !is null; r = r.next)
|
|
||||||
{
|
|
||||||
block1 = cast(Block) (cast(void*) r + regionEntrySize);
|
|
||||||
do
|
|
||||||
{
|
|
||||||
if (block1.free && block1.size >= size)
|
|
||||||
{
|
|
||||||
break RegionLoop;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while ((block1 = block1.next) !is null);
|
|
||||||
}
|
|
||||||
if (block1 is null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else if (block1.size >= size + alignment + blockEntrySize)
|
|
||||||
{ // Split the block if needed
|
|
||||||
Block block2 = cast(Block) (cast(void*) block1 + blockEntrySize + size);
|
|
||||||
block2.prev = block1;
|
|
||||||
if (block1.next is null)
|
|
||||||
{
|
|
||||||
block2.next = null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
block2.next = block1.next.next;
|
|
||||||
}
|
|
||||||
block1.next = block2;
|
|
||||||
|
|
||||||
block1.free = false;
|
// Merge block with the next one.
|
||||||
block2.free = true;
|
private void mergeNext(Block block) shared const pure nothrow @safe @nogc
|
||||||
|
{
|
||||||
|
block.size = block.size + BlockEntry.sizeof + block.next.size;
|
||||||
|
if (block.next.next !is null)
|
||||||
|
{
|
||||||
|
block.next.next.prev = block;
|
||||||
|
}
|
||||||
|
block.next = block.next.next;
|
||||||
|
}
|
||||||
|
|
||||||
block2.size = block1.size - blockEntrySize - size;
|
/**
|
||||||
block1.size = size;
|
* 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 nothrow @nogc
|
||||||
|
{
|
||||||
|
if (p is null)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
block2.region = block1.region;
|
Block block = cast(Block) (p.ptr - BlockEntry.sizeof);
|
||||||
atomicOp!"+="(block1.region.blocks, 1);
|
if (block.region.blocks <= 1)
|
||||||
}
|
{
|
||||||
else
|
if (block.region.prev !is null)
|
||||||
{
|
{
|
||||||
block1.free = false;
|
block.region.prev.next = block.region.next;
|
||||||
atomicOp!"+="(block1.region.blocks, 1);
|
}
|
||||||
}
|
else // Replace the list head. It is being deallocated
|
||||||
return cast(void*) block1 + blockEntrySize;
|
{
|
||||||
}
|
head = block.region.next;
|
||||||
|
}
|
||||||
|
if (block.region.next !is null)
|
||||||
|
{
|
||||||
|
block.region.next.prev = block.region.prev;
|
||||||
|
}
|
||||||
|
version (Posix)
|
||||||
|
{
|
||||||
|
return munmap(cast(void*) block.region, block.region.size) == 0;
|
||||||
|
}
|
||||||
|
version (Windows)
|
||||||
|
{
|
||||||
|
return VirtualFree(cast(void*) block.region, 0, MEM_RELEASE) == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Merge blocks if neigbours are free.
|
||||||
|
if (block.next !is null && block.next.free)
|
||||||
|
{
|
||||||
|
mergeNext(block);
|
||||||
|
}
|
||||||
|
if (block.prev !is null && block.prev.free)
|
||||||
|
{
|
||||||
|
block.prev.size = block.prev.size + BlockEntry.sizeof + block.size;
|
||||||
|
if (block.next !is null)
|
||||||
|
{
|
||||||
|
block.next.prev = block.prev;
|
||||||
|
}
|
||||||
|
block.prev.next = block.next;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
block.free = true;
|
||||||
|
}
|
||||||
|
block.region.blocks = block.region.blocks - 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
///
|
||||||
* Deallocates a memory block.
|
nothrow unittest
|
||||||
*
|
{
|
||||||
* Params:
|
auto p = MmapPool.instance.allocate(20);
|
||||||
* p = A pointer to the memory block to be freed.
|
|
||||||
*
|
|
||||||
* Returns: Whether the deallocation was successful.
|
|
||||||
*/
|
|
||||||
bool deallocate(void[] p) @nogc @trusted nothrow
|
|
||||||
{
|
|
||||||
if (p is null)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Block block = cast(Block) (p.ptr - blockEntrySize);
|
assert(MmapPool.instance.deallocate(p));
|
||||||
if (block.region.blocks <= 1)
|
}
|
||||||
{
|
|
||||||
if (block.region.prev !is null)
|
|
||||||
{
|
|
||||||
block.region.prev.next = block.region.next;
|
|
||||||
}
|
|
||||||
else // Replace the list head. It is being deallocated
|
|
||||||
{
|
|
||||||
head = block.region.next;
|
|
||||||
}
|
|
||||||
if (block.region.next !is null)
|
|
||||||
{
|
|
||||||
block.region.next.prev = block.region.prev;
|
|
||||||
}
|
|
||||||
version (Posix)
|
|
||||||
{
|
|
||||||
return munmap(cast(void*) block.region, block.region.size) == 0;
|
|
||||||
}
|
|
||||||
version (Windows)
|
|
||||||
{
|
|
||||||
return VirtualFree(cast(void*) block.region, 0, MEM_RELEASE) == 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
block.free = true;
|
|
||||||
atomicOp!"-="(block.region.blocks, 1);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
/**
|
||||||
@nogc @safe nothrow unittest
|
* Expands a memory block in place.
|
||||||
{
|
*
|
||||||
auto p = MmapPool.instance.allocate(20);
|
* Params:
|
||||||
|
* p = A pointer to the memory block.
|
||||||
|
* size = Size of the reallocated block.
|
||||||
|
*
|
||||||
|
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
||||||
|
*/
|
||||||
|
bool expand(ref void[] p, in size_t size) shared nothrow @nogc
|
||||||
|
{
|
||||||
|
if (size <= p.length)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (p is null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Block block1 = cast(Block) (p.ptr - BlockEntry.sizeof);
|
||||||
|
|
||||||
assert(MmapPool.instance.deallocate(p));
|
if (block1.size >= size)
|
||||||
}
|
{
|
||||||
|
// Enough space in the current block. Can happen because of the alignment.
|
||||||
|
p = p.ptr[0 .. size];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
immutable dataSize = addAlignment(size);
|
||||||
|
immutable delta = dataSize - p.length;
|
||||||
|
|
||||||
/**
|
if (block1.next is null
|
||||||
* Increases or decreases the size of a memory block.
|
|| !block1.next.free
|
||||||
*
|
|| block1.next.size + BlockEntry.sizeof < delta)
|
||||||
* Params:
|
{
|
||||||
* p = A pointer to the memory block.
|
// It is the last block in the region or the next block is too small or not
|
||||||
* size = Size of the reallocated block.
|
// free.
|
||||||
*
|
return false;
|
||||||
* Returns: Whether the reallocation was successful.
|
}
|
||||||
*/
|
if (block1.next.size >= delta + alignment_)
|
||||||
bool reallocate(ref void[] p, size_t size) @nogc @trusted nothrow
|
{
|
||||||
{
|
// We should move the start position of the next block. The order may be
|
||||||
void[] reallocP;
|
// important because the old block and the new one can overlap.
|
||||||
|
auto block2 = cast(Block) (p.ptr + dataSize);
|
||||||
|
block2.size = block1.next.size - delta;
|
||||||
|
block2.free = true;
|
||||||
|
block2.region = block1.region;
|
||||||
|
block2.next = block1.next.next;
|
||||||
|
block2.prev = block1;
|
||||||
|
|
||||||
if (size == p.length)
|
block1.size = block1.size + delta;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (size > 0)
|
|
||||||
{
|
|
||||||
reallocP = allocate(size);
|
|
||||||
if (reallocP is null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (p !is null)
|
if (block1.next.next !is null)
|
||||||
{
|
{
|
||||||
if (size > p.length)
|
block1.next.next.prev = block2;
|
||||||
{
|
}
|
||||||
reallocP[0..p.length] = p[0..$];
|
block1.next = block2;
|
||||||
}
|
}
|
||||||
else if (size > 0)
|
else
|
||||||
{
|
{
|
||||||
reallocP[0..size] = p[0..size];
|
// The next block has enough space, but is too small for further
|
||||||
}
|
// allocations. Merge it with the current block.
|
||||||
deallocate(p);
|
mergeNext(block1);
|
||||||
}
|
}
|
||||||
p = reallocP;
|
|
||||||
|
|
||||||
return true;
|
p = p.ptr[0 .. size];
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@nogc nothrow unittest
|
nothrow unittest
|
||||||
{
|
{
|
||||||
void[] p;
|
void[] p;
|
||||||
MmapPool.instance.reallocate(p, 10 * int.sizeof);
|
assert(!MmapPool.instance.expand(p, 5));
|
||||||
(cast(int[]) p)[7] = 123;
|
assert(p is null);
|
||||||
|
|
||||||
assert(p.length == 40);
|
p = MmapPool.instance.allocate(1);
|
||||||
|
auto orig = p.ptr;
|
||||||
|
|
||||||
MmapPool.instance.reallocate(p, 8 * int.sizeof);
|
assert(MmapPool.instance.expand(p, 2));
|
||||||
|
assert(p.length == 2);
|
||||||
|
assert(p.ptr == orig);
|
||||||
|
|
||||||
assert(p.length == 32);
|
assert(MmapPool.instance.expand(p, 4));
|
||||||
assert((cast(int[]) p)[7] == 123);
|
assert(p.length == 4);
|
||||||
|
assert(p.ptr == orig);
|
||||||
|
|
||||||
MmapPool.instance.reallocate(p, 20 * int.sizeof);
|
assert(MmapPool.instance.expand(p, 2));
|
||||||
(cast(int[]) p)[15] = 8;
|
assert(p.length == 4);
|
||||||
|
assert(p.ptr == orig);
|
||||||
|
|
||||||
assert(p.length == 80);
|
MmapPool.instance.deallocate(p);
|
||||||
assert((cast(int[]) p)[15] == 8);
|
}
|
||||||
assert((cast(int[]) p)[7] == 123);
|
|
||||||
|
|
||||||
MmapPool.instance.reallocate(p, 8 * int.sizeof);
|
/**
|
||||||
|
* 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, in size_t size) shared nothrow @nogc
|
||||||
|
{
|
||||||
|
if (size == 0)
|
||||||
|
{
|
||||||
|
if (deallocate(p))
|
||||||
|
{
|
||||||
|
p = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (size <= p.length)
|
||||||
|
{
|
||||||
|
// Leave the block as is.
|
||||||
|
p = p.ptr[0 .. size];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (expand(p, size))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Can't extend, allocate a new block, copy and delete the previous.
|
||||||
|
void[] reallocP = allocate(size);
|
||||||
|
if (reallocP is null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (p !is null)
|
||||||
|
{
|
||||||
|
memcpy(reallocP.ptr, p.ptr, min(p.length, size));
|
||||||
|
deallocate(p);
|
||||||
|
}
|
||||||
|
p = reallocP;
|
||||||
|
|
||||||
assert(p.length == 32);
|
return true;
|
||||||
assert((cast(int[]) p)[7] == 123);
|
}
|
||||||
|
|
||||||
MmapPool.instance.deallocate(p);
|
///
|
||||||
}
|
nothrow unittest
|
||||||
|
{
|
||||||
|
void[] p;
|
||||||
|
MmapPool.instance.reallocate(p, 10 * int.sizeof);
|
||||||
|
(cast(int[]) p)[7] = 123;
|
||||||
|
|
||||||
/**
|
assert(p.length == 40);
|
||||||
* Static allocator instance and initializer.
|
|
||||||
*
|
|
||||||
* Returns: Global $(D_PSYMBOL MmapPool) instance.
|
|
||||||
*/
|
|
||||||
static @property ref MmapPool instance() @nogc @trusted nothrow
|
|
||||||
{
|
|
||||||
if (instance_ is null)
|
|
||||||
{
|
|
||||||
immutable instanceSize = addAlignment(__traits(classInstanceSize, MmapPool));
|
|
||||||
|
|
||||||
Region head; // Will become soon our region list head
|
MmapPool.instance.reallocate(p, 8 * int.sizeof);
|
||||||
void* data = initializeRegion(instanceSize, head);
|
|
||||||
if (data !is null)
|
|
||||||
{
|
|
||||||
data[0..instanceSize] = typeid(MmapPool).initializer[];
|
|
||||||
instance_ = cast(MmapPool) data;
|
|
||||||
instance_.head = head;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return instance_;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
assert(p.length == 32);
|
||||||
@nogc @safe nothrow unittest
|
assert((cast(int[]) p)[7] == 123);
|
||||||
{
|
|
||||||
assert(instance is instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
MmapPool.instance.reallocate(p, 20 * int.sizeof);
|
||||||
* Initializes a region for one element.
|
(cast(int[]) p)[15] = 8;
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* size = Aligned size of the first data block in the region.
|
|
||||||
* head = Region list head.
|
|
||||||
*
|
|
||||||
* Returns: A pointer to the data.
|
|
||||||
*/
|
|
||||||
private static void* initializeRegion(size_t size,
|
|
||||||
ref Region head) @nogc nothrow
|
|
||||||
{
|
|
||||||
immutable regionSize = calculateRegionSize(size);
|
|
||||||
|
|
||||||
version (Posix)
|
assert(p.length == 80);
|
||||||
{
|
assert((cast(int[]) p)[15] == 8);
|
||||||
void* p = mmap(null,
|
assert((cast(int[]) p)[7] == 123);
|
||||||
regionSize,
|
|
||||||
PROT_READ | PROT_WRITE,
|
|
||||||
MAP_PRIVATE | MAP_ANON,
|
|
||||||
-1,
|
|
||||||
0);
|
|
||||||
if (p is MAP_FAILED)
|
|
||||||
{
|
|
||||||
if (errno == ENOMEM)
|
|
||||||
{
|
|
||||||
onOutOfMemoryError();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else version (Windows)
|
|
||||||
{
|
|
||||||
void* p = VirtualAlloc(null,
|
|
||||||
regionSize,
|
|
||||||
MEM_COMMIT,
|
|
||||||
PAGE_READWRITE);
|
|
||||||
if (p is null)
|
|
||||||
{
|
|
||||||
if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY)
|
|
||||||
{
|
|
||||||
onOutOfMemoryError();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Region region = cast(Region) p;
|
MmapPool.instance.reallocate(p, 8 * int.sizeof);
|
||||||
region.blocks = 1;
|
|
||||||
region.size = regionSize;
|
|
||||||
|
|
||||||
// Set the pointer to the head of the region list
|
assert(p.length == 32);
|
||||||
if (head !is null)
|
assert((cast(int[]) p)[7] == 123);
|
||||||
{
|
|
||||||
head.prev = region;
|
|
||||||
}
|
|
||||||
region.next = head;
|
|
||||||
region.prev = null;
|
|
||||||
head = region;
|
|
||||||
|
|
||||||
// Initialize the data block
|
MmapPool.instance.deallocate(p);
|
||||||
void* memoryPointer = p + regionEntrySize;
|
}
|
||||||
Block block1 = cast(Block) memoryPointer;
|
|
||||||
block1.size = size;
|
|
||||||
block1.free = false;
|
|
||||||
|
|
||||||
// It is what we want to return
|
/**
|
||||||
void* data = memoryPointer + blockEntrySize;
|
* Static allocator instance and initializer.
|
||||||
|
*
|
||||||
|
* Returns: Global $(D_PSYMBOL MmapPool) instance.
|
||||||
|
*/
|
||||||
|
static @property ref shared(MmapPool) instance() nothrow @nogc
|
||||||
|
{
|
||||||
|
if (instance_ is null)
|
||||||
|
{
|
||||||
|
// Get system dependend page size.
|
||||||
|
version (Posix)
|
||||||
|
{
|
||||||
|
pageSize = sysconf(_SC_PAGE_SIZE);
|
||||||
|
if (pageSize < 65536)
|
||||||
|
{
|
||||||
|
pageSize = pageSize * 65536 / pageSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else version (Windows)
|
||||||
|
{
|
||||||
|
SYSTEM_INFO si;
|
||||||
|
GetSystemInfo(&si);
|
||||||
|
pageSize = si.dwPageSize;
|
||||||
|
}
|
||||||
|
|
||||||
// Free block after data
|
immutable instanceSize = addAlignment(__traits(classInstanceSize, MmapPool));
|
||||||
memoryPointer = data + size;
|
|
||||||
Block block2 = cast(Block) memoryPointer;
|
|
||||||
block1.prev = block2.next = null;
|
|
||||||
block1.next = block2;
|
|
||||||
block2.prev = block1;
|
|
||||||
block2.size = regionSize - size - regionEntrySize - blockEntrySize * 2;
|
|
||||||
block2.free = true;
|
|
||||||
block1.region = block2.region = region;
|
|
||||||
|
|
||||||
return data;
|
Region head; // Will become soon our region list head
|
||||||
}
|
void* data = initializeRegion(instanceSize, head);
|
||||||
|
if (data !is null)
|
||||||
|
{
|
||||||
|
memcpy(data, typeid(MmapPool).initializer.ptr, instanceSize);
|
||||||
|
instance_ = cast(shared MmapPool) data;
|
||||||
|
instance_.head = head;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance_;
|
||||||
|
}
|
||||||
|
|
||||||
/// Ditto.
|
///
|
||||||
private void* initializeRegion(size_t size) @nogc nothrow
|
nothrow unittest
|
||||||
{
|
{
|
||||||
return initializeRegion(size, head);
|
assert(instance is instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Params:
|
* Initializes a region for one element.
|
||||||
* x = Space to be aligned.
|
*
|
||||||
*
|
* Params:
|
||||||
* Returns: Aligned size of $(D_PARAM x).
|
* size = Aligned size of the first data block in the region.
|
||||||
*/
|
* head = Region list head.
|
||||||
pragma(inline)
|
*
|
||||||
private static immutable(size_t) addAlignment(size_t x)
|
* Returns: A pointer to the data.
|
||||||
@nogc @safe pure nothrow
|
*/
|
||||||
out (result)
|
private static void* initializeRegion(size_t size, ref Region head)
|
||||||
{
|
nothrow @nogc
|
||||||
assert(result > 0);
|
{
|
||||||
}
|
immutable regionSize = calculateRegionSize(size);
|
||||||
body
|
|
||||||
{
|
|
||||||
return (x - 1) / alignment_ * alignment_ + alignment_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
version (Posix)
|
||||||
* Params:
|
{
|
||||||
* x = Required space.
|
void* p = mmap(null,
|
||||||
*
|
regionSize,
|
||||||
* Returns: Minimum region size (a multiple of $(D_PSYMBOL pageSize)).
|
PROT_READ | PROT_WRITE,
|
||||||
*/
|
MAP_PRIVATE | MAP_ANON,
|
||||||
pragma(inline)
|
-1,
|
||||||
private static immutable(size_t) calculateRegionSize(size_t x)
|
0);
|
||||||
@nogc @safe pure nothrow
|
if (p is MAP_FAILED)
|
||||||
out (result)
|
{
|
||||||
{
|
return null;
|
||||||
assert(result > 0);
|
}
|
||||||
}
|
}
|
||||||
body
|
else version (Windows)
|
||||||
{
|
{
|
||||||
x += regionEntrySize + blockEntrySize * 2;
|
void* p = VirtualAlloc(null,
|
||||||
return x / pageSize * pageSize + pageSize;
|
regionSize,
|
||||||
}
|
MEM_COMMIT,
|
||||||
|
PAGE_READWRITE);
|
||||||
|
if (p is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@property uint alignment() const @nogc @safe pure nothrow
|
Region region = cast(Region) p;
|
||||||
{
|
region.blocks = 1;
|
||||||
return alignment_;
|
region.size = regionSize;
|
||||||
}
|
|
||||||
private enum alignment_ = 8;
|
|
||||||
|
|
||||||
private static MmapPool instance_;
|
// Set the pointer to the head of the region list
|
||||||
|
if (head !is null)
|
||||||
|
{
|
||||||
|
head.prev = region;
|
||||||
|
}
|
||||||
|
region.next = head;
|
||||||
|
region.prev = null;
|
||||||
|
head = region;
|
||||||
|
|
||||||
private shared static immutable size_t pageSize;
|
// Initialize the data block
|
||||||
|
void* memoryPointer = p + RegionEntry.sizeof;
|
||||||
|
Block block1 = cast(Block) memoryPointer;
|
||||||
|
block1.size = size;
|
||||||
|
block1.free = false;
|
||||||
|
|
||||||
private shared struct RegionEntry
|
// It is what we want to return
|
||||||
{
|
void* data = memoryPointer + BlockEntry.sizeof;
|
||||||
Region prev;
|
|
||||||
Region next;
|
|
||||||
uint blocks;
|
|
||||||
size_t size;
|
|
||||||
}
|
|
||||||
private alias Region = shared RegionEntry*;
|
|
||||||
private enum regionEntrySize = 32;
|
|
||||||
|
|
||||||
private shared Region head;
|
// Free block after data
|
||||||
|
memoryPointer = data + size;
|
||||||
|
Block block2 = cast(Block) memoryPointer;
|
||||||
|
block1.prev = block2.next = null;
|
||||||
|
block1.next = block2;
|
||||||
|
block2.prev = block1;
|
||||||
|
block2.size = regionSize - size - RegionEntry.sizeof - BlockEntry.sizeof * 2;
|
||||||
|
block2.free = true;
|
||||||
|
block1.region = block2.region = region;
|
||||||
|
|
||||||
private shared struct BlockEntry
|
return data;
|
||||||
{
|
}
|
||||||
Block prev;
|
|
||||||
Block next;
|
private void* initializeRegion(size_t size) shared nothrow @nogc
|
||||||
bool free;
|
{
|
||||||
size_t size;
|
return initializeRegion(size, head);
|
||||||
Region region;
|
}
|
||||||
}
|
|
||||||
private alias Block = shared BlockEntry*;
|
/*
|
||||||
private enum blockEntrySize = 40;
|
* Params:
|
||||||
|
* x = Space to be aligned.
|
||||||
|
*
|
||||||
|
* Returns: Aligned size of $(D_PARAM x).
|
||||||
|
*/
|
||||||
|
private static immutable(size_t) addAlignment(size_t x)
|
||||||
|
pure nothrow @safe @nogc
|
||||||
|
out (result)
|
||||||
|
{
|
||||||
|
assert(result > 0);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
return (x - 1) / alignment_ * alignment_ + alignment_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Params:
|
||||||
|
* x = Required space.
|
||||||
|
*
|
||||||
|
* Returns: Minimum region size (a multiple of $(D_PSYMBOL pageSize)).
|
||||||
|
*/
|
||||||
|
private static immutable(size_t) calculateRegionSize(size_t x)
|
||||||
|
nothrow @safe @nogc
|
||||||
|
out (result)
|
||||||
|
{
|
||||||
|
assert(result > 0);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
x += RegionEntry.sizeof + BlockEntry.sizeof * 2;
|
||||||
|
return x / pageSize * pageSize + pageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns: Alignment offered.
|
||||||
|
*/
|
||||||
|
@property uint alignment() shared const pure nothrow @safe @nogc
|
||||||
|
{
|
||||||
|
return alignment_;
|
||||||
|
}
|
||||||
|
private enum alignment_ = 8;
|
||||||
|
|
||||||
|
private shared static MmapPool instance_;
|
||||||
|
private shared static size_t pageSize;
|
||||||
|
|
||||||
|
private shared struct RegionEntry
|
||||||
|
{
|
||||||
|
Region prev;
|
||||||
|
Region next;
|
||||||
|
uint blocks;
|
||||||
|
size_t size;
|
||||||
|
}
|
||||||
|
private alias Region = shared RegionEntry*;
|
||||||
|
private shared Region head;
|
||||||
|
|
||||||
|
private shared struct BlockEntry
|
||||||
|
{
|
||||||
|
Block prev;
|
||||||
|
Block next;
|
||||||
|
Region region;
|
||||||
|
size_t size;
|
||||||
|
bool free;
|
||||||
|
}
|
||||||
|
private alias Block = shared BlockEntry*;
|
||||||
}
|
}
|
||||||
|
@ -10,5 +10,225 @@
|
|||||||
*/
|
*/
|
||||||
module tanya.memory;
|
module tanya.memory;
|
||||||
|
|
||||||
|
import core.exception;
|
||||||
|
public import std.experimental.allocator : make, makeArray;
|
||||||
|
import std.traits;
|
||||||
public import tanya.memory.allocator;
|
public import tanya.memory.allocator;
|
||||||
public import std.experimental.allocator;
|
public import tanya.memory.types;
|
||||||
|
|
||||||
|
// From druntime
|
||||||
|
private extern (C) void _d_monitordelete(Object h, bool det) nothrow @nogc;
|
||||||
|
|
||||||
|
shared Allocator allocator;
|
||||||
|
|
||||||
|
shared static this() nothrow @trusted @nogc
|
||||||
|
{
|
||||||
|
import tanya.memory.mmappool;
|
||||||
|
allocator = MmapPool.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property ref shared(Allocator) defaultAllocator() nothrow @safe @nogc
|
||||||
|
{
|
||||||
|
return allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property void defaultAllocator(shared(Allocator) allocator) nothrow @safe @nogc
|
||||||
|
{
|
||||||
|
.allocator = allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the size in bytes of the state that needs to be allocated to hold an
|
||||||
|
* object of type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Object type.
|
||||||
|
*/
|
||||||
|
template stateSize(T)
|
||||||
|
{
|
||||||
|
static if (is(T == class) || is(T == interface))
|
||||||
|
{
|
||||||
|
enum stateSize = __traits(classInstanceSize, T);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
enum stateSize = T.sizeof;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* size = Raw size.
|
||||||
|
* alignment = Alignment.
|
||||||
|
*
|
||||||
|
* Returns: Aligned size.
|
||||||
|
*/
|
||||||
|
size_t alignedSize(in size_t size, in size_t alignment = 8) pure nothrow @safe @nogc
|
||||||
|
{
|
||||||
|
return (size - 1) / alignment * alignment + alignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal function used to create, resize or destroy a dynamic array. It
|
||||||
|
* throws $(D_PSYMBOL OutOfMemoryError) if $(D_PARAM Throws) is set. 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.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Element type of the array being created.
|
||||||
|
* Init = If should be initialized.
|
||||||
|
* Throws = If $(D_PSYMBOL OutOfMemoryError) should be throwsn.
|
||||||
|
* allocator = The allocator used for getting memory.
|
||||||
|
* array = A reference to the array being changed.
|
||||||
|
* length = New array length.
|
||||||
|
*
|
||||||
|
* Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could
|
||||||
|
* not be reallocated. In the latter
|
||||||
|
*/
|
||||||
|
package(tanya) bool resize(T,
|
||||||
|
bool Init = true,
|
||||||
|
bool Throws = true)
|
||||||
|
(shared Allocator allocator,
|
||||||
|
ref T[] array,
|
||||||
|
in size_t length) @trusted
|
||||||
|
{
|
||||||
|
void[] buf = array;
|
||||||
|
static if (Init)
|
||||||
|
{
|
||||||
|
immutable oldLength = array.length;
|
||||||
|
}
|
||||||
|
if (!allocator.reallocate(buf, length * T.sizeof))
|
||||||
|
{
|
||||||
|
static if (Throws)
|
||||||
|
{
|
||||||
|
onOutOfMemoryError;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 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 true;
|
||||||
|
}
|
||||||
|
package(tanya) alias resizeArray = resize;
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
int[] p;
|
||||||
|
|
||||||
|
defaultAllocator.resizeArray(p, 20);
|
||||||
|
assert(p.length == 20);
|
||||||
|
|
||||||
|
defaultAllocator.resizeArray(p, 30);
|
||||||
|
assert(p.length == 30);
|
||||||
|
|
||||||
|
defaultAllocator.resizeArray(p, 10);
|
||||||
|
assert(p.length == 10);
|
||||||
|
|
||||||
|
defaultAllocator.resizeArray(p, 0);
|
||||||
|
assert(p is null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys and deallocates $(D_PARAM p) of type $(D_PARAM T).
|
||||||
|
* It is assumed the respective entities had been allocated with the same
|
||||||
|
* allocator.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Type of $(D_PARAM p).
|
||||||
|
* allocator = Allocator the $(D_PARAM p) was allocated with.
|
||||||
|
* p = Object or array to be destroyed.
|
||||||
|
*/
|
||||||
|
void dispose(T)(shared Allocator allocator, auto ref T* p)
|
||||||
|
{
|
||||||
|
static if (hasElaborateDestructor!T)
|
||||||
|
{
|
||||||
|
destroy(*p);
|
||||||
|
}
|
||||||
|
() @trusted { allocator.deallocate((cast(void*) p)[0 .. T.sizeof]); }();
|
||||||
|
p = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
void dispose(T)(shared Allocator allocator, auto ref T p)
|
||||||
|
if (is(T == class) || is(T == interface))
|
||||||
|
{
|
||||||
|
if (p is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
static if (is(T == interface))
|
||||||
|
{
|
||||||
|
version(Windows)
|
||||||
|
{
|
||||||
|
import core.sys.windows.unknwn : IUnknown;
|
||||||
|
static assert(!is(T: IUnknown), "COM interfaces can't be destroyed in "
|
||||||
|
~ __PRETTY_FUNCTION__);
|
||||||
|
}
|
||||||
|
auto ob = cast(Object) p;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias ob = p;
|
||||||
|
}
|
||||||
|
auto ptr = cast(void *) ob;
|
||||||
|
|
||||||
|
auto support = ptr[0 .. typeid(ob).initializer.length];
|
||||||
|
scope (success)
|
||||||
|
{
|
||||||
|
() @trusted { allocator.deallocate(support); }();
|
||||||
|
p = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ppv = cast(void**) ptr;
|
||||||
|
if (!*ppv)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto pc = cast(ClassInfo*) *ppv;
|
||||||
|
scope (exit)
|
||||||
|
{
|
||||||
|
*ppv = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto c = *pc;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
// Assume the destructor is @nogc. Leave it nothrow since the destructor
|
||||||
|
// shouldn't throw and if it does, it is an error anyway.
|
||||||
|
if (c.destructor)
|
||||||
|
{
|
||||||
|
(cast(void function (Object) nothrow @safe @nogc) c.destructor)(ob);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while ((c = c.base) !is null);
|
||||||
|
|
||||||
|
if (ppv[1]) // if monitor is not null
|
||||||
|
{
|
||||||
|
_d_monitordelete(cast(Object) ptr, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
void dispose(T)(shared Allocator allocator, auto ref T[] array)
|
||||||
|
{
|
||||||
|
static if (hasElaborateDestructor!(typeof(array[0])))
|
||||||
|
{
|
||||||
|
foreach (ref e; array)
|
||||||
|
{
|
||||||
|
destroy(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
() @trusted { allocator.deallocate(array); }();
|
||||||
|
array = null;
|
||||||
|
}
|
||||||
|
458
source/tanya/memory/types.d
Normal file
458
source/tanya/memory/types.d
Normal file
@ -0,0 +1,458 @@
|
|||||||
|
/* 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:belka@caraus.de, Eugene Wissner)
|
||||||
|
*/
|
||||||
|
module tanya.memory.types;
|
||||||
|
|
||||||
|
import core.exception;
|
||||||
|
import std.algorithm.comparison;
|
||||||
|
import std.algorithm.mutation;
|
||||||
|
import std.conv;
|
||||||
|
import std.traits;
|
||||||
|
import tanya.memory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reference-counted object containing a $(D_PARAM T) value as payload.
|
||||||
|
* $(D_PSYMBOL RefCounted) keeps track of all references of an object, and
|
||||||
|
* when the reference count goes down to zero, frees the underlying store.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Type of the reference-counted value.
|
||||||
|
*/
|
||||||
|
struct RefCounted(T)
|
||||||
|
{
|
||||||
|
static if (is(T == class) || is(T == interface))
|
||||||
|
{
|
||||||
|
private alias Payload = T;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
private alias Payload = T*;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Storage
|
||||||
|
{
|
||||||
|
private Payload payload;
|
||||||
|
private size_t counter = 1;
|
||||||
|
|
||||||
|
private final size_t opUnary(string op)() pure nothrow @safe @nogc
|
||||||
|
if (op == "--" || op == "++")
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(counter > 0);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
mixin("return " ~ op ~ "counter;");
|
||||||
|
}
|
||||||
|
|
||||||
|
private final int opCmp(size_t counter) const pure nothrow @safe @nogc
|
||||||
|
{
|
||||||
|
if (this.counter > counter)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if (this.counter < counter)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final int opEquals(size_t counter) const pure nothrow @safe @nogc
|
||||||
|
{
|
||||||
|
return this.counter == counter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final class RefCountedStorage : Storage
|
||||||
|
{
|
||||||
|
private shared Allocator allocator;
|
||||||
|
|
||||||
|
this(shared Allocator allocator) pure nothrow @safe @nogc
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(allocator !is null);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
this.allocator = allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
~this() nothrow @nogc
|
||||||
|
{
|
||||||
|
allocator.dispose(payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Storage storage;
|
||||||
|
|
||||||
|
invariant
|
||||||
|
{
|
||||||
|
assert(storage is null || allocator_ !is null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes ownership over $(D_PARAM value), setting the counter to 1.
|
||||||
|
* $(D_PARAM value) may be a pointer, an object or a dynamic array.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* value = Value whose ownership is taken over.
|
||||||
|
* allocator = Allocator used to destroy the $(D_PARAM value) and to
|
||||||
|
* allocate/deallocate internal storage.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE allocator !is null)
|
||||||
|
*/
|
||||||
|
this(Payload value, shared Allocator allocator = defaultAllocator)
|
||||||
|
{
|
||||||
|
this(allocator);
|
||||||
|
storage = allocator.make!RefCountedStorage(allocator);
|
||||||
|
move(value, storage.payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
this(shared Allocator allocator)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(allocator !is null);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
this.allocator_ = allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increases the reference counter by one.
|
||||||
|
*/
|
||||||
|
this(this)
|
||||||
|
{
|
||||||
|
if (count != 0)
|
||||||
|
{
|
||||||
|
++storage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decreases the reference counter by one.
|
||||||
|
*
|
||||||
|
* If the counter reaches 0, destroys the owned object.
|
||||||
|
*/
|
||||||
|
~this()
|
||||||
|
{
|
||||||
|
if (storage !is null && !(storage.counter && --storage))
|
||||||
|
{
|
||||||
|
allocator_.dispose(storage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes ownership over $(D_PARAM rhs). Initializes this
|
||||||
|
* $(D_PSYMBOL RefCounted) if needed.
|
||||||
|
*
|
||||||
|
* If it is the last reference of the previously owned object,
|
||||||
|
* it will be destroyed.
|
||||||
|
*
|
||||||
|
* To reset the $(D_PSYMBOL RefCounted) assign $(D_KEYWORD null).
|
||||||
|
*
|
||||||
|
* If the allocator wasn't set before, $(D_PSYMBOL defaultAllocator) will
|
||||||
|
* be used. If you need a different allocator, create a new
|
||||||
|
* $(D_PSYMBOL RefCounted) and assign it.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* rhs = $(D_KEYWORD this).
|
||||||
|
*/
|
||||||
|
ref typeof(this) opAssign(Payload rhs)
|
||||||
|
{
|
||||||
|
if (storage is null)
|
||||||
|
{
|
||||||
|
storage = allocator.make!RefCountedStorage(allocator);
|
||||||
|
}
|
||||||
|
else if (storage > 1)
|
||||||
|
{
|
||||||
|
--storage;
|
||||||
|
storage = allocator.make!RefCountedStorage(allocator);
|
||||||
|
}
|
||||||
|
else if (cast(RefCountedStorage) storage is null)
|
||||||
|
{
|
||||||
|
// Created with refCounted. Always destroyed togethter with the pointer.
|
||||||
|
assert(storage.counter != 0);
|
||||||
|
allocator.dispose(storage);
|
||||||
|
storage = allocator.make!RefCountedStorage(allocator);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
allocator.dispose(storage.payload);
|
||||||
|
}
|
||||||
|
move(rhs, storage.payload);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
ref typeof(this) opAssign(typeof(null))
|
||||||
|
{
|
||||||
|
if (storage is null)
|
||||||
|
{
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
else if (storage > 1)
|
||||||
|
{
|
||||||
|
--storage;
|
||||||
|
storage = null;
|
||||||
|
}
|
||||||
|
else if (cast(RefCountedStorage) storage is null)
|
||||||
|
{
|
||||||
|
// Created with refCounted. Always destroyed togethter with the pointer.
|
||||||
|
assert(storage.counter != 0);
|
||||||
|
allocator.dispose(storage);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
allocator.dispose(storage.payload);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
ref typeof(this) opAssign(typeof(this) rhs)
|
||||||
|
{
|
||||||
|
swap(allocator_, rhs.allocator_);
|
||||||
|
swap(storage, rhs.storage);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns: Reference to the owned object.
|
||||||
|
*/
|
||||||
|
inout(Payload) get() inout pure nothrow @safe @nogc
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(count > 0, "Attempted to access an uninitialized reference.");
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
return storage.payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (isPointer!Payload)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* op = Operation.
|
||||||
|
*
|
||||||
|
* Dereferences the pointer. It is defined only for pointers, not for
|
||||||
|
* reference types like classes, that can be accessed directly.
|
||||||
|
*
|
||||||
|
* Returns: Reference to the pointed value.
|
||||||
|
*/
|
||||||
|
ref T opUnary(string op)()
|
||||||
|
if (op == "*")
|
||||||
|
{
|
||||||
|
return *storage.payload;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns: Whether this $(D_PSYMBOL RefCounted) already has an internal
|
||||||
|
* storage.
|
||||||
|
*/
|
||||||
|
@property bool isInitialized() const
|
||||||
|
{
|
||||||
|
return storage !is null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns: The number of $(D_PSYMBOL RefCounted) instances that share
|
||||||
|
* ownership over the same pointer (including $(D_KEYWORD this)).
|
||||||
|
* If this $(D_PSYMBOL RefCounted) isn't initialized, returns `0`.
|
||||||
|
*/
|
||||||
|
@property size_t count() const
|
||||||
|
{
|
||||||
|
return storage is null ? 0 : storage.counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
mixin DefaultAllocator;
|
||||||
|
alias get this;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
auto rc = RefCounted!int(defaultAllocator.make!int(5), defaultAllocator);
|
||||||
|
auto val = rc.get;
|
||||||
|
|
||||||
|
*val = 8;
|
||||||
|
assert(*rc.storage.payload == 8);
|
||||||
|
|
||||||
|
val = null;
|
||||||
|
assert(rc.storage.payload !is null);
|
||||||
|
assert(*rc.storage.payload == 8);
|
||||||
|
|
||||||
|
*rc = 9;
|
||||||
|
assert(*rc.storage.payload == 9);
|
||||||
|
}
|
||||||
|
|
||||||
|
version (unittest)
|
||||||
|
{
|
||||||
|
private class A
|
||||||
|
{
|
||||||
|
uint *destroyed;
|
||||||
|
|
||||||
|
this(ref uint destroyed) @nogc
|
||||||
|
{
|
||||||
|
this.destroyed = &destroyed;
|
||||||
|
}
|
||||||
|
|
||||||
|
~this() @nogc
|
||||||
|
{
|
||||||
|
++(*destroyed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct B
|
||||||
|
{
|
||||||
|
int prop;
|
||||||
|
@disable this();
|
||||||
|
this(int param1) @nogc
|
||||||
|
{
|
||||||
|
prop = param1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private unittest
|
||||||
|
{
|
||||||
|
uint destroyed;
|
||||||
|
auto a = defaultAllocator.make!A(destroyed);
|
||||||
|
|
||||||
|
assert(destroyed == 0);
|
||||||
|
{
|
||||||
|
auto rc = RefCounted!A(a, defaultAllocator);
|
||||||
|
assert(rc.count == 1);
|
||||||
|
|
||||||
|
void func(RefCounted!A rc)
|
||||||
|
{
|
||||||
|
assert(rc.count == 2);
|
||||||
|
}
|
||||||
|
func(rc);
|
||||||
|
|
||||||
|
assert(rc.count == 1);
|
||||||
|
}
|
||||||
|
assert(destroyed == 1);
|
||||||
|
|
||||||
|
RefCounted!int rc;
|
||||||
|
assert(rc.count == 0);
|
||||||
|
rc = defaultAllocator.make!int(8);
|
||||||
|
assert(rc.count == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private unittest
|
||||||
|
{
|
||||||
|
static assert(is(typeof(RefCounted!int.storage.payload) == int*));
|
||||||
|
static assert(is(typeof(RefCounted!A.storage.payload) == A));
|
||||||
|
|
||||||
|
static assert(is(RefCounted!B));
|
||||||
|
static assert(is(RefCounted!A));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new object of type $(D_PARAM T) and wraps it in a
|
||||||
|
* $(D_PSYMBOL RefCounted) using $(D_PARAM args) as the parameter list for
|
||||||
|
* the constructor of $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* This function is more efficient than the using of $(D_PSYMBOL RefCounted)
|
||||||
|
* directly, since it allocates only ones (the internal storage and the
|
||||||
|
* object).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Type of the constructed object.
|
||||||
|
* A = Types of the arguments to the constructor of $(D_PARAM T).
|
||||||
|
* allocator = Allocator.
|
||||||
|
* args = Constructor arguments of $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Returns: Newly created $(D_PSYMBOL RefCounted!T).
|
||||||
|
*/
|
||||||
|
RefCounted!T refCounted(T, A...)(shared Allocator allocator, auto ref A args)
|
||||||
|
if (!is(T == interface) && !isAbstractClass!T
|
||||||
|
&& !isArray!T && !isAssociativeArray!T)
|
||||||
|
{
|
||||||
|
auto rc = typeof(return)(allocator);
|
||||||
|
|
||||||
|
immutable storageSize = alignedSize(stateSize!(RefCounted!T.Storage));
|
||||||
|
immutable size = alignedSize(stateSize!T + storageSize);
|
||||||
|
|
||||||
|
auto mem = (() @trusted => allocator.allocate(size))();
|
||||||
|
if (mem is null)
|
||||||
|
{
|
||||||
|
onOutOfMemoryError();
|
||||||
|
}
|
||||||
|
scope (failure)
|
||||||
|
{
|
||||||
|
() @trusted { allocator.deallocate(mem); }();
|
||||||
|
}
|
||||||
|
rc.storage = emplace!(RefCounted!T.Storage)(mem[0 .. storageSize]);
|
||||||
|
|
||||||
|
static if (is(T == class))
|
||||||
|
{
|
||||||
|
rc.storage.payload = emplace!T(mem[storageSize .. $], args);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto ptr = (() @trusted => (cast(T*) mem[storageSize .. $].ptr))();
|
||||||
|
rc.storage.payload = emplace!T(ptr, args);
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
auto rc = defaultAllocator.refCounted!int(5);
|
||||||
|
assert(rc.count == 1);
|
||||||
|
|
||||||
|
void func(RefCounted!int param)
|
||||||
|
{
|
||||||
|
if (param.count == 2)
|
||||||
|
{
|
||||||
|
func(param);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
assert(param.count == 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func(rc);
|
||||||
|
|
||||||
|
assert(rc.count == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private @nogc unittest
|
||||||
|
{
|
||||||
|
struct E
|
||||||
|
{
|
||||||
|
}
|
||||||
|
auto b = defaultAllocator.refCounted!B(15);
|
||||||
|
static assert(is(typeof(b.storage.payload) == B*));
|
||||||
|
static assert(is(typeof(b.prop) == int));
|
||||||
|
static assert(!is(typeof(defaultAllocator.refCounted!B())));
|
||||||
|
|
||||||
|
static assert(is(typeof(defaultAllocator.refCounted!E())));
|
||||||
|
static assert(!is(typeof(defaultAllocator.refCounted!E(5))));
|
||||||
|
{
|
||||||
|
auto rc = defaultAllocator.refCounted!B(3);
|
||||||
|
assert(rc.get.prop == 3);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto rc = defaultAllocator.refCounted!E();
|
||||||
|
assert(rc.count);
|
||||||
|
}
|
||||||
|
}
|
49
source/tanya/meta/gen.d
Normal file
49
source/tanya/meta/gen.d
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Templates that generate values.
|
||||||
|
*
|
||||||
|
* 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.meta.gen;
|
||||||
|
|
||||||
|
import std.traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializer list.
|
||||||
|
*
|
||||||
|
* Generates a static array with elements from $(D_PARAM args). All elements
|
||||||
|
* should have the same type. It can be used in constructors which accept a
|
||||||
|
* list of the elements of the same type in the situations where variadic
|
||||||
|
* functions and templates can't be used.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* Args = Argument type.
|
||||||
|
* args = Arguments.
|
||||||
|
*/
|
||||||
|
enum IL(Args...)(Args args)
|
||||||
|
if (Args.length > 0)
|
||||||
|
{
|
||||||
|
alias BaseType = typeof(args[0]);
|
||||||
|
|
||||||
|
BaseType[args.length] result;
|
||||||
|
|
||||||
|
foreach (i, a; args)
|
||||||
|
{
|
||||||
|
static assert(isImplicitlyConvertible!(typeof(a), BaseType));
|
||||||
|
result[i] = a;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
static assert(IL(1, 5, 8).length == 3);
|
||||||
|
static assert(IL(1, 5, 8).sizeof == 3 * int.sizeof);
|
||||||
|
}
|
15
source/tanya/meta/package.d
Normal file
15
source/tanya/meta/package.d
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metaprogramming.
|
||||||
|
*
|
||||||
|
* 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.meta;
|
||||||
|
|
||||||
|
public import tanya.meta.gen;
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user