Skip to content

Commit

Permalink
Merge pull request #2777 from vibe-d/vibe_container_allocators
Browse files Browse the repository at this point in the history
Deprecate vibe.internal.utilallocator and forward to vibe.container's version
  • Loading branch information
s-ludwig authored Feb 9, 2024
2 parents bf95d90 + 2217723 commit 1c062b4
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 197 deletions.
2 changes: 1 addition & 1 deletion data/vibe/data/bson.d
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,7 @@ struct BsonSerializer {

this(ubyte[] buffer)
@safe {
import vibe.internal.utilallocator;
import vibe.container.internal.utilallocator;
m_dst = () @trusted { return AllocAppender!(ubyte[])(vibeThreadAllocator(), buffer); } ();
}

Expand Down
6 changes: 3 additions & 3 deletions http/vibe/http/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import vibe.stream.wrapper : createConnectionProxyStream;
import vibe.stream.zlib;
import vibe.utils.array;
import vibe.utils.dictionarylist;
import vibe.internal.allocator;
import vibe.container.internal.utilallocator;
import vibe.internal.freelistref;
import vibe.internal.interfaceproxy : InterfaceProxy, interfaceProxy;

Expand Down Expand Up @@ -468,7 +468,7 @@ 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.internal.utilallocator: RegionListAllocator;
import vibe.container.internal.utilallocator: RegionListAllocator;
version (VibeManualMemoryManagement)
scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance);
else
Expand Down Expand Up @@ -542,7 +542,7 @@ final class HTTPClient {
*/
void request(scope void delegate(scope HTTPClientRequest req) requester, scope void delegate(scope HTTPClientResponse) responder)
@trusted { // scope new
import vibe.internal.utilallocator: RegionListAllocator;
import vibe.container.internal.utilallocator: RegionListAllocator;
version (VibeManualMemoryManagement)
scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance);
else
Expand Down
2 changes: 1 addition & 1 deletion http/vibe/http/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import vibe.stream.operations;
import vibe.textfilter.urlencode : urlEncode, urlDecode;
import vibe.utils.array;
import vibe.utils.dictionarylist;
import vibe.internal.allocator;
import vibe.container.internal.utilallocator;
import vibe.internal.freelistref;
import vibe.internal.interfaceproxy : InterfaceProxy, interfaceProxy;
import vibe.utils.string;
Expand Down
4 changes: 2 additions & 2 deletions http/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import vibe.stream.wrapper : ConnectionProxyStream, createConnectionProxyStream,
import vibe.stream.zlib;
import vibe.textfilter.urlencode;
import vibe.utils.array;
import vibe.internal.allocator;
import vibe.container.internal.utilallocator;
import vibe.internal.freelistref;
import vibe.utils.string;

Expand Down Expand Up @@ -243,7 +243,7 @@ void handleHTTPConnection(TCPConnection connection, HTTPServerContext context)
}

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

version (VibeManualMemoryManagement)
scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance);
Expand Down
2 changes: 1 addition & 1 deletion inet/vibe/inet/message.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import vibe.core.log;
import vibe.core.stream;
import vibe.stream.operations;
import vibe.utils.array;
import vibe.internal.allocator;
import vibe.container.internal.utilallocator;
import vibe.utils.string;
import vibe.utils.dictionarylist;

Expand Down
2 changes: 1 addition & 1 deletion redis/vibe/db/redis/redis.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import vibe.core.connectionpool;
import vibe.core.core;
import vibe.core.log;
import vibe.inet.url;
import vibe.internal.allocator;
import vibe.container.internal.utilallocator;
import vibe.internal.freelistref;
import vibe.stream.operations;
import std.conv;
Expand Down
2 changes: 1 addition & 1 deletion stream/vibe/stream/memory.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module vibe.stream.memory;

import vibe.core.stream;
import vibe.utils.array;
import vibe.internal.allocator;
import vibe.container.internal.utilallocator;

import std.algorithm;
import std.array;
Expand Down
2 changes: 1 addition & 1 deletion stream/vibe/stream/operations.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public import vibe.core.stream;

import vibe.core.log;
import vibe.utils.array : AllocAppender;
import vibe.internal.allocator;
import vibe.container.internal.utilallocator;
import vibe.internal.freelistref;
import vibe.stream.wrapper : ProxyStream;

