Skip to content

Commit c8fe75b

Browse files
Make emplace assumeNoGC
1 parent aab4afe commit c8fe75b

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

source/numem/io/stream/reader.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import numem.mem.vector;
1212
import std.traits;
1313

1414
/**
15-
A stream writer.
15+
A stream reader.
1616
17-
Allows easy writing to a stream.
17+
Allows easy reading from a stream.
1818
*/
1919
class StreamReader(Endianess endian) {
2020
@nogc nothrow:

source/numem/mem/package.d

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
module numem.mem;
88
import core.stdc.stdlib : free, exit, malloc;
99
import std.traits;
10+
import numem.mem.utils;
1011

1112
version(Have_tinyd_rt) {
1213
private __gshared
@@ -21,28 +22,6 @@ nothrow @nogc:
2122
// MANUAL MEMORY MANAGMENT
2223
//
2324
private {
24-
// Based on code from dplug:core
25-
// which is released under the Boost license.
26-
27-
auto assumeNoGC(T) (T t) {
28-
static if (isFunctionPointer!T || isDelegate!T)
29-
{
30-
enum attrs = functionAttributes!T | FunctionAttribute.nogc;
31-
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
32-
}
33-
else
34-
static assert(false);
35-
}
36-
37-
auto assumeNothrowNoGC(T) (T t) {
38-
static if (isFunctionPointer!T || isDelegate!T)
39-
{
40-
enum attrs = functionAttributes!T | FunctionAttribute.nogc | FunctionAttribute.nothrow_;
41-
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
42-
}
43-
else
44-
static assert(false);
45-
}
4625

4726
version(minimal_rt) {
4827

@@ -128,9 +107,9 @@ T* nogc_new(T, Args...)(Args args) if (is(T == struct)) {
128107

129108
T* obj = cast(T*)rawMemory;
130109
static if (hasUDA!(T, AllowInitEmpty) && args.length == 0) {
131-
emplace!T(obj);
110+
nogc_emplace!T(obj);
132111
} else {
133-
emplace!T(obj, args);
112+
nogc_emplace!T(obj, args);
134113
}
135114

136115
return obj;
@@ -142,6 +121,8 @@ T* nogc_new(T, Args...)(Args args) if (is(T == struct)) {
142121
Immediately exits the application if out of memory.
143122
*/
144123
T nogc_new(T, Args...)(Args args) if (is(T == class)) {
124+
alias emplaceFunc = typeof(&emplace!T);
125+
145126
version(Have_tinyd_rt) {
146127
return (assumeNothrowNoGC(&__gc_new!(T, Args)))(args);
147128
} else version(minimal_rt) {
@@ -155,10 +136,10 @@ T nogc_new(T, Args...)(Args args) if (is(T == class)) {
155136
}
156137

157138
// Allocate class destructor list
158-
emplace!_impl_destructorStruct(rawMemory[0..destructorObjSize], &_impl_destructorCall!T);
139+
nogc_emplace!classDestructorList(rawMemory[0..destructorObjSize], &_impl_destructorCall!T);
159140

160141
// Allocate class
161-
T obj = emplace!T(rawMemory[destructorObjSize .. allocSize], args);
142+
T obj = nogc_emplace!T(rawMemory[destructorObjSize .. allocSize], args);
162143
return obj;
163144

164145
} else {
@@ -168,7 +149,7 @@ T nogc_new(T, Args...)(Args args) if (is(T == class)) {
168149
exit(-1);
169150
}
170151

171-
return emplace!T(rawMemory[0 .. allocSize], args);
152+
return nogc_emplace!T(rawMemory[0 .. allocSize], args);
172153
}
173154
}
174155

@@ -269,4 +250,24 @@ void nogc_delete(T)(ref T obj_) {
269250
}
270251
}
271252
}
253+
}
254+
255+
/**
256+
nogc emplace function
257+
*/
258+
auto nogc_emplace(T, Args...)(T* chunk, Args args) {
259+
alias t = typeof(&emplace!(T, Args));
260+
return assumeNothrowNoGC!t(&emplace!(T, Args))(chunk, args);
261+
}
262+
263+
/**
264+
nogc emplace function
265+
*/
266+
auto nogc_emplace(T, Args...)(void[] chunk, Args args) {
267+
alias t = T function(void[], Args);
268+
auto ifunc = (void[] chunk, Args args) {
269+
return emplace!(T, Args)(chunk, args);
270+
};
271+
272+
return assumeNothrowNoGC!t(ifunc)(chunk, args);
272273
}

source/numem/mem/utils.d

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module numem.mem.utils;
2+
import std.traits;
3+
4+
// Based on code from dplug:core
5+
// which is released under the Boost license.
6+
7+
/**
8+
Forces a function to assume that it's nogc compatible.
9+
*/
10+
auto assumeNoGC(T) (T t) {
11+
static if (isFunctionPointer!T || isDelegate!T) {
12+
enum attrs = functionAttributes!T | FunctionAttribute.nogc;
13+
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
14+
} else static assert(false);
15+
}
16+
17+
/**
18+
Forces a function to assume that it's nothrow nogc compatible.
19+
*/
20+
auto assumeNothrowNoGC(T) (T t) {
21+
static if (isFunctionPointer!T || isDelegate!T) {
22+
enum attrs = functionAttributes!T | FunctionAttribute.nogc | FunctionAttribute.nothrow_;
23+
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
24+
} else static assert(false);
25+
}

0 commit comments

Comments
 (0)