Skip to content

Commit

Permalink
Merge pull request #385 from vibe-d/vibe_container_allocator
Browse files Browse the repository at this point in the history
Use vibe.container.internal.allocator
  • Loading branch information
s-ludwig authored Feb 9, 2024
2 parents a05716c + 899bc34 commit 5d79a5d
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 72 deletions.
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ copyright "Copyright © 2016-2020, Sönke Ludwig"
license "MIT"

dependency "eventcore" version="~>0.9.27"
dependency "vibe-container" version="~>1.0"
dependency "vibe-container" version=">=1.0.2 <2.0.0-0"

targetName "vibe_core"

Expand Down
2 changes: 1 addition & 1 deletion source/vibe/core/channel.d
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ struct Channel(T, size_t buffer_size = 100) {

private final class ChannelImpl(T, size_t buffer_size) {
import vibe.core.concurrency : isWeaklyIsolated;
import vibe.internal.allocator : Mallocator, makeGCSafe, disposeGCSafe;
import vibe.container.internal.utilallocator : Mallocator, makeGCSafe, disposeGCSafe;
static assert(isWeaklyIsolated!T, "Channel data type "~T.stringof~" is not safe to pass between threads.");

private {
Expand Down
2 changes: 1 addition & 1 deletion source/vibe/core/net.d
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ struct TCPConnection {
if (isInputStream!InputStream)
{
import std.algorithm.comparison : min;
import vibe.internal.allocator : theAllocator, makeArray, dispose;
import vibe.container.internal.utilallocator : theAllocator, makeArray, dispose;

scope buffer = () @trusted { return cast(ubyte[]) theAllocator.allocate(64*1024); }();
scope (exit) () @trusted { theAllocator.dispose(buffer); }();
Expand Down
2 changes: 1 addition & 1 deletion source/vibe/core/process.d
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ string collectOutput(InputStream)(InputStream stream, size_t nbytes = size_t.max
output.reserve(nbytes);
}

import vibe.internal.allocator : theAllocator, dispose;
import vibe.container.internal.utilallocator : theAllocator, dispose;

scope buffer = cast(ubyte[]) theAllocator.allocate(64*1024);
scope (exit) theAllocator.dispose(buffer);
Expand Down
2 changes: 1 addition & 1 deletion source/vibe/core/stream.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ulong pipe(InputStream, OutputStream)(scope InputStream source, scope OutputStre
ulong nbytes, PipeConfig config) @blocking @trusted
if (isOutputStream!OutputStream && isInputStream!InputStream)
{
import vibe.internal.allocator : Mallocator, makeArray, dispose;
import vibe.container.internal.utilallocator : Mallocator, makeArray, dispose;
import vibe.core.core : runTask;
import vibe.core.sync : LocalManualEvent, createManualEvent;
import vibe.core.task : InterruptException;
Expand Down
4 changes: 2 additions & 2 deletions source/vibe/core/sync.d
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ struct LocalManualEvent {

private void initialize()
nothrow {
import vibe.internal.allocator : Mallocator, makeGCSafe;
import vibe.container.internal.utilallocator : Mallocator, makeGCSafe;
m_waiter = () @trusted { return Mallocator.instance.makeGCSafe!Waiter; } ();
}

Expand All @@ -1153,7 +1153,7 @@ struct LocalManualEvent {

~this()
nothrow {
import vibe.internal.allocator : Mallocator, disposeGCSafe;
import vibe.container.internal.utilallocator : Mallocator, disposeGCSafe;
if (m_waiter) {
if (!m_waiter.releaseRef()) {
static if (__VERSION__ < 2087) scope (failure) assert(false);
Expand Down
63 changes: 2 additions & 61 deletions source/vibe/internal/allocator.d
Original file line number Diff line number Diff line change
@@ -1,63 +1,4 @@
deprecated("Import `vibe.container.internal.utilallocator` instead.")
module vibe.internal.allocator;

public import stdx.allocator;
public import stdx.allocator.building_blocks.allocator_list;
public import stdx.allocator.building_blocks.null_allocator;
public import stdx.allocator.building_blocks.region;
public import stdx.allocator.building_blocks.stats_collector;
public import stdx.allocator.gc_allocator;
public import stdx.allocator.mallocator;

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

auto makeGCSafe(T, Allocator, A...)(Allocator allocator, A args)
{
import core.memory : GC;
import std.traits : hasIndirections;

auto ret = allocator.make!T(args);
static if (is (T == class)) enum tsize = __traits(classInstanceSize, T);
else enum tsize = T.sizeof;
static if (hasIndirections!T)
() @trusted { GC.addRange(cast(void*)ret, tsize, typeid(T)); } ();
return ret;
}

void disposeGCSafe(T, Allocator)(Allocator allocator, T obj)
{
import core.memory : GC;
import std.traits : hasIndirections;

static if (hasIndirections!T)
GC.removeRange(cast(void*)obj);
allocator.dispose(obj);
}

void ensureNotInGC(T)(string info = null) nothrow
{
import core.exception : InvalidMemoryOperationError;
try
{
import core.memory : GC;
cast(void) GC.malloc(1);
return;
}
catch(InvalidMemoryOperationError e)
{
import core.stdc.stdio : fprintf, stderr;
import core.stdc.stdlib : exit;
fprintf(stderr,
"Error: clean-up of %s incorrectly depends on destructors called by the GC.\n",
T.stringof.ptr);
if (info)
fprintf(stderr, "Info: %s\n", info.ptr);
assert(false);
}
}
public import vibe.container.internal.utilallocator;
2 changes: 1 addition & 1 deletion source/vibe/internal/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
module vibe.internal.array;

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

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

import vibe.internal.allocator;
import vibe.container.internal.utilallocator;
import vibe.internal.traits : synchronizedIsNothrow;

import core.exception : OutOfMemoryError;
Expand Down
2 changes: 1 addition & 1 deletion source/vibe/internal/interfaceproxy.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module vibe.internal.interfaceproxy;

import vibe.internal.traits;
import vibe.internal.freelistref;
import vibe.internal.allocator;
import vibe.container.internal.utilallocator;
import std.algorithm.mutation : move, swap;
import std.meta : staticMap;
import std.traits : BaseTypeTuple;
Expand Down
2 changes: 1 addition & 1 deletion source/vibe/internal/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module vibe.internal.string;
public import std.string;

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

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

0 comments on commit 5d79a5d

Please sign in to comment.