From c8fe75b85b0f14fec9c71b321f3a70a46f6397b4 Mon Sep 17 00:00:00 2001 From: LunaTheFoxgirl Date: Mon, 13 May 2024 10:58:44 +0200 Subject: [PATCH] Make emplace assumeNoGC --- source/numem/io/stream/reader.d | 4 +-- source/numem/mem/package.d | 55 +++++++++++++++++---------------- source/numem/mem/utils.d | 25 +++++++++++++++ 3 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 source/numem/mem/utils.d diff --git a/source/numem/io/stream/reader.d b/source/numem/io/stream/reader.d index 03e4d1e..8357c8b 100644 --- a/source/numem/io/stream/reader.d +++ b/source/numem/io/stream/reader.d @@ -12,9 +12,9 @@ import numem.mem.vector; import std.traits; /** - A stream writer. + A stream reader. - Allows easy writing to a stream. + Allows easy reading from a stream. */ class StreamReader(Endianess endian) { @nogc nothrow: diff --git a/source/numem/mem/package.d b/source/numem/mem/package.d index 5785c0a..e6aba76 100644 --- a/source/numem/mem/package.d +++ b/source/numem/mem/package.d @@ -7,6 +7,7 @@ module numem.mem; import core.stdc.stdlib : free, exit, malloc; import std.traits; +import numem.mem.utils; version(Have_tinyd_rt) { private __gshared @@ -21,28 +22,6 @@ nothrow @nogc: // MANUAL MEMORY MANAGMENT // private { - // Based on code from dplug:core - // which is released under the Boost license. - - auto assumeNoGC(T) (T t) { - static if (isFunctionPointer!T || isDelegate!T) - { - enum attrs = functionAttributes!T | FunctionAttribute.nogc; - return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t; - } - else - static assert(false); - } - - auto assumeNothrowNoGC(T) (T t) { - static if (isFunctionPointer!T || isDelegate!T) - { - enum attrs = functionAttributes!T | FunctionAttribute.nogc | FunctionAttribute.nothrow_; - return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t; - } - else - static assert(false); - } version(minimal_rt) { @@ -128,9 +107,9 @@ T* nogc_new(T, Args...)(Args args) if (is(T == struct)) { T* obj = cast(T*)rawMemory; static if (hasUDA!(T, AllowInitEmpty) && args.length == 0) { - emplace!T(obj); + nogc_emplace!T(obj); } else { - emplace!T(obj, args); + nogc_emplace!T(obj, args); } return obj; @@ -142,6 +121,8 @@ T* nogc_new(T, Args...)(Args args) if (is(T == struct)) { Immediately exits the application if out of memory. */ T nogc_new(T, Args...)(Args args) if (is(T == class)) { + alias emplaceFunc = typeof(&emplace!T); + version(Have_tinyd_rt) { return (assumeNothrowNoGC(&__gc_new!(T, Args)))(args); } else version(minimal_rt) { @@ -155,10 +136,10 @@ T nogc_new(T, Args...)(Args args) if (is(T == class)) { } // Allocate class destructor list - emplace!_impl_destructorStruct(rawMemory[0..destructorObjSize], &_impl_destructorCall!T); + nogc_emplace!classDestructorList(rawMemory[0..destructorObjSize], &_impl_destructorCall!T); // Allocate class - T obj = emplace!T(rawMemory[destructorObjSize .. allocSize], args); + T obj = nogc_emplace!T(rawMemory[destructorObjSize .. allocSize], args); return obj; } else { @@ -168,7 +149,7 @@ T nogc_new(T, Args...)(Args args) if (is(T == class)) { exit(-1); } - return emplace!T(rawMemory[0 .. allocSize], args); + return nogc_emplace!T(rawMemory[0 .. allocSize], args); } } @@ -269,4 +250,24 @@ void nogc_delete(T)(ref T obj_) { } } } +} + +/** + nogc emplace function +*/ +auto nogc_emplace(T, Args...)(T* chunk, Args args) { + alias t = typeof(&emplace!(T, Args)); + return assumeNothrowNoGC!t(&emplace!(T, Args))(chunk, args); +} + +/** + nogc emplace function +*/ +auto nogc_emplace(T, Args...)(void[] chunk, Args args) { + alias t = T function(void[], Args); + auto ifunc = (void[] chunk, Args args) { + return emplace!(T, Args)(chunk, args); + }; + + return assumeNothrowNoGC!t(ifunc)(chunk, args); } \ No newline at end of file diff --git a/source/numem/mem/utils.d b/source/numem/mem/utils.d new file mode 100644 index 0000000..ea43712 --- /dev/null +++ b/source/numem/mem/utils.d @@ -0,0 +1,25 @@ +module numem.mem.utils; +import std.traits; + +// Based on code from dplug:core +// which is released under the Boost license. + +/** + Forces a function to assume that it's nogc compatible. +*/ +auto assumeNoGC(T) (T t) { + static if (isFunctionPointer!T || isDelegate!T) { + enum attrs = functionAttributes!T | FunctionAttribute.nogc; + return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t; + } else static assert(false); +} + +/** + Forces a function to assume that it's nothrow nogc compatible. +*/ +auto assumeNothrowNoGC(T) (T t) { + static if (isFunctionPointer!T || isDelegate!T) { + enum attrs = functionAttributes!T | FunctionAttribute.nogc | FunctionAttribute.nothrow_; + return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t; + } else static assert(false); +} \ No newline at end of file