Expand Down
2 changes: 1 addition & 1 deletion tls/vibe/stream/openssl.d
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ final class OpenSSLStream : TLSStream {
private void setClientALPN(string[] alpn_list)
{
logDebug("SetClientALPN: ", alpn_list);
import vibe.internal.allocator : dispose, makeArray, vibeThreadAllocator;
import vibe.container.internal.utilallocator : dispose, makeArray, vibeThreadAllocator;
ubyte[] alpn;
size_t len;
foreach (string alpn_val; alpn_list)
Expand Down
2 changes: 1 addition & 1 deletion utils/dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ description "Low level utility functionality"
targetType "library"
sourcePaths "."
importPaths "."
dependency "vibe-container" version="~>1.0"
dependency "vibe-container" version=">=1.0.2 <2.0.0-0"
2 changes: 1 addition & 1 deletion utils/vibe/internal/memory_legacy.d
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ final class DebugAllocator : Allocator {

this(Allocator base_allocator) nothrow @safe
{
import vibe.internal.utilallocator : Mallocator, allocatorObject;
import vibe.container.internal.utilallocator : Mallocator, allocatorObject;
m_baseAlloc = base_allocator;
m_blocks = HashMap!(void*, size_t)(() @trusted { return Mallocator.instance.allocatorObject; } ());
}
Expand Down
183 changes: 2 additions & 181 deletions utils/vibe/internal/utilallocator.d
Original file line number Diff line number Diff line change
@@ -1,183 +1,4 @@
deprecated("Import vibe.container.internal.utilallocator instead")
module vibe.internal.utilallocator;

public import stdx.allocator : allocatorObject, CAllocatorImpl, dispose,
expandArray, IAllocator, make, makeArray, shrinkArray, theAllocator;
public import stdx.allocator.mallocator;
public import stdx.allocator.building_blocks.affix_allocator;

// NOTE: this needs to be used instead of theAllocator due to Phobos issue 17564
@property IAllocator vibeThreadAllocator()
@safe nothrow @nogc {
import stdx.allocator.gc_allocator;
static IAllocator s_threadAllocator;
if (!s_threadAllocator)
s_threadAllocator = () @trusted { return allocatorObject(GCAllocator.instance); } ();
return s_threadAllocator;
}

final class RegionListAllocator(Allocator, bool leak = false) : IAllocator {
import vibe.internal.memory_legacy : AllocSize, alignedSize;
import std.algorithm.comparison : min, max;
import std.conv : emplace;

import std.typecons : Ternary;

static struct Pool { Pool* next; void[] data; void[] remaining; }
private {
Allocator m_baseAllocator;
Pool* m_freePools;
Pool* m_fullPools;
size_t m_poolSize;
}

this(size_t pool_size, Allocator base) @safe nothrow
{
m_poolSize = pool_size;
m_baseAllocator = base;
}

~this()
{
deallocateAll();
}

override @property uint alignment() const { return 0x10; }

@property size_t totalSize()
@safe nothrow @nogc {
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()
@safe nothrow @nogc {
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;
}

override void[] allocate(size_t sz, TypeInfo ti = null)
{
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.allocate(AllocSize!Pool);

p = emplace!Pool(cast(Pool*)pmem.ptr);
p.data = m_baseAllocator.allocate(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];
}

override void[] alignedAllocate(size_t n, uint a) { return null; }
override bool alignedReallocate(ref void[] b, size_t size, uint alignment) { return false; }
override void[] allocateAll() { return null; }
override @property Ternary empty() const { return m_fullPools !is null ? Ternary.no : Ternary.yes; }
override size_t goodAllocSize(size_t s) { return alignedSize(s); }

import std.traits : Parameters;
static if (is(Parameters!(IAllocator.resolveInternalPointer)[0] == const(void*))) {
override Ternary resolveInternalPointer(const void* p, ref void[] result) { return Ternary.unknown; }
} else {
override Ternary resolveInternalPointer(void* p, ref void[] result) { return Ternary.unknown; }
}
static if (is(Parameters!(IAllocator.owns)[0] == const(void[]))) {
override Ternary owns(const void[] b) { return Ternary.unknown; }
} else {
override Ternary owns(void[] b) { return Ternary.unknown; }
}


override bool reallocate(ref void[] arr, size_t newsize)
{
return expand(arr, newsize);
}

override bool expand(ref void[] arr, size_t newsize)
{
auto aligned_sz = alignedSize(arr.length);
auto aligned_newsz = alignedSize(newsize);

if (aligned_newsz <= aligned_sz) {
arr = arr[0 .. newsize]; // TODO: back up remaining
return true;
}

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!?");
arr = arr[0 .. newsize];
} else {
auto ret = allocate(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)];
arr = ret;
}
return true;
}

override bool deallocate(void[] mem)
{
return false;
}

override bool deallocateAll()
{
// 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;

Pool* pnext;
for (auto p = cast(Pool*)m_freePools; p; p = pnext) {
pnext = p.next;
static if (!leak) {
m_baseAllocator.deallocate(p.data);
m_baseAllocator.deallocate((cast(void*)p)[0 .. AllocSize!Pool]);
}
}
m_freePools = null;

return true;
}
}
public import vibe.container.internal.utilallocator;
2 changes: 1 addition & 1 deletion utils/vibe/utils/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
module vibe.utils.array;

import vibe.internal.utilallocator;
import vibe.container.internal.utilallocator;

import std.algorithm;
import std.range : isInputRange, isOutputRange;
Expand Down
2 changes: 1 addition & 1 deletion utils/vibe/utils/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module vibe.utils.string;
public import std.string;

import vibe.utils.array;
import vibe.internal.utilallocator;
import vibe.container.internal.utilallocator;

import std.algorithm;
import std.array;
Expand Down

0 comments on commit 1c062b4

Please sign in to comment.