Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the code forward compatible with std.experimental.allocator #2782

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions http/vibe/http/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,8 @@ final class HTTPClient {
private void doProxyRequest(T, U)(ref T res, U requester, ref bool close_conn, ref bool has_body)
@trusted { // scope new
import std.conv : to;
import vibe.container.internal.utilallocator: RegionListAllocator;
version (VibeManualMemoryManagement)
scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance);
else
scope request_allocator = new RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance);
scope request_allocator = createRequestAllocator();
scope (exit) freeRequestAllocator(request_allocator);

res.dropBody();
scope(failure)
Expand Down Expand Up @@ -542,11 +539,8 @@ final class HTTPClient {
*/
void request(scope void delegate(scope HTTPClientRequest req) requester, scope void delegate(scope HTTPClientResponse) responder)
@trusted { // scope new
import vibe.container.internal.utilallocator: RegionListAllocator;
version (VibeManualMemoryManagement)
scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance);
else
scope request_allocator = new RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance);
scope request_allocator = createRequestAllocator();
scope (exit) freeRequestAllocator(request_allocator);

scope (failure) {
m_responding = false;
Expand Down Expand Up @@ -1030,7 +1024,7 @@ final class HTTPClientResponse : HTTPResponse {
m_closeConn = close_conn;
}

private void initialize(bool has_body, IAllocator alloc, SysTime connected_time = Clock.currTime(UTC()))
private void initialize(Allocator)(bool has_body, Allocator alloc, SysTime connected_time = Clock.currTime(UTC()))
{
scope(failure) finalize(true);

Expand Down
39 changes: 36 additions & 3 deletions http/vibe/http/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ final class ChunkedOutputStream : OutputStream {
}

/// private
this(InterfaceProxy!OutputStream stream, IAllocator alloc, bool dummy)
this(Allocator)(InterfaceProxy!OutputStream stream, Allocator alloc, bool dummy)
{
m_out = stream;
m_buffer = AllocAppender!(ubyte[])(alloc);
Expand Down Expand Up @@ -611,13 +611,23 @@ final class ChunkedOutputStream : OutputStream {
}

/// Creates a new `ChunkedInputStream` instance.
ChunkedOutputStream createChunkedOutputStream(OS)(OS destination_stream, IAllocator allocator = theAllocator()) if (isOutputStream!OS)
ChunkedOutputStream createChunkedOutputStream(OS)(OS destination_stream) if (isOutputStream!OS)
{
return createChunkedOutputStream(destination_stream, theAllocator());
}
/// ditto
ChunkedOutputStream createChunkedOutputStream(OS, Allocator)(OS destination_stream, Allocator allocator) if (isOutputStream!OS)
{
return new ChunkedOutputStream(interfaceProxy!OutputStream(destination_stream), allocator, true);
}

/// Creates a new `ChunkedOutputStream` instance.
FreeListRef!ChunkedOutputStream createChunkedOutputStreamFL(OS)(OS destination_stream, IAllocator allocator = theAllocator()) if (isOutputStream!OS)
FreeListRef!ChunkedOutputStream createChunkedOutputStreamFL(OS)(OS destination_stream) if (isOutputStream!OS)
{
return createChunkedOutputStreamFL(destination_stream, theAllocator());
}
/// ditto
FreeListRef!ChunkedOutputStream createChunkedOutputStreamFL(OS, Allocator)(OS destination_stream, Allocator allocator) if (isOutputStream!OS)
{
return FreeListRef!ChunkedOutputStream(interfaceProxy!OutputStream(destination_stream), allocator, true);
}
Expand Down Expand Up @@ -1069,3 +1079,26 @@ unittest {
*("foo" in m) = "baz";
assert(m["foo"] == "baz");
}


package auto createRequestAllocator()
{
import vibe.container.internal.utilallocator: RegionListAllocator;

static if (is(RegionListAllocator!(shared(GCAllocator), true) == struct)) {
version (VibeManualMemoryManagement)
return allocatorObject(RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance));
else
return allocatorObject(RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance));
} else {
version (VibeManualMemoryManagement)
return new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance);
else
return new RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance);
}
}

package void freeRequestAllocator(Allocator)(ref Allocator alloc)
{
destroy(alloc);
}
20 changes: 9 additions & 11 deletions http/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,8 @@ void handleHTTPConnection(TCPConnection connection, HTTPServerContext context)
}

() @trusted {
import vibe.container.internal.utilallocator: RegionListAllocator;

version (VibeManualMemoryManagement)
scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance);
else
scope request_allocator = new RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance);
scope request_allocator = createRequestAllocator();
scope (exit) freeRequestAllocator(request_allocator);

handleRequest(http_stream, connection, context, settings, keep_alive, request_allocator);
} ();
Expand Down Expand Up @@ -1183,11 +1179,13 @@ final class HTTPServerRequest : HTTPRequest {
Represents a HTTP response as sent from the server side.
*/
final class HTTPServerResponse : HTTPResponse {
alias Allocator = typeof(vibeThreadAllocator());

private {
InterfaceProxy!Stream m_conn;
InterfaceProxy!ConnectionStream m_rawConnection;
InterfaceProxy!OutputStream m_bodyWriter;
IAllocator m_requestAlloc;
Allocator m_requestAlloc;
FreeListRef!ChunkedOutputStream m_chunkedBodyWriter;
FreeListRef!CountingOutputStream m_countingWriter;
FreeListRef!ZlibOutputStream m_zlibOutputStream;
Expand All @@ -1201,13 +1199,13 @@ final class HTTPServerResponse : HTTPResponse {
}

static if (!is(Stream == InterfaceProxy!Stream)) {
this(Stream conn, ConnectionStream raw_connection, HTTPServerSettings settings, IAllocator req_alloc)
this(Stream conn, ConnectionStream raw_connection, HTTPServerSettings settings, Allocator req_alloc)
@safe scope {
this(InterfaceProxy!Stream(conn), InterfaceProxy!ConnectionStream(raw_connection), settings, req_alloc);
}
}

this(InterfaceProxy!Stream conn, InterfaceProxy!ConnectionStream raw_connection, HTTPServerSettings settings, IAllocator req_alloc)
this(InterfaceProxy!Stream conn, InterfaceProxy!ConnectionStream raw_connection, HTTPServerSettings settings, Allocator req_alloc)
@safe scope {
m_conn = conn;
m_rawConnection = raw_connection;
Expand Down Expand Up @@ -2105,7 +2103,7 @@ private HTTPListener listenHTTPPlain(HTTPServerSettings settings, HTTPServerRequ
private alias TLSStreamType = ReturnType!(createTLSStreamFL!(InterfaceProxy!Stream));


private bool handleRequest(InterfaceProxy!Stream http_stream, TCPConnection tcp_connection, HTTPServerContext listen_info, ref HTTPServerSettings settings, ref bool keep_alive, scope IAllocator request_allocator)
private bool handleRequest(Allocator)(InterfaceProxy!Stream http_stream, TCPConnection tcp_connection, HTTPServerContext listen_info, ref HTTPServerSettings settings, ref bool keep_alive, scope Allocator request_allocator)
@safe {
import std.algorithm.searching : canFind;

Expand Down Expand Up @@ -2375,7 +2373,7 @@ private bool handleRequest(InterfaceProxy!Stream http_stream, TCPConnection tcp_
}


private void parseRequestHeader(InputStream)(HTTPServerRequest req, InputStream http_stream, IAllocator alloc, ulong max_header_size, size_t max_header_line_size)
private void parseRequestHeader(InputStream, Allocator)(HTTPServerRequest req, InputStream http_stream, Allocator alloc, ulong max_header_size, size_t max_header_line_size)
if (isInputStream!InputStream)
{
auto stream = FreeListRef!LimitedHTTPInputStream(http_stream, max_header_size);
Expand Down
8 changes: 7 additions & 1 deletion inet/vibe/inet/message.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ import std.string;
alloc = Custom allocator to use for allocating strings
rfc822_compatible = Flag indicating that duplicate fields should be merged using a comma
*/
void parseRFC5322Header(InputStream)(InputStream input, ref InetHeaderMap dst, size_t max_line_length = 1000, IAllocator alloc = vibeThreadAllocator(), bool rfc822_compatible = true)
void parseRFC5322Header(InputStream)(InputStream input, ref InetHeaderMap dst, size_t max_line_length = 1000)
if (isInputStream!InputStream)
{
parseRFC5322Header(input, dst, max_line_length, vibeThreadAllocator());
}
/// ditto
void parseRFC5322Header(InputStream, Allocator)(InputStream input, ref InetHeaderMap dst, size_t max_line_length, Allocator alloc, bool rfc822_compatible = true)
if (isInputStream!InputStream)
{
string hdr, hdrvalue;
Expand Down
21 changes: 12 additions & 9 deletions stream/vibe/stream/memory.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import std.array;
import std.exception;
import std.typecons;

MemoryOutputStream createMemoryOutputStream(IAllocator alloc = vibeThreadAllocator())
MemoryOutputStream createMemoryOutputStream(Allocator)(Allocator alloc = vibeThreadAllocator())
@safe nothrow {
return new MemoryOutputStream(alloc, true);
return new MemoryOutputStream(alloc, MemoryOutputStream.Dummy.init);
}

/** Creates a new stream with the given data array as its contents.
Expand All @@ -39,23 +39,26 @@ MemoryStream createMemoryStream(ubyte[] data, bool writable = true, size_t initi
*/
final class MemoryOutputStream : OutputStream {
@safe:
private struct Dummy {}

private {
AllocAppender!(ubyte[]) m_destination;
}

deprecated("Use createMemoryOutputStream isntead.")
this(IAllocator alloc = vibeThreadAllocator())
{
this(alloc, true);
}

/// private
this(IAllocator alloc, bool dummy)
this(IAllocator alloc, Dummy)
nothrow {
m_destination = AllocAppender!(ubyte[])(alloc);
}

static if (is(RCIAllocator)) {
/// private
this(RCIAllocator alloc, Dummy)
nothrow {
m_destination = AllocAppender!(ubyte[])(alloc);
}
}

/// An array with all data written to the stream so far.
@property ubyte[] data() nothrow { return m_destination.data(); }

Expand Down
10 changes: 8 additions & 2 deletions stream/vibe/stream/operations.d
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ import core.time : Duration, seconds;
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", IAllocator alloc = vibeThreadAllocator()) /*@ufcs*/
ubyte[] readLine(InputStream)(InputStream stream, size_t max_bytes = size_t.max, string linesep = "\r\n") /*@ufcs*/
if (isInputStream!InputStream)
{
return readLine(stream, max_bytes, linesep, vibeThreadAllocator());
}
/// ditto
ubyte[] readLine(InputStream, Allocator)(InputStream stream, size_t max_bytes, string linesep, Allocator alloc) /*@ufcs*/
if (isInputStream!InputStream)
{
auto output = AllocAppender!(ubyte[])(alloc);
Expand Down Expand Up @@ -116,7 +122,7 @@ void readLine(R, InputStream)(InputStream stream, ref R dst, size_t max_bytes =
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, IAllocator alloc = vibeThreadAllocator()) /*@ufcs*/
ubyte[] readUntil(InputStream, Allocator)(InputStream stream, in ubyte[] end_marker, size_t max_bytes = size_t.max, Allocator alloc = vibeThreadAllocator()) /*@ufcs*/
if (isInputStream!InputStream)
{
auto output = AllocAppender!(ubyte[])(alloc);
Expand Down
Loading
Loading