Initial commit.
The library is able to support simple TCP servers in the current state. The API is still mostly compatible with mainline vibe.d, but the driver systen has been replaced by the eventcore library and sockets/files/timers/... are now structs with automatic reference counting instead of GC collected classes. The stream interfaces have been removed for now.
This commit is contained in:
commit
7e2d1dd038
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.dub
|
7
dub.sdl
Normal file
7
dub.sdl
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
name "vibe-core"
|
||||||
|
description "The I/O core library of vibe.d."
|
||||||
|
authors "Sönke Ludwig"
|
||||||
|
copyright "Copyright © 2016, rejectedsoftware e.K."
|
||||||
|
license "MIT"
|
||||||
|
|
||||||
|
dependency "eventcore" version="*"
|
6
dub.selections.json
Normal file
6
dub.selections.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"fileVersion": 1,
|
||||||
|
"versions": {
|
||||||
|
"eventcore": "~master"
|
||||||
|
}
|
||||||
|
}
|
6
examples/bench-dummy-http-server/dub.json
Normal file
6
examples/bench-dummy-http-server/dub.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"name": "bench-http-server",
|
||||||
|
"dependencies": {
|
||||||
|
"vibe-core": {"path": "../../"},
|
||||||
|
}
|
||||||
|
}
|
300
examples/bench-dummy-http-server/source/app.d
Normal file
300
examples/bench-dummy-http-server/source/app.d
Normal file
|
@ -0,0 +1,300 @@
|
||||||
|
import vibe.core.core;
|
||||||
|
import vibe.core.log;
|
||||||
|
import vibe.core.net;
|
||||||
|
//import vibe.stream.operations;
|
||||||
|
|
||||||
|
import std.functional : toDelegate;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
void staticAnswer(TCPConnection conn)
|
||||||
|
nothrow @safe {
|
||||||
|
try {
|
||||||
|
while (!conn.empty) {
|
||||||
|
while (true) {
|
||||||
|
CountingRange r;
|
||||||
|
conn.readLine(r);
|
||||||
|
if (!r.count) break;
|
||||||
|
}
|
||||||
|
conn.write(cast(const(ubyte)[])"HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\n\r\nHello, World!");
|
||||||
|
conn.flush();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
scope (failure) assert(false);
|
||||||
|
logError("Error processing request: %s", e.msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto listener = listenTCP(8080, &staticAnswer, "127.0.0.1");
|
||||||
|
|
||||||
|
runEventLoop();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct CountingRange {
|
||||||
|
@safe nothrow @nogc:
|
||||||
|
ulong count = 0;
|
||||||
|
void put(ubyte) { count++; }
|
||||||
|
void put(in ubyte[] arr) { count += arr.length; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
import std.range.primitives : isOutputRange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reads and returns a single line from the stream.
|
||||||
|
|
||||||
|
Throws:
|
||||||
|
An exception if either the stream end was hit without hitting a newline first, or
|
||||||
|
if more than max_bytes have been read from the stream.
|
||||||
|
*/
|
||||||
|
ubyte[] readLine(InputStream)(InputStream stream, size_t max_bytes = size_t.max, string linesep = "\r\n", Allocator alloc = defaultAllocator()) /*@ufcs*/
|
||||||
|
{
|
||||||
|
auto output = AllocAppender!(ubyte[])(alloc);
|
||||||
|
output.reserve(max_bytes < 64 ? max_bytes : 64);
|
||||||
|
readLine(stream, output, max_bytes, linesep);
|
||||||
|
return output.data();
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void readLine(InputStream, OutputStream)(InputStream stream, OutputStream dst, size_t max_bytes = size_t.max, string linesep = "\r\n")
|
||||||
|
{
|
||||||
|
import vibe.stream.wrapper;
|
||||||
|
auto dstrng = StreamOutputRange(dst);
|
||||||
|
readLine(stream, dstrng, max_bytes, linesep);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void readLine(R, InputStream)(InputStream stream, ref R dst, size_t max_bytes = size_t.max, string linesep = "\r\n")
|
||||||
|
if (isOutputRange!(R, ubyte))
|
||||||
|
{
|
||||||
|
readUntil(stream, dst, cast(const(ubyte)[])linesep, max_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reads all data of a stream until the specified end marker is detected.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
stream = The input stream which is searched for end_marker
|
||||||
|
end_marker = The byte sequence which is searched in the stream
|
||||||
|
max_bytes = An optional limit of how much data is to be read from the
|
||||||
|
input stream; if the limit is reaached before hitting the end
|
||||||
|
marker, an exception is thrown.
|
||||||
|
alloc = An optional allocator that is used to build the result string
|
||||||
|
in the string variant of this function
|
||||||
|
dst = The output stream, to which the prefix to the end marker of the
|
||||||
|
input stream is written
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The string variant of this function returns the complete prefix to the
|
||||||
|
end marker of the input stream, excluding the end marker itself.
|
||||||
|
|
||||||
|
Throws:
|
||||||
|
An exception if either the stream end was hit without hitting a marker
|
||||||
|
first, or if more than max_bytes have been read from the stream in
|
||||||
|
case of max_bytes != 0.
|
||||||
|
|
||||||
|
Remarks:
|
||||||
|
This function uses an algorithm inspired by the
|
||||||
|
$(LINK2 http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm,
|
||||||
|
Boyer-Moore string search algorithm). However, contrary to the original
|
||||||
|
algorithm, it will scan the whole input string exactly once, without
|
||||||
|
jumping over portions of it. This allows the algorithm to work with
|
||||||
|
constant memory requirements and without the memory copies that would
|
||||||
|
be necessary for streams that do not hold their complete data in
|
||||||
|
memory.
|
||||||
|
|
||||||
|
The current implementation has a run time complexity of O(n*m+m²) and
|
||||||
|
O(n+m) in typical cases, with n being the length of the scanned input
|
||||||
|
string and m the length of the marker.
|
||||||
|
*/
|
||||||
|
ubyte[] readUntil(InputStream)(InputStream stream, in ubyte[] end_marker, size_t max_bytes = size_t.max, Allocator alloc = defaultAllocator()) /*@ufcs*/
|
||||||
|
{
|
||||||
|
auto output = AllocAppender!(ubyte[])(alloc);
|
||||||
|
output.reserve(max_bytes < 64 ? max_bytes : 64);
|
||||||
|
readUntil(stream, output, end_marker, max_bytes);
|
||||||
|
return output.data();
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void readUntil(InputStream, OutputStream)(InputStream stream, OutputStream dst, in ubyte[] end_marker, ulong max_bytes = ulong.max) /*@ufcs*/
|
||||||
|
{
|
||||||
|
import vibe.stream.wrapper;
|
||||||
|
auto dstrng = StreamOutputRange(dst);
|
||||||
|
readUntil(stream, dstrng, end_marker, max_bytes);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void readUntil(R, InputStream)(InputStream stream, ref R dst, in ubyte[] end_marker, ulong max_bytes = ulong.max) /*@ufcs*/
|
||||||
|
if (isOutputRange!(R, ubyte))
|
||||||
|
{
|
||||||
|
assert(max_bytes > 0 && end_marker.length > 0);
|
||||||
|
|
||||||
|
if (end_marker.length <= 2)
|
||||||
|
readUntilSmall(stream, dst, end_marker, max_bytes);
|
||||||
|
else
|
||||||
|
readUntilGeneric(stream, dst, end_marker, max_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readUntilSmall(R, InputStream)(InputStream stream, ref R dst, in ubyte[] end_marker, ulong max_bytes = ulong.max)
|
||||||
|
@safe {
|
||||||
|
import std.algorithm.comparison : min, max;
|
||||||
|
import std.algorithm.searching : countUntil;
|
||||||
|
|
||||||
|
assert(end_marker.length >= 1 && end_marker.length <= 2);
|
||||||
|
|
||||||
|
size_t nmatched = 0;
|
||||||
|
size_t nmarker = end_marker.length;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
enforce(!stream.empty, "Reached EOF while searching for end marker.");
|
||||||
|
enforce(max_bytes > 0, "Reached maximum number of bytes while searching for end marker.");
|
||||||
|
auto max_peek = max(max_bytes, max_bytes+nmarker); // account for integer overflow
|
||||||
|
auto pm = stream.peek()[0 .. min($, max_bytes)];
|
||||||
|
if (!pm.length) { // no peek support - inefficient route
|
||||||
|
ubyte[2] buf = void;
|
||||||
|
auto l = nmarker - nmatched;
|
||||||
|
stream.read(buf[0 .. l]);
|
||||||
|
foreach (i; 0 .. l) {
|
||||||
|
if (buf[i] == end_marker[nmatched]) {
|
||||||
|
nmatched++;
|
||||||
|
} else if (buf[i] == end_marker[0]) {
|
||||||
|
foreach (j; 0 .. nmatched) dst.put(end_marker[j]);
|
||||||
|
nmatched = 1;
|
||||||
|
} else {
|
||||||
|
foreach (j; 0 .. nmatched) dst.put(end_marker[j]);
|
||||||
|
nmatched = 0;
|
||||||
|
dst.put(buf[i]);
|
||||||
|
}
|
||||||
|
if (nmatched == nmarker) return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto idx = pm.countUntil(end_marker[0]);
|
||||||
|
if (idx < 0) {
|
||||||
|
dst.put(pm);
|
||||||
|
max_bytes -= pm.length;
|
||||||
|
stream.skip(pm.length);
|
||||||
|
} else {
|
||||||
|
dst.put(pm[0 .. idx]);
|
||||||
|
stream.skip(idx+1);
|
||||||
|
if (nmarker == 2) {
|
||||||
|
ubyte[1] next;
|
||||||
|
stream.read(next);
|
||||||
|
if (next[0] == end_marker[1])
|
||||||
|
return;
|
||||||
|
dst.put(end_marker[0]);
|
||||||
|
dst.put(next[0]);
|
||||||
|
} else return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final class Buffer { ubyte[64*1024-4*size_t.sizeof] bytes = void; } // 64k - some headroom for
|
||||||
|
|
||||||
|
private void readUntilGeneric(R, InputStream)(InputStream stream, ref R dst, in ubyte[] end_marker, ulong max_bytes = ulong.max) /*@ufcs*/
|
||||||
|
if (isOutputRange!(R, ubyte))
|
||||||
|
{
|
||||||
|
import std.algorithm.comparison : min;
|
||||||
|
// allocate internal jump table to optimize the number of comparisons
|
||||||
|
size_t[8] nmatchoffsetbuffer = void;
|
||||||
|
size_t[] nmatchoffset;
|
||||||
|
if (end_marker.length <= nmatchoffsetbuffer.length) nmatchoffset = nmatchoffsetbuffer[0 .. end_marker.length];
|
||||||
|
else nmatchoffset = new size_t[end_marker.length];
|
||||||
|
|
||||||
|
// precompute the jump table
|
||||||
|
nmatchoffset[0] = 0;
|
||||||
|
foreach( i; 1 .. end_marker.length ){
|
||||||
|
nmatchoffset[i] = i;
|
||||||
|
foreach_reverse( j; 1 .. i )
|
||||||
|
if( end_marker[j .. i] == end_marker[0 .. i-j] ){
|
||||||
|
nmatchoffset[i] = i-j;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
assert(nmatchoffset[i] > 0 && nmatchoffset[i] <= i);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nmatched = 0;
|
||||||
|
scope bufferobj = new Buffer; // FIXME: use heap allocation
|
||||||
|
auto buf = bufferobj.bytes[];
|
||||||
|
|
||||||
|
ulong bytes_read = 0;
|
||||||
|
|
||||||
|
void skip2(size_t nbytes)
|
||||||
|
{
|
||||||
|
bytes_read += nbytes;
|
||||||
|
stream.skip(nbytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
while( !stream.empty ){
|
||||||
|
enforce(bytes_read < max_bytes, "Reached byte limit before reaching end marker.");
|
||||||
|
|
||||||
|
// try to get as much data as possible, either by peeking into the stream or
|
||||||
|
// by reading as much as isguaranteed to not exceed the end marker length
|
||||||
|
// the block size is also always limited by the max_bytes parameter.
|
||||||
|
size_t nread = 0;
|
||||||
|
auto least_size = stream.leastSize(); // NOTE: blocks until data is available
|
||||||
|
auto max_read = max_bytes - bytes_read;
|
||||||
|
auto str = stream.peek(); // try to get some data for free
|
||||||
|
if( str.length == 0 ){ // if not, read as much as possible without reading past the end
|
||||||
|
nread = min(least_size, end_marker.length-nmatched, buf.length, max_read);
|
||||||
|
stream.read(buf[0 .. nread]);
|
||||||
|
str = buf[0 .. nread];
|
||||||
|
bytes_read += nread;
|
||||||
|
} else if( str.length > max_read ){
|
||||||
|
str.length = cast(size_t)max_read;
|
||||||
|
}
|
||||||
|
|
||||||
|
// remember how much of the marker was already matched before processing the current block
|
||||||
|
size_t nmatched_start = nmatched;
|
||||||
|
|
||||||
|
// go through the current block trying to match the marker
|
||||||
|
size_t i = 0;
|
||||||
|
for (i = 0; i < str.length; i++) {
|
||||||
|
auto ch = str[i];
|
||||||
|
// if we have a mismatch, use the jump table to try other possible prefixes
|
||||||
|
// of the marker
|
||||||
|
while( nmatched > 0 && ch != end_marker[nmatched] )
|
||||||
|
nmatched -= nmatchoffset[nmatched];
|
||||||
|
|
||||||
|
// if we then have a match, increase the match count and test for full match
|
||||||
|
if (ch == end_marker[nmatched])
|
||||||
|
if (++nmatched == end_marker.length) {
|
||||||
|
i++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// write out any false match part of previous blocks
|
||||||
|
if( nmatched_start > 0 ){
|
||||||
|
if( nmatched <= i ) dst.put(end_marker[0 .. nmatched_start]);
|
||||||
|
else dst.put(end_marker[0 .. nmatched_start-nmatched+i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// write out any unmatched part of the current block
|
||||||
|
if( nmatched < i ) dst.put(str[0 .. i-nmatched]);
|
||||||
|
|
||||||
|
// got a full, match => out
|
||||||
|
if (nmatched >= end_marker.length) {
|
||||||
|
// in case of a full match skip data in the stream until the end of
|
||||||
|
// the marker
|
||||||
|
skip2(i - nread);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise skip this block in the stream
|
||||||
|
skip2(str.length - nread);
|
||||||
|
}
|
||||||
|
|
||||||
|
enforce(false, "Reached EOF before reaching end marker.");
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (!is(typeof(TCPConnection.init.skip(0))))
|
||||||
|
{
|
||||||
|
private void skip(ref TCPConnection str, ulong count)
|
||||||
|
{
|
||||||
|
ubyte[156] buf = void;
|
||||||
|
while (count > 0) {
|
||||||
|
auto n = min(buf.length, count);
|
||||||
|
str.read(buf[0 .. n]);
|
||||||
|
count -= n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
225
source/vibe/core/args.d
Normal file
225
source/vibe/core/args.d
Normal file
|
@ -0,0 +1,225 @@
|
||||||
|
/**
|
||||||
|
Parses and allows querying the command line arguments and configuration
|
||||||
|
file.
|
||||||
|
|
||||||
|
The optional configuration file (vibe.conf) is a JSON file, containing an
|
||||||
|
object with the keys corresponding to option names, and values corresponding
|
||||||
|
to their values. It is searched for in the local directory, user's home
|
||||||
|
directory, or /etc/vibe/ (POSIX only), whichever is found first.
|
||||||
|
|
||||||
|
Copyright: © 2012-2016 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig, Vladimir Panteleev
|
||||||
|
*/
|
||||||
|
module vibe.core.args;
|
||||||
|
|
||||||
|
import vibe.core.log;
|
||||||
|
//import vibe.data.json;
|
||||||
|
|
||||||
|
import std.algorithm : any, map, sort;
|
||||||
|
import std.array : array, join, replicate, split;
|
||||||
|
import std.exception;
|
||||||
|
import std.file;
|
||||||
|
import std.getopt;
|
||||||
|
import std.path : buildPath;
|
||||||
|
import std.string : format, stripRight, wrap;
|
||||||
|
|
||||||
|
import core.runtime;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Finds and reads an option from the configuration file or command line.
|
||||||
|
|
||||||
|
Command line options take precedence over configuration file entries.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
names = Option names. Separate multiple name variants with "|",
|
||||||
|
as for $(D std.getopt).
|
||||||
|
pvalue = Pointer to store the value. Unchanged if value was not found.
|
||||||
|
help_text = Text to be displayed when the application is run with
|
||||||
|
--help.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
$(D true) if the value was found, $(D false) otherwise.
|
||||||
|
|
||||||
|
See_Also: readRequiredOption
|
||||||
|
*/
|
||||||
|
bool readOption(T)(string names, T* pvalue, string help_text)
|
||||||
|
{
|
||||||
|
// May happen due to http://d.puremagic.com/issues/show_bug.cgi?id=9881
|
||||||
|
if (g_args is null) init();
|
||||||
|
|
||||||
|
OptionInfo info;
|
||||||
|
info.names = names.split("|").sort!((a, b) => a.length < b.length)().array();
|
||||||
|
info.hasValue = !is(T == bool);
|
||||||
|
info.helpText = help_text;
|
||||||
|
assert(!g_options.any!(o => o.names == info.names)(), "readOption() may only be called once per option name.");
|
||||||
|
g_options ~= info;
|
||||||
|
|
||||||
|
immutable olen = g_args.length;
|
||||||
|
getopt(g_args, getoptConfig, names, pvalue);
|
||||||
|
if (g_args.length < olen) return true;
|
||||||
|
|
||||||
|
/*if (g_haveConfig) {
|
||||||
|
foreach (name; info.names)
|
||||||
|
if (auto pv = name in g_config) {
|
||||||
|
*pvalue = pv.to!T;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
The same as readOption, but throws an exception if the given option is missing.
|
||||||
|
|
||||||
|
See_Also: readOption
|
||||||
|
*/
|
||||||
|
T readRequiredOption(T)(string names, string help_text)
|
||||||
|
{
|
||||||
|
string formattedNames() {
|
||||||
|
return names.split("|").map!(s => s.length == 1 ? "-" ~ s : "--" ~ s).join("/");
|
||||||
|
}
|
||||||
|
T ret;
|
||||||
|
enforce(readOption(names, &ret, help_text) || g_help,
|
||||||
|
format("Missing mandatory option %s.", formattedNames()));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Prints a help screen consisting of all options encountered in getOption calls.
|
||||||
|
*/
|
||||||
|
void printCommandLineHelp()
|
||||||
|
{
|
||||||
|
enum dcolumn = 20;
|
||||||
|
enum ncolumns = 80;
|
||||||
|
|
||||||
|
logInfo("Usage: %s <options>\n", g_args[0]);
|
||||||
|
foreach (opt; g_options) {
|
||||||
|
string shortopt;
|
||||||
|
string[] longopts;
|
||||||
|
if (opt.names[0].length == 1 && !opt.hasValue) {
|
||||||
|
shortopt = "-"~opt.names[0];
|
||||||
|
longopts = opt.names[1 .. $];
|
||||||
|
} else {
|
||||||
|
shortopt = " ";
|
||||||
|
longopts = opt.names;
|
||||||
|
}
|
||||||
|
|
||||||
|
string optionString(string name)
|
||||||
|
{
|
||||||
|
if (name.length == 1) return "-"~name~(opt.hasValue ? " <value>" : "");
|
||||||
|
else return "--"~name~(opt.hasValue ? "=<value>" : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
string[] lopts; foreach(lo; longopts) lopts ~= optionString(lo);
|
||||||
|
auto optstr = format(" %s %s", shortopt, lopts.join(", "));
|
||||||
|
if (optstr.length < dcolumn) optstr ~= replicate(" ", dcolumn - optstr.length);
|
||||||
|
|
||||||
|
auto indent = replicate(" ", dcolumn+1);
|
||||||
|
auto desc = wrap(opt.helpText, ncolumns - dcolumn - 2, optstr.length > dcolumn ? indent : "", indent).stripRight();
|
||||||
|
|
||||||
|
if (optstr.length > dcolumn)
|
||||||
|
logInfo("%s\n%s", optstr, desc);
|
||||||
|
else logInfo("%s %s", optstr, desc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks for unrecognized command line options and display a help screen.
|
||||||
|
|
||||||
|
This function is called automatically from vibe.appmain to check for
|
||||||
|
correct command line usage. It will print a help screen in case of
|
||||||
|
unrecognized options.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
args_out = Optional parameter for storing any arguments not handled
|
||||||
|
by any readOption call. If this is left to null, an error
|
||||||
|
will be triggered whenever unhandled arguments exist.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
If "--help" was passed, the function returns false. In all other
|
||||||
|
cases either true is returned or an exception is thrown.
|
||||||
|
*/
|
||||||
|
bool finalizeCommandLineOptions(string[]* args_out = null)
|
||||||
|
{
|
||||||
|
scope(exit) g_args = null;
|
||||||
|
|
||||||
|
if (args_out) {
|
||||||
|
*args_out = g_args;
|
||||||
|
} else if (g_args.length > 1) {
|
||||||
|
logError("Unrecognized command line option: %s\n", g_args[1]);
|
||||||
|
printCommandLineHelp();
|
||||||
|
throw new Exception("Unrecognized command line option.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_help) {
|
||||||
|
printCommandLineHelp();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private struct OptionInfo {
|
||||||
|
string[] names;
|
||||||
|
bool hasValue;
|
||||||
|
string helpText;
|
||||||
|
}
|
||||||
|
|
||||||
|
private {
|
||||||
|
__gshared string[] g_args;
|
||||||
|
__gshared bool g_haveConfig;
|
||||||
|
//__gshared Json g_config;
|
||||||
|
__gshared OptionInfo[] g_options;
|
||||||
|
__gshared bool g_help;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string[] getConfigPaths()
|
||||||
|
{
|
||||||
|
string[] result = [""];
|
||||||
|
import std.process : environment;
|
||||||
|
version (Windows)
|
||||||
|
result ~= environment.get("USERPROFILE");
|
||||||
|
else
|
||||||
|
result ~= [environment.get("HOME"), "/etc/vibe/"];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is invoked by the first readOption call (at least vibe.core will perform one)
|
||||||
|
private void init()
|
||||||
|
{
|
||||||
|
version (VibeDisableCommandLineParsing) {}
|
||||||
|
else g_args = Runtime.args;
|
||||||
|
|
||||||
|
if (!g_args.length) g_args = ["dummy"];
|
||||||
|
|
||||||
|
// TODO: let different config files override individual fields
|
||||||
|
auto searchpaths = getConfigPaths();
|
||||||
|
foreach (spath; searchpaths) {
|
||||||
|
auto cpath = buildPath(spath, configName);
|
||||||
|
if (cpath.exists) {
|
||||||
|
scope(failure) logError("Failed to parse config file %s.", cpath);
|
||||||
|
auto text = cpath.readText();
|
||||||
|
//g_config = text.parseJson();
|
||||||
|
g_haveConfig = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!g_haveConfig)
|
||||||
|
logDiagnostic("No config file found in %s", searchpaths);
|
||||||
|
|
||||||
|
readOption("h|help", &g_help, "Prints this help screen.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum configName = "vibe.conf";
|
||||||
|
|
||||||
|
private template ValueTuple(T...) { alias ValueTuple = T; }
|
||||||
|
|
||||||
|
private alias getoptConfig = ValueTuple!(std.getopt.config.passThrough, std.getopt.config.bundling);
|
1190
source/vibe/core/concurrency.d
Normal file
1190
source/vibe/core/concurrency.d
Normal file
File diff suppressed because it is too large
Load diff
149
source/vibe/core/connectionpool.d
Normal file
149
source/vibe/core/connectionpool.d
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
/**
|
||||||
|
Generic connection pool for reusing persistent connections across fibers.
|
||||||
|
|
||||||
|
Copyright: © 2012-2016 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
*/
|
||||||
|
module vibe.core.connectionpool;
|
||||||
|
|
||||||
|
import vibe.core.log;
|
||||||
|
|
||||||
|
import core.thread;
|
||||||
|
import vibe.core.sync;
|
||||||
|
//import vibe.utils.memory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generic connection pool class.
|
||||||
|
|
||||||
|
The connection pool is creating connections using the supplied factory
|
||||||
|
function as needed whenever `lockConnection` is called. Connections are
|
||||||
|
associated to the calling fiber, as long as any copy of the returned
|
||||||
|
`LockedConnection` object still exists. Connections that are not associated
|
||||||
|
to any fiber will be kept in a pool of open connections for later reuse.
|
||||||
|
|
||||||
|
Note that, after retrieving a connection with `lockConnection`, the caller
|
||||||
|
has to make sure that the connection is actually physically open and to
|
||||||
|
reopen it if necessary. The `ConnectionPool` class has no knowledge of the
|
||||||
|
internals of the connection objects.
|
||||||
|
*/
|
||||||
|
class ConnectionPool(Connection)
|
||||||
|
{
|
||||||
|
private {
|
||||||
|
Connection delegate() m_connectionFactory;
|
||||||
|
Connection[] m_connections;
|
||||||
|
int[const(Connection)] m_lockCount;
|
||||||
|
FreeListRef!LocalTaskSemaphore m_sem;
|
||||||
|
debug Thread m_thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(Connection delegate() connection_factory, uint max_concurrent = uint.max)
|
||||||
|
{
|
||||||
|
m_connectionFactory = connection_factory;
|
||||||
|
m_sem = FreeListRef!LocalTaskSemaphore(max_concurrent);
|
||||||
|
debug m_thread = Thread.getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Determines the maximum number of concurrently open connections.
|
||||||
|
|
||||||
|
Attempting to lock more connections that this number will cause the
|
||||||
|
calling fiber to be blocked until one of the locked connections
|
||||||
|
becomes available for reuse.
|
||||||
|
*/
|
||||||
|
@property void maxConcurrency(uint max_concurrent) {
|
||||||
|
m_sem.maxLocks = max_concurrent;
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
@property uint maxConcurrency() {
|
||||||
|
return m_sem.maxLocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Retrieves a connection to temporarily associate with the calling fiber.
|
||||||
|
|
||||||
|
The returned `LockedConnection` object uses RAII and reference counting
|
||||||
|
to determine when to unlock the connection.
|
||||||
|
*/
|
||||||
|
LockedConnection!Connection lockConnection()
|
||||||
|
{
|
||||||
|
debug assert(m_thread is Thread.getThis(), "ConnectionPool was called from a foreign thread!");
|
||||||
|
|
||||||
|
m_sem.lock();
|
||||||
|
size_t cidx = size_t.max;
|
||||||
|
foreach( i, c; m_connections ){
|
||||||
|
auto plc = c in m_lockCount;
|
||||||
|
if( !plc || *plc == 0 ){
|
||||||
|
cidx = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
if( cidx != size_t.max ){
|
||||||
|
logTrace("returning %s connection %d of %d", Connection.stringof, cidx, m_connections.length);
|
||||||
|
conn = m_connections[cidx];
|
||||||
|
} else {
|
||||||
|
logDebug("creating new %s connection, all %d are in use", Connection.stringof, m_connections.length);
|
||||||
|
conn = m_connectionFactory(); // NOTE: may block
|
||||||
|
logDebug(" ... %s", cast(void*)conn);
|
||||||
|
}
|
||||||
|
m_lockCount[conn] = 1;
|
||||||
|
if( cidx == size_t.max ){
|
||||||
|
m_connections ~= conn;
|
||||||
|
logDebug("Now got %d connections", m_connections.length);
|
||||||
|
}
|
||||||
|
auto ret = LockedConnection!Connection(this, conn);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LockedConnection(Connection) {
|
||||||
|
private {
|
||||||
|
ConnectionPool!Connection m_pool;
|
||||||
|
Task m_task;
|
||||||
|
Connection m_conn;
|
||||||
|
debug uint m_magic = 0xB1345AC2;
|
||||||
|
}
|
||||||
|
|
||||||
|
private this(ConnectionPool!Connection pool, Connection conn)
|
||||||
|
{
|
||||||
|
assert(conn !is null);
|
||||||
|
m_pool = pool;
|
||||||
|
m_conn = conn;
|
||||||
|
m_task = Task.getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
this(this)
|
||||||
|
{
|
||||||
|
debug assert(m_magic == 0xB1345AC2, "LockedConnection value corrupted.");
|
||||||
|
if( m_conn ){
|
||||||
|
auto fthis = Task.getThis();
|
||||||
|
assert(fthis is m_task);
|
||||||
|
m_pool.m_lockCount[m_conn]++;
|
||||||
|
logTrace("conn %s copy %d", cast(void*)m_conn, m_pool.m_lockCount[m_conn]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~this()
|
||||||
|
{
|
||||||
|
debug assert(m_magic == 0xB1345AC2, "LockedConnection value corrupted.");
|
||||||
|
if( m_conn ){
|
||||||
|
auto fthis = Task.getThis();
|
||||||
|
assert(fthis is m_task, "Locked connection destroyed in foreign task.");
|
||||||
|
auto plc = m_conn in m_pool.m_lockCount;
|
||||||
|
assert(plc !is null);
|
||||||
|
assert(*plc >= 1);
|
||||||
|
//logTrace("conn %s destroy %d", cast(void*)m_conn, *plc-1);
|
||||||
|
if( --*plc == 0 ){
|
||||||
|
m_pool.m_sem.unlock();
|
||||||
|
//logTrace("conn %s release", cast(void*)m_conn);
|
||||||
|
}
|
||||||
|
m_conn = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@property int __refCount() const { return m_pool.m_lockCount.get(m_conn, 0); }
|
||||||
|
@property inout(Connection) __conn() inout { return m_conn; }
|
||||||
|
|
||||||
|
alias __conn this;
|
||||||
|
}
|
1844
source/vibe/core/core.d
Normal file
1844
source/vibe/core/core.d
Normal file
File diff suppressed because it is too large
Load diff
638
source/vibe/core/file.d
Normal file
638
source/vibe/core/file.d
Normal file
|
@ -0,0 +1,638 @@
|
||||||
|
/**
|
||||||
|
File handling functions and types.
|
||||||
|
|
||||||
|
Copyright: © 2012-2016 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
*/
|
||||||
|
module vibe.core.file;
|
||||||
|
|
||||||
|
//public import vibe.core.stream;
|
||||||
|
//public import vibe.inet.url;
|
||||||
|
import vibe.core.path;
|
||||||
|
|
||||||
|
import core.stdc.stdio;
|
||||||
|
import core.sys.posix.unistd;
|
||||||
|
import core.sys.posix.fcntl;
|
||||||
|
import core.sys.posix.sys.stat;
|
||||||
|
import std.conv : octal;
|
||||||
|
import vibe.core.log;
|
||||||
|
import std.datetime;
|
||||||
|
import std.exception;
|
||||||
|
import std.file;
|
||||||
|
import std.path;
|
||||||
|
import std.string;
|
||||||
|
|
||||||
|
|
||||||
|
version(Posix){
|
||||||
|
private extern(C) int mkstemps(char* templ, int suffixlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Opens a file stream with the specified mode.
|
||||||
|
*/
|
||||||
|
FileStream openFile(Path path, FileMode mode = FileMode.read)
|
||||||
|
{
|
||||||
|
assert(false);
|
||||||
|
//return eventDriver.openFile(path, mode);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
FileStream openFile(string path, FileMode mode = FileMode.read)
|
||||||
|
{
|
||||||
|
return openFile(Path(path), mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Read a whole file into a buffer.
|
||||||
|
|
||||||
|
If the supplied buffer is large enough, it will be used to store the
|
||||||
|
contents of the file. Otherwise, a new buffer will be allocated.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
path = The path of the file to read
|
||||||
|
buffer = An optional buffer to use for storing the file contents
|
||||||
|
*/
|
||||||
|
ubyte[] readFile(Path path, ubyte[] buffer = null, size_t max_size = size_t.max)
|
||||||
|
{
|
||||||
|
auto fil = openFile(path);
|
||||||
|
scope (exit) fil.close();
|
||||||
|
enforce(fil.size <= max_size, "File is too big.");
|
||||||
|
auto sz = cast(size_t)fil.size;
|
||||||
|
auto ret = sz <= buffer.length ? buffer[0 .. sz] : new ubyte[sz];
|
||||||
|
fil.read(ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
ubyte[] readFile(string path, ubyte[] buffer = null, size_t max_size = size_t.max)
|
||||||
|
{
|
||||||
|
return readFile(Path(path), buffer, max_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Write a whole file at once.
|
||||||
|
*/
|
||||||
|
void writeFile(Path path, in ubyte[] contents)
|
||||||
|
{
|
||||||
|
auto fil = openFile(path, FileMode.createTrunc);
|
||||||
|
scope (exit) fil.close();
|
||||||
|
fil.write(contents);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void writeFile(string path, in ubyte[] contents)
|
||||||
|
{
|
||||||
|
writeFile(Path(path), contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Convenience function to append to a file.
|
||||||
|
*/
|
||||||
|
void appendToFile(Path path, string data) {
|
||||||
|
auto fil = openFile(path, FileMode.append);
|
||||||
|
scope(exit) fil.close();
|
||||||
|
fil.write(data);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void appendToFile(string path, string data)
|
||||||
|
{
|
||||||
|
appendToFile(Path(path), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Read a whole UTF-8 encoded file into a string.
|
||||||
|
|
||||||
|
The resulting string will be sanitized and will have the
|
||||||
|
optional byte order mark (BOM) removed.
|
||||||
|
*/
|
||||||
|
string readFileUTF8(Path path)
|
||||||
|
{
|
||||||
|
import vibe.internal.string;
|
||||||
|
|
||||||
|
return stripUTF8Bom(sanitizeUTF8(readFile(path)));
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
string readFileUTF8(string path)
|
||||||
|
{
|
||||||
|
return readFileUTF8(Path(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Write a string into a UTF-8 encoded file.
|
||||||
|
|
||||||
|
The file will have a byte order mark (BOM) prepended.
|
||||||
|
*/
|
||||||
|
void writeFileUTF8(Path path, string contents)
|
||||||
|
{
|
||||||
|
static immutable ubyte[] bom = [0xEF, 0xBB, 0xBF];
|
||||||
|
auto fil = openFile(path, FileMode.createTrunc);
|
||||||
|
scope (exit) fil.close();
|
||||||
|
fil.write(bom);
|
||||||
|
fil.write(contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates and opens a temporary file for writing.
|
||||||
|
*/
|
||||||
|
FileStream createTempFile(string suffix = null)
|
||||||
|
{
|
||||||
|
version(Windows){
|
||||||
|
import std.conv : to;
|
||||||
|
char[L_tmpnam] tmp;
|
||||||
|
tmpnam(tmp.ptr);
|
||||||
|
auto tmpname = to!string(tmp.ptr);
|
||||||
|
if( tmpname.startsWith("\\") ) tmpname = tmpname[1 .. $];
|
||||||
|
tmpname ~= suffix;
|
||||||
|
return openFile(tmpname, FileMode.createTrunc);
|
||||||
|
} else {
|
||||||
|
enum pattern ="/tmp/vtmp.XXXXXX";
|
||||||
|
scope templ = new char[pattern.length+suffix.length+1];
|
||||||
|
templ[0 .. pattern.length] = pattern;
|
||||||
|
templ[pattern.length .. $-1] = (suffix)[];
|
||||||
|
templ[$-1] = '\0';
|
||||||
|
assert(suffix.length <= int.max);
|
||||||
|
auto fd = mkstemps(templ.ptr, cast(int)suffix.length);
|
||||||
|
enforce(fd >= 0, "Failed to create temporary file.");
|
||||||
|
assert(false);
|
||||||
|
//return eventDriver.adoptFile(fd, Path(templ[0 .. $-1].idup), FileMode.createTrunc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Moves or renames a file.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
from = Path to the file/directory to move/rename.
|
||||||
|
to = The target path
|
||||||
|
copy_fallback = Determines if copy/remove should be used in case of the
|
||||||
|
source and destination path pointing to different devices.
|
||||||
|
*/
|
||||||
|
void moveFile(Path from, Path to, bool copy_fallback = false)
|
||||||
|
{
|
||||||
|
moveFile(from.toNativeString(), to.toNativeString(), copy_fallback);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void moveFile(string from, string to, bool copy_fallback = false)
|
||||||
|
{
|
||||||
|
if (!copy_fallback) {
|
||||||
|
std.file.rename(from, to);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
std.file.rename(from, to);
|
||||||
|
} catch (FileException e) {
|
||||||
|
std.file.copy(from, to);
|
||||||
|
std.file.remove(from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Copies a file.
|
||||||
|
|
||||||
|
Note that attributes and time stamps are currently not retained.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
from = Path of the source file
|
||||||
|
to = Path for the destination file
|
||||||
|
overwrite = If true, any file existing at the destination path will be
|
||||||
|
overwritten. If this is false, an exception will be thrown should
|
||||||
|
a file already exist at the destination path.
|
||||||
|
|
||||||
|
Throws:
|
||||||
|
An Exception if the copy operation fails for some reason.
|
||||||
|
*/
|
||||||
|
void copyFile(Path from, Path to, bool overwrite = false)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
auto src = openFile(from, FileMode.read);
|
||||||
|
scope(exit) src.close();
|
||||||
|
enforce(overwrite || !existsFile(to), "Destination file already exists.");
|
||||||
|
auto dst = openFile(to, FileMode.createTrunc);
|
||||||
|
scope(exit) dst.close();
|
||||||
|
dst.write(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: retain attributes and time stamps
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void copyFile(string from, string to)
|
||||||
|
{
|
||||||
|
copyFile(Path(from), Path(to));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Removes a file
|
||||||
|
*/
|
||||||
|
void removeFile(Path path)
|
||||||
|
{
|
||||||
|
removeFile(path.toNativeString());
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void removeFile(string path)
|
||||||
|
{
|
||||||
|
std.file.remove(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks if a file exists
|
||||||
|
*/
|
||||||
|
bool existsFile(Path path) nothrow
|
||||||
|
{
|
||||||
|
return existsFile(path.toNativeString());
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
bool existsFile(string path) nothrow
|
||||||
|
{
|
||||||
|
// This was *annotated* nothrow in 2.067.
|
||||||
|
static if (__VERSION__ < 2067)
|
||||||
|
scope(failure) assert(0, "Error: existsFile should never throw");
|
||||||
|
return std.file.exists(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Stores information about the specified file/directory into 'info'
|
||||||
|
|
||||||
|
Throws: A `FileException` is thrown if the file does not exist.
|
||||||
|
*/
|
||||||
|
FileInfo getFileInfo(Path path)
|
||||||
|
{
|
||||||
|
auto ent = DirEntry(path.toNativeString());
|
||||||
|
return makeFileInfo(ent);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
FileInfo getFileInfo(string path)
|
||||||
|
{
|
||||||
|
return getFileInfo(Path(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates a new directory.
|
||||||
|
*/
|
||||||
|
void createDirectory(Path path)
|
||||||
|
{
|
||||||
|
mkdir(path.toNativeString());
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void createDirectory(string path)
|
||||||
|
{
|
||||||
|
createDirectory(Path(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Enumerates all files in the specified directory.
|
||||||
|
*/
|
||||||
|
void listDirectory(Path path, scope bool delegate(FileInfo info) del)
|
||||||
|
{
|
||||||
|
foreach( DirEntry ent; dirEntries(path.toNativeString(), SpanMode.shallow) )
|
||||||
|
if( !del(makeFileInfo(ent)) )
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void listDirectory(string path, scope bool delegate(FileInfo info) del)
|
||||||
|
{
|
||||||
|
listDirectory(Path(path), del);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
int delegate(scope int delegate(ref FileInfo)) iterateDirectory(Path path)
|
||||||
|
{
|
||||||
|
int iterator(scope int delegate(ref FileInfo) del){
|
||||||
|
int ret = 0;
|
||||||
|
listDirectory(path, (fi){
|
||||||
|
ret = del(fi);
|
||||||
|
return ret == 0;
|
||||||
|
});
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return &iterator;
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
int delegate(scope int delegate(ref FileInfo)) iterateDirectory(string path)
|
||||||
|
{
|
||||||
|
return iterateDirectory(Path(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Starts watching a directory for changes.
|
||||||
|
*/
|
||||||
|
DirectoryWatcher watchDirectory(Path path, bool recursive = true)
|
||||||
|
{
|
||||||
|
assert(false);
|
||||||
|
//return eventDriver.watchDirectory(path, recursive);
|
||||||
|
}
|
||||||
|
// ditto
|
||||||
|
DirectoryWatcher watchDirectory(string path, bool recursive = true)
|
||||||
|
{
|
||||||
|
return watchDirectory(Path(path), recursive);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the current working directory.
|
||||||
|
*/
|
||||||
|
Path getWorkingDirectory()
|
||||||
|
{
|
||||||
|
return Path(std.file.getcwd());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Contains general information about a file.
|
||||||
|
*/
|
||||||
|
struct FileInfo {
|
||||||
|
/// Name of the file (not including the path)
|
||||||
|
string name;
|
||||||
|
|
||||||
|
/// Size of the file (zero for directories)
|
||||||
|
ulong size;
|
||||||
|
|
||||||
|
/// Time of the last modification
|
||||||
|
SysTime timeModified;
|
||||||
|
|
||||||
|
/// Time of creation (not available on all operating systems/file systems)
|
||||||
|
SysTime timeCreated;
|
||||||
|
|
||||||
|
/// True if this is a symlink to an actual file
|
||||||
|
bool isSymlink;
|
||||||
|
|
||||||
|
/// True if this is a directory or a symlink pointing to a directory
|
||||||
|
bool isDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Specifies how a file is manipulated on disk.
|
||||||
|
*/
|
||||||
|
enum FileMode {
|
||||||
|
/// The file is opened read-only.
|
||||||
|
read,
|
||||||
|
/// The file is opened for read-write random access.
|
||||||
|
readWrite,
|
||||||
|
/// The file is truncated if it exists or created otherwise and then opened for read-write access.
|
||||||
|
createTrunc,
|
||||||
|
/// The file is opened for appending data to it and created if it does not exist.
|
||||||
|
append
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Accesses the contents of a file as a stream.
|
||||||
|
*/
|
||||||
|
struct FileStream {
|
||||||
|
import std.algorithm.comparison : min;
|
||||||
|
import vibe.core.core : yield;
|
||||||
|
import core.stdc.errno;
|
||||||
|
|
||||||
|
version (Windows) {} else
|
||||||
|
{
|
||||||
|
enum O_BINARY = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private {
|
||||||
|
int m_fileDescriptor;
|
||||||
|
Path m_path;
|
||||||
|
ulong m_size;
|
||||||
|
ulong m_ptr = 0;
|
||||||
|
FileMode m_mode;
|
||||||
|
bool m_ownFD = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(Path path, FileMode mode)
|
||||||
|
{
|
||||||
|
auto pathstr = path.toNativeString();
|
||||||
|
final switch(mode){
|
||||||
|
case FileMode.read:
|
||||||
|
m_fileDescriptor = open(pathstr.toStringz(), O_RDONLY|O_BINARY);
|
||||||
|
break;
|
||||||
|
case FileMode.readWrite:
|
||||||
|
m_fileDescriptor = open(pathstr.toStringz(), O_RDWR|O_BINARY);
|
||||||
|
break;
|
||||||
|
case FileMode.createTrunc:
|
||||||
|
m_fileDescriptor = open(pathstr.toStringz(), O_RDWR|O_CREAT|O_TRUNC|O_BINARY, octal!644);
|
||||||
|
break;
|
||||||
|
case FileMode.append:
|
||||||
|
m_fileDescriptor = open(pathstr.toStringz(), O_WRONLY|O_CREAT|O_APPEND|O_BINARY, octal!644);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if( m_fileDescriptor < 0 )
|
||||||
|
//throw new Exception(format("Failed to open '%s' with %s: %d", pathstr, cast(int)mode, errno));
|
||||||
|
throw new Exception("Failed to open file '"~pathstr~"'.");
|
||||||
|
|
||||||
|
this(m_fileDescriptor, path, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
this(int fd, Path path, FileMode mode)
|
||||||
|
{
|
||||||
|
assert(fd >= 0);
|
||||||
|
m_fileDescriptor = fd;
|
||||||
|
m_path = path;
|
||||||
|
m_mode = mode;
|
||||||
|
|
||||||
|
version(linux){
|
||||||
|
// stat_t seems to be defined wrong on linux/64
|
||||||
|
m_size = lseek(m_fileDescriptor, 0, SEEK_END);
|
||||||
|
} else {
|
||||||
|
stat_t st;
|
||||||
|
fstat(m_fileDescriptor, &st);
|
||||||
|
m_size = st.st_size;
|
||||||
|
|
||||||
|
// (at least) on windows, the created file is write protected
|
||||||
|
version(Windows){
|
||||||
|
if( mode == FileMode.createTrunc )
|
||||||
|
chmod(path.toNativeString().toStringz(), S_IREAD|S_IWRITE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lseek(m_fileDescriptor, 0, SEEK_SET);
|
||||||
|
|
||||||
|
logDebug("opened file %s with %d bytes as %d", path.toNativeString(), m_size, m_fileDescriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
~this()
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@property int fd() { return m_fileDescriptor; }
|
||||||
|
|
||||||
|
/// The path of the file.
|
||||||
|
@property Path path() const { return m_path; }
|
||||||
|
|
||||||
|
/// Determines if the file stream is still open
|
||||||
|
@property bool isOpen() const { return m_fileDescriptor >= 0; }
|
||||||
|
@property ulong size() const { return m_size; }
|
||||||
|
@property bool readable() const { return m_mode != FileMode.append; }
|
||||||
|
@property bool writable() const { return m_mode != FileMode.read; }
|
||||||
|
|
||||||
|
void takeOwnershipOfFD()
|
||||||
|
{
|
||||||
|
enforce(m_ownFD);
|
||||||
|
m_ownFD = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void seek(ulong offset)
|
||||||
|
{
|
||||||
|
version (Win32) {
|
||||||
|
enforce(offset <= off_t.max, "Cannot seek above 4GB on Windows x32.");
|
||||||
|
auto pos = lseek(m_fileDescriptor, cast(off_t)offset, SEEK_SET);
|
||||||
|
} else auto pos = lseek(m_fileDescriptor, offset, SEEK_SET);
|
||||||
|
enforce(pos == offset, "Failed to seek in file.");
|
||||||
|
m_ptr = offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong tell() { return m_ptr; }
|
||||||
|
|
||||||
|
/// Closes the file handle.
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
if( m_fileDescriptor != -1 && m_ownFD ){
|
||||||
|
.close(m_fileDescriptor);
|
||||||
|
m_fileDescriptor = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@property bool empty() const { assert(this.readable); return m_ptr >= m_size; }
|
||||||
|
@property ulong leastSize() const { assert(this.readable); return m_size - m_ptr; }
|
||||||
|
@property bool dataAvailableForRead() { return true; }
|
||||||
|
|
||||||
|
const(ubyte)[] peek()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read(ubyte[] dst)
|
||||||
|
{
|
||||||
|
assert(this.readable);
|
||||||
|
while (dst.length > 0) {
|
||||||
|
enforce(dst.length <= leastSize);
|
||||||
|
auto sz = min(dst.length, 4096);
|
||||||
|
enforce(.read(m_fileDescriptor, dst.ptr, cast(int)sz) == sz, "Failed to read data from disk.");
|
||||||
|
dst = dst[sz .. $];
|
||||||
|
m_ptr += sz;
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(in ubyte[] bytes_)
|
||||||
|
{
|
||||||
|
const(ubyte)[] bytes = bytes_;
|
||||||
|
assert(this.writable);
|
||||||
|
while (bytes.length > 0) {
|
||||||
|
auto sz = min(bytes.length, 4096);
|
||||||
|
auto ret = .write(m_fileDescriptor, bytes.ptr, cast(int)sz);
|
||||||
|
import std.format : format;
|
||||||
|
enforce(ret == sz, format("Failed to write data to disk. %s %s %s %s", sz, errno, ret, m_fileDescriptor));
|
||||||
|
bytes = bytes[sz .. $];
|
||||||
|
m_ptr += sz;
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(InputStream)(InputStream stream, ulong nbytes = 0)
|
||||||
|
{
|
||||||
|
writeDefault(stream, nbytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
void flush()
|
||||||
|
{
|
||||||
|
assert(this.writable);
|
||||||
|
}
|
||||||
|
|
||||||
|
void finalize()
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeDefault(OutputStream, InputStream)(ref OutputStream dst, InputStream stream, ulong nbytes = 0)
|
||||||
|
{
|
||||||
|
assert(false);
|
||||||
|
/*
|
||||||
|
static struct Buffer { ubyte[64*1024] bytes = void; }
|
||||||
|
auto bufferobj = FreeListRef!(Buffer, false)();
|
||||||
|
auto buffer = bufferobj.bytes[];
|
||||||
|
|
||||||
|
//logTrace("default write %d bytes, empty=%s", nbytes, stream.empty);
|
||||||
|
if (nbytes == 0) {
|
||||||
|
while (!stream.empty) {
|
||||||
|
size_t chunk = min(stream.leastSize, buffer.length);
|
||||||
|
assert(chunk > 0, "leastSize returned zero for non-empty stream.");
|
||||||
|
//logTrace("read pipe chunk %d", chunk);
|
||||||
|
stream.read(buffer[0 .. chunk]);
|
||||||
|
dst.write(buffer[0 .. chunk]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (nbytes > 0) {
|
||||||
|
size_t chunk = min(nbytes, buffer.length);
|
||||||
|
//logTrace("read pipe chunk %d", chunk);
|
||||||
|
stream.read(buffer[0 .. chunk]);
|
||||||
|
dst.write(buffer[0 .. chunk]);
|
||||||
|
nbytes -= chunk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Interface for directory watcher implementations.
|
||||||
|
|
||||||
|
Directory watchers monitor the contents of a directory (wither recursively or non-recursively)
|
||||||
|
for changes, such as file additions, deletions or modifications.
|
||||||
|
*/
|
||||||
|
interface DirectoryWatcher {
|
||||||
|
/// The path of the watched directory
|
||||||
|
@property Path path() const;
|
||||||
|
|
||||||
|
/// Indicates if the directory is watched recursively
|
||||||
|
@property bool recursive() const;
|
||||||
|
|
||||||
|
/** Fills the destination array with all changes that occurred since the last call.
|
||||||
|
|
||||||
|
The function will block until either directory changes have occurred or until the
|
||||||
|
timeout has elapsed. Specifying a negative duration will cause the function to
|
||||||
|
wait without a timeout.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
dst = The destination array to which the changes will be appended
|
||||||
|
timeout = Optional timeout for the read operation
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
If the call completed successfully, true is returned.
|
||||||
|
*/
|
||||||
|
bool readChanges(ref DirectoryChange[] dst, Duration timeout = dur!"seconds"(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Specifies the kind of change in a watched directory.
|
||||||
|
*/
|
||||||
|
enum DirectoryChangeType {
|
||||||
|
/// A file or directory was added
|
||||||
|
added,
|
||||||
|
/// A file or directory was deleted
|
||||||
|
removed,
|
||||||
|
/// A file or directory was modified
|
||||||
|
modified
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Describes a single change in a watched directory.
|
||||||
|
*/
|
||||||
|
struct DirectoryChange {
|
||||||
|
/// The type of change
|
||||||
|
DirectoryChangeType type;
|
||||||
|
|
||||||
|
/// Path of the file/directory that was changed
|
||||||
|
Path path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private FileInfo makeFileInfo(DirEntry ent)
|
||||||
|
{
|
||||||
|
FileInfo ret;
|
||||||
|
ret.name = baseName(ent.name);
|
||||||
|
if( ret.name.length == 0 ) ret.name = ent.name;
|
||||||
|
assert(ret.name.length > 0);
|
||||||
|
ret.size = ent.size;
|
||||||
|
ret.timeModified = ent.timeLastModified;
|
||||||
|
version(Windows) ret.timeCreated = ent.timeCreated;
|
||||||
|
else ret.timeCreated = ent.timeLastModified;
|
||||||
|
ret.isSymlink = ent.isSymlink;
|
||||||
|
ret.isDirectory = ent.isDir;
|
||||||
|
return ret;
|
||||||
|
}
|
879
source/vibe/core/log.d
Normal file
879
source/vibe/core/log.d
Normal file
|
@ -0,0 +1,879 @@
|
||||||
|
/**
|
||||||
|
Central logging facility for vibe.
|
||||||
|
|
||||||
|
Copyright: © 2012-2014 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
*/
|
||||||
|
module vibe.core.log;
|
||||||
|
|
||||||
|
import vibe.core.args;
|
||||||
|
import vibe.core.concurrency : ScopedLock, lock;
|
||||||
|
import vibe.core.sync;
|
||||||
|
|
||||||
|
import std.algorithm;
|
||||||
|
import std.array;
|
||||||
|
import std.datetime;
|
||||||
|
import std.format;
|
||||||
|
import std.stdio;
|
||||||
|
import core.atomic;
|
||||||
|
import core.thread;
|
||||||
|
|
||||||
|
import std.traits : isSomeString;
|
||||||
|
import std.range.primitives : isInputRange, isOutputRange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets the minimum log level to be printed using the default console logger.
|
||||||
|
|
||||||
|
This level applies to the default stdout/stderr logger only.
|
||||||
|
*/
|
||||||
|
void setLogLevel(LogLevel level)
|
||||||
|
nothrow @safe {
|
||||||
|
if (ss_stdoutLogger)
|
||||||
|
ss_stdoutLogger.lock().minLevel = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets the log format used for the default console logger.
|
||||||
|
|
||||||
|
This level applies to the default stdout/stderr logger only.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
fmt = The log format for the stderr (default is `FileLogger.Format.thread`)
|
||||||
|
infoFmt = The log format for the stdout (default is `FileLogger.Format.plain`)
|
||||||
|
*/
|
||||||
|
void setLogFormat(FileLogger.Format fmt, FileLogger.Format infoFmt = FileLogger.Format.plain)
|
||||||
|
nothrow @safe {
|
||||||
|
if (ss_stdoutLogger) {
|
||||||
|
auto l = ss_stdoutLogger.lock();
|
||||||
|
l.format = fmt;
|
||||||
|
l.infoFormat = infoFmt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets a log file for disk file logging.
|
||||||
|
|
||||||
|
Multiple calls to this function will register multiple log
|
||||||
|
files for output.
|
||||||
|
*/
|
||||||
|
void setLogFile(string filename, LogLevel min_level = LogLevel.error)
|
||||||
|
{
|
||||||
|
auto logger = cast(shared)new FileLogger(filename);
|
||||||
|
{
|
||||||
|
auto l = logger.lock();
|
||||||
|
l.minLevel = min_level;
|
||||||
|
l.format = FileLogger.Format.threadTime;
|
||||||
|
}
|
||||||
|
registerLogger(logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Registers a new logger instance.
|
||||||
|
|
||||||
|
The specified Logger will receive all log messages in its Logger.log
|
||||||
|
method after it has been registered.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
---
|
||||||
|
auto logger = cast(shared)new HTMLLogger("log.html");
|
||||||
|
logger.lock().format = FileLogger.Format.threadTime;
|
||||||
|
registerLogger(logger);
|
||||||
|
---
|
||||||
|
|
||||||
|
See_Also: deregisterLogger
|
||||||
|
*/
|
||||||
|
void registerLogger(shared(Logger) logger)
|
||||||
|
nothrow {
|
||||||
|
ss_loggers ~= logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Deregisters an active logger instance.
|
||||||
|
|
||||||
|
See_Also: registerLogger
|
||||||
|
*/
|
||||||
|
void deregisterLogger(shared(Logger) logger)
|
||||||
|
nothrow {
|
||||||
|
for (size_t i = 0; i < ss_loggers.length; ) {
|
||||||
|
if (ss_loggers[i] !is logger) i++;
|
||||||
|
else ss_loggers = ss_loggers[0 .. i] ~ ss_loggers[i+1 .. $];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Logs a message.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
level = The log level for the logged message
|
||||||
|
fmt = See http://dlang.org/phobos/std_format.html#format-string
|
||||||
|
args = Any input values needed for formatting
|
||||||
|
*/
|
||||||
|
void log(LogLevel level, /*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args)
|
||||||
|
nothrow if (isSomeString!S)
|
||||||
|
{
|
||||||
|
static assert(level != LogLevel.none);
|
||||||
|
try {
|
||||||
|
foreach (l; getLoggers())
|
||||||
|
if (l.minLevel <= level) { // WARNING: TYPE SYSTEM HOLE: accessing field of shared class!
|
||||||
|
auto ll = l.lock();
|
||||||
|
auto rng = LogOutputRange(ll, file, line, level);
|
||||||
|
/*() @trusted {*/ rng.formattedWrite(fmt, args); //} (); // formattedWrite is not @safe at least up to 2.068.0
|
||||||
|
rng.finalize();
|
||||||
|
}
|
||||||
|
} catch(Exception e) debug assert(false, e.msg);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
void logTrace(/*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.trace/*, mod, func*/, file, line)(fmt, args); }
|
||||||
|
/// ditto
|
||||||
|
void logDebugV(/*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.debugV/*, mod, func*/, file, line)(fmt, args); }
|
||||||
|
/// ditto
|
||||||
|
void logDebug(/*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.debug_/*, mod, func*/, file, line)(fmt, args); }
|
||||||
|
/// ditto
|
||||||
|
void logDiagnostic(/*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.diagnostic/*, mod, func*/, file, line)(fmt, args); }
|
||||||
|
/// ditto
|
||||||
|
void logInfo(/*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.info/*, mod, func*/, file, line)(fmt, args); }
|
||||||
|
/// ditto
|
||||||
|
void logWarn(/*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.warn/*, mod, func*/, file, line)(fmt, args); }
|
||||||
|
/// ditto
|
||||||
|
void logError(/*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.error/*, mod, func*/, file, line)(fmt, args); }
|
||||||
|
/// ditto
|
||||||
|
void logCritical(/*string mod = __MODULE__, string func = __FUNCTION__,*/ string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.critical/*, mod, func*/, file, line)(fmt, args); }
|
||||||
|
/// ditto
|
||||||
|
void logFatal(string file = __FILE__, int line = __LINE__, S, T...)(S fmt, lazy T args) nothrow { log!(LogLevel.fatal, file, line)(fmt, args); }
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe unittest {
|
||||||
|
void test() nothrow
|
||||||
|
{
|
||||||
|
logInfo("Hello, World!");
|
||||||
|
logWarn("This may not be %s.", "good");
|
||||||
|
log!(LogLevel.info)("This is a %s.", "test");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Specifies the log level for a particular log message.
|
||||||
|
enum LogLevel {
|
||||||
|
trace, /// Developer information for locating events when no useful stack traces are available
|
||||||
|
debugV, /// Developer information useful for algorithm debugging - for verbose output
|
||||||
|
debug_, /// Developer information useful for algorithm debugging
|
||||||
|
diagnostic, /// Extended user information (e.g. for more detailed error information)
|
||||||
|
info, /// Informational message for normal user education
|
||||||
|
warn, /// Unexpected condition that could indicate an error but has no direct consequences
|
||||||
|
error, /// Normal error that is handled gracefully
|
||||||
|
critical, /// Error that severely influences the execution of the application
|
||||||
|
fatal, /// Error that forces the application to terminate
|
||||||
|
none, /// Special value used to indicate no logging when set as the minimum log level
|
||||||
|
|
||||||
|
verbose1 = diagnostic, /// Alias for diagnostic messages
|
||||||
|
verbose2 = debug_, /// Alias for debug messages
|
||||||
|
verbose3 = debugV, /// Alias for verbose debug messages
|
||||||
|
verbose4 = trace, /// Alias for trace messages
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a single logged line
|
||||||
|
struct LogLine {
|
||||||
|
string mod;
|
||||||
|
string func;
|
||||||
|
string file;
|
||||||
|
int line;
|
||||||
|
LogLevel level;
|
||||||
|
Thread thread;
|
||||||
|
string threadName;
|
||||||
|
uint threadID;
|
||||||
|
Fiber fiber;
|
||||||
|
uint fiberID;
|
||||||
|
SysTime time;
|
||||||
|
string text; /// Legacy field used in `Logger.log`
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Abstract base class for all loggers
|
||||||
|
class Logger {
|
||||||
|
LogLevel minLevel = LogLevel.min;
|
||||||
|
|
||||||
|
private {
|
||||||
|
LogLine m_curLine;
|
||||||
|
Appender!string m_curLineText;
|
||||||
|
}
|
||||||
|
|
||||||
|
final bool acceptsLevel(LogLevel value) nothrow pure @safe { return value >= this.minLevel; }
|
||||||
|
|
||||||
|
/** Legacy logging interface relying on dynamic memory allocation.
|
||||||
|
|
||||||
|
Override `beginLine`, `put`, `endLine` instead for a more efficient and
|
||||||
|
possibly allocation-free implementation.
|
||||||
|
*/
|
||||||
|
void log(ref LogLine line) @safe {}
|
||||||
|
|
||||||
|
/// Starts a new log line.
|
||||||
|
void beginLine(ref LogLine line_info)
|
||||||
|
@safe {
|
||||||
|
m_curLine = line_info;
|
||||||
|
m_curLineText = appender!string();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Writes part of a log line message.
|
||||||
|
void put(scope const(char)[] text)
|
||||||
|
@safe {
|
||||||
|
m_curLineText.put(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finalizes a log line.
|
||||||
|
void endLine()
|
||||||
|
@safe {
|
||||||
|
m_curLine.text = m_curLineText.data;
|
||||||
|
log(m_curLine);
|
||||||
|
m_curLine.text = null;
|
||||||
|
m_curLineText = Appender!string.init;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Plain-text based logger for logging to regular files or stdout/stderr
|
||||||
|
*/
|
||||||
|
final class FileLogger : Logger {
|
||||||
|
/// The log format used by the FileLogger
|
||||||
|
enum Format {
|
||||||
|
plain, /// Output only the plain log message
|
||||||
|
thread, /// Prefix "[thread-id:fiber-id loglevel]"
|
||||||
|
threadTime /// Prefix "[thread-id:fiber-id timestamp loglevel]"
|
||||||
|
}
|
||||||
|
|
||||||
|
private {
|
||||||
|
File m_infoFile;
|
||||||
|
File m_diagFile;
|
||||||
|
File m_curFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
Format format = Format.thread;
|
||||||
|
Format infoFormat = Format.plain;
|
||||||
|
|
||||||
|
this(File info_file, File diag_file)
|
||||||
|
{
|
||||||
|
m_infoFile = info_file;
|
||||||
|
m_diagFile = diag_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(string filename)
|
||||||
|
{
|
||||||
|
m_infoFile = File(filename, "ab");
|
||||||
|
m_diagFile = m_infoFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
override void beginLine(ref LogLine msg)
|
||||||
|
@trusted // FILE isn't @safe (as of DMD 2.065)
|
||||||
|
{
|
||||||
|
string pref;
|
||||||
|
final switch (msg.level) {
|
||||||
|
case LogLevel.trace: pref = "trc"; m_curFile = m_diagFile; break;
|
||||||
|
case LogLevel.debugV: pref = "dbv"; m_curFile = m_diagFile; break;
|
||||||
|
case LogLevel.debug_: pref = "dbg"; m_curFile = m_diagFile; break;
|
||||||
|
case LogLevel.diagnostic: pref = "dia"; m_curFile = m_diagFile; break;
|
||||||
|
case LogLevel.info: pref = "INF"; m_curFile = m_infoFile; break;
|
||||||
|
case LogLevel.warn: pref = "WRN"; m_curFile = m_diagFile; break;
|
||||||
|
case LogLevel.error: pref = "ERR"; m_curFile = m_diagFile; break;
|
||||||
|
case LogLevel.critical: pref = "CRITICAL"; m_curFile = m_diagFile; break;
|
||||||
|
case LogLevel.fatal: pref = "FATAL"; m_curFile = m_diagFile; break;
|
||||||
|
case LogLevel.none: assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto fmt = (m_curFile is m_diagFile) ? this.format : this.infoFormat;
|
||||||
|
|
||||||
|
final switch (fmt) {
|
||||||
|
case Format.plain: break;
|
||||||
|
case Format.thread: m_curFile.writef("[%08X:%08X %s] ", msg.threadID, msg.fiberID, pref); break;
|
||||||
|
case Format.threadTime:
|
||||||
|
auto tm = msg.time;
|
||||||
|
static if (is(typeof(tm.fracSecs))) auto msecs = tm.fracSecs.total!"msecs"; // 2.069 has deprecated "fracSec"
|
||||||
|
else auto msecs = tm.fracSec.msecs;
|
||||||
|
m_curFile.writef("[%08X:%08X %d.%02d.%02d %02d:%02d:%02d.%03d %s] ",
|
||||||
|
msg.threadID, msg.fiberID,
|
||||||
|
tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second, msecs,
|
||||||
|
pref);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override void put(scope const(char)[] text)
|
||||||
|
{
|
||||||
|
static if (__VERSION__ <= 2066)
|
||||||
|
() @trusted { m_curFile.write(text); } ();
|
||||||
|
else m_curFile.write(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
override void endLine()
|
||||||
|
{
|
||||||
|
static if (__VERSION__ <= 2066)
|
||||||
|
() @trusted { m_curFile.writeln(); } ();
|
||||||
|
else m_curFile.writeln();
|
||||||
|
m_curFile.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Logger implementation for logging to an HTML file with dynamic filtering support.
|
||||||
|
*/
|
||||||
|
final class HTMLLogger : Logger {
|
||||||
|
private {
|
||||||
|
File m_logFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(string filename = "log.html")
|
||||||
|
{
|
||||||
|
m_logFile = File(filename, "wt");
|
||||||
|
writeHeader();
|
||||||
|
}
|
||||||
|
|
||||||
|
~this()
|
||||||
|
{
|
||||||
|
//version(FinalizerDebug) writeln("HtmlLogWritet.~this");
|
||||||
|
writeFooter();
|
||||||
|
m_logFile.close();
|
||||||
|
//version(FinalizerDebug) writeln("HtmlLogWritet.~this out");
|
||||||
|
}
|
||||||
|
|
||||||
|
@property void minLogLevel(LogLevel value) pure nothrow @safe { this.minLevel = value; }
|
||||||
|
|
||||||
|
override void beginLine(ref LogLine msg)
|
||||||
|
@trusted // FILE isn't @safe (as of DMD 2.065)
|
||||||
|
{
|
||||||
|
if( !m_logFile.isOpen ) return;
|
||||||
|
|
||||||
|
final switch (msg.level) {
|
||||||
|
case LogLevel.none: assert(false);
|
||||||
|
case LogLevel.trace: m_logFile.write(`<div class="trace">`); break;
|
||||||
|
case LogLevel.debugV: m_logFile.write(`<div class="debugv">`); break;
|
||||||
|
case LogLevel.debug_: m_logFile.write(`<div class="debug">`); break;
|
||||||
|
case LogLevel.diagnostic: m_logFile.write(`<div class="diagnostic">`); break;
|
||||||
|
case LogLevel.info: m_logFile.write(`<div class="info">`); break;
|
||||||
|
case LogLevel.warn: m_logFile.write(`<div class="warn">`); break;
|
||||||
|
case LogLevel.error: m_logFile.write(`<div class="error">`); break;
|
||||||
|
case LogLevel.critical: m_logFile.write(`<div class="critical">`); break;
|
||||||
|
case LogLevel.fatal: m_logFile.write(`<div class="fatal">`); break;
|
||||||
|
}
|
||||||
|
m_logFile.writef(`<div class="timeStamp">%s</div>`, msg.time.toISOExtString());
|
||||||
|
if (msg.thread)
|
||||||
|
m_logFile.writef(`<div class="threadName">%s</div>`, msg.thread.name);
|
||||||
|
m_logFile.write(`<div class="message">`);
|
||||||
|
}
|
||||||
|
|
||||||
|
override void put(scope const(char)[] text)
|
||||||
|
{
|
||||||
|
auto dst = () @trusted { return m_logFile.lockingTextWriter(); } (); // LockingTextWriter not @safe for DMD 2.066
|
||||||
|
while (!text.empty && (text.front == ' ' || text.front == '\t')) {
|
||||||
|
foreach (i; 0 .. text.front == ' ' ? 1 : 4)
|
||||||
|
() @trusted { dst.put(" "); } (); // LockingTextWriter not @safe for DMD 2.066
|
||||||
|
text.popFront();
|
||||||
|
}
|
||||||
|
() @trusted { filterHTMLEscape(dst, text); } (); // LockingTextWriter not @safe for DMD 2.066
|
||||||
|
}
|
||||||
|
|
||||||
|
override void endLine()
|
||||||
|
{
|
||||||
|
() @trusted { // not @safe for DMD 2.066
|
||||||
|
m_logFile.write(`</div>`);
|
||||||
|
m_logFile.writeln(`</div>`);
|
||||||
|
} ();
|
||||||
|
m_logFile.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeHeader(){
|
||||||
|
if( !m_logFile.isOpen ) return;
|
||||||
|
|
||||||
|
m_logFile.writeln(
|
||||||
|
`<html>
|
||||||
|
<head>
|
||||||
|
<title>HTML Log</title>
|
||||||
|
<style content="text/css">
|
||||||
|
.trace { position: relative; color: #E0E0E0; font-size: 9pt; }
|
||||||
|
.debugv { position: relative; color: #E0E0E0; font-size: 9pt; }
|
||||||
|
.debug { position: relative; color: #808080; }
|
||||||
|
.diagnostic { position: relative; color: #808080; }
|
||||||
|
.info { position: relative; color: black; }
|
||||||
|
.warn { position: relative; color: #E08000; }
|
||||||
|
.error { position: relative; color: red; }
|
||||||
|
.critical { position: relative; color: red; background-color: black; }
|
||||||
|
.fatal { position: relative; color: red; background-color: black; }
|
||||||
|
|
||||||
|
.log { margin-left: 10pt; }
|
||||||
|
.code {
|
||||||
|
font-family: "Courier New";
|
||||||
|
background-color: #F0F0F0;
|
||||||
|
border: 1px solid gray;
|
||||||
|
margin-bottom: 10pt;
|
||||||
|
margin-left: 30pt;
|
||||||
|
margin-right: 10pt;
|
||||||
|
padding-left: 0pt;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.timeStamp {
|
||||||
|
position: absolute;
|
||||||
|
width: 150pt;
|
||||||
|
}
|
||||||
|
div.threadName {
|
||||||
|
position: absolute;
|
||||||
|
top: 0pt;
|
||||||
|
left: 150pt;
|
||||||
|
width: 100pt;
|
||||||
|
}
|
||||||
|
div.message {
|
||||||
|
position: relative;
|
||||||
|
top: 0pt;
|
||||||
|
left: 250pt;
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
font-family: Tahoma, Arial, sans-serif;
|
||||||
|
font-size: 10pt;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script language="JavaScript">
|
||||||
|
function enableStyle(i){
|
||||||
|
var style = document.styleSheets[0].cssRules[i].style;
|
||||||
|
style.display = "block";
|
||||||
|
}
|
||||||
|
|
||||||
|
function disableStyle(i){
|
||||||
|
var style = document.styleSheets[0].cssRules[i].style;
|
||||||
|
style.display = "none";
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateLevels(){
|
||||||
|
var sel = document.getElementById("Level");
|
||||||
|
var level = sel.value;
|
||||||
|
for( i = 0; i < level; i++ ) disableStyle(i);
|
||||||
|
for( i = level; i < 5; i++ ) enableStyle(i);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body style="padding: 0px; margin: 0px;" onLoad="updateLevels(); updateCode();">
|
||||||
|
<div style="position: fixed; z-index: 100; padding: 4pt; width:100%; background-color: lightgray; border-bottom: 1px solid black;">
|
||||||
|
<form style="margin: 0px;">
|
||||||
|
Minimum Log Level:
|
||||||
|
<select id="Level" onChange="updateLevels()">
|
||||||
|
<option value="0">Trace</option>
|
||||||
|
<option value="1">Verbose</option>
|
||||||
|
<option value="2">Debug</option>
|
||||||
|
<option value="3">Diagnostic</option>
|
||||||
|
<option value="4">Info</option>
|
||||||
|
<option value="5">Warn</option>
|
||||||
|
<option value="6">Error</option>
|
||||||
|
<option value="7">Critical</option>
|
||||||
|
<option value="8">Fatal</option>
|
||||||
|
</select>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div style="height: 30pt;"></div>
|
||||||
|
<div class="log">`);
|
||||||
|
m_logFile.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeFooter(){
|
||||||
|
if( !m_logFile.isOpen ) return;
|
||||||
|
|
||||||
|
m_logFile.writeln(
|
||||||
|
` </div>
|
||||||
|
</body>
|
||||||
|
</html>`);
|
||||||
|
m_logFile.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Helper stuff.
|
||||||
|
*/
|
||||||
|
/** Writes the HTML escaped version of a given string to an output range.
|
||||||
|
*/
|
||||||
|
void filterHTMLEscape(R, S)(ref R dst, S str, HTMLEscapeFlags flags = HTMLEscapeFlags.escapeNewline)
|
||||||
|
if (isOutputRange!(R, dchar) && isInputRange!S)
|
||||||
|
{
|
||||||
|
for (;!str.empty;str.popFront())
|
||||||
|
filterHTMLEscape(dst, str.front, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Writes the HTML escaped version of a character to an output range.
|
||||||
|
*/
|
||||||
|
void filterHTMLEscape(R)(ref R dst, dchar ch, HTMLEscapeFlags flags = HTMLEscapeFlags.escapeNewline )
|
||||||
|
{
|
||||||
|
switch (ch) {
|
||||||
|
default:
|
||||||
|
if (flags & HTMLEscapeFlags.escapeUnknown) {
|
||||||
|
dst.put("&#");
|
||||||
|
dst.put(to!string(cast(uint)ch));
|
||||||
|
dst.put(';');
|
||||||
|
} else dst.put(ch);
|
||||||
|
break;
|
||||||
|
case '"':
|
||||||
|
if (flags & HTMLEscapeFlags.escapeQuotes) dst.put(""");
|
||||||
|
else dst.put('"');
|
||||||
|
break;
|
||||||
|
case '\'':
|
||||||
|
if (flags & HTMLEscapeFlags.escapeQuotes) dst.put("'");
|
||||||
|
else dst.put('\'');
|
||||||
|
break;
|
||||||
|
case '\r', '\n':
|
||||||
|
if (flags & HTMLEscapeFlags.escapeNewline) {
|
||||||
|
dst.put("&#");
|
||||||
|
dst.put(to!string(cast(uint)ch));
|
||||||
|
dst.put(';');
|
||||||
|
} else dst.put(ch);
|
||||||
|
break;
|
||||||
|
case 'a': .. case 'z': goto case;
|
||||||
|
case 'A': .. case 'Z': goto case;
|
||||||
|
case '0': .. case '9': goto case;
|
||||||
|
case ' ', '\t', '-', '_', '.', ':', ',', ';',
|
||||||
|
'#', '+', '*', '?', '=', '(', ')', '/', '!',
|
||||||
|
'%' , '{', '}', '[', ']', '`', '´', '$', '^', '~':
|
||||||
|
dst.put(cast(char)ch);
|
||||||
|
break;
|
||||||
|
case '<': dst.put("<"); break;
|
||||||
|
case '>': dst.put(">"); break;
|
||||||
|
case '&': dst.put("&"); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum HTMLEscapeFlags {
|
||||||
|
escapeMinimal = 0,
|
||||||
|
escapeQuotes = 1<<0,
|
||||||
|
escapeNewline = 1<<1,
|
||||||
|
escapeUnknown = 1<<2
|
||||||
|
}
|
||||||
|
/*****************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
import std.conv;
|
||||||
|
/**
|
||||||
|
A logger that logs in syslog format according to RFC 5424.
|
||||||
|
|
||||||
|
Messages can be logged to files (via file streams) or over the network (via
|
||||||
|
TCP or SSL streams).
|
||||||
|
|
||||||
|
Standards: Conforms to RFC 5424.
|
||||||
|
*/
|
||||||
|
final class SyslogLogger(OutputStream) : Logger {
|
||||||
|
private {
|
||||||
|
string m_hostName;
|
||||||
|
string m_appName;
|
||||||
|
OutputStream m_ostream;
|
||||||
|
Facility m_facility;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Facilities
|
||||||
|
enum Facility {
|
||||||
|
kern, /// kernel messages
|
||||||
|
user, /// user-level messages
|
||||||
|
mail, /// mail system
|
||||||
|
daemon, /// system daemons
|
||||||
|
auth, /// security/authorization messages
|
||||||
|
syslog, /// messages generated internally by syslogd
|
||||||
|
lpr, /// line printer subsystem
|
||||||
|
news, /// network news subsystem
|
||||||
|
uucp, /// UUCP subsystem
|
||||||
|
clockDaemon, /// clock daemon
|
||||||
|
authpriv, /// security/authorization messages
|
||||||
|
ftp, /// FTP daemon
|
||||||
|
ntp, /// NTP subsystem
|
||||||
|
logAudit, /// log audit
|
||||||
|
logAlert, /// log alert
|
||||||
|
cron, /// clock daemon
|
||||||
|
local0, /// local use 0
|
||||||
|
local1, /// local use 1
|
||||||
|
local2, /// local use 2
|
||||||
|
local3, /// local use 3
|
||||||
|
local4, /// local use 4
|
||||||
|
local5, /// local use 5
|
||||||
|
local6, /// local use 6
|
||||||
|
local7, /// local use 7
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Severities
|
||||||
|
private enum Severity {
|
||||||
|
emergency, /// system is unusable
|
||||||
|
alert, /// action must be taken immediately
|
||||||
|
critical, /// critical conditions
|
||||||
|
error, /// error conditions
|
||||||
|
warning, /// warning conditions
|
||||||
|
notice, /// normal but significant condition
|
||||||
|
info, /// informational messages
|
||||||
|
debug_, /// debug-level messages
|
||||||
|
}
|
||||||
|
|
||||||
|
/// syslog message format (version 1)
|
||||||
|
/// see section 6 in RFC 5424
|
||||||
|
private enum SYSLOG_MESSAGE_FORMAT_VERSION1 = "<%.3s>1 %s %.255s %.48s %.128s %.32s %s %s";
|
||||||
|
///
|
||||||
|
private enum NILVALUE = "-";
|
||||||
|
///
|
||||||
|
private enum BOM = x"EFBBBF";
|
||||||
|
|
||||||
|
/**
|
||||||
|
Construct a SyslogLogger.
|
||||||
|
|
||||||
|
The log messages are sent to the given OutputStream stream using the given
|
||||||
|
Facility facility.Optionally the appName and hostName can be set. The
|
||||||
|
appName defaults to null. The hostName defaults to hostName().
|
||||||
|
|
||||||
|
Note that the passed stream's write function must not use logging with
|
||||||
|
a level for that this Logger's acceptsLevel returns true. Because this
|
||||||
|
Logger uses the stream's write function when it logs and would hence
|
||||||
|
log forevermore.
|
||||||
|
*/
|
||||||
|
this(OutputStream stream, Facility facility, string appName = null, string hostName = hostName())
|
||||||
|
{
|
||||||
|
m_hostName = hostName != "" ? hostName : NILVALUE;
|
||||||
|
m_appName = appName != "" ? appName : NILVALUE;
|
||||||
|
m_ostream = stream;
|
||||||
|
m_facility = facility;
|
||||||
|
this.minLevel = LogLevel.debug_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Logs the given LogLine msg.
|
||||||
|
|
||||||
|
It uses the msg's time, level, and text field.
|
||||||
|
*/
|
||||||
|
override void beginLine(ref LogLine msg)
|
||||||
|
@trusted { // OutputStream isn't @safe
|
||||||
|
auto tm = msg.time;
|
||||||
|
import core.time;
|
||||||
|
// at most 6 digits for fractional seconds according to RFC
|
||||||
|
static if (is(typeof(tm.fracSecs))) tm.fracSecs = tm.fracSecs.total!"usecs".dur!"usecs";
|
||||||
|
else tm.fracSec = FracSec.from!"usecs"(tm.fracSec.usecs);
|
||||||
|
auto timestamp = tm.toISOExtString();
|
||||||
|
|
||||||
|
Severity syslogSeverity;
|
||||||
|
// map LogLevel to syslog's severity
|
||||||
|
final switch(msg.level) {
|
||||||
|
case LogLevel.none: assert(false);
|
||||||
|
case LogLevel.trace: return;
|
||||||
|
case LogLevel.debugV: return;
|
||||||
|
case LogLevel.debug_: syslogSeverity = Severity.debug_; break;
|
||||||
|
case LogLevel.diagnostic: syslogSeverity = Severity.info; break;
|
||||||
|
case LogLevel.info: syslogSeverity = Severity.notice; break;
|
||||||
|
case LogLevel.warn: syslogSeverity = Severity.warning; break;
|
||||||
|
case LogLevel.error: syslogSeverity = Severity.error; break;
|
||||||
|
case LogLevel.critical: syslogSeverity = Severity.critical; break;
|
||||||
|
case LogLevel.fatal: syslogSeverity = Severity.alert; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(msg.level >= LogLevel.debug_);
|
||||||
|
import std.conv : to; // temporary workaround for issue 1016 (DMD cross-module template overloads error out before second attempted module)
|
||||||
|
auto priVal = m_facility * 8 + syslogSeverity;
|
||||||
|
|
||||||
|
alias procId = NILVALUE;
|
||||||
|
alias msgId = NILVALUE;
|
||||||
|
alias structuredData = NILVALUE;
|
||||||
|
|
||||||
|
auto text = msg.text;
|
||||||
|
import std.format : formattedWrite;
|
||||||
|
import vibe.stream.wrapper : StreamOutputRange;
|
||||||
|
auto str = StreamOutputRange(m_ostream);
|
||||||
|
(&str).formattedWrite(SYSLOG_MESSAGE_FORMAT_VERSION1, priVal,
|
||||||
|
timestamp, m_hostName, BOM ~ m_appName, procId, msgId,
|
||||||
|
structuredData, BOM);
|
||||||
|
}
|
||||||
|
|
||||||
|
override void put(scope const(char)[] text)
|
||||||
|
@trusted {
|
||||||
|
m_ostream.write(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
override void endLine()
|
||||||
|
@trusted {
|
||||||
|
m_ostream.write("\n");
|
||||||
|
m_ostream.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import vibe.core.file;
|
||||||
|
auto fstream = createTempFile();
|
||||||
|
auto logger = new SyslogLogger(fstream, Facility.local1, "appname", null);
|
||||||
|
LogLine msg;
|
||||||
|
import std.datetime;
|
||||||
|
import core.thread;
|
||||||
|
static if (is(typeof(SysTime.init.fracSecs))) auto fs = 1.dur!"usecs";
|
||||||
|
else auto fs = FracSec.from!"usecs"(1);
|
||||||
|
msg.time = SysTime(DateTime(0, 1, 1, 0, 0, 0), fs);
|
||||||
|
|
||||||
|
foreach (lvl; [LogLevel.debug_, LogLevel.diagnostic, LogLevel.info, LogLevel.warn, LogLevel.error, LogLevel.critical, LogLevel.fatal]) {
|
||||||
|
msg.level = lvl;
|
||||||
|
logger.beginLine(msg);
|
||||||
|
logger.put("αβγ");
|
||||||
|
logger.endLine();
|
||||||
|
}
|
||||||
|
fstream.close();
|
||||||
|
|
||||||
|
import std.file;
|
||||||
|
import std.string;
|
||||||
|
auto lines = splitLines(readText(fstream.path().toNativeString()), KeepTerminator.yes);
|
||||||
|
assert(lines.length == 7);
|
||||||
|
assert(lines[0] == "<143>1 0000-01-01T00:00:00.000001 - " ~ BOM ~ "appname - - - " ~ BOM ~ "αβγ\n");
|
||||||
|
assert(lines[1] == "<142>1 0000-01-01T00:00:00.000001 - " ~ BOM ~ "appname - - - " ~ BOM ~ "αβγ\n");
|
||||||
|
assert(lines[2] == "<141>1 0000-01-01T00:00:00.000001 - " ~ BOM ~ "appname - - - " ~ BOM ~ "αβγ\n");
|
||||||
|
assert(lines[3] == "<140>1 0000-01-01T00:00:00.000001 - " ~ BOM ~ "appname - - - " ~ BOM ~ "αβγ\n");
|
||||||
|
assert(lines[4] == "<139>1 0000-01-01T00:00:00.000001 - " ~ BOM ~ "appname - - - " ~ BOM ~ "αβγ\n");
|
||||||
|
assert(lines[5] == "<138>1 0000-01-01T00:00:00.000001 - " ~ BOM ~ "appname - - - " ~ BOM ~ "αβγ\n");
|
||||||
|
assert(lines[6] == "<137>1 0000-01-01T00:00:00.000001 - " ~ BOM ~ "appname - - - " ~ BOM ~ "αβγ\n");
|
||||||
|
removeFile(fstream.path().toNativeString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns: this host's host name.
|
||||||
|
///
|
||||||
|
/// If the host name cannot be determined the function returns null.
|
||||||
|
private string hostName()
|
||||||
|
{
|
||||||
|
string hostName;
|
||||||
|
|
||||||
|
version (Posix) {
|
||||||
|
import core.sys.posix.sys.utsname;
|
||||||
|
utsname name;
|
||||||
|
if (uname(&name)) return hostName;
|
||||||
|
hostName = name.nodename.to!string();
|
||||||
|
|
||||||
|
import std.socket;
|
||||||
|
auto ih = new InternetHost;
|
||||||
|
if (!ih.getHostByName(hostName)) return hostName;
|
||||||
|
hostName = ih.name;
|
||||||
|
}
|
||||||
|
// TODO: determine proper host name on windows
|
||||||
|
|
||||||
|
return hostName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private {
|
||||||
|
__gshared shared(Logger)[] ss_loggers;
|
||||||
|
shared(FileLogger) ss_stdoutLogger;
|
||||||
|
}
|
||||||
|
|
||||||
|
private shared(Logger)[] getLoggers() nothrow @trusted { return ss_loggers; }
|
||||||
|
|
||||||
|
package void initializeLogModule()
|
||||||
|
{
|
||||||
|
version (Windows) {
|
||||||
|
version (VibeWinrtDriver) enum disable_stdout = true;
|
||||||
|
else {
|
||||||
|
enum disable_stdout = false;
|
||||||
|
if (!GetStdHandle(STD_OUTPUT_HANDLE) || !GetStdHandle(STD_ERROR_HANDLE)) return;
|
||||||
|
}
|
||||||
|
} else enum disable_stdout = false;
|
||||||
|
|
||||||
|
static if (!disable_stdout) {
|
||||||
|
ss_stdoutLogger = cast(shared)new FileLogger(stdout, stderr);
|
||||||
|
{
|
||||||
|
auto l = ss_stdoutLogger.lock();
|
||||||
|
l.minLevel = LogLevel.info;
|
||||||
|
l.format = FileLogger.Format.plain;
|
||||||
|
}
|
||||||
|
registerLogger(ss_stdoutLogger);
|
||||||
|
|
||||||
|
bool[4] verbose;
|
||||||
|
version (VibeNoDefaultArgs) {}
|
||||||
|
else {
|
||||||
|
readOption("verbose|v" , &verbose[0], "Enables diagnostic messages (verbosity level 1).");
|
||||||
|
readOption("vverbose|vv", &verbose[1], "Enables debugging output (verbosity level 2).");
|
||||||
|
readOption("vvv" , &verbose[2], "Enables high frequency debugging output (verbosity level 3).");
|
||||||
|
readOption("vvvv" , &verbose[3], "Enables high frequency trace output (verbosity level 4).");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach_reverse (i, v; verbose)
|
||||||
|
if (v) {
|
||||||
|
setLogFormat(FileLogger.Format.thread);
|
||||||
|
setLogLevel(cast(LogLevel)(LogLevel.diagnostic - i));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct LogOutputRange {
|
||||||
|
LogLine info;
|
||||||
|
ScopedLock!Logger* logger;
|
||||||
|
|
||||||
|
@safe:
|
||||||
|
|
||||||
|
this(ref ScopedLock!Logger logger, string file, int line, LogLevel level)
|
||||||
|
{
|
||||||
|
() @trusted { this.logger = &logger; } ();
|
||||||
|
try {
|
||||||
|
() @trusted { this.info.time = Clock.currTime(UTC()); }(); // not @safe as of 2.065
|
||||||
|
//this.info.mod = mod;
|
||||||
|
//this.info.func = func;
|
||||||
|
this.info.file = file;
|
||||||
|
this.info.line = line;
|
||||||
|
this.info.level = level;
|
||||||
|
this.info.thread = () @trusted { return Thread.getThis(); }(); // not @safe as of 2.065
|
||||||
|
this.info.threadID = makeid(this.info.thread);
|
||||||
|
this.info.fiber = () @trusted { return Fiber.getThis(); }(); // not @safe as of 2.065
|
||||||
|
this.info.fiberID = makeid(this.info.fiber);
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
() @trusted { writefln("Error during logging: %s", e.toString()); }(); // not @safe as of 2.065
|
||||||
|
} catch(Exception) {}
|
||||||
|
assert(false, "Exception during logging: "~e.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.beginLine(info);
|
||||||
|
}
|
||||||
|
|
||||||
|
void finalize()
|
||||||
|
{
|
||||||
|
logger.endLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(scope const(char)[] text)
|
||||||
|
{
|
||||||
|
import std.string : indexOf;
|
||||||
|
auto idx = text.indexOf('\n');
|
||||||
|
if (idx >= 0) {
|
||||||
|
logger.put(text[0 .. idx]);
|
||||||
|
logger.endLine();
|
||||||
|
logger.beginLine(info);
|
||||||
|
logger.put(text[idx+1 .. $]);
|
||||||
|
} else logger.put(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(char ch) @trusted { put((&ch)[0 .. 1]); }
|
||||||
|
|
||||||
|
void put(dchar ch)
|
||||||
|
{
|
||||||
|
if (ch < 128) put(cast(char)ch);
|
||||||
|
else {
|
||||||
|
char[4] buf;
|
||||||
|
auto len = std.utf.encode(buf, ch);
|
||||||
|
put(buf[0 .. len]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private uint makeid(T)(T ptr) @trusted { return (cast(ulong)cast(void*)ptr & 0xFFFFFFFF) ^ (cast(ulong)cast(void*)ptr >> 32); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private version (Windows) {
|
||||||
|
import core.sys.windows.windows;
|
||||||
|
enum STD_OUTPUT_HANDLE = cast(DWORD)-11;
|
||||||
|
enum STD_ERROR_HANDLE = cast(DWORD)-12;
|
||||||
|
extern(System) HANDLE GetStdHandle(DWORD nStdHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest { // make sure the default logger doesn't allocate/is usable within finalizers
|
||||||
|
bool destroyed = false;
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
~this()
|
||||||
|
{
|
||||||
|
logInfo("logInfo doesn't allocate.");
|
||||||
|
destroyed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto t = new Test;
|
||||||
|
destroy(t);
|
||||||
|
assert(destroyed);
|
||||||
|
}
|
540
source/vibe/core/net.d
Normal file
540
source/vibe/core/net.d
Normal file
|
@ -0,0 +1,540 @@
|
||||||
|
/**
|
||||||
|
TCP/UDP connection and server handling.
|
||||||
|
|
||||||
|
Copyright: © 2012-2016 RejectedSoftware e.K.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
module vibe.core.net;
|
||||||
|
|
||||||
|
import eventcore.core;
|
||||||
|
import std.exception : enforce;
|
||||||
|
import std.format : format;
|
||||||
|
import std.functional : toDelegate;
|
||||||
|
import std.socket : AddressFamily, UnknownAddress;
|
||||||
|
import vibe.core.log;
|
||||||
|
import vibe.internal.async;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Resolves the given host name/IP address string.
|
||||||
|
|
||||||
|
Setting use_dns to false will only allow IP address strings but also guarantees
|
||||||
|
that the call will not block.
|
||||||
|
*/
|
||||||
|
NetworkAddress resolveHost(string host, AddressFamily address_family = AddressFamily.UNSPEC, bool use_dns = true)
|
||||||
|
{
|
||||||
|
return resolveHost(host, cast(ushort)address_family, use_dns);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
NetworkAddress resolveHost(string host, ushort address_family, bool use_dns = true)
|
||||||
|
{
|
||||||
|
NetworkAddress ret;
|
||||||
|
ret.family = address_family;
|
||||||
|
if (host == "127.0.0.1") {
|
||||||
|
ret.family = AddressFamily.INET;
|
||||||
|
ret.sockAddrInet4.sin_addr.s_addr = 0x0100007F;
|
||||||
|
} else assert(false);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Starts listening on the specified port.
|
||||||
|
|
||||||
|
'connection_callback' will be called for each client that connects to the
|
||||||
|
server socket. Each new connection gets its own fiber. The stream parameter
|
||||||
|
then allows to perform blocking I/O on the client socket.
|
||||||
|
|
||||||
|
The address parameter can be used to specify the network
|
||||||
|
interface on which the server socket is supposed to listen for connections.
|
||||||
|
By default, all IPv4 and IPv6 interfaces will be used.
|
||||||
|
*/
|
||||||
|
TCPListener[] listenTCP(ushort port, TCPConnectionDelegate connection_callback, TCPListenOptions options = TCPListenOptions.defaults)
|
||||||
|
{
|
||||||
|
TCPListener[] ret;
|
||||||
|
try ret ~= listenTCP(port, connection_callback, "::", options);
|
||||||
|
catch (Exception e) logDiagnostic("Failed to listen on \"::\": %s", e.msg);
|
||||||
|
try ret ~= listenTCP(port, connection_callback, "0.0.0.0", options);
|
||||||
|
catch (Exception e) logDiagnostic("Failed to listen on \"0.0.0.0\": %s", e.msg);
|
||||||
|
enforce(ret.length > 0, format("Failed to listen on all interfaces on port %s", port));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
TCPListener listenTCP(ushort port, TCPConnectionDelegate connection_callback, string address, TCPListenOptions options = TCPListenOptions.defaults)
|
||||||
|
{
|
||||||
|
auto addr = resolveHost(address);
|
||||||
|
addr.port = port;
|
||||||
|
auto sock = eventDriver.listenStream(addr.toUnknownAddress, (StreamListenSocketFD ls, StreamSocketFD s) @safe nothrow {
|
||||||
|
import vibe.core.core : runTask;
|
||||||
|
runTask(connection_callback, TCPConnection(s));
|
||||||
|
});
|
||||||
|
return TCPListener(sock);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Starts listening on the specified port.
|
||||||
|
|
||||||
|
This function is the same as listenTCP but takes a function callback instead of a delegate.
|
||||||
|
*/
|
||||||
|
TCPListener[] listenTCP_s(ushort port, TCPConnectionFunction connection_callback, TCPListenOptions options = TCPListenOptions.defaults)
|
||||||
|
{
|
||||||
|
return listenTCP(port, toDelegate(connection_callback), options);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
TCPListener listenTCP_s(ushort port, TCPConnectionFunction connection_callback, string address, TCPListenOptions options = TCPListenOptions.defaults)
|
||||||
|
{
|
||||||
|
return listenTCP(port, toDelegate(connection_callback), address, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Establishes a connection to the given host/port.
|
||||||
|
*/
|
||||||
|
TCPConnection connectTCP(string host, ushort port)
|
||||||
|
{
|
||||||
|
NetworkAddress addr = resolveHost(host);
|
||||||
|
addr.port = port;
|
||||||
|
return connectTCP(addr);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
TCPConnection connectTCP(NetworkAddress addr)
|
||||||
|
{
|
||||||
|
import std.conv : to;
|
||||||
|
|
||||||
|
scope uaddr = new UnknownAddress;
|
||||||
|
addr.toUnknownAddress(uaddr);
|
||||||
|
auto result = eventDriver.asyncAwait!"connectStream"(uaddr);
|
||||||
|
enforce(result[1] == ConnectStatus.connected, "Failed to connect to "~addr.toString()~": "~result[1].to!string);
|
||||||
|
return TCPConnection(result[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates a bound UDP socket suitable for sending and receiving packets.
|
||||||
|
*/
|
||||||
|
UDPConnection listenUDP(ushort port, string bind_address = "0.0.0.0")
|
||||||
|
{
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Callback invoked for incoming TCP connections.
|
||||||
|
@safe nothrow alias TCPConnectionDelegate = void delegate(TCPConnection stream);
|
||||||
|
/// ditto
|
||||||
|
@safe nothrow alias TCPConnectionFunction = void delegate(TCPConnection stream);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Represents a network/socket address.
|
||||||
|
*/
|
||||||
|
struct NetworkAddress {
|
||||||
|
version (Windows) import std.c.windows.winsock;
|
||||||
|
else import core.sys.posix.netinet.in_;
|
||||||
|
|
||||||
|
@safe:
|
||||||
|
|
||||||
|
private union {
|
||||||
|
sockaddr addr;
|
||||||
|
sockaddr_in addr_ip4;
|
||||||
|
sockaddr_in6 addr_ip6;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Family of the socket address.
|
||||||
|
*/
|
||||||
|
@property ushort family() const pure nothrow { return addr.sa_family; }
|
||||||
|
/// ditto
|
||||||
|
@property void family(AddressFamily val) pure nothrow { addr.sa_family = cast(ubyte)val; }
|
||||||
|
/// ditto
|
||||||
|
@property void family(ushort val) pure nothrow { addr.sa_family = cast(ubyte)val; }
|
||||||
|
|
||||||
|
/** The port in host byte order.
|
||||||
|
*/
|
||||||
|
@property ushort port()
|
||||||
|
const pure nothrow {
|
||||||
|
ushort nport;
|
||||||
|
switch (this.family) {
|
||||||
|
default: assert(false, "port() called for invalid address family.");
|
||||||
|
case AF_INET: nport = addr_ip4.sin_port; break;
|
||||||
|
case AF_INET6: nport = addr_ip6.sin6_port; break;
|
||||||
|
}
|
||||||
|
return () @trusted { return ntoh(nport); } ();
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
@property void port(ushort val)
|
||||||
|
pure nothrow {
|
||||||
|
auto nport = () @trusted { return hton(val); } ();
|
||||||
|
switch (this.family) {
|
||||||
|
default: assert(false, "port() called for invalid address family.");
|
||||||
|
case AF_INET: addr_ip4.sin_port = nport; break;
|
||||||
|
case AF_INET6: addr_ip6.sin6_port = nport; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A pointer to a sockaddr struct suitable for passing to socket functions.
|
||||||
|
*/
|
||||||
|
@property inout(sockaddr)* sockAddr() inout pure nothrow { return &addr; }
|
||||||
|
|
||||||
|
/** Size of the sockaddr struct that is returned by sockAddr().
|
||||||
|
*/
|
||||||
|
@property int sockAddrLen()
|
||||||
|
const pure nothrow {
|
||||||
|
switch (this.family) {
|
||||||
|
default: assert(false, "sockAddrLen() called for invalid address family.");
|
||||||
|
case AF_INET: return addr_ip4.sizeof;
|
||||||
|
case AF_INET6: return addr_ip6.sizeof;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@property inout(sockaddr_in)* sockAddrInet4() inout pure nothrow
|
||||||
|
in { assert (family == AF_INET); }
|
||||||
|
body { return &addr_ip4; }
|
||||||
|
|
||||||
|
@property inout(sockaddr_in6)* sockAddrInet6() inout pure nothrow
|
||||||
|
in { assert (family == AF_INET6); }
|
||||||
|
body { return &addr_ip6; }
|
||||||
|
|
||||||
|
/** Returns a string representation of the IP address
|
||||||
|
*/
|
||||||
|
string toAddressString()
|
||||||
|
const {
|
||||||
|
import std.array : appender;
|
||||||
|
import std.string : format;
|
||||||
|
import std.format : formattedWrite;
|
||||||
|
ubyte[2] _dummy = void; // Workaround for DMD regression in master
|
||||||
|
|
||||||
|
switch (this.family) {
|
||||||
|
default: assert(false, "toAddressString() called for invalid address family.");
|
||||||
|
case AF_INET:
|
||||||
|
ubyte[4] ip = () @trusted { return (cast(ubyte*)&addr_ip4.sin_addr.s_addr)[0 .. 4]; } ();
|
||||||
|
return format("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
||||||
|
case AF_INET6:
|
||||||
|
ubyte[16] ip = addr_ip6.sin6_addr.s6_addr;
|
||||||
|
auto ret = appender!string();
|
||||||
|
ret.reserve(40);
|
||||||
|
foreach (i; 0 .. 8) {
|
||||||
|
if (i > 0) ret.put(':');
|
||||||
|
_dummy[] = ip[i*2 .. i*2+2];
|
||||||
|
ret.formattedWrite("%x", bigEndianToNative!ushort(_dummy));
|
||||||
|
}
|
||||||
|
return ret.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns a full string representation of the address, including the port number.
|
||||||
|
*/
|
||||||
|
string toString()
|
||||||
|
const {
|
||||||
|
auto ret = toAddressString();
|
||||||
|
switch (this.family) {
|
||||||
|
default: assert(false, "toString() called for invalid address family.");
|
||||||
|
case AF_INET: return ret ~ format(":%s", port);
|
||||||
|
case AF_INET6: return format("[%s]:%s", ret, port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UnknownAddress toUnknownAddress()
|
||||||
|
const {
|
||||||
|
auto ret = new UnknownAddress;
|
||||||
|
toUnknownAddress(ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void toUnknownAddress(scope UnknownAddress addr)
|
||||||
|
const {
|
||||||
|
*addr.name = *this.sockAddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
version(Have_libev) {}
|
||||||
|
else {
|
||||||
|
unittest {
|
||||||
|
void test(string ip) {
|
||||||
|
auto res = () @trusted { return resolveHost(ip, AF_UNSPEC, false); } ().toAddressString();
|
||||||
|
assert(res == ip,
|
||||||
|
"IP "~ip~" yielded wrong string representation: "~res);
|
||||||
|
}
|
||||||
|
test("1.2.3.4");
|
||||||
|
test("102:304:506:708:90a:b0c:d0e:f10");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Represents a single TCP connection.
|
||||||
|
*/
|
||||||
|
struct TCPConnection {
|
||||||
|
@safe:
|
||||||
|
|
||||||
|
import core.time : seconds;
|
||||||
|
import vibe.internal.array : FixedRingBuffer;
|
||||||
|
//static assert(isConnectionStream!TCPConnection);
|
||||||
|
|
||||||
|
struct Context {
|
||||||
|
FixedRingBuffer!ubyte readBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private {
|
||||||
|
StreamSocketFD m_socket;
|
||||||
|
Context* m_context;
|
||||||
|
}
|
||||||
|
|
||||||
|
private this(StreamSocketFD socket)
|
||||||
|
nothrow {
|
||||||
|
m_socket = socket;
|
||||||
|
m_context = &eventDriver.userData!Context(socket);
|
||||||
|
m_context.readBuffer.capacity = 4096;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(this)
|
||||||
|
nothrow {
|
||||||
|
if (m_socket != StreamSocketFD.invalid)
|
||||||
|
eventDriver.addRef(m_socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
~this()
|
||||||
|
nothrow {
|
||||||
|
if (m_socket != StreamSocketFD.invalid)
|
||||||
|
eventDriver.releaseRef(m_socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
@property void tcpNoDelay(bool enabled) { eventDriver.setTCPNoDelay(m_socket, enabled); }
|
||||||
|
@property bool tcpNoDelay() const { assert(false); }
|
||||||
|
@property void keepAlive(bool enable) { assert(false); }
|
||||||
|
@property bool keepAlive() const { assert(false); }
|
||||||
|
@property void readTimeout(Duration duration) { }
|
||||||
|
@property Duration readTimeout() const { assert(false); }
|
||||||
|
@property string peerAddress() const { return ""; }
|
||||||
|
@property NetworkAddress localAddress() const { return NetworkAddress.init; }
|
||||||
|
@property NetworkAddress remoteAddress() const { return NetworkAddress.init; }
|
||||||
|
@property bool connected()
|
||||||
|
const {
|
||||||
|
if (m_socket == StreamSocketFD.invalid) return false;
|
||||||
|
auto s = eventDriver.getConnectionState(m_socket);
|
||||||
|
return s >= ConnectionState.connected && s < ConnectionState.activeClose;
|
||||||
|
}
|
||||||
|
@property bool empty() { return leastSize == 0; }
|
||||||
|
@property ulong leastSize() { waitForData(); return m_context.readBuffer.length; }
|
||||||
|
@property bool dataAvailableForRead() { return waitForData(0.seconds); }
|
||||||
|
|
||||||
|
void close()
|
||||||
|
nothrow {
|
||||||
|
//logInfo("close %s", cast(int)m_fd);
|
||||||
|
if (m_socket != StreamSocketFD.invalid) {
|
||||||
|
eventDriver.shutdownSocket(m_socket);
|
||||||
|
eventDriver.releaseRef(m_socket);
|
||||||
|
m_socket = StreamSocketFD.invalid;
|
||||||
|
m_context = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool waitForData(Duration timeout = Duration.max)
|
||||||
|
{
|
||||||
|
mixin(tracer);
|
||||||
|
// TODO: timeout!!
|
||||||
|
if (m_context.readBuffer.length > 0) return true;
|
||||||
|
auto mode = timeout <= 0.seconds ? IOMode.immediate : IOMode.once;
|
||||||
|
auto res = eventDriver.asyncAwait!"readSocket"(m_socket, m_context.readBuffer.peekDst(), mode);
|
||||||
|
logTrace("Socket %s, read %s bytes: %s", res[0], res[2], res[1]);
|
||||||
|
|
||||||
|
assert(m_context.readBuffer.length == 0);
|
||||||
|
m_context.readBuffer.putN(res[2]);
|
||||||
|
switch (res[1]) {
|
||||||
|
default:
|
||||||
|
logInfo("read status %s", res[1]);
|
||||||
|
throw new Exception("Error reading data from socket.");
|
||||||
|
case IOStatus.ok: break;
|
||||||
|
case IOStatus.wouldBlock: assert(mode == IOMode.immediate); break;
|
||||||
|
case IOStatus.disconnected: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_context.readBuffer.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const(ubyte)[] peek() { return m_context.readBuffer.peek(); }
|
||||||
|
|
||||||
|
void skip(ulong count)
|
||||||
|
{
|
||||||
|
import std.algorithm.comparison : min;
|
||||||
|
|
||||||
|
while (count > 0) {
|
||||||
|
waitForData();
|
||||||
|
auto n = min(count, m_context.readBuffer.length);
|
||||||
|
m_context.readBuffer.popFrontN(n);
|
||||||
|
if (m_context.readBuffer.empty) m_context.readBuffer.clear(); // start filling at index 0 again
|
||||||
|
count -= n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void read(ubyte[] dst)
|
||||||
|
{
|
||||||
|
mixin(tracer);
|
||||||
|
import std.algorithm.comparison : min;
|
||||||
|
while (dst.length > 0) {
|
||||||
|
enforce(waitForData(), "Reached end of stream while reading data.");
|
||||||
|
assert(m_context.readBuffer.length > 0);
|
||||||
|
auto l = min(dst.length, m_context.readBuffer.length);
|
||||||
|
m_context.readBuffer.read(dst[0 .. l]);
|
||||||
|
if (m_context.readBuffer.empty) m_context.readBuffer.clear(); // start filling at index 0 again
|
||||||
|
dst = dst[l .. $];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(in ubyte[] bytes)
|
||||||
|
{
|
||||||
|
mixin(tracer);
|
||||||
|
if (bytes.length == 0) return;
|
||||||
|
|
||||||
|
auto res = eventDriver.asyncAwait!"writeSocket"(m_socket, bytes, IOMode.all);
|
||||||
|
|
||||||
|
switch (res[1]) {
|
||||||
|
default:
|
||||||
|
throw new Exception("Error writing data to socket.");
|
||||||
|
case IOStatus.ok: break;
|
||||||
|
case IOStatus.disconnected: break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void flush() {
|
||||||
|
mixin(tracer);
|
||||||
|
}
|
||||||
|
void finalize() {}
|
||||||
|
void write(InputStream)(InputStream stream, ulong nbytes = 0) { writeDefault(stream, nbytes); }
|
||||||
|
|
||||||
|
private void writeDefault(InputStream)(InputStream stream, ulong nbytes = 0)
|
||||||
|
{
|
||||||
|
import std.algorithm.comparison : min;
|
||||||
|
|
||||||
|
static struct Buffer { ubyte[64*1024 - 4*size_t.sizeof] bytes = void; }
|
||||||
|
scope bufferobj = new Buffer; // FIXME: use heap allocation
|
||||||
|
auto buffer = bufferobj.bytes[];
|
||||||
|
|
||||||
|
//logTrace("default write %d bytes, empty=%s", nbytes, stream.empty);
|
||||||
|
if( nbytes == 0 ){
|
||||||
|
while( !stream.empty ){
|
||||||
|
size_t chunk = min(stream.leastSize, buffer.length);
|
||||||
|
assert(chunk > 0, "leastSize returned zero for non-empty stream.");
|
||||||
|
//logTrace("read pipe chunk %d", chunk);
|
||||||
|
stream.read(buffer[0 .. chunk]);
|
||||||
|
write(buffer[0 .. chunk]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while( nbytes > 0 ){
|
||||||
|
size_t chunk = min(nbytes, buffer.length);
|
||||||
|
//logTrace("read pipe chunk %d", chunk);
|
||||||
|
stream.read(buffer[0 .. chunk]);
|
||||||
|
write(buffer[0 .. chunk]);
|
||||||
|
nbytes -= chunk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Represents a listening TCP socket.
|
||||||
|
*/
|
||||||
|
struct TCPListener {
|
||||||
|
private {
|
||||||
|
StreamListenSocketFD m_socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(StreamListenSocketFD socket)
|
||||||
|
{
|
||||||
|
m_socket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The local address at which TCP connections are accepted.
|
||||||
|
@property NetworkAddress bindAddress()
|
||||||
|
{
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stops listening and closes the socket.
|
||||||
|
void stopListening()
|
||||||
|
{
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Represents a bound and possibly 'connected' UDP socket.
|
||||||
|
*/
|
||||||
|
struct UDPConnection {
|
||||||
|
/** Returns the address to which the UDP socket is bound.
|
||||||
|
*/
|
||||||
|
@property string bindAddress() const { assert(false); }
|
||||||
|
|
||||||
|
/** Determines if the socket is allowed to send to broadcast addresses.
|
||||||
|
*/
|
||||||
|
@property bool canBroadcast() const { assert(false); }
|
||||||
|
/// ditto
|
||||||
|
@property void canBroadcast(bool val) { assert(false); }
|
||||||
|
|
||||||
|
/// The local/bind address of the underlying socket.
|
||||||
|
@property NetworkAddress localAddress() const { assert(false); }
|
||||||
|
|
||||||
|
/** Stops listening for datagrams and frees all resources.
|
||||||
|
*/
|
||||||
|
void close() { assert(false); }
|
||||||
|
|
||||||
|
/** Locks the UDP connection to a certain peer.
|
||||||
|
|
||||||
|
Once connected, the UDPConnection can only communicate with the specified peer.
|
||||||
|
Otherwise communication with any reachable peer is possible.
|
||||||
|
*/
|
||||||
|
void connect(string host, ushort port) { assert(false); }
|
||||||
|
/// ditto
|
||||||
|
void connect(NetworkAddress address) { assert(false); }
|
||||||
|
|
||||||
|
/** Sends a single packet.
|
||||||
|
|
||||||
|
If peer_address is given, the packet is send to that address. Otherwise the packet
|
||||||
|
will be sent to the address specified by a call to connect().
|
||||||
|
*/
|
||||||
|
void send(in ubyte[] data, in NetworkAddress* peer_address = null) { assert(false); }
|
||||||
|
|
||||||
|
/** Receives a single packet.
|
||||||
|
|
||||||
|
If a buffer is given, it must be large enough to hold the full packet.
|
||||||
|
|
||||||
|
The timeout overload will throw an Exception if no data arrives before the
|
||||||
|
specified duration has elapsed.
|
||||||
|
*/
|
||||||
|
ubyte[] recv(ubyte[] buf = null, NetworkAddress* peer_address = null) { assert(false); }
|
||||||
|
/// ditto
|
||||||
|
ubyte[] recv(Duration timeout, ubyte[] buf = null, NetworkAddress* peer_address = null) { assert(false); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Flags to control the behavior of listenTCP.
|
||||||
|
*/
|
||||||
|
enum TCPListenOptions {
|
||||||
|
/// Don't enable any particular option
|
||||||
|
defaults = 0,
|
||||||
|
/// Causes incoming connections to be distributed across the thread pool
|
||||||
|
distribute = 1<<0,
|
||||||
|
/// Disables automatic closing of the connection when the connection callback exits
|
||||||
|
disableAutoClose = 1<<1,
|
||||||
|
}
|
||||||
|
|
||||||
|
private pure nothrow {
|
||||||
|
import std.bitmanip;
|
||||||
|
|
||||||
|
ushort ntoh(ushort val)
|
||||||
|
{
|
||||||
|
version (LittleEndian) return swapEndian(val);
|
||||||
|
else version (BigEndian) return val;
|
||||||
|
else static assert(false, "Unknown endianness.");
|
||||||
|
}
|
||||||
|
|
||||||
|
ushort hton(ushort val)
|
||||||
|
{
|
||||||
|
version (LittleEndian) return swapEndian(val);
|
||||||
|
else version (BigEndian) return val;
|
||||||
|
else static assert(false, "Unknown endianness.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum tracer = "";
|
25
source/vibe/core/path.d
Normal file
25
source/vibe/core/path.d
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
module vibe.core.path;
|
||||||
|
|
||||||
|
struct Path {
|
||||||
|
nothrow: @safe:
|
||||||
|
private string m_path;
|
||||||
|
|
||||||
|
this(string p)
|
||||||
|
{
|
||||||
|
m_path = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
string toString() const { return m_path; }
|
||||||
|
|
||||||
|
string toNativeString() const { return m_path; }
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PathEntry {
|
||||||
|
nothrow: @safe:
|
||||||
|
private string m_name;
|
||||||
|
|
||||||
|
this(string name)
|
||||||
|
{
|
||||||
|
m_name = name;
|
||||||
|
}
|
||||||
|
}
|
1346
source/vibe/core/sync.d
Normal file
1346
source/vibe/core/sync.d
Normal file
File diff suppressed because it is too large
Load diff
153
source/vibe/core/task.d
Normal file
153
source/vibe/core/task.d
Normal file
|
@ -0,0 +1,153 @@
|
||||||
|
/**
|
||||||
|
Contains interfaces and enums for evented I/O drivers.
|
||||||
|
|
||||||
|
Copyright: © 2012-2016 RejectedSoftware e.K.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
module vibe.core.task;
|
||||||
|
|
||||||
|
import vibe.core.sync;
|
||||||
|
import vibe.internal.array : FixedRingBuffer;
|
||||||
|
|
||||||
|
import core.thread;
|
||||||
|
import std.exception;
|
||||||
|
import std.traits;
|
||||||
|
import std.typecons;
|
||||||
|
import std.variant;
|
||||||
|
|
||||||
|
|
||||||
|
/** Represents a single task as started using vibe.core.runTask.
|
||||||
|
|
||||||
|
Note that the Task type is considered weakly isolated and thus can be
|
||||||
|
passed between threads using vibe.core.concurrency.send or by passing
|
||||||
|
it as a parameter to vibe.core.core.runWorkerTask.
|
||||||
|
*/
|
||||||
|
struct Task {
|
||||||
|
private {
|
||||||
|
shared(TaskFiber) m_fiber;
|
||||||
|
size_t m_taskCounter;
|
||||||
|
import std.concurrency : ThreadInfo, Tid;
|
||||||
|
static ThreadInfo s_tidInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private this(TaskFiber fiber, size_t task_counter)
|
||||||
|
@safe nothrow {
|
||||||
|
() @trusted { m_fiber = cast(shared)fiber; } ();
|
||||||
|
m_taskCounter = task_counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(in Task other) nothrow { m_fiber = cast(shared(TaskFiber))other.m_fiber; m_taskCounter = other.m_taskCounter; }
|
||||||
|
|
||||||
|
/** Returns the Task instance belonging to the calling task.
|
||||||
|
*/
|
||||||
|
static Task getThis() nothrow @safe
|
||||||
|
{
|
||||||
|
// In 2067, synchronized statements where annotated nothrow.
|
||||||
|
// DMD#4115, Druntime#1013, Druntime#1021, Phobos#2704
|
||||||
|
// However, they were "logically" nothrow before.
|
||||||
|
static if (__VERSION__ <= 2066)
|
||||||
|
scope (failure) assert(0, "Internal error: function should be nothrow");
|
||||||
|
|
||||||
|
auto fiber = () @trusted { return Fiber.getThis(); } ();
|
||||||
|
if (!fiber) return Task.init;
|
||||||
|
auto tfiber = cast(TaskFiber)fiber;
|
||||||
|
assert(tfiber !is null, "Invalid or null fiber used to construct Task handle.");
|
||||||
|
if (!tfiber.m_running) return Task.init;
|
||||||
|
return () @trusted { return Task(tfiber, tfiber.m_taskCounter); } ();
|
||||||
|
}
|
||||||
|
|
||||||
|
nothrow {
|
||||||
|
@property inout(TaskFiber) fiber() inout @trusted { return cast(inout(TaskFiber))m_fiber; }
|
||||||
|
@property size_t taskCounter() const @safe { return m_taskCounter; }
|
||||||
|
@property inout(Thread) thread() inout @safe { if (m_fiber) return this.fiber.thread; return null; }
|
||||||
|
|
||||||
|
/** Determines if the task is still running.
|
||||||
|
*/
|
||||||
|
@property bool running()
|
||||||
|
const @trusted {
|
||||||
|
assert(m_fiber !is null, "Invalid task handle");
|
||||||
|
try if (this.fiber.state == Fiber.State.TERM) return false; catch (Throwable) {}
|
||||||
|
return this.fiber.m_running && this.fiber.m_taskCounter == m_taskCounter;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: this is not thread safe!
|
||||||
|
@property ref ThreadInfo tidInfo() { return m_fiber ? fiber.tidInfo : s_tidInfo; }
|
||||||
|
@property Tid tid() { return tidInfo.ident; }
|
||||||
|
}
|
||||||
|
|
||||||
|
T opCast(T)() const nothrow if (is(T == bool)) { return m_fiber !is null; }
|
||||||
|
|
||||||
|
void join() { if (running) fiber.join(); }
|
||||||
|
void interrupt() { if (running) fiber.interrupt(); }
|
||||||
|
void terminate() { if (running) fiber.terminate(); }
|
||||||
|
|
||||||
|
string toString() const { import std.string; return format("%s:%s", cast(void*)m_fiber, m_taskCounter); }
|
||||||
|
|
||||||
|
bool opEquals(in ref Task other) const nothrow @safe { return m_fiber is other.m_fiber && m_taskCounter == other.m_taskCounter; }
|
||||||
|
bool opEquals(in Task other) const nothrow @safe { return m_fiber is other.m_fiber && m_taskCounter == other.m_taskCounter; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** The base class for a task aka Fiber.
|
||||||
|
|
||||||
|
This class represents a single task that is executed concurrently
|
||||||
|
with other tasks. Each task is owned by a single thread.
|
||||||
|
*/
|
||||||
|
class TaskFiber : Fiber {
|
||||||
|
private {
|
||||||
|
Thread m_thread;
|
||||||
|
import std.concurrency : ThreadInfo;
|
||||||
|
ThreadInfo m_tidInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected {
|
||||||
|
shared size_t m_taskCounter;
|
||||||
|
shared bool m_running;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected this(void delegate() fun, size_t stack_size)
|
||||||
|
nothrow {
|
||||||
|
super(fun, stack_size);
|
||||||
|
m_thread = Thread.getThis();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the thread that owns this task.
|
||||||
|
*/
|
||||||
|
@property inout(Thread) thread() inout @safe nothrow { return m_thread; }
|
||||||
|
|
||||||
|
/** Returns the handle of the current Task running on this fiber.
|
||||||
|
*/
|
||||||
|
@property Task task() @safe nothrow { return Task(this, m_taskCounter); }
|
||||||
|
|
||||||
|
@property ref inout(ThreadInfo) tidInfo() inout nothrow { return m_tidInfo; }
|
||||||
|
|
||||||
|
/** Blocks until the task has ended.
|
||||||
|
*/
|
||||||
|
abstract void join();
|
||||||
|
|
||||||
|
/** Throws an InterruptExeption within the task as soon as it calls a blocking function.
|
||||||
|
*/
|
||||||
|
abstract void interrupt();
|
||||||
|
|
||||||
|
/** Terminates the task without notice as soon as it calls a blocking function.
|
||||||
|
*/
|
||||||
|
abstract void terminate();
|
||||||
|
|
||||||
|
void bumpTaskCounter()
|
||||||
|
@safe nothrow {
|
||||||
|
import core.atomic : atomicOp;
|
||||||
|
() @trusted { atomicOp!"+="(this.m_taskCounter, 1); } ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Exception that is thrown by Task.interrupt.
|
||||||
|
*/
|
||||||
|
class InterruptException : Exception {
|
||||||
|
this()
|
||||||
|
{
|
||||||
|
super("Task interrupted.");
|
||||||
|
}
|
||||||
|
}
|
634
source/vibe/internal/array.d
Normal file
634
source/vibe/internal/array.d
Normal file
|
@ -0,0 +1,634 @@
|
||||||
|
/**
|
||||||
|
Utility functions for array processing
|
||||||
|
|
||||||
|
Copyright: © 2012 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
*/
|
||||||
|
module vibe.internal.array;
|
||||||
|
|
||||||
|
import vibe.internal.memory;
|
||||||
|
|
||||||
|
import std.algorithm;
|
||||||
|
import std.range : isInputRange, isOutputRange;
|
||||||
|
import std.traits;
|
||||||
|
static import std.utf;
|
||||||
|
|
||||||
|
|
||||||
|
void removeFromArray(T)(ref T[] array, T item)
|
||||||
|
{
|
||||||
|
foreach( i; 0 .. array.length )
|
||||||
|
if( array[i] is item ){
|
||||||
|
removeFromArrayIdx(array, i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeFromArrayIdx(T)(ref T[] array, size_t idx)
|
||||||
|
{
|
||||||
|
foreach( j; idx+1 .. array.length)
|
||||||
|
array[j-1] = array[j];
|
||||||
|
array.length = array.length-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum AppenderResetMode {
|
||||||
|
keepData,
|
||||||
|
freeData,
|
||||||
|
reuseData
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AllocAppender(ArrayType : E[], E) {
|
||||||
|
alias ElemType = Unqual!E;
|
||||||
|
|
||||||
|
static assert(!hasIndirections!E && !hasElaborateDestructor!E);
|
||||||
|
|
||||||
|
private {
|
||||||
|
ElemType[] m_data;
|
||||||
|
ElemType[] m_remaining;
|
||||||
|
Allocator m_alloc;
|
||||||
|
bool m_allocatedBuffer = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(Allocator alloc, ElemType[] initial_buffer = null)
|
||||||
|
{
|
||||||
|
m_alloc = alloc;
|
||||||
|
m_data = initial_buffer;
|
||||||
|
m_remaining = initial_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@disable this(this);
|
||||||
|
|
||||||
|
@property ArrayType data() { return cast(ArrayType)m_data[0 .. m_data.length - m_remaining.length]; }
|
||||||
|
|
||||||
|
void reset(AppenderResetMode reset_mode = AppenderResetMode.keepData)
|
||||||
|
{
|
||||||
|
if (reset_mode == AppenderResetMode.keepData) m_data = null;
|
||||||
|
else if (reset_mode == AppenderResetMode.freeData) { if (m_allocatedBuffer) m_alloc.free(m_data); m_data = null; }
|
||||||
|
m_remaining = m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Grows the capacity of the internal buffer so that it can hold a minumum amount of elements.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
amount = The minimum amount of elements that shall be appendable without
|
||||||
|
triggering a re-allocation.
|
||||||
|
|
||||||
|
*/
|
||||||
|
void reserve(size_t amount)
|
||||||
|
{
|
||||||
|
size_t nelems = m_data.length - m_remaining.length;
|
||||||
|
if (!m_data.length) {
|
||||||
|
m_data = cast(ElemType[])m_alloc.alloc(amount*E.sizeof);
|
||||||
|
m_remaining = m_data;
|
||||||
|
m_allocatedBuffer = true;
|
||||||
|
}
|
||||||
|
if (m_remaining.length < amount) {
|
||||||
|
debug {
|
||||||
|
import std.digest.crc;
|
||||||
|
auto checksum = crc32Of(m_data[0 .. nelems]);
|
||||||
|
}
|
||||||
|
if (m_allocatedBuffer) m_data = cast(ElemType[])m_alloc.realloc(m_data, (nelems+amount)*E.sizeof);
|
||||||
|
else {
|
||||||
|
auto newdata = cast(ElemType[])m_alloc.alloc((nelems+amount)*E.sizeof);
|
||||||
|
newdata[0 .. nelems] = m_data[0 .. nelems];
|
||||||
|
m_data = newdata;
|
||||||
|
m_allocatedBuffer = true;
|
||||||
|
}
|
||||||
|
debug assert(crc32Of(m_data[0 .. nelems]) == checksum);
|
||||||
|
}
|
||||||
|
m_remaining = m_data[nelems .. m_data.length];
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(E el)
|
||||||
|
{
|
||||||
|
if( m_remaining.length == 0 ) grow(1);
|
||||||
|
m_remaining[0] = el;
|
||||||
|
m_remaining = m_remaining[1 .. $];
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(ArrayType arr)
|
||||||
|
{
|
||||||
|
if (m_remaining.length < arr.length) grow(arr.length);
|
||||||
|
m_remaining[0 .. arr.length] = arr[];
|
||||||
|
m_remaining = m_remaining[arr.length .. $];
|
||||||
|
}
|
||||||
|
|
||||||
|
static if( !hasAliasing!E ){
|
||||||
|
void put(in ElemType[] arr){
|
||||||
|
put(cast(ArrayType)arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static if( is(ElemType == char) ){
|
||||||
|
void put(dchar el)
|
||||||
|
{
|
||||||
|
if( el < 128 ) put(cast(char)el);
|
||||||
|
else {
|
||||||
|
char[4] buf;
|
||||||
|
auto len = std.utf.encode(buf, el);
|
||||||
|
put(cast(ArrayType)buf[0 .. len]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static if( is(ElemType == wchar) ){
|
||||||
|
void put(dchar el)
|
||||||
|
{
|
||||||
|
if( el < 128 ) put(cast(wchar)el);
|
||||||
|
else {
|
||||||
|
wchar[3] buf;
|
||||||
|
auto len = std.utf.encode(buf, el);
|
||||||
|
put(cast(ArrayType)buf[0 .. len]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (!is(E == immutable) || !hasAliasing!E) {
|
||||||
|
/** Appends a number of bytes in-place.
|
||||||
|
|
||||||
|
The delegate will get the memory slice of the memory that follows
|
||||||
|
the already written data. Use `reserve` to ensure that this slice
|
||||||
|
has enough room. The delegate should overwrite as much of the
|
||||||
|
slice as desired and then has to return the number of elements
|
||||||
|
that should be appended (counting from the start of the slice).
|
||||||
|
*/
|
||||||
|
void append(scope size_t delegate(scope ElemType[] dst) del)
|
||||||
|
{
|
||||||
|
auto n = del(m_remaining);
|
||||||
|
assert(n <= m_remaining.length);
|
||||||
|
m_remaining = m_remaining[n .. $];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void grow(size_t min_free)
|
||||||
|
{
|
||||||
|
if( !m_data.length && min_free < 16 ) min_free = 16;
|
||||||
|
|
||||||
|
auto min_size = m_data.length + min_free - m_remaining.length;
|
||||||
|
auto new_size = max(m_data.length, 16);
|
||||||
|
while( new_size < min_size )
|
||||||
|
new_size = (new_size * 3) / 2;
|
||||||
|
reserve(new_size - m_data.length + m_remaining.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
auto a = AllocAppender!string(defaultAllocator());
|
||||||
|
a.put("Hello");
|
||||||
|
a.put(' ');
|
||||||
|
a.put("World");
|
||||||
|
assert(a.data == "Hello World");
|
||||||
|
a.reset();
|
||||||
|
assert(a.data == "");
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
char[4] buf;
|
||||||
|
auto a = AllocAppender!string(defaultAllocator(), buf);
|
||||||
|
a.put("He");
|
||||||
|
assert(a.data == "He");
|
||||||
|
assert(a.data.ptr == buf.ptr);
|
||||||
|
a.put("ll");
|
||||||
|
assert(a.data == "Hell");
|
||||||
|
assert(a.data.ptr == buf.ptr);
|
||||||
|
a.put('o');
|
||||||
|
assert(a.data == "Hello");
|
||||||
|
assert(a.data.ptr != buf.ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
char[4] buf;
|
||||||
|
auto a = AllocAppender!string(defaultAllocator(), buf);
|
||||||
|
a.put("Hello");
|
||||||
|
assert(a.data == "Hello");
|
||||||
|
assert(a.data.ptr != buf.ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
auto app = AllocAppender!(int[])(defaultAllocator);
|
||||||
|
app.reserve(2);
|
||||||
|
app.append((scope mem) {
|
||||||
|
assert(mem.length >= 2);
|
||||||
|
mem[0] = 1;
|
||||||
|
mem[1] = 2;
|
||||||
|
return 2;
|
||||||
|
});
|
||||||
|
assert(app.data == [1, 2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
auto app = AllocAppender!string(defaultAllocator);
|
||||||
|
app.reserve(3);
|
||||||
|
app.append((scope mem) {
|
||||||
|
assert(mem.length >= 3);
|
||||||
|
mem[0] = 'f';
|
||||||
|
mem[1] = 'o';
|
||||||
|
mem[2] = 'o';
|
||||||
|
return 3;
|
||||||
|
});
|
||||||
|
assert(app.data == "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct FixedAppender(ArrayType : E[], size_t NELEM, E) {
|
||||||
|
alias ElemType = Unqual!E;
|
||||||
|
private {
|
||||||
|
ElemType[NELEM] m_data;
|
||||||
|
size_t m_fill;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
m_fill = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(E el)
|
||||||
|
{
|
||||||
|
m_data[m_fill++] = el;
|
||||||
|
}
|
||||||
|
|
||||||
|
static if( is(ElemType == char) ){
|
||||||
|
void put(dchar el)
|
||||||
|
{
|
||||||
|
if( el < 128 ) put(cast(char)el);
|
||||||
|
else {
|
||||||
|
char[4] buf;
|
||||||
|
auto len = std.utf.encode(buf, el);
|
||||||
|
put(cast(ArrayType)buf[0 .. len]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static if( is(ElemType == wchar) ){
|
||||||
|
void put(dchar el)
|
||||||
|
{
|
||||||
|
if( el < 128 ) put(cast(wchar)el);
|
||||||
|
else {
|
||||||
|
wchar[3] buf;
|
||||||
|
auto len = std.utf.encode(buf, el);
|
||||||
|
put(cast(ArrayType)buf[0 .. len]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(ArrayType arr)
|
||||||
|
{
|
||||||
|
m_data[m_fill .. m_fill+arr.length] = (cast(ElemType[])arr)[];
|
||||||
|
m_fill += arr.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property ArrayType data() { return cast(ArrayType)m_data[0 .. m_fill]; }
|
||||||
|
|
||||||
|
static if (!is(E == immutable)) {
|
||||||
|
void reset() { m_fill = 0; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
TODO: clear ring buffer fields upon removal (to run struct destructors, if T is a struct)
|
||||||
|
*/
|
||||||
|
struct FixedRingBuffer(T, size_t N = 0, bool INITIALIZE = true) {
|
||||||
|
private {
|
||||||
|
static if( N > 0 ) {
|
||||||
|
static if (INITIALIZE) T[N] m_buffer;
|
||||||
|
else T[N] m_buffer = void;
|
||||||
|
} else T[] m_buffer;
|
||||||
|
size_t m_start = 0;
|
||||||
|
size_t m_fill = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static if( N == 0 ){
|
||||||
|
bool m_freeOnDestruct;
|
||||||
|
this(size_t capacity) { m_buffer = new T[capacity]; }
|
||||||
|
~this() { if (m_freeOnDestruct && m_buffer.length > 0) delete m_buffer; }
|
||||||
|
}
|
||||||
|
|
||||||
|
@property bool empty() const { return m_fill == 0; }
|
||||||
|
|
||||||
|
@property bool full() const { return m_fill == m_buffer.length; }
|
||||||
|
|
||||||
|
@property size_t length() const { return m_fill; }
|
||||||
|
|
||||||
|
@property size_t freeSpace() const { return m_buffer.length - m_fill; }
|
||||||
|
|
||||||
|
@property size_t capacity() const { return m_buffer.length; }
|
||||||
|
|
||||||
|
static if( N == 0 ){
|
||||||
|
deprecated @property void freeOnDestruct(bool b) { m_freeOnDestruct = b; }
|
||||||
|
|
||||||
|
/// Resets the capacity to zero and explicitly frees the memory for the buffer.
|
||||||
|
void dispose()
|
||||||
|
{
|
||||||
|
delete m_buffer;
|
||||||
|
m_buffer = null;
|
||||||
|
m_start = m_fill = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property void capacity(size_t new_size)
|
||||||
|
{
|
||||||
|
if( m_buffer.length ){
|
||||||
|
auto newbuffer = new T[new_size];
|
||||||
|
auto dst = newbuffer;
|
||||||
|
auto newfill = min(m_fill, new_size);
|
||||||
|
read(dst[0 .. newfill]);
|
||||||
|
if (m_freeOnDestruct && m_buffer.length > 0) delete m_buffer;
|
||||||
|
m_buffer = newbuffer;
|
||||||
|
m_start = 0;
|
||||||
|
m_fill = newfill;
|
||||||
|
} else {
|
||||||
|
if (m_freeOnDestruct && m_buffer.length > 0) delete m_buffer;
|
||||||
|
m_buffer = new T[new_size];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@property ref inout(T) front() inout { assert(!empty); return m_buffer[m_start]; }
|
||||||
|
|
||||||
|
@property ref inout(T) back() inout { assert(!empty); return m_buffer[mod(m_start+m_fill-1)]; }
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
popFrontN(length);
|
||||||
|
assert(m_fill == 0);
|
||||||
|
m_start = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void put()(T itm) { assert(m_fill < m_buffer.length); m_buffer[mod(m_start + m_fill++)] = itm; }
|
||||||
|
void put(TC : T)(TC[] itms)
|
||||||
|
{
|
||||||
|
if( !itms.length ) return;
|
||||||
|
assert(m_fill+itms.length <= m_buffer.length);
|
||||||
|
if( mod(m_start+m_fill) >= mod(m_start+m_fill+itms.length) ){
|
||||||
|
size_t chunk1 = m_buffer.length - (m_start+m_fill);
|
||||||
|
size_t chunk2 = itms.length - chunk1;
|
||||||
|
m_buffer[m_start+m_fill .. m_buffer.length] = itms[0 .. chunk1];
|
||||||
|
m_buffer[0 .. chunk2] = itms[chunk1 .. $];
|
||||||
|
} else {
|
||||||
|
m_buffer[mod(m_start+m_fill) .. mod(m_start+m_fill)+itms.length] = itms[];
|
||||||
|
}
|
||||||
|
m_fill += itms.length;
|
||||||
|
}
|
||||||
|
void putN(size_t n) { assert(m_fill+n <= m_buffer.length); m_fill += n; }
|
||||||
|
|
||||||
|
void popFront() { assert(!empty); m_start = mod(m_start+1); m_fill--; }
|
||||||
|
void popFrontN(size_t n) { assert(length >= n); m_start = mod(m_start + n); m_fill -= n; }
|
||||||
|
|
||||||
|
void popBack() { assert(!empty); m_fill--; }
|
||||||
|
void popBackN(size_t n) { assert(length >= n); m_fill -= n; }
|
||||||
|
|
||||||
|
void removeAt(Range r)
|
||||||
|
{
|
||||||
|
assert(r.m_buffer is m_buffer);
|
||||||
|
if( m_start + m_fill > m_buffer.length ){
|
||||||
|
assert(r.m_start >= m_start && r.m_start < m_buffer.length || r.m_start < mod(m_start+m_fill));
|
||||||
|
if( r.m_start > m_start ){
|
||||||
|
foreach(i; r.m_start .. m_buffer.length-1)
|
||||||
|
m_buffer[i] = m_buffer[i+1];
|
||||||
|
m_buffer[$-1] = m_buffer[0];
|
||||||
|
foreach(i; 0 .. mod(m_start + m_fill - 1))
|
||||||
|
m_buffer[i] = m_buffer[i+1];
|
||||||
|
} else {
|
||||||
|
foreach(i; r.m_start .. mod(m_start + m_fill - 1))
|
||||||
|
m_buffer[i] = m_buffer[i+1];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
assert(r.m_start >= m_start && r.m_start < m_start+m_fill);
|
||||||
|
foreach(i; r.m_start .. m_start+m_fill-1)
|
||||||
|
m_buffer[i] = m_buffer[i+1];
|
||||||
|
}
|
||||||
|
m_fill--;
|
||||||
|
destroy(m_buffer[mod(m_start+m_fill)]); // TODO: only call destroy for non-POD T
|
||||||
|
}
|
||||||
|
|
||||||
|
inout(T)[] peek() inout { return m_buffer[m_start .. min(m_start+m_fill, m_buffer.length)]; }
|
||||||
|
T[] peekDst() {
|
||||||
|
if (!m_buffer.length) return null;
|
||||||
|
if( m_start + m_fill < m_buffer.length ) return m_buffer[m_start+m_fill .. $];
|
||||||
|
else return m_buffer[mod(m_start+m_fill) .. m_start];
|
||||||
|
}
|
||||||
|
|
||||||
|
void read(T[] dst)
|
||||||
|
{
|
||||||
|
assert(dst.length <= length);
|
||||||
|
if( !dst.length ) return;
|
||||||
|
if( mod(m_start) >= mod(m_start+dst.length) ){
|
||||||
|
size_t chunk1 = m_buffer.length - m_start;
|
||||||
|
size_t chunk2 = dst.length - chunk1;
|
||||||
|
dst[0 .. chunk1] = m_buffer[m_start .. $];
|
||||||
|
dst[chunk1 .. $] = m_buffer[0 .. chunk2];
|
||||||
|
} else {
|
||||||
|
dst[] = m_buffer[m_start .. m_start+dst.length];
|
||||||
|
}
|
||||||
|
popFrontN(dst.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
int opApply(scope int delegate(ref T itm) del)
|
||||||
|
{
|
||||||
|
if( m_start+m_fill > m_buffer.length ){
|
||||||
|
foreach(i; m_start .. m_buffer.length)
|
||||||
|
if( auto ret = del(m_buffer[i]) )
|
||||||
|
return ret;
|
||||||
|
foreach(i; 0 .. mod(m_start+m_fill))
|
||||||
|
if( auto ret = del(m_buffer[i]) )
|
||||||
|
return ret;
|
||||||
|
} else {
|
||||||
|
foreach(i; m_start .. m_start+m_fill)
|
||||||
|
if( auto ret = del(m_buffer[i]) )
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// iterate through elements with index
|
||||||
|
int opApply(scope int delegate(size_t i, ref T itm) del)
|
||||||
|
{
|
||||||
|
if( m_start+m_fill > m_buffer.length ){
|
||||||
|
foreach(i; m_start .. m_buffer.length)
|
||||||
|
if( auto ret = del(i - m_start, m_buffer[i]) )
|
||||||
|
return ret;
|
||||||
|
foreach(i; 0 .. mod(m_start+m_fill))
|
||||||
|
if( auto ret = del(i + m_buffer.length - m_start, m_buffer[i]) )
|
||||||
|
return ret;
|
||||||
|
} else {
|
||||||
|
foreach(i; m_start .. m_start+m_fill)
|
||||||
|
if( auto ret = del(i - m_start, m_buffer[i]) )
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ref inout(T) opIndex(size_t idx) inout { assert(idx < length); return m_buffer[mod(m_start+idx)]; }
|
||||||
|
|
||||||
|
Range opSlice() { return Range(m_buffer, m_start, m_fill); }
|
||||||
|
|
||||||
|
Range opSlice(size_t from, size_t to)
|
||||||
|
{
|
||||||
|
assert(from <= to);
|
||||||
|
assert(to <= m_fill);
|
||||||
|
return Range(m_buffer, mod(m_start+from), to-from);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t opDollar(size_t dim)() const if(dim == 0) { return length; }
|
||||||
|
|
||||||
|
private size_t mod(size_t n)
|
||||||
|
const {
|
||||||
|
static if( N == 0 ){
|
||||||
|
/*static if(PotOnly){
|
||||||
|
return x & (m_buffer.length-1);
|
||||||
|
} else {*/
|
||||||
|
return n % m_buffer.length;
|
||||||
|
//}
|
||||||
|
} else static if( ((N - 1) & N) == 0 ){
|
||||||
|
return n & (N - 1);
|
||||||
|
} else return n % N;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct Range {
|
||||||
|
private {
|
||||||
|
T[] m_buffer;
|
||||||
|
size_t m_start;
|
||||||
|
size_t m_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
private this(T[] buffer, size_t start, size_t length)
|
||||||
|
{
|
||||||
|
m_buffer = buffer;
|
||||||
|
m_start = start;
|
||||||
|
m_length = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property bool empty() const { return m_length == 0; }
|
||||||
|
|
||||||
|
@property inout(T) front() inout { assert(!empty); return m_buffer[m_start]; }
|
||||||
|
|
||||||
|
void popFront()
|
||||||
|
{
|
||||||
|
assert(!empty);
|
||||||
|
m_start++;
|
||||||
|
m_length--;
|
||||||
|
if( m_start >= m_buffer.length )
|
||||||
|
m_start = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
static assert(isInputRange!(FixedRingBuffer!int) && isOutputRange!(FixedRingBuffer!int, int));
|
||||||
|
|
||||||
|
FixedRingBuffer!(int, 5) buf;
|
||||||
|
assert(buf.length == 0 && buf.freeSpace == 5); buf.put(1); // |1 . . . .
|
||||||
|
assert(buf.length == 1 && buf.freeSpace == 4); buf.put(2); // |1 2 . . .
|
||||||
|
assert(buf.length == 2 && buf.freeSpace == 3); buf.put(3); // |1 2 3 . .
|
||||||
|
assert(buf.length == 3 && buf.freeSpace == 2); buf.put(4); // |1 2 3 4 .
|
||||||
|
assert(buf.length == 4 && buf.freeSpace == 1); buf.put(5); // |1 2 3 4 5
|
||||||
|
assert(buf.length == 5 && buf.freeSpace == 0);
|
||||||
|
assert(buf.front == 1);
|
||||||
|
buf.popFront(); // .|2 3 4 5
|
||||||
|
assert(buf.front == 2);
|
||||||
|
buf.popFrontN(2); // . . .|4 5
|
||||||
|
assert(buf.front == 4);
|
||||||
|
assert(buf.length == 2 && buf.freeSpace == 3);
|
||||||
|
buf.put([6, 7, 8]); // 6 7 8|4 5
|
||||||
|
assert(buf.length == 5 && buf.freeSpace == 0);
|
||||||
|
int[5] dst;
|
||||||
|
buf.read(dst); // . . .|. .
|
||||||
|
assert(dst == [4, 5, 6, 7, 8]);
|
||||||
|
assert(buf.length == 0 && buf.freeSpace == 5);
|
||||||
|
buf.put([1, 2]); // . . .|1 2
|
||||||
|
assert(buf.length == 2 && buf.freeSpace == 3);
|
||||||
|
buf.read(dst[0 .. 2]); //|. . . . .
|
||||||
|
assert(dst[0 .. 2] == [1, 2]);
|
||||||
|
|
||||||
|
buf.put([0, 0, 0, 1, 2]); //|0 0 0 1 2
|
||||||
|
buf.popFrontN(2); //. .|0 1 2
|
||||||
|
buf.put([3, 4]); // 3 4|0 1 2
|
||||||
|
foreach(i, item; buf)
|
||||||
|
{
|
||||||
|
assert(i == item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Write a single batch and drain
|
||||||
|
struct BatchBuffer(T, size_t N = 0) {
|
||||||
|
private {
|
||||||
|
size_t m_fill;
|
||||||
|
size_t m_first;
|
||||||
|
static if (N == 0) T[] m_buffer;
|
||||||
|
else T[N] m_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (N == 0) {
|
||||||
|
@property void capacity(size_t n) { assert(n >= m_fill); m_buffer.length = n; }
|
||||||
|
}
|
||||||
|
|
||||||
|
@property bool empty() { return m_first >= m_fill; }
|
||||||
|
@property size_t capacity() const { return m_buffer.length; }
|
||||||
|
@property size_t length() { return m_fill - m_first; }
|
||||||
|
@property ref inout(T) front() inout { assert(!empty); return m_buffer[m_first]; }
|
||||||
|
void popFront() { assert(!m_empty); m_first++; }
|
||||||
|
void popFrontN(size_t n) { assert(n <= length); m_first += n; }
|
||||||
|
inout(T)[] peek() inout { return m_buffer[m_first .. m_fill]; }
|
||||||
|
T[] peekDst() { assert(empty); return m_buffer; }
|
||||||
|
void putN(size_t n) { assert(empty && n <= m_buffer.length); m_fill = n; }
|
||||||
|
void putN(T[] elems) { assert(empty && elems.length <= m_buffer.length); m_buffer[0 .. elems.length] = elems[]; m_fill = elems.length; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct ArraySet(Key)
|
||||||
|
{
|
||||||
|
private {
|
||||||
|
Key[4] m_staticEntries;
|
||||||
|
Key[] m_entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property ArraySet dup()
|
||||||
|
{
|
||||||
|
return ArraySet(m_staticEntries, m_entries.dup);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool opBinaryRight(string op)(Key key) if (op == "in") { return contains(key); }
|
||||||
|
|
||||||
|
int opApply(int delegate(ref Key) del)
|
||||||
|
{
|
||||||
|
foreach (ref k; m_staticEntries)
|
||||||
|
if (k != Key.init)
|
||||||
|
if (auto ret = del(k))
|
||||||
|
return ret;
|
||||||
|
foreach (ref k; m_entries)
|
||||||
|
if (k != Key.init)
|
||||||
|
if (auto ret = del(k))
|
||||||
|
return ret;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool contains(Key key)
|
||||||
|
const {
|
||||||
|
foreach (ref k; m_staticEntries) if (k == key) return true;
|
||||||
|
foreach (ref k; m_entries) if (k == key) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert(Key key)
|
||||||
|
{
|
||||||
|
if (contains(key)) return;
|
||||||
|
foreach (ref k; m_staticEntries)
|
||||||
|
if (k == Key.init) {
|
||||||
|
k = key;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
foreach (ref k; m_entries)
|
||||||
|
if (k == Key.init) {
|
||||||
|
k = key;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_entries ~= key;
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(Key key)
|
||||||
|
{
|
||||||
|
foreach (ref k; m_staticEntries) if (k == key) { k = Key.init; return; }
|
||||||
|
foreach (ref k; m_entries) if (k == key) { k = Key.init; return; }
|
||||||
|
}
|
||||||
|
}
|
45
source/vibe/internal/async.d
Normal file
45
source/vibe/internal/async.d
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
module vibe.internal.async;
|
||||||
|
|
||||||
|
import std.traits : ParameterTypeTuple;
|
||||||
|
import std.typecons : tuple;
|
||||||
|
import vibe.core.core;
|
||||||
|
import vibe.core.log;
|
||||||
|
import core.time : Duration, seconds;
|
||||||
|
|
||||||
|
|
||||||
|
auto asyncAwait(string method, Object, ARGS...)(Object object, ARGS args)
|
||||||
|
{
|
||||||
|
alias CB = ParameterTypeTuple!(__traits(getMember, Object, method))[$-1];
|
||||||
|
alias CBTypes = ParameterTypeTuple!CB;
|
||||||
|
|
||||||
|
bool fired = false;
|
||||||
|
CBTypes ret;
|
||||||
|
Task t;
|
||||||
|
|
||||||
|
void callback(CBTypes params)
|
||||||
|
@safe nothrow {
|
||||||
|
logTrace("Got result.");
|
||||||
|
fired = true;
|
||||||
|
ret = params;
|
||||||
|
if (t != Task.init)
|
||||||
|
resumeTask(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
logTrace("Calling %s...", method);
|
||||||
|
__traits(getMember, object, method)(args, &callback);
|
||||||
|
if (!fired) {
|
||||||
|
logTrace("Need to wait...");
|
||||||
|
t = Task.getThis();
|
||||||
|
do yieldForEvent();
|
||||||
|
while (!fired);
|
||||||
|
}
|
||||||
|
logTrace("Return result.");
|
||||||
|
return tuple(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto asyncAwait(string method, Object, ARGS...)(Duration timeout, Object object, ARGS args)
|
||||||
|
{
|
||||||
|
assert(timeout >= 0.seconds);
|
||||||
|
if (timeout == Duration.max) return asyncAwait(object, args);
|
||||||
|
else assert(false, "TODO!");
|
||||||
|
}
|
375
source/vibe/internal/hashmap.d
Normal file
375
source/vibe/internal/hashmap.d
Normal file
|
@ -0,0 +1,375 @@
|
||||||
|
/**
|
||||||
|
Internal hash map implementation.
|
||||||
|
|
||||||
|
Copyright: © 2013 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
*/
|
||||||
|
module vibe.internal.hashmap;
|
||||||
|
|
||||||
|
import vibe.internal.memory;
|
||||||
|
|
||||||
|
import std.conv : emplace;
|
||||||
|
import std.traits;
|
||||||
|
|
||||||
|
|
||||||
|
struct DefaultHashMapTraits(Key) {
|
||||||
|
enum clearValue = Key.init;
|
||||||
|
static bool equals(in Key a, in Key b)
|
||||||
|
{
|
||||||
|
static if (is(Key == class)) return a is b;
|
||||||
|
else return a == b;
|
||||||
|
}
|
||||||
|
static size_t hashOf(in ref Key k)
|
||||||
|
{
|
||||||
|
static if (is(Key == class) && &Key.toHash == &Object.toHash)
|
||||||
|
return cast(size_t)cast(void*)k;
|
||||||
|
else static if (__traits(compiles, Key.init.toHash()))
|
||||||
|
return k.toHash();
|
||||||
|
else static if (__traits(compiles, Key.init.toHashShared()))
|
||||||
|
return k.toHashShared();
|
||||||
|
else {
|
||||||
|
// evil casts to be able to get the most basic operations of
|
||||||
|
// HashMap nothrow and @nogc
|
||||||
|
static size_t hashWrapper(in ref Key k) {
|
||||||
|
static typeinfo = typeid(Key);
|
||||||
|
return typeinfo.getHash(&k);
|
||||||
|
}
|
||||||
|
static @nogc nothrow size_t properlyTypedWrapper(in ref Key k) { return 0; }
|
||||||
|
return (cast(typeof(&properlyTypedWrapper))&hashWrapper)(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct HashMap(TKey, TValue, Traits = DefaultHashMapTraits!TKey)
|
||||||
|
{
|
||||||
|
import vibe.internal.traits : isOpApplyDg;
|
||||||
|
|
||||||
|
alias Key = TKey;
|
||||||
|
alias Value = TValue;
|
||||||
|
|
||||||
|
struct TableEntry {
|
||||||
|
UnConst!Key key = Traits.clearValue;
|
||||||
|
Value value;
|
||||||
|
|
||||||
|
this(Key key, Value value) { this.key = cast(UnConst!Key)key; this.value = value; }
|
||||||
|
}
|
||||||
|
private {
|
||||||
|
TableEntry[] m_table; // NOTE: capacity is always POT
|
||||||
|
size_t m_length;
|
||||||
|
Allocator m_allocator;
|
||||||
|
bool m_resizing;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(Allocator allocator)
|
||||||
|
{
|
||||||
|
m_allocator = allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
~this()
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
if (m_table.ptr !is null) freeArray(m_allocator, m_table);
|
||||||
|
}
|
||||||
|
|
||||||
|
@disable this(this);
|
||||||
|
|
||||||
|
@property size_t length() const { return m_length; }
|
||||||
|
|
||||||
|
void remove(Key key)
|
||||||
|
{
|
||||||
|
auto idx = findIndex(key);
|
||||||
|
assert (idx != size_t.max, "Removing non-existent element.");
|
||||||
|
auto i = idx;
|
||||||
|
while (true) {
|
||||||
|
m_table[i].key = Traits.clearValue;
|
||||||
|
m_table[i].value = Value.init;
|
||||||
|
|
||||||
|
size_t j = i, r;
|
||||||
|
do {
|
||||||
|
if (++i >= m_table.length) i -= m_table.length;
|
||||||
|
if (Traits.equals(m_table[i].key, Traits.clearValue)) {
|
||||||
|
m_length--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
r = Traits.hashOf(m_table[i].key) & (m_table.length-1);
|
||||||
|
} while ((j<r && r<=i) || (i<j && j<r) || (r<=i && i<j));
|
||||||
|
m_table[j] = m_table[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Value get(Key key, lazy Value default_value = Value.init)
|
||||||
|
{
|
||||||
|
auto idx = findIndex(key);
|
||||||
|
if (idx == size_t.max) return default_value;
|
||||||
|
return m_table[idx].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Workaround #12647
|
||||||
|
package Value getNothrow(Key key, Value default_value = Value.init)
|
||||||
|
{
|
||||||
|
auto idx = findIndex(key);
|
||||||
|
if (idx == size_t.max) return default_value;
|
||||||
|
return m_table[idx].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (!is(typeof({ Value v; const(Value) vc; v = vc; }))) {
|
||||||
|
const(Value) get(Key key, lazy const(Value) default_value = Value.init)
|
||||||
|
{
|
||||||
|
auto idx = findIndex(key);
|
||||||
|
if (idx == size_t.max) return default_value;
|
||||||
|
return m_table[idx].value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
foreach (i; 0 .. m_table.length)
|
||||||
|
if (!Traits.equals(m_table[i].key, Traits.clearValue)) {
|
||||||
|
m_table[i].key = Traits.clearValue;
|
||||||
|
m_table[i].value = Value.init;
|
||||||
|
}
|
||||||
|
m_length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void opIndexAssign(Value value, Key key)
|
||||||
|
{
|
||||||
|
assert(!Traits.equals(key, Traits.clearValue), "Inserting clear value into hash map.");
|
||||||
|
grow(1);
|
||||||
|
auto i = findInsertIndex(key);
|
||||||
|
if (!Traits.equals(m_table[i].key, key)) m_length++;
|
||||||
|
m_table[i] = TableEntry(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
ref inout(Value) opIndex(Key key)
|
||||||
|
inout {
|
||||||
|
auto idx = findIndex(key);
|
||||||
|
assert (idx != size_t.max, "Accessing non-existent key.");
|
||||||
|
return m_table[idx].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
inout(Value)* opBinaryRight(string op)(Key key)
|
||||||
|
inout if (op == "in") {
|
||||||
|
auto idx = findIndex(key);
|
||||||
|
if (idx == size_t.max) return null;
|
||||||
|
return &m_table[idx].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int opApply(DG)(scope DG del) if (isOpApplyDg!(DG, Key, Value))
|
||||||
|
{
|
||||||
|
import std.traits : arity;
|
||||||
|
foreach (i; 0 .. m_table.length)
|
||||||
|
if (!Traits.equals(m_table[i].key, Traits.clearValue)) {
|
||||||
|
static assert(arity!del >= 1 && arity!del <= 2,
|
||||||
|
"isOpApplyDg should have prevented this");
|
||||||
|
static if (arity!del == 1) {
|
||||||
|
if (int ret = del(m_table[i].value))
|
||||||
|
return ret;
|
||||||
|
} else
|
||||||
|
if (int ret = del(m_table[i].key, m_table[i].value))
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private size_t findIndex(Key key)
|
||||||
|
const {
|
||||||
|
if (m_length == 0) return size_t.max;
|
||||||
|
size_t start = Traits.hashOf(key) & (m_table.length-1);
|
||||||
|
auto i = start;
|
||||||
|
while (!Traits.equals(m_table[i].key, key)) {
|
||||||
|
if (Traits.equals(m_table[i].key, Traits.clearValue)) return size_t.max;
|
||||||
|
if (++i >= m_table.length) i -= m_table.length;
|
||||||
|
if (i == start) return size_t.max;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
private size_t findInsertIndex(Key key)
|
||||||
|
const {
|
||||||
|
auto hash = Traits.hashOf(key);
|
||||||
|
size_t target = hash & (m_table.length-1);
|
||||||
|
auto i = target;
|
||||||
|
while (!Traits.equals(m_table[i].key, Traits.clearValue) && !Traits.equals(m_table[i].key, key)) {
|
||||||
|
if (++i >= m_table.length) i -= m_table.length;
|
||||||
|
assert (i != target, "No free bucket found, HashMap full!?");
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void grow(size_t amount)
|
||||||
|
{
|
||||||
|
auto newsize = m_length + amount;
|
||||||
|
if (newsize < (m_table.length*2)/3) return;
|
||||||
|
auto newcap = m_table.length ? m_table.length : 16;
|
||||||
|
while (newsize >= (newcap*2)/3) newcap *= 2;
|
||||||
|
resize(newcap);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize(size_t new_size)
|
||||||
|
@trusted {
|
||||||
|
assert(!m_resizing);
|
||||||
|
m_resizing = true;
|
||||||
|
scope(exit) m_resizing = false;
|
||||||
|
|
||||||
|
if (!m_allocator) m_allocator = defaultAllocator();
|
||||||
|
|
||||||
|
uint pot = 0;
|
||||||
|
while (new_size > 1) pot++, new_size /= 2;
|
||||||
|
new_size = 1 << pot;
|
||||||
|
|
||||||
|
auto oldtable = m_table;
|
||||||
|
|
||||||
|
// allocate the new array, automatically initializes with empty entries (Traits.clearValue)
|
||||||
|
m_table = allocArray!TableEntry(m_allocator, new_size);
|
||||||
|
|
||||||
|
// perform a move operation of all non-empty elements from the old array to the new one
|
||||||
|
foreach (ref el; oldtable)
|
||||||
|
if (!Traits.equals(el.key, Traits.clearValue)) {
|
||||||
|
auto idx = findInsertIndex(el.key);
|
||||||
|
(cast(ubyte[])(&m_table[idx])[0 .. 1])[] = (cast(ubyte[])(&el)[0 .. 1])[];
|
||||||
|
}
|
||||||
|
|
||||||
|
// all elements have been moved to the new array, so free the old one without calling destructors
|
||||||
|
if (oldtable !is null) freeArray(m_allocator, oldtable, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
import std.conv;
|
||||||
|
|
||||||
|
HashMap!(string, string) map;
|
||||||
|
|
||||||
|
foreach (i; 0 .. 100) {
|
||||||
|
map[to!string(i)] = to!string(i) ~ "+";
|
||||||
|
assert(map.length == i+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (i; 0 .. 100) {
|
||||||
|
auto str = to!string(i);
|
||||||
|
auto pe = str in map;
|
||||||
|
assert(pe !is null && *pe == str ~ "+");
|
||||||
|
assert(map[str] == str ~ "+");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (i; 0 .. 50) {
|
||||||
|
map.remove(to!string(i));
|
||||||
|
assert(map.length == 100-i-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (i; 50 .. 100) {
|
||||||
|
auto str = to!string(i);
|
||||||
|
auto pe = str in map;
|
||||||
|
assert(pe !is null && *pe == str ~ "+");
|
||||||
|
assert(map[str] == str ~ "+");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// test for nothrow/@nogc compliance
|
||||||
|
static if (__VERSION__ >= 2066)
|
||||||
|
nothrow unittest {
|
||||||
|
HashMap!(int, int) map1;
|
||||||
|
HashMap!(string, string) map2;
|
||||||
|
map1[1] = 2;
|
||||||
|
map2["1"] = "2";
|
||||||
|
|
||||||
|
@nogc nothrow void performNoGCOps()
|
||||||
|
{
|
||||||
|
foreach (int v; map1) {}
|
||||||
|
foreach (int k, int v; map1) {}
|
||||||
|
assert(1 in map1);
|
||||||
|
assert(map1.length == 1);
|
||||||
|
assert(map1[1] == 2);
|
||||||
|
assert(map1.getNothrow(1, -1) == 2);
|
||||||
|
|
||||||
|
foreach (string v; map2) {}
|
||||||
|
foreach (string k, string v; map2) {}
|
||||||
|
assert("1" in map2);
|
||||||
|
assert(map2.length == 1);
|
||||||
|
assert(map2["1"] == "2");
|
||||||
|
assert(map2.getNothrow("1", "") == "2");
|
||||||
|
}
|
||||||
|
|
||||||
|
performNoGCOps();
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest { // test for proper use of constructor/post-blit/destructor
|
||||||
|
static struct Test {
|
||||||
|
static size_t constructedCounter = 0;
|
||||||
|
bool constructed = false;
|
||||||
|
this(int) { constructed = true; constructedCounter++; }
|
||||||
|
this(this) { if (constructed) constructedCounter++; }
|
||||||
|
~this() { if (constructed) constructedCounter--; }
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(Test.constructedCounter == 0);
|
||||||
|
|
||||||
|
{ // sanity check
|
||||||
|
Test t;
|
||||||
|
assert(Test.constructedCounter == 0);
|
||||||
|
t = Test(1);
|
||||||
|
assert(Test.constructedCounter == 1);
|
||||||
|
auto u = t;
|
||||||
|
assert(Test.constructedCounter == 2);
|
||||||
|
t = Test.init;
|
||||||
|
assert(Test.constructedCounter == 1);
|
||||||
|
}
|
||||||
|
assert(Test.constructedCounter == 0);
|
||||||
|
|
||||||
|
{ // basic insertion and hash map resizing
|
||||||
|
HashMap!(int, Test) map;
|
||||||
|
foreach (i; 1 .. 67) {
|
||||||
|
map[i] = Test(1);
|
||||||
|
assert(Test.constructedCounter == i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(Test.constructedCounter == 0);
|
||||||
|
|
||||||
|
{ // test clear() and overwriting existing entries
|
||||||
|
HashMap!(int, Test) map;
|
||||||
|
foreach (i; 1 .. 67) {
|
||||||
|
map[i] = Test(1);
|
||||||
|
assert(Test.constructedCounter == i);
|
||||||
|
}
|
||||||
|
map.clear();
|
||||||
|
foreach (i; 1 .. 67) {
|
||||||
|
map[i] = Test(1);
|
||||||
|
assert(Test.constructedCounter == i);
|
||||||
|
}
|
||||||
|
foreach (i; 1 .. 67) {
|
||||||
|
map[i] = Test(1);
|
||||||
|
assert(Test.constructedCounter == 66);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(Test.constructedCounter == 0);
|
||||||
|
|
||||||
|
{ // test removing entries and adding entries after remove
|
||||||
|
HashMap!(int, Test) map;
|
||||||
|
foreach (i; 1 .. 67) {
|
||||||
|
map[i] = Test(1);
|
||||||
|
assert(Test.constructedCounter == i);
|
||||||
|
}
|
||||||
|
foreach (i; 1 .. 33) {
|
||||||
|
map.remove(i);
|
||||||
|
assert(Test.constructedCounter == 66 - i);
|
||||||
|
}
|
||||||
|
foreach (i; 67 .. 130) {
|
||||||
|
map[i] = Test(1);
|
||||||
|
assert(Test.constructedCounter == i - 32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(Test.constructedCounter == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private template UnConst(T) {
|
||||||
|
static if (is(T U == const(U))) {
|
||||||
|
alias UnConst = U;
|
||||||
|
} else static if (is(T V == immutable(V))) {
|
||||||
|
alias UnConst = V;
|
||||||
|
} else alias UnConst = T;
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (__VERSION__ < 2066) private static bool nogc() { return false; }
|
872
source/vibe/internal/memory.d
Normal file
872
source/vibe/internal/memory.d
Normal file
|
@ -0,0 +1,872 @@
|
||||||
|
/**
|
||||||
|
Utility functions for memory management
|
||||||
|
|
||||||
|
Note that this module currently is a big sand box for testing allocation related stuff.
|
||||||
|
Nothing here, including the interfaces, is final but rather a lot of experimentation.
|
||||||
|
|
||||||
|
Copyright: © 2012-2013 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
*/
|
||||||
|
module vibe.internal.memory;
|
||||||
|
|
||||||
|
import vibe.internal.traits : synchronizedIsNothrow;
|
||||||
|
|
||||||
|
import core.exception : OutOfMemoryError;
|
||||||
|
import core.stdc.stdlib;
|
||||||
|
import core.memory;
|
||||||
|
import std.conv;
|
||||||
|
import std.exception : enforceEx;
|
||||||
|
import std.traits;
|
||||||
|
import std.algorithm;
|
||||||
|
|
||||||
|
Allocator defaultAllocator() nothrow
|
||||||
|
{
|
||||||
|
version(VibeManualMemoryManagement){
|
||||||
|
return manualAllocator();
|
||||||
|
} else {
|
||||||
|
static __gshared Allocator alloc;
|
||||||
|
if (!alloc) {
|
||||||
|
alloc = new GCAllocator;
|
||||||
|
//alloc = new AutoFreeListAllocator(alloc);
|
||||||
|
//alloc = new DebugAllocator(alloc);
|
||||||
|
alloc = new LockAllocator(alloc);
|
||||||
|
}
|
||||||
|
return alloc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Allocator manualAllocator() nothrow
|
||||||
|
{
|
||||||
|
static __gshared Allocator alloc;
|
||||||
|
if( !alloc ){
|
||||||
|
alloc = new MallocAllocator;
|
||||||
|
alloc = new AutoFreeListAllocator(alloc);
|
||||||
|
//alloc = new DebugAllocator(alloc);
|
||||||
|
alloc = new LockAllocator(alloc);
|
||||||
|
}
|
||||||
|
return alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Allocator threadLocalAllocator() nothrow
|
||||||
|
{
|
||||||
|
static Allocator alloc;
|
||||||
|
if (!alloc) {
|
||||||
|
version(VibeManualMemoryManagement) alloc = new MallocAllocator;
|
||||||
|
else alloc = new GCAllocator;
|
||||||
|
alloc = new AutoFreeListAllocator(alloc);
|
||||||
|
// alloc = new DebugAllocator(alloc);
|
||||||
|
}
|
||||||
|
return alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Allocator threadLocalManualAllocator() nothrow
|
||||||
|
{
|
||||||
|
static Allocator alloc;
|
||||||
|
if (!alloc) {
|
||||||
|
alloc = new MallocAllocator;
|
||||||
|
alloc = new AutoFreeListAllocator(alloc);
|
||||||
|
// alloc = new DebugAllocator(alloc);
|
||||||
|
}
|
||||||
|
return alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto allocObject(T, bool MANAGED = true, ARGS...)(Allocator allocator, ARGS args)
|
||||||
|
{
|
||||||
|
auto mem = allocator.alloc(AllocSize!T);
|
||||||
|
static if( MANAGED ){
|
||||||
|
static if( hasIndirections!T )
|
||||||
|
GC.addRange(mem.ptr, mem.length);
|
||||||
|
return internalEmplace!T(mem, args);
|
||||||
|
}
|
||||||
|
else static if( is(T == class) ) return cast(T)mem.ptr;
|
||||||
|
else return cast(T*)mem.ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
T[] allocArray(T, bool MANAGED = true)(Allocator allocator, size_t n)
|
||||||
|
{
|
||||||
|
auto mem = allocator.alloc(T.sizeof * n);
|
||||||
|
auto ret = cast(T[])mem;
|
||||||
|
static if( MANAGED ){
|
||||||
|
static if( hasIndirections!T )
|
||||||
|
GC.addRange(mem.ptr, mem.length);
|
||||||
|
// TODO: use memset for class, pointers and scalars
|
||||||
|
foreach (ref el; ret) {
|
||||||
|
internalEmplace!T(cast(void[])((&el)[0 .. 1]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeArray(T, bool MANAGED = true)(Allocator allocator, ref T[] array, bool call_destructors = true)
|
||||||
|
{
|
||||||
|
static if (MANAGED) {
|
||||||
|
static if (hasIndirections!T)
|
||||||
|
GC.removeRange(array.ptr);
|
||||||
|
static if (hasElaborateDestructor!T)
|
||||||
|
if (call_destructors)
|
||||||
|
foreach_reverse (ref el; array)
|
||||||
|
destroy(el);
|
||||||
|
}
|
||||||
|
allocator.free(cast(void[])array);
|
||||||
|
array = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface Allocator {
|
||||||
|
nothrow:
|
||||||
|
enum size_t alignment = 0x10;
|
||||||
|
enum size_t alignmentMask = alignment-1;
|
||||||
|
|
||||||
|
void[] alloc(size_t sz)
|
||||||
|
out { assert((cast(size_t)__result.ptr & alignmentMask) == 0, "alloc() returned misaligned data."); }
|
||||||
|
|
||||||
|
void[] realloc(void[] mem, size_t new_sz)
|
||||||
|
in {
|
||||||
|
assert(mem.ptr !is null, "realloc() called with null array.");
|
||||||
|
assert((cast(size_t)mem.ptr & alignmentMask) == 0, "misaligned pointer passed to realloc().");
|
||||||
|
}
|
||||||
|
out { assert((cast(size_t)__result.ptr & alignmentMask) == 0, "realloc() returned misaligned data."); }
|
||||||
|
|
||||||
|
void free(void[] mem)
|
||||||
|
in {
|
||||||
|
assert(mem.ptr !is null, "free() called with null array.");
|
||||||
|
assert((cast(size_t)mem.ptr & alignmentMask) == 0, "misaligned pointer passed to free().");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Simple proxy allocator protecting its base allocator with a mutex.
|
||||||
|
*/
|
||||||
|
class LockAllocator : Allocator {
|
||||||
|
private {
|
||||||
|
Allocator m_base;
|
||||||
|
}
|
||||||
|
this(Allocator base) nothrow { m_base = base; }
|
||||||
|
void[] alloc(size_t sz) {
|
||||||
|
static if (!synchronizedIsNothrow)
|
||||||
|
scope (failure) assert(0, "Internal error: function should be nothrow");
|
||||||
|
|
||||||
|
synchronized (this)
|
||||||
|
return m_base.alloc(sz);
|
||||||
|
}
|
||||||
|
void[] realloc(void[] mem, size_t new_sz)
|
||||||
|
in {
|
||||||
|
assert(mem.ptr !is null, "realloc() called with null array.");
|
||||||
|
assert((cast(size_t)mem.ptr & alignmentMask) == 0, "misaligned pointer passed to realloc().");
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
static if (!synchronizedIsNothrow)
|
||||||
|
scope (failure) assert(0, "Internal error: function should be nothrow");
|
||||||
|
|
||||||
|
synchronized(this)
|
||||||
|
return m_base.realloc(mem, new_sz);
|
||||||
|
}
|
||||||
|
void free(void[] mem)
|
||||||
|
in {
|
||||||
|
assert(mem.ptr !is null, "free() called with null array.");
|
||||||
|
assert((cast(size_t)mem.ptr & alignmentMask) == 0, "misaligned pointer passed to free().");
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
static if (!synchronizedIsNothrow)
|
||||||
|
scope (failure) assert(0, "Internal error: function should be nothrow");
|
||||||
|
synchronized(this)
|
||||||
|
m_base.free(mem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class DebugAllocator : Allocator {
|
||||||
|
import vibe.internal.hashmap : HashMap;
|
||||||
|
private {
|
||||||
|
Allocator m_baseAlloc;
|
||||||
|
HashMap!(void*, size_t) m_blocks;
|
||||||
|
size_t m_bytes;
|
||||||
|
size_t m_maxBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(Allocator base_allocator) nothrow
|
||||||
|
{
|
||||||
|
m_baseAlloc = base_allocator;
|
||||||
|
m_blocks = HashMap!(void*, size_t)(manualAllocator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@property size_t allocatedBlockCount() const { return m_blocks.length; }
|
||||||
|
@property size_t bytesAllocated() const { return m_bytes; }
|
||||||
|
@property size_t maxBytesAllocated() const { return m_maxBytes; }
|
||||||
|
|
||||||
|
void[] alloc(size_t sz)
|
||||||
|
{
|
||||||
|
auto ret = m_baseAlloc.alloc(sz);
|
||||||
|
assert(ret.length == sz, "base.alloc() returned block with wrong size.");
|
||||||
|
assert(m_blocks.getNothrow(ret.ptr, size_t.max) == size_t.max, "base.alloc() returned block that is already allocated.");
|
||||||
|
m_blocks[ret.ptr] = sz;
|
||||||
|
m_bytes += sz;
|
||||||
|
if( m_bytes > m_maxBytes ){
|
||||||
|
m_maxBytes = m_bytes;
|
||||||
|
logDebug_("New allocation maximum: %d (%d blocks)", m_maxBytes, m_blocks.length);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void[] realloc(void[] mem, size_t new_size)
|
||||||
|
{
|
||||||
|
auto sz = m_blocks.getNothrow(mem.ptr, size_t.max);
|
||||||
|
assert(sz != size_t.max, "realloc() called with non-allocated pointer.");
|
||||||
|
assert(sz == mem.length, "realloc() called with block of wrong size.");
|
||||||
|
auto ret = m_baseAlloc.realloc(mem, new_size);
|
||||||
|
assert(ret.length == new_size, "base.realloc() returned block with wrong size.");
|
||||||
|
assert(ret.ptr is mem.ptr || m_blocks.getNothrow(ret.ptr, size_t.max) == size_t.max, "base.realloc() returned block that is already allocated.");
|
||||||
|
m_bytes -= sz;
|
||||||
|
m_blocks.remove(mem.ptr);
|
||||||
|
m_blocks[ret.ptr] = new_size;
|
||||||
|
m_bytes += new_size;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
void free(void[] mem)
|
||||||
|
{
|
||||||
|
auto sz = m_blocks.getNothrow(mem.ptr, size_t.max);
|
||||||
|
assert(sz != size_t.max, "free() called with non-allocated object.");
|
||||||
|
assert(sz == mem.length, "free() called with block of wrong size.");
|
||||||
|
m_baseAlloc.free(mem);
|
||||||
|
m_bytes -= sz;
|
||||||
|
m_blocks.remove(mem.ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class MallocAllocator : Allocator {
|
||||||
|
void[] alloc(size_t sz)
|
||||||
|
{
|
||||||
|
static err = new immutable OutOfMemoryError;
|
||||||
|
auto ptr = .malloc(sz + Allocator.alignment);
|
||||||
|
if (ptr is null) throw err;
|
||||||
|
return adjustPointerAlignment(ptr)[0 .. sz];
|
||||||
|
}
|
||||||
|
|
||||||
|
void[] realloc(void[] mem, size_t new_size)
|
||||||
|
{
|
||||||
|
size_t csz = min(mem.length, new_size);
|
||||||
|
auto p = extractUnalignedPointer(mem.ptr);
|
||||||
|
size_t oldmisalign = mem.ptr - p;
|
||||||
|
|
||||||
|
auto pn = cast(ubyte*).realloc(p, new_size+Allocator.alignment);
|
||||||
|
if (p == pn) return pn[oldmisalign .. new_size+oldmisalign];
|
||||||
|
|
||||||
|
auto pna = cast(ubyte*)adjustPointerAlignment(pn);
|
||||||
|
auto newmisalign = pna - pn;
|
||||||
|
|
||||||
|
// account for changed alignment after realloc (move memory back to aligned position)
|
||||||
|
if (oldmisalign != newmisalign) {
|
||||||
|
if (newmisalign > oldmisalign) {
|
||||||
|
foreach_reverse (i; 0 .. csz)
|
||||||
|
pn[i + newmisalign] = pn[i + oldmisalign];
|
||||||
|
} else {
|
||||||
|
foreach (i; 0 .. csz)
|
||||||
|
pn[i + newmisalign] = pn[i + oldmisalign];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pna[0 .. new_size];
|
||||||
|
}
|
||||||
|
|
||||||
|
void free(void[] mem)
|
||||||
|
{
|
||||||
|
.free(extractUnalignedPointer(mem.ptr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class GCAllocator : Allocator {
|
||||||
|
void[] alloc(size_t sz)
|
||||||
|
{
|
||||||
|
auto mem = GC.malloc(sz+Allocator.alignment);
|
||||||
|
auto alignedmem = adjustPointerAlignment(mem);
|
||||||
|
assert(alignedmem - mem <= Allocator.alignment);
|
||||||
|
auto ret = alignedmem[0 .. sz];
|
||||||
|
ensureValidMemory(ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
void[] realloc(void[] mem, size_t new_size)
|
||||||
|
{
|
||||||
|
size_t csz = min(mem.length, new_size);
|
||||||
|
|
||||||
|
auto p = extractUnalignedPointer(mem.ptr);
|
||||||
|
size_t misalign = mem.ptr - p;
|
||||||
|
assert(misalign <= Allocator.alignment);
|
||||||
|
|
||||||
|
void[] ret;
|
||||||
|
auto extended = GC.extend(p, new_size - mem.length, new_size - mem.length);
|
||||||
|
if (extended) {
|
||||||
|
assert(extended >= new_size+Allocator.alignment);
|
||||||
|
ret = p[misalign .. new_size+misalign];
|
||||||
|
} else {
|
||||||
|
ret = alloc(new_size);
|
||||||
|
ret[0 .. csz] = mem[0 .. csz];
|
||||||
|
}
|
||||||
|
ensureValidMemory(ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
void free(void[] mem)
|
||||||
|
{
|
||||||
|
// For safety reasons, the GCAllocator should never explicitly free memory.
|
||||||
|
//GC.free(extractUnalignedPointer(mem.ptr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class AutoFreeListAllocator : Allocator {
|
||||||
|
import std.typetuple;
|
||||||
|
|
||||||
|
private {
|
||||||
|
enum minExponent = 5;
|
||||||
|
enum freeListCount = 14;
|
||||||
|
FreeListAlloc[freeListCount] m_freeLists;
|
||||||
|
Allocator m_baseAlloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(Allocator base_allocator) nothrow
|
||||||
|
{
|
||||||
|
m_baseAlloc = base_allocator;
|
||||||
|
foreach (i; iotaTuple!freeListCount)
|
||||||
|
m_freeLists[i] = new FreeListAlloc(nthFreeListSize!(i), m_baseAlloc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void[] alloc(size_t sz)
|
||||||
|
{
|
||||||
|
auto idx = getAllocatorIndex(sz);
|
||||||
|
return idx < freeListCount ? m_freeLists[idx].alloc()[0 .. sz] : m_baseAlloc.alloc(sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
void[] realloc(void[] data, size_t sz)
|
||||||
|
{
|
||||||
|
auto curidx = getAllocatorIndex(data.length);
|
||||||
|
auto newidx = getAllocatorIndex(sz);
|
||||||
|
|
||||||
|
if (curidx == newidx) {
|
||||||
|
if (curidx == freeListCount) {
|
||||||
|
// forward large blocks to the base allocator
|
||||||
|
return m_baseAlloc.realloc(data, sz);
|
||||||
|
} else {
|
||||||
|
// just grow the slice if it still fits into the free list slot
|
||||||
|
return data.ptr[0 .. sz];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise re-allocate manually
|
||||||
|
auto newd = alloc(sz);
|
||||||
|
assert(newd.ptr+sz <= data.ptr || newd.ptr >= data.ptr+data.length, "New block overlaps old one!?");
|
||||||
|
auto len = min(data.length, sz);
|
||||||
|
newd[0 .. len] = data[0 .. len];
|
||||||
|
free(data);
|
||||||
|
return newd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free(void[] data)
|
||||||
|
{
|
||||||
|
//logTrace("AFL free %08X(%s)", data.ptr, data.length);
|
||||||
|
auto idx = getAllocatorIndex(data.length);
|
||||||
|
if (idx < freeListCount) m_freeLists[idx].free(data.ptr[0 .. 1 << (idx + minExponent)]);
|
||||||
|
else m_baseAlloc.free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// does a CT optimized binary search for the right allocater
|
||||||
|
private int getAllocatorIndex(size_t sz)
|
||||||
|
@safe nothrow @nogc {
|
||||||
|
//pragma(msg, getAllocatorIndexStr!(0, freeListCount));
|
||||||
|
return mixin(getAllocatorIndexStr!(0, freeListCount));
|
||||||
|
}
|
||||||
|
|
||||||
|
private template getAllocatorIndexStr(int low, int high)
|
||||||
|
{
|
||||||
|
static if (__VERSION__ <= 2066) import std.string : format;
|
||||||
|
else import std.format : format;
|
||||||
|
static if (low == high) enum getAllocatorIndexStr = format("%s", low);
|
||||||
|
else {
|
||||||
|
enum mid = (low + high) / 2;
|
||||||
|
enum getAllocatorIndexStr =
|
||||||
|
"sz > nthFreeListSize!%s ? %s : %s"
|
||||||
|
.format(mid, getAllocatorIndexStr!(mid+1, high), getAllocatorIndexStr!(low, mid));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
auto a = new AutoFreeListAllocator(null);
|
||||||
|
assert(a.getAllocatorIndex(0) == 0);
|
||||||
|
foreach (i; iotaTuple!freeListCount) {
|
||||||
|
assert(a.getAllocatorIndex(nthFreeListSize!i-1) == i);
|
||||||
|
assert(a.getAllocatorIndex(nthFreeListSize!i) == i);
|
||||||
|
assert(a.getAllocatorIndex(nthFreeListSize!i+1) == i+1);
|
||||||
|
}
|
||||||
|
assert(a.getAllocatorIndex(size_t.max) == freeListCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static pure size_t nthFreeListSize(size_t i)() { return 1 << (i + minExponent); }
|
||||||
|
private template iotaTuple(size_t i) {
|
||||||
|
static if (i > 1) alias iotaTuple = TypeTuple!(iotaTuple!(i-1), i-1);
|
||||||
|
else alias iotaTuple = TypeTuple!(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class PoolAllocator : Allocator {
|
||||||
|
static struct Pool { Pool* next; void[] data; void[] remaining; }
|
||||||
|
static struct Destructor { Destructor* next; void function(void*) destructor; void* object; }
|
||||||
|
private {
|
||||||
|
Allocator m_baseAllocator;
|
||||||
|
Pool* m_freePools;
|
||||||
|
Pool* m_fullPools;
|
||||||
|
Destructor* m_destructors;
|
||||||
|
size_t m_poolSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(size_t pool_size, Allocator base) nothrow
|
||||||
|
{
|
||||||
|
m_poolSize = pool_size;
|
||||||
|
m_baseAllocator = base;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property size_t totalSize()
|
||||||
|
{
|
||||||
|
size_t amt = 0;
|
||||||
|
for (auto p = m_fullPools; p; p = p.next)
|
||||||
|
amt += p.data.length;
|
||||||
|
for (auto p = m_freePools; p; p = p.next)
|
||||||
|
amt += p.data.length;
|
||||||
|
return amt;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property size_t allocatedSize()
|
||||||
|
{
|
||||||
|
size_t amt = 0;
|
||||||
|
for (auto p = m_fullPools; p; p = p.next)
|
||||||
|
amt += p.data.length;
|
||||||
|
for (auto p = m_freePools; p; p = p.next)
|
||||||
|
amt += p.data.length - p.remaining.length;
|
||||||
|
return amt;
|
||||||
|
}
|
||||||
|
|
||||||
|
void[] alloc(size_t sz)
|
||||||
|
{
|
||||||
|
auto aligned_sz = alignedSize(sz);
|
||||||
|
|
||||||
|
Pool* pprev = null;
|
||||||
|
Pool* p = cast(Pool*)m_freePools;
|
||||||
|
while( p && p.remaining.length < aligned_sz ){
|
||||||
|
pprev = p;
|
||||||
|
p = p.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( !p ){
|
||||||
|
auto pmem = m_baseAllocator.alloc(AllocSize!Pool);
|
||||||
|
|
||||||
|
p = emplace!Pool(cast(Pool*)pmem.ptr);
|
||||||
|
p.data = m_baseAllocator.alloc(max(aligned_sz, m_poolSize));
|
||||||
|
p.remaining = p.data;
|
||||||
|
p.next = cast(Pool*)m_freePools;
|
||||||
|
m_freePools = p;
|
||||||
|
pprev = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ret = p.remaining[0 .. aligned_sz];
|
||||||
|
p.remaining = p.remaining[aligned_sz .. $];
|
||||||
|
if( !p.remaining.length ){
|
||||||
|
if( pprev ){
|
||||||
|
pprev.next = p.next;
|
||||||
|
} else {
|
||||||
|
m_freePools = p.next;
|
||||||
|
}
|
||||||
|
p.next = cast(Pool*)m_fullPools;
|
||||||
|
m_fullPools = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret[0 .. sz];
|
||||||
|
}
|
||||||
|
|
||||||
|
void[] realloc(void[] arr, size_t newsize)
|
||||||
|
{
|
||||||
|
auto aligned_sz = alignedSize(arr.length);
|
||||||
|
auto aligned_newsz = alignedSize(newsize);
|
||||||
|
|
||||||
|
if( aligned_newsz <= aligned_sz ) return arr[0 .. newsize]; // TODO: back up remaining
|
||||||
|
|
||||||
|
auto pool = m_freePools;
|
||||||
|
bool last_in_pool = pool && arr.ptr+aligned_sz == pool.remaining.ptr;
|
||||||
|
if( last_in_pool && pool.remaining.length+aligned_sz >= aligned_newsz ){
|
||||||
|
pool.remaining = pool.remaining[aligned_newsz-aligned_sz .. $];
|
||||||
|
arr = arr.ptr[0 .. aligned_newsz];
|
||||||
|
assert(arr.ptr+arr.length == pool.remaining.ptr, "Last block does not align with the remaining space!?");
|
||||||
|
return arr[0 .. newsize];
|
||||||
|
} else {
|
||||||
|
auto ret = alloc(newsize);
|
||||||
|
assert(ret.ptr >= arr.ptr+aligned_sz || ret.ptr+ret.length <= arr.ptr, "New block overlaps old one!?");
|
||||||
|
ret[0 .. min(arr.length, newsize)] = arr[0 .. min(arr.length, newsize)];
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void free(void[] mem)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeAll()
|
||||||
|
{
|
||||||
|
version(VibeManualMemoryManagement){
|
||||||
|
// destroy all initialized objects
|
||||||
|
for (auto d = m_destructors; d; d = d.next)
|
||||||
|
d.destructor(cast(void*)d.object);
|
||||||
|
m_destructors = null;
|
||||||
|
|
||||||
|
// put all full Pools into the free pools list
|
||||||
|
for (Pool* p = cast(Pool*)m_fullPools, pnext; p; p = pnext) {
|
||||||
|
pnext = p.next;
|
||||||
|
p.next = cast(Pool*)m_freePools;
|
||||||
|
m_freePools = cast(Pool*)p;
|
||||||
|
}
|
||||||
|
|
||||||
|
// free up all pools
|
||||||
|
for (Pool* p = cast(Pool*)m_freePools; p; p = p.next)
|
||||||
|
p.remaining = p.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset()
|
||||||
|
{
|
||||||
|
version(VibeManualMemoryManagement){
|
||||||
|
freeAll();
|
||||||
|
Pool* pnext;
|
||||||
|
for (auto p = cast(Pool*)m_freePools; p; p = pnext) {
|
||||||
|
pnext = p.next;
|
||||||
|
m_baseAllocator.free(p.data);
|
||||||
|
m_baseAllocator.free((cast(void*)p)[0 .. AllocSize!Pool]);
|
||||||
|
}
|
||||||
|
m_freePools = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static destroy(T)(void* ptr)
|
||||||
|
{
|
||||||
|
static if( is(T == class) ) .destroy(cast(T)ptr);
|
||||||
|
else .destroy(*cast(T*)ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class FreeListAlloc : Allocator
|
||||||
|
{
|
||||||
|
nothrow:
|
||||||
|
private static struct FreeListSlot { FreeListSlot* next; }
|
||||||
|
private {
|
||||||
|
FreeListSlot* m_firstFree = null;
|
||||||
|
size_t m_nalloc = 0;
|
||||||
|
size_t m_nfree = 0;
|
||||||
|
Allocator m_baseAlloc;
|
||||||
|
immutable size_t m_elemSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(size_t elem_size, Allocator base_allocator)
|
||||||
|
{
|
||||||
|
assert(elem_size >= size_t.sizeof);
|
||||||
|
m_elemSize = elem_size;
|
||||||
|
m_baseAlloc = base_allocator;
|
||||||
|
logDebug_("Create FreeListAlloc %d", m_elemSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
@property size_t elementSize() const { return m_elemSize; }
|
||||||
|
|
||||||
|
void[] alloc(size_t sz)
|
||||||
|
{
|
||||||
|
assert(sz == m_elemSize, "Invalid allocation size.");
|
||||||
|
return alloc();
|
||||||
|
}
|
||||||
|
|
||||||
|
void[] alloc()
|
||||||
|
{
|
||||||
|
void[] mem;
|
||||||
|
if( m_firstFree ){
|
||||||
|
auto slot = m_firstFree;
|
||||||
|
m_firstFree = slot.next;
|
||||||
|
slot.next = null;
|
||||||
|
mem = (cast(void*)slot)[0 .. m_elemSize];
|
||||||
|
debug m_nfree--;
|
||||||
|
} else {
|
||||||
|
mem = m_baseAlloc.alloc(m_elemSize);
|
||||||
|
//logInfo("Alloc %d bytes: alloc: %d, free: %d", SZ, s_nalloc, s_nfree);
|
||||||
|
}
|
||||||
|
debug m_nalloc++;
|
||||||
|
//logInfo("Alloc %d bytes: alloc: %d, free: %d", SZ, s_nalloc, s_nfree);
|
||||||
|
return mem;
|
||||||
|
}
|
||||||
|
|
||||||
|
void[] realloc(void[] mem, size_t sz)
|
||||||
|
{
|
||||||
|
assert(mem.length == m_elemSize);
|
||||||
|
assert(sz == m_elemSize);
|
||||||
|
return mem;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free(void[] mem)
|
||||||
|
{
|
||||||
|
assert(mem.length == m_elemSize, "Memory block passed to free has wrong size.");
|
||||||
|
auto s = cast(FreeListSlot*)mem.ptr;
|
||||||
|
s.next = m_firstFree;
|
||||||
|
m_firstFree = s;
|
||||||
|
m_nalloc--;
|
||||||
|
m_nfree++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FreeListObjectAlloc(T, bool USE_GC = true, bool INIT = true)
|
||||||
|
{
|
||||||
|
enum ElemSize = AllocSize!T;
|
||||||
|
enum ElemSlotSize = max(AllocSize!T, Slot.sizeof);
|
||||||
|
|
||||||
|
static if( is(T == class) ){
|
||||||
|
alias TR = T;
|
||||||
|
} else {
|
||||||
|
alias TR = T*;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Slot { Slot* next; }
|
||||||
|
|
||||||
|
private static Slot* s_firstFree;
|
||||||
|
|
||||||
|
static TR alloc(ARGS...)(ARGS args)
|
||||||
|
{
|
||||||
|
void[] mem;
|
||||||
|
if (s_firstFree !is null) {
|
||||||
|
auto ret = s_firstFree;
|
||||||
|
s_firstFree = s_firstFree.next;
|
||||||
|
ret.next = null;
|
||||||
|
mem = (cast(void*)ret)[0 .. ElemSize];
|
||||||
|
} else {
|
||||||
|
//logInfo("alloc %s/%d", T.stringof, ElemSize);
|
||||||
|
mem = manualAllocator().alloc(ElemSlotSize);
|
||||||
|
static if( hasIndirections!T ) GC.addRange(mem.ptr, ElemSlotSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (INIT) return cast(TR)internalEmplace!(Unqual!T)(mem, args); // FIXME: this emplace has issues with qualified types, but Unqual!T may result in the wrong constructor getting called.
|
||||||
|
else return cast(TR)mem.ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void free(TR obj)
|
||||||
|
{
|
||||||
|
static if (INIT) {
|
||||||
|
scope (failure) assert(0, "You shouldn't throw in destructors");
|
||||||
|
auto objc = obj;
|
||||||
|
static if (is(TR == T*)) .destroy(*objc);//typeid(T).destroy(cast(void*)obj);
|
||||||
|
else .destroy(objc);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto sl = cast(Slot*)obj;
|
||||||
|
sl.next = s_firstFree;
|
||||||
|
s_firstFree = sl;
|
||||||
|
//static if( hasIndirections!T ) GC.removeRange(cast(void*)obj);
|
||||||
|
//manualAllocator().free((cast(void*)obj)[0 .. ElemSlotSize]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template AllocSize(T)
|
||||||
|
{
|
||||||
|
static if (is(T == class)) {
|
||||||
|
// workaround for a strange bug where AllocSize!SSLStream == 0: TODO: dustmite!
|
||||||
|
enum dummy = T.stringof ~ __traits(classInstanceSize, T).stringof;
|
||||||
|
enum AllocSize = __traits(classInstanceSize, T);
|
||||||
|
} else {
|
||||||
|
enum AllocSize = T.sizeof;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FreeListRef(T, bool INIT = true)
|
||||||
|
{
|
||||||
|
alias ObjAlloc = FreeListObjectAlloc!(T, true, INIT);
|
||||||
|
enum ElemSize = AllocSize!T;
|
||||||
|
|
||||||
|
static if( is(T == class) ){
|
||||||
|
alias TR = T;
|
||||||
|
} else {
|
||||||
|
alias TR = T*;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TR m_object;
|
||||||
|
private size_t m_magic = 0x1EE75817; // workaround for compiler bug
|
||||||
|
|
||||||
|
static FreeListRef opCall(ARGS...)(ARGS args)
|
||||||
|
{
|
||||||
|
//logInfo("refalloc %s/%d", T.stringof, ElemSize);
|
||||||
|
FreeListRef ret;
|
||||||
|
ret.m_object = ObjAlloc.alloc(args);
|
||||||
|
ret.refCount = 1;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
~this()
|
||||||
|
{
|
||||||
|
//if( m_object ) logInfo("~this!%s(): %d", T.stringof, this.refCount);
|
||||||
|
//if( m_object ) logInfo("ref %s destructor %d", T.stringof, refCount);
|
||||||
|
//else logInfo("ref %s destructor %d", T.stringof, 0);
|
||||||
|
clear();
|
||||||
|
m_magic = 0;
|
||||||
|
m_object = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this(this)
|
||||||
|
{
|
||||||
|
checkInvariants();
|
||||||
|
if( m_object ){
|
||||||
|
//if( m_object ) logInfo("this!%s(this): %d", T.stringof, this.refCount);
|
||||||
|
this.refCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void opAssign(FreeListRef other)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
m_object = other.m_object;
|
||||||
|
if( m_object ){
|
||||||
|
//logInfo("opAssign!%s(): %d", T.stringof, this.refCount);
|
||||||
|
refCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
checkInvariants();
|
||||||
|
if (m_object) {
|
||||||
|
if (--this.refCount == 0)
|
||||||
|
ObjAlloc.free(m_object);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_object = null;
|
||||||
|
m_magic = 0x1EE75817;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property const(TR) get() const { checkInvariants(); return m_object; }
|
||||||
|
@property TR get() { checkInvariants(); return m_object; }
|
||||||
|
alias get this;
|
||||||
|
|
||||||
|
private @property ref int refCount()
|
||||||
|
const {
|
||||||
|
auto ptr = cast(ubyte*)cast(void*)m_object;
|
||||||
|
ptr += ElemSize;
|
||||||
|
return *cast(int*)ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkInvariants()
|
||||||
|
const {
|
||||||
|
assert(m_magic == 0x1EE75817);
|
||||||
|
assert(!m_object || refCount > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void* extractUnalignedPointer(void* base) nothrow
|
||||||
|
{
|
||||||
|
ubyte misalign = *(cast(ubyte*)base-1);
|
||||||
|
assert(misalign <= Allocator.alignment);
|
||||||
|
return base - misalign;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void* adjustPointerAlignment(void* base) nothrow
|
||||||
|
{
|
||||||
|
ubyte misalign = Allocator.alignment - (cast(size_t)base & Allocator.alignmentMask);
|
||||||
|
base += misalign;
|
||||||
|
*(cast(ubyte*)base-1) = misalign;
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
void test_align(void* p, size_t adjustment) {
|
||||||
|
void* pa = adjustPointerAlignment(p);
|
||||||
|
assert((cast(size_t)pa & Allocator.alignmentMask) == 0, "Non-aligned pointer.");
|
||||||
|
assert(*(cast(ubyte*)pa-1) == adjustment, "Invalid adjustment "~to!string(p)~": "~to!string(*(cast(ubyte*)pa-1)));
|
||||||
|
void* pr = extractUnalignedPointer(pa);
|
||||||
|
assert(pr == p, "Recovered base != original");
|
||||||
|
}
|
||||||
|
void* ptr = .malloc(0x40);
|
||||||
|
ptr += Allocator.alignment - (cast(size_t)ptr & Allocator.alignmentMask);
|
||||||
|
test_align(ptr++, 0x10);
|
||||||
|
test_align(ptr++, 0x0F);
|
||||||
|
test_align(ptr++, 0x0E);
|
||||||
|
test_align(ptr++, 0x0D);
|
||||||
|
test_align(ptr++, 0x0C);
|
||||||
|
test_align(ptr++, 0x0B);
|
||||||
|
test_align(ptr++, 0x0A);
|
||||||
|
test_align(ptr++, 0x09);
|
||||||
|
test_align(ptr++, 0x08);
|
||||||
|
test_align(ptr++, 0x07);
|
||||||
|
test_align(ptr++, 0x06);
|
||||||
|
test_align(ptr++, 0x05);
|
||||||
|
test_align(ptr++, 0x04);
|
||||||
|
test_align(ptr++, 0x03);
|
||||||
|
test_align(ptr++, 0x02);
|
||||||
|
test_align(ptr++, 0x01);
|
||||||
|
test_align(ptr++, 0x10);
|
||||||
|
}
|
||||||
|
|
||||||
|
private size_t alignedSize(size_t sz) nothrow
|
||||||
|
{
|
||||||
|
return ((sz + Allocator.alignment - 1) / Allocator.alignment) * Allocator.alignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
foreach( i; 0 .. 20 ){
|
||||||
|
auto ia = alignedSize(i);
|
||||||
|
assert(ia >= i);
|
||||||
|
assert((ia & Allocator.alignmentMask) == 0);
|
||||||
|
assert(ia < i+Allocator.alignment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureValidMemory(void[] mem) nothrow
|
||||||
|
{
|
||||||
|
auto bytes = cast(ubyte[])mem;
|
||||||
|
swap(bytes[0], bytes[$-1]);
|
||||||
|
swap(bytes[0], bytes[$-1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// See issue #14194
|
||||||
|
private T internalEmplace(T, Args...)(void[] chunk, auto ref Args args)
|
||||||
|
if (is(T == class))
|
||||||
|
in {
|
||||||
|
import std.string, std.format;
|
||||||
|
assert(chunk.length >= T.sizeof,
|
||||||
|
format("emplace: Chunk size too small: %s < %s size = %s",
|
||||||
|
chunk.length, T.stringof, T.sizeof));
|
||||||
|
assert((cast(size_t) chunk.ptr) % T.alignof == 0,
|
||||||
|
format("emplace: Misaligned memory block (0x%X): it must be %s-byte aligned for type %s", chunk.ptr, T.alignof, T.stringof));
|
||||||
|
|
||||||
|
} body {
|
||||||
|
enum classSize = __traits(classInstanceSize, T);
|
||||||
|
auto result = cast(T) chunk.ptr;
|
||||||
|
|
||||||
|
// Initialize the object in its pre-ctor state
|
||||||
|
chunk[0 .. classSize] = typeid(T).init[];
|
||||||
|
|
||||||
|
// Call the ctor if any
|
||||||
|
static if (is(typeof(result.__ctor(args))))
|
||||||
|
{
|
||||||
|
// T defines a genuine constructor accepting args
|
||||||
|
// Go the classic route: write .init first, then call ctor
|
||||||
|
result.__ctor(args);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static assert(args.length == 0 && !is(typeof(&T.__ctor)),
|
||||||
|
"Don't know how to initialize an object of type "
|
||||||
|
~ T.stringof ~ " with arguments " ~ Args.stringof);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dittor
|
||||||
|
private auto internalEmplace(T, Args...)(void[] chunk, auto ref Args args)
|
||||||
|
if (!is(T == class))
|
||||||
|
in {
|
||||||
|
import std.string, std.format;
|
||||||
|
assert(chunk.length >= T.sizeof,
|
||||||
|
format("emplace: Chunk size too small: %s < %s size = %s",
|
||||||
|
chunk.length, T.stringof, T.sizeof));
|
||||||
|
assert((cast(size_t) chunk.ptr) % T.alignof == 0,
|
||||||
|
format("emplace: Misaligned memory block (0x%X): it must be %s-byte aligned for type %s", chunk.ptr, T.alignof, T.stringof));
|
||||||
|
|
||||||
|
} body {
|
||||||
|
return emplace(cast(T*)chunk.ptr, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logDebug_(ARGS...)(string msg, ARGS args) {}
|
235
source/vibe/internal/string.d
Normal file
235
source/vibe/internal/string.d
Normal file
|
@ -0,0 +1,235 @@
|
||||||
|
/**
|
||||||
|
Utility functions for string processing
|
||||||
|
|
||||||
|
Copyright: © 2012-2014 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig
|
||||||
|
*/
|
||||||
|
module vibe.internal.string;
|
||||||
|
|
||||||
|
public import std.string;
|
||||||
|
|
||||||
|
import vibe.internal.array;
|
||||||
|
import vibe.internal.memory;
|
||||||
|
|
||||||
|
import std.algorithm;
|
||||||
|
import std.array;
|
||||||
|
import std.ascii;
|
||||||
|
import std.format;
|
||||||
|
import std.uni;
|
||||||
|
import std.utf;
|
||||||
|
import core.exception;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Takes a string with possibly invalid UTF8 sequences and outputs a valid UTF8 string as near to
|
||||||
|
the original as possible.
|
||||||
|
*/
|
||||||
|
string sanitizeUTF8(in ubyte[] str)
|
||||||
|
@safe pure {
|
||||||
|
import std.utf;
|
||||||
|
auto ret = appender!string();
|
||||||
|
ret.reserve(str.length);
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
while (i < str.length) {
|
||||||
|
dchar ch = str[i];
|
||||||
|
try ch = std.utf.decode(cast(const(char[]))str, i);
|
||||||
|
catch( UTFException ){ i++; }
|
||||||
|
//catch( AssertError ){ i++; }
|
||||||
|
char[4] dst;
|
||||||
|
auto len = std.utf.encode(dst, ch);
|
||||||
|
ret.put(dst[0 .. len]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Strips the byte order mark of an UTF8 encoded string.
|
||||||
|
This is useful when the string is coming from a file.
|
||||||
|
*/
|
||||||
|
string stripUTF8Bom(string str)
|
||||||
|
@safe pure nothrow {
|
||||||
|
if (str.length >= 3 && str[0 .. 3] == [0xEF, 0xBB, 0xBF])
|
||||||
|
return str[3 ..$];
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks if all characters in 'str' are contained in 'chars'.
|
||||||
|
*/
|
||||||
|
bool allOf(string str, string chars)
|
||||||
|
@safe pure {
|
||||||
|
foreach (dchar ch; str)
|
||||||
|
if (!chars.canFind(ch))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptrdiff_t indexOfCT(Char)(in Char[] s, dchar c, CaseSensitive cs = CaseSensitive.yes)
|
||||||
|
@safe pure {
|
||||||
|
if (__ctfe) {
|
||||||
|
if (cs == CaseSensitive.yes) {
|
||||||
|
foreach (i, dchar ch; s)
|
||||||
|
if (ch == c)
|
||||||
|
return i;
|
||||||
|
} else {
|
||||||
|
c = std.uni.toLower(c);
|
||||||
|
foreach (i, dchar ch; s)
|
||||||
|
if (std.uni.toLower(ch) == c)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
} else return std.string.indexOf(s, c, cs);
|
||||||
|
}
|
||||||
|
ptrdiff_t indexOfCT(Char)(in Char[] s, in Char[] needle)
|
||||||
|
{
|
||||||
|
if (__ctfe) {
|
||||||
|
if (s.length < needle.length) return -1;
|
||||||
|
foreach (i; 0 .. s.length - needle.length)
|
||||||
|
if (s[i .. i+needle.length] == needle)
|
||||||
|
return i;
|
||||||
|
return -1;
|
||||||
|
} else return std.string.indexOf(s, needle);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks if any character in 'str' is contained in 'chars'.
|
||||||
|
*/
|
||||||
|
bool anyOf(string str, string chars)
|
||||||
|
@safe pure {
|
||||||
|
foreach (ch; str)
|
||||||
|
if (chars.canFind(ch))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// ASCII whitespace trimming (space and tab)
|
||||||
|
string stripLeftA(string s)
|
||||||
|
@safe pure nothrow {
|
||||||
|
while (s.length > 0 && (s[0] == ' ' || s[0] == '\t'))
|
||||||
|
s = s[1 .. $];
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ASCII whitespace trimming (space and tab)
|
||||||
|
string stripRightA(string s)
|
||||||
|
@safe pure nothrow {
|
||||||
|
while (s.length > 0 && (s[$-1] == ' ' || s[$-1] == '\t'))
|
||||||
|
s = s[0 .. $-1];
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ASCII whitespace trimming (space and tab)
|
||||||
|
string stripA(string s)
|
||||||
|
@safe pure nothrow {
|
||||||
|
return stripLeftA(stripRightA(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finds the first occurence of any of the characters in `chars`
|
||||||
|
sizediff_t indexOfAny(string str, string chars)
|
||||||
|
@safe pure {
|
||||||
|
foreach (i, char ch; str)
|
||||||
|
if (chars.canFind(ch))
|
||||||
|
return i;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
alias countUntilAny = indexOfAny;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Finds the closing bracket (works with any of '[', '$(LPAREN)', '<', '{').
|
||||||
|
|
||||||
|
Params:
|
||||||
|
str = input string
|
||||||
|
nested = whether to skip nested brackets
|
||||||
|
Returns:
|
||||||
|
The index of the closing bracket or -1 for unbalanced strings
|
||||||
|
and strings that don't start with a bracket.
|
||||||
|
*/
|
||||||
|
sizediff_t matchBracket(string str, bool nested = true)
|
||||||
|
@safe pure nothrow {
|
||||||
|
if (str.length < 2) return -1;
|
||||||
|
|
||||||
|
char open = str[0], close = void;
|
||||||
|
switch (str[0]) {
|
||||||
|
case '[': close = ']'; break;
|
||||||
|
case '(': close = ')'; break;
|
||||||
|
case '<': close = '>'; break;
|
||||||
|
case '{': close = '}'; break;
|
||||||
|
default: return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t level = 1;
|
||||||
|
foreach (i, char c; str[1 .. $]) {
|
||||||
|
if (nested && c == open) ++level;
|
||||||
|
else if (c == close) --level;
|
||||||
|
if (level == 0) return i + 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
static struct Test { string str; sizediff_t res; }
|
||||||
|
enum tests = [
|
||||||
|
Test("[foo]", 4), Test("<bar>", 4), Test("{baz}", 4),
|
||||||
|
Test("[", -1), Test("[foo", -1), Test("ab[f]", -1),
|
||||||
|
Test("[foo[bar]]", 9), Test("[foo{bar]]", 8),
|
||||||
|
];
|
||||||
|
foreach (test; tests)
|
||||||
|
assert(matchBracket(test.str) == test.res);
|
||||||
|
assert(matchBracket("[foo[bar]]", false) == 8);
|
||||||
|
static assert(matchBracket("[foo]") == 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Same as std.string.format, just using an allocator.
|
||||||
|
string formatAlloc(ARGS...)(Allocator alloc, string fmt, ARGS args)
|
||||||
|
{
|
||||||
|
auto app = AllocAppender!string(alloc);
|
||||||
|
formattedWrite(&app, fmt, args);
|
||||||
|
return app.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Special version of icmp() with optimization for ASCII characters
|
||||||
|
int icmp2(string a, string b)
|
||||||
|
@safe pure {
|
||||||
|
size_t i = 0, j = 0;
|
||||||
|
|
||||||
|
// fast skip equal prefix
|
||||||
|
size_t min_len = min(a.length, b.length);
|
||||||
|
while( i < min_len && a[i] == b[i] ) i++;
|
||||||
|
if( i > 0 && (a[i-1] & 0x80) ) i--; // don't stop half-way in a UTF-8 sequence
|
||||||
|
j = i;
|
||||||
|
|
||||||
|
// compare the differing character and the rest of the string
|
||||||
|
while(i < a.length && j < b.length){
|
||||||
|
uint ac = cast(uint)a[i];
|
||||||
|
uint bc = cast(uint)b[j];
|
||||||
|
if( !((ac | bc) & 0x80) ){
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
if( ac >= 'A' && ac <= 'Z' ) ac += 'a' - 'A';
|
||||||
|
if( bc >= 'A' && bc <= 'Z' ) bc += 'a' - 'A';
|
||||||
|
if( ac < bc ) return -1;
|
||||||
|
else if( ac > bc ) return 1;
|
||||||
|
} else {
|
||||||
|
dchar acp = decode(a, i);
|
||||||
|
dchar bcp = decode(b, j);
|
||||||
|
if( acp != bcp ){
|
||||||
|
acp = std.uni.toLower(acp);
|
||||||
|
bcp = std.uni.toLower(bcp);
|
||||||
|
if( acp < bcp ) return -1;
|
||||||
|
else if( acp > bcp ) return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( i < a.length ) return 1;
|
||||||
|
else if( j < b.length ) return -1;
|
||||||
|
|
||||||
|
assert(i == a.length || j == b.length, "Strings equal but we didn't fully compare them!?");
|
||||||
|
return 0;
|
||||||
|
}
|
384
source/vibe/internal/traits.d
Normal file
384
source/vibe/internal/traits.d
Normal file
|
@ -0,0 +1,384 @@
|
||||||
|
/**
|
||||||
|
Extensions to `std.traits` module of Phobos. Some may eventually make it into Phobos,
|
||||||
|
some are dirty hacks that work only for vibe.d
|
||||||
|
|
||||||
|
Copyright: © 2012 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Sönke Ludwig, Михаил Страшун
|
||||||
|
*/
|
||||||
|
|
||||||
|
module vibe.internal.traits;
|
||||||
|
|
||||||
|
import vibe.internal.typetuple;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks if given type is a getter function type
|
||||||
|
|
||||||
|
Returns: `true` if argument is a getter
|
||||||
|
*/
|
||||||
|
template isPropertyGetter(T...)
|
||||||
|
if (T.length == 1)
|
||||||
|
{
|
||||||
|
import std.traits : functionAttributes, FunctionAttribute, ReturnType,
|
||||||
|
isSomeFunction;
|
||||||
|
static if (isSomeFunction!(T[0])) {
|
||||||
|
enum isPropertyGetter =
|
||||||
|
(functionAttributes!(T[0]) & FunctionAttribute.property) != 0
|
||||||
|
&& !is(ReturnType!T == void);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
enum isPropertyGetter = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
interface Test
|
||||||
|
{
|
||||||
|
@property int getter();
|
||||||
|
@property void setter(int);
|
||||||
|
int simple();
|
||||||
|
}
|
||||||
|
|
||||||
|
static assert(isPropertyGetter!(typeof(&Test.getter)));
|
||||||
|
static assert(!isPropertyGetter!(typeof(&Test.setter)));
|
||||||
|
static assert(!isPropertyGetter!(typeof(&Test.simple)));
|
||||||
|
static assert(!isPropertyGetter!int);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks if given type is a setter function type
|
||||||
|
|
||||||
|
Returns: `true` if argument is a setter
|
||||||
|
*/
|
||||||
|
template isPropertySetter(T...)
|
||||||
|
if (T.length == 1)
|
||||||
|
{
|
||||||
|
import std.traits : functionAttributes, FunctionAttribute, ReturnType,
|
||||||
|
isSomeFunction;
|
||||||
|
|
||||||
|
static if (isSomeFunction!(T[0])) {
|
||||||
|
enum isPropertySetter =
|
||||||
|
(functionAttributes!(T) & FunctionAttribute.property) != 0
|
||||||
|
&& is(ReturnType!(T[0]) == void);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
enum isPropertySetter = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
interface Test
|
||||||
|
{
|
||||||
|
@property int getter();
|
||||||
|
@property void setter(int);
|
||||||
|
int simple();
|
||||||
|
}
|
||||||
|
|
||||||
|
static assert(isPropertySetter!(typeof(&Test.setter)));
|
||||||
|
static assert(!isPropertySetter!(typeof(&Test.getter)));
|
||||||
|
static assert(!isPropertySetter!(typeof(&Test.simple)));
|
||||||
|
static assert(!isPropertySetter!int);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Deduces single base interface for a type. Multiple interfaces
|
||||||
|
will result in compile-time error.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
T = interface or class type
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
T if it is an interface. If T is a class, interface it implements.
|
||||||
|
*/
|
||||||
|
template baseInterface(T)
|
||||||
|
if (is(T == interface) || is(T == class))
|
||||||
|
{
|
||||||
|
import std.traits : InterfacesTuple;
|
||||||
|
|
||||||
|
static if (is(T == interface)) {
|
||||||
|
alias baseInterface = T;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias Ifaces = InterfacesTuple!T;
|
||||||
|
static assert (
|
||||||
|
Ifaces.length == 1,
|
||||||
|
"Type must be either provided as an interface or implement only one interface"
|
||||||
|
);
|
||||||
|
alias baseInterface = Ifaces[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
interface I1 { }
|
||||||
|
class A : I1 { }
|
||||||
|
interface I2 { }
|
||||||
|
class B : I1, I2 { }
|
||||||
|
|
||||||
|
static assert (is(baseInterface!I1 == I1));
|
||||||
|
static assert (is(baseInterface!A == I1));
|
||||||
|
static assert (!is(typeof(baseInterface!B)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Determins if a member is a public, non-static data field.
|
||||||
|
*/
|
||||||
|
template isRWPlainField(T, string M)
|
||||||
|
{
|
||||||
|
static if (!isRWField!(T, M)) enum isRWPlainField = false;
|
||||||
|
else {
|
||||||
|
//pragma(msg, T.stringof~"."~M~":"~typeof(__traits(getMember, T, M)).stringof);
|
||||||
|
enum isRWPlainField = __traits(compiles, *(&__traits(getMember, Tgen!T(), M)) = *(&__traits(getMember, Tgen!T(), M)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Determines if a member is a public, non-static, de-facto data field.
|
||||||
|
|
||||||
|
In addition to plain data fields, R/W properties are also accepted.
|
||||||
|
*/
|
||||||
|
template isRWField(T, string M)
|
||||||
|
{
|
||||||
|
import std.traits;
|
||||||
|
import std.typetuple;
|
||||||
|
|
||||||
|
static void testAssign()() {
|
||||||
|
T t = void;
|
||||||
|
__traits(getMember, t, M) = __traits(getMember, t, M);
|
||||||
|
}
|
||||||
|
|
||||||
|
// reject type aliases
|
||||||
|
static if (is(TypeTuple!(__traits(getMember, T, M)))) enum isRWField = false;
|
||||||
|
// reject non-public members
|
||||||
|
else static if (!isPublicMember!(T, M)) enum isRWField = false;
|
||||||
|
// reject static members
|
||||||
|
else static if (!isNonStaticMember!(T, M)) enum isRWField = false;
|
||||||
|
// reject non-typed members
|
||||||
|
else static if (!is(typeof(__traits(getMember, T, M)))) enum isRWField = false;
|
||||||
|
// reject void typed members (includes templates)
|
||||||
|
else static if (is(typeof(__traits(getMember, T, M)) == void)) enum isRWField = false;
|
||||||
|
// reject non-assignable members
|
||||||
|
else static if (!__traits(compiles, testAssign!()())) enum isRWField = false;
|
||||||
|
else static if (anySatisfy!(isSomeFunction, __traits(getMember, T, M))) {
|
||||||
|
// If M is a function, reject if not @property or returns by ref
|
||||||
|
private enum FA = functionAttributes!(__traits(getMember, T, M));
|
||||||
|
enum isRWField = (FA & FunctionAttribute.property) != 0;
|
||||||
|
} else {
|
||||||
|
enum isRWField = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
import std.algorithm;
|
||||||
|
|
||||||
|
struct S {
|
||||||
|
alias a = int; // alias
|
||||||
|
int i; // plain RW field
|
||||||
|
enum j = 42; // manifest constant
|
||||||
|
static int k = 42; // static field
|
||||||
|
private int privateJ; // private RW field
|
||||||
|
|
||||||
|
this(Args...)(Args args) {}
|
||||||
|
|
||||||
|
// read-write property (OK)
|
||||||
|
@property int p1() { return privateJ; }
|
||||||
|
@property void p1(int j) { privateJ = j; }
|
||||||
|
// read-only property (NO)
|
||||||
|
@property int p2() { return privateJ; }
|
||||||
|
// write-only property (NO)
|
||||||
|
@property void p3(int value) { privateJ = value; }
|
||||||
|
// ref returning property (OK)
|
||||||
|
@property ref int p4() { return i; }
|
||||||
|
// parameter-less template property (OK)
|
||||||
|
@property ref int p5()() { return i; }
|
||||||
|
// not treated as a property by DMD, so not a field
|
||||||
|
@property int p6()() { return privateJ; }
|
||||||
|
@property void p6(int j)() { privateJ = j; }
|
||||||
|
|
||||||
|
static @property int p7() { return k; }
|
||||||
|
static @property void p7(int value) { k = value; }
|
||||||
|
|
||||||
|
ref int f1() { return i; } // ref returning function (no field)
|
||||||
|
|
||||||
|
int f2(Args...)(Args args) { return i; }
|
||||||
|
|
||||||
|
ref int f3(Args...)(Args args) { return i; }
|
||||||
|
|
||||||
|
void someMethod() {}
|
||||||
|
|
||||||
|
ref int someTempl()() { return i; }
|
||||||
|
}
|
||||||
|
|
||||||
|
enum plainFields = ["i"];
|
||||||
|
enum fields = ["i", "p1", "p4", "p5"];
|
||||||
|
|
||||||
|
foreach (mem; __traits(allMembers, S)) {
|
||||||
|
static if (isRWField!(S, mem)) static assert(fields.canFind(mem), mem~" detected as field.");
|
||||||
|
else static assert(!fields.canFind(mem), mem~" not detected as field.");
|
||||||
|
|
||||||
|
static if (isRWPlainField!(S, mem)) static assert(plainFields.canFind(mem), mem~" not detected as plain field.");
|
||||||
|
else static assert(!plainFields.canFind(mem), mem~" not detected as plain field.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
package T Tgen(T)(){ return T.init; }
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Tests if the protection of a member is public.
|
||||||
|
*/
|
||||||
|
template isPublicMember(T, string M)
|
||||||
|
{
|
||||||
|
import std.algorithm, std.typetuple : TypeTuple;
|
||||||
|
|
||||||
|
static if (!__traits(compiles, TypeTuple!(__traits(getMember, T, M)))) enum isPublicMember = false;
|
||||||
|
else {
|
||||||
|
alias MEM = TypeTuple!(__traits(getMember, T, M));
|
||||||
|
enum isPublicMember = __traits(getProtection, MEM).among("public", "export");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
class C {
|
||||||
|
int a;
|
||||||
|
export int b;
|
||||||
|
protected int c;
|
||||||
|
private int d;
|
||||||
|
package int e;
|
||||||
|
void f() {}
|
||||||
|
static void g() {}
|
||||||
|
private void h() {}
|
||||||
|
private static void i() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
static assert (isPublicMember!(C, "a"));
|
||||||
|
static assert (isPublicMember!(C, "b"));
|
||||||
|
static assert (!isPublicMember!(C, "c"));
|
||||||
|
static assert (!isPublicMember!(C, "d"));
|
||||||
|
static assert (!isPublicMember!(C, "e"));
|
||||||
|
static assert (isPublicMember!(C, "f"));
|
||||||
|
static assert (isPublicMember!(C, "g"));
|
||||||
|
static assert (!isPublicMember!(C, "h"));
|
||||||
|
static assert (!isPublicMember!(C, "i"));
|
||||||
|
|
||||||
|
struct S {
|
||||||
|
int a;
|
||||||
|
export int b;
|
||||||
|
private int d;
|
||||||
|
package int e;
|
||||||
|
}
|
||||||
|
static assert (isPublicMember!(S, "a"));
|
||||||
|
static assert (isPublicMember!(S, "b"));
|
||||||
|
static assert (!isPublicMember!(S, "d"));
|
||||||
|
static assert (!isPublicMember!(S, "e"));
|
||||||
|
|
||||||
|
S s;
|
||||||
|
s.a = 21;
|
||||||
|
assert(s.a == 21);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Tests if a member requires $(D this) to be used.
|
||||||
|
*/
|
||||||
|
template isNonStaticMember(T, string M)
|
||||||
|
{
|
||||||
|
import std.typetuple;
|
||||||
|
import std.traits;
|
||||||
|
|
||||||
|
alias MF = TypeTuple!(__traits(getMember, T, M));
|
||||||
|
static if (M.length == 0) {
|
||||||
|
enum isNonStaticMember = false;
|
||||||
|
} else static if (anySatisfy!(isSomeFunction, MF)) {
|
||||||
|
enum isNonStaticMember = !__traits(isStaticFunction, MF);
|
||||||
|
} else {
|
||||||
|
enum isNonStaticMember = !__traits(compiles, (){ auto x = __traits(getMember, T, M); }());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest { // normal fields
|
||||||
|
struct S {
|
||||||
|
int a;
|
||||||
|
static int b;
|
||||||
|
enum c = 42;
|
||||||
|
void f();
|
||||||
|
static void g();
|
||||||
|
ref int h() { return a; }
|
||||||
|
static ref int i() { return b; }
|
||||||
|
}
|
||||||
|
static assert(isNonStaticMember!(S, "a"));
|
||||||
|
static assert(!isNonStaticMember!(S, "b"));
|
||||||
|
static assert(!isNonStaticMember!(S, "c"));
|
||||||
|
static assert(isNonStaticMember!(S, "f"));
|
||||||
|
static assert(!isNonStaticMember!(S, "g"));
|
||||||
|
static assert(isNonStaticMember!(S, "h"));
|
||||||
|
static assert(!isNonStaticMember!(S, "i"));
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest { // tuple fields
|
||||||
|
struct S(T...) {
|
||||||
|
T a;
|
||||||
|
static T b;
|
||||||
|
}
|
||||||
|
|
||||||
|
alias T = S!(int, float);
|
||||||
|
auto p = T.b;
|
||||||
|
static assert(isNonStaticMember!(T, "a"));
|
||||||
|
static assert(!isNonStaticMember!(T, "b"));
|
||||||
|
|
||||||
|
alias U = S!();
|
||||||
|
static assert(!isNonStaticMember!(U, "a"));
|
||||||
|
static assert(!isNonStaticMember!(U, "b"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Tests if a Group of types is implicitly convertible to a Group of target types.
|
||||||
|
*/
|
||||||
|
bool areConvertibleTo(alias TYPES, alias TARGET_TYPES)()
|
||||||
|
if (isGroup!TYPES && isGroup!TARGET_TYPES)
|
||||||
|
{
|
||||||
|
static assert(TYPES.expand.length == TARGET_TYPES.expand.length);
|
||||||
|
foreach (i, V; TYPES.expand)
|
||||||
|
if (!is(V : TARGET_TYPES.expand[i]))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test if the type $(D DG) is a correct delegate for an opApply where the
|
||||||
|
/// key/index is of type $(D TKEY) and the value of type $(D TVALUE).
|
||||||
|
template isOpApplyDg(DG, TKEY, TVALUE) {
|
||||||
|
import std.traits;
|
||||||
|
static if (is(DG == delegate) && is(ReturnType!DG : int)) {
|
||||||
|
private alias PTT = ParameterTypeTuple!(DG);
|
||||||
|
private alias PSCT = ParameterStorageClassTuple!(DG);
|
||||||
|
private alias STC = ParameterStorageClass;
|
||||||
|
// Just a value
|
||||||
|
static if (PTT.length == 1) {
|
||||||
|
enum isOpApplyDg = (is(PTT[0] == TVALUE));
|
||||||
|
} else static if (PTT.length == 2) {
|
||||||
|
enum isOpApplyDg = (is(PTT[0] == TKEY))
|
||||||
|
&& (is(PTT[1] == TVALUE));
|
||||||
|
} else
|
||||||
|
enum isOpApplyDg = false;
|
||||||
|
} else {
|
||||||
|
enum isOpApplyDg = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
static assert(isOpApplyDg!(int delegate(int, string), int, string));
|
||||||
|
static assert(isOpApplyDg!(int delegate(ref int, ref string), int, string));
|
||||||
|
static assert(isOpApplyDg!(int delegate(int, ref string), int, string));
|
||||||
|
static assert(isOpApplyDg!(int delegate(ref int, string), int, string));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Synchronized statements are logically nothrow but dmd still marks them as throwing.
|
||||||
|
// DMD#4115, Druntime#1013, Druntime#1021, Phobos#2704
|
||||||
|
import core.sync.mutex : Mutex;
|
||||||
|
enum synchronizedIsNothrow = __traits(compiles, (Mutex m) nothrow { synchronized(m) {} });
|
123
source/vibe/internal/typetuple.d
Normal file
123
source/vibe/internal/typetuple.d
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
/**
|
||||||
|
Additions to std.typetuple pending for inclusion into Phobos.
|
||||||
|
|
||||||
|
Copyright: © 2013 RejectedSoftware e.K.
|
||||||
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
|
Authors: Михаил Страшун
|
||||||
|
*/
|
||||||
|
|
||||||
|
module vibe.internal.typetuple;
|
||||||
|
|
||||||
|
import std.typetuple;
|
||||||
|
import std.traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
TypeTuple which does not auto-expand.
|
||||||
|
|
||||||
|
Useful when you need
|
||||||
|
to multiple several type tuples as different template argument
|
||||||
|
list parameters, without merging those.
|
||||||
|
*/
|
||||||
|
template Group(T...)
|
||||||
|
{
|
||||||
|
alias expand = T;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
alias group = Group!(int, double, string);
|
||||||
|
static assert (!is(typeof(group.length)));
|
||||||
|
static assert (group.expand.length == 3);
|
||||||
|
static assert (is(group.expand[1] == double));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
template isGroup(T...)
|
||||||
|
{
|
||||||
|
static if (T.length != 1) enum isGroup = false;
|
||||||
|
else enum isGroup =
|
||||||
|
!is(T[0]) && is(typeof(T[0]) == void) // does not evaluate to something
|
||||||
|
&& is(typeof(T[0].expand.length) : size_t) // expands to something with length
|
||||||
|
&& !is(typeof(&(T[0].expand))); // expands to not addressable
|
||||||
|
}
|
||||||
|
|
||||||
|
version (unittest) // NOTE: GDC complains about template definitions in unittest blocks
|
||||||
|
{
|
||||||
|
alias group = Group!(int, double, string);
|
||||||
|
alias group2 = Group!();
|
||||||
|
|
||||||
|
template Fake(T...)
|
||||||
|
{
|
||||||
|
int[] expand;
|
||||||
|
}
|
||||||
|
alias fake = Fake!(int, double, string);
|
||||||
|
|
||||||
|
alias fake2 = TypeTuple!(int, double, string);
|
||||||
|
|
||||||
|
static assert (isGroup!group);
|
||||||
|
static assert (isGroup!group2);
|
||||||
|
static assert (!isGroup!fake);
|
||||||
|
static assert (!isGroup!fake2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Copied from Phobos as it is private there.
|
||||||
|
*/
|
||||||
|
private template isSame(ab...)
|
||||||
|
if (ab.length == 2)
|
||||||
|
{
|
||||||
|
static if (is(ab[0]) && is(ab[1]))
|
||||||
|
{
|
||||||
|
enum isSame = is(ab[0] == ab[1]);
|
||||||
|
}
|
||||||
|
else static if (!is(ab[0]) &&
|
||||||
|
!is(ab[1]) &&
|
||||||
|
is(typeof(ab[0] == ab[1]) == bool) &&
|
||||||
|
(ab[0] == ab[1]))
|
||||||
|
{
|
||||||
|
static if (!__traits(compiles, &ab[0]) ||
|
||||||
|
!__traits(compiles, &ab[1]))
|
||||||
|
enum isSame = (ab[0] == ab[1]);
|
||||||
|
else
|
||||||
|
enum isSame = __traits(isSame, ab[0], ab[1]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
enum isSame = __traits(isSame, ab[0], ab[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Compares two groups for element identity
|
||||||
|
|
||||||
|
Params:
|
||||||
|
Group1, Group2 = any instances of `Group`
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
`true` if each element of Group1 is identical to
|
||||||
|
the one of Group2 at the same index
|
||||||
|
*/
|
||||||
|
template Compare(alias Group1, alias Group2)
|
||||||
|
if (isGroup!Group1 && isGroup!Group2)
|
||||||
|
{
|
||||||
|
private template implementation(size_t index)
|
||||||
|
{
|
||||||
|
static if (Group1.expand.length != Group2.expand.length) enum implementation = false;
|
||||||
|
else static if (index >= Group1.expand.length) enum implementation = true;
|
||||||
|
else static if (!isSame!(Group1.expand[index], Group2.expand[index])) enum implementation = false;
|
||||||
|
else enum implementation = implementation!(index+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Compare = implementation!0;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
alias one = Group!(int, double);
|
||||||
|
alias two = Group!(int, double);
|
||||||
|
alias three = Group!(double, int);
|
||||||
|
static assert (Compare!(one, two));
|
||||||
|
static assert (!Compare!(one, three));
|
||||||
|
}
|
Loading…
Reference in a new issue