From e7688361bafe72f1ffb55ee9eb54101b9c0547c7 Mon Sep 17 00:00:00 2001 From: Geod24 Date: Mon, 13 Jan 2020 17:01:04 +0900 Subject: [PATCH] Let malloc infer its attributes Malloc should not force '@safe' or '@nogc' on constructors which are not. For example, TaskPool's ctor is not '@nogc' but was assumed as such thanks to the delegate cast happening in malloc. Likewise, the ctor or the arguments might not be '@safe', or any other attributes, but they were be mistakenly marked as such. --- source/eventcore/internal/utils.d | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/source/eventcore/internal/utils.d b/source/eventcore/internal/utils.d index 3f37769..1756751 100644 --- a/source/eventcore/internal/utils.d +++ b/source/eventcore/internal/utils.d @@ -18,20 +18,18 @@ void print(ARGS...)(string str, ARGS args) } T mallocT(T, ARGS...)(ARGS args) -@trusted @nogc { +{ import core.stdc.stdlib : malloc; import std.conv : emplace; - enum size = __traits(classInstanceSize, T); - auto ret = cast(T)malloc(size); - static if (hasIndirections!T) - GC.addRange(cast(void*)ret, __traits(classInstanceSize, T)); - scope doit = { emplace!T((cast(void*)ret)[0 .. size], args); }; - static if (__traits(compiles, () nothrow { typeof(doit).init(); })) // NOTE: doing the typeof thing here, because LDC 1.7.0 otherwise thinks doit gets escaped here - (cast(void delegate() @nogc nothrow)doit)(); - else - (cast(void delegate() @nogc)doit)(); - return ret; + T ret = () @trusted { + enum size = __traits(classInstanceSize, T); + T r = cast(T)malloc(size); + static if (hasIndirections!T) + GC.addRange(cast(void*)r, size); + return r; + }(); + return emplace!T(ret, args); } void freeT(T)(ref T inst) @nogc