7
7
module numem.mem ;
8
8
import core.stdc.stdlib : free, exit, malloc;
9
9
import std.traits ;
10
+ import numem.mem.utils;
10
11
11
12
version (Have_tinyd_rt) {
12
13
private __gshared
@@ -21,28 +22,6 @@ nothrow @nogc:
21
22
// MANUAL MEMORY MANAGMENT
22
23
//
23
24
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
- }
46
25
47
26
version (minimal_rt) {
48
27
@@ -128,9 +107,9 @@ T* nogc_new(T, Args...)(Args args) if (is(T == struct)) {
128
107
129
108
T* obj = cast (T* )rawMemory;
130
109
static if (hasUDA! (T, AllowInitEmpty) && args.length == 0 ) {
131
- emplace ! T(obj);
110
+ nogc_emplace ! T(obj);
132
111
} else {
133
- emplace ! T(obj, args);
112
+ nogc_emplace ! T(obj, args);
134
113
}
135
114
136
115
return obj;
@@ -142,6 +121,8 @@ T* nogc_new(T, Args...)(Args args) if (is(T == struct)) {
142
121
Immediately exits the application if out of memory.
143
122
*/
144
123
T nogc_new (T, Args... )(Args args) if (is (T == class )) {
124
+ alias emplaceFunc = typeof (&emplace! T);
125
+
145
126
version (Have_tinyd_rt) {
146
127
return (assumeNothrowNoGC(&__gc_new! (T, Args)))(args);
147
128
} else version (minimal_rt) {
@@ -155,10 +136,10 @@ T nogc_new(T, Args...)(Args args) if (is(T == class)) {
155
136
}
156
137
157
138
// Allocate class destructor list
158
- emplace ! _impl_destructorStruct (rawMemory[0 .. destructorObjSize], &_impl_destructorCall! T);
139
+ nogc_emplace ! classDestructorList (rawMemory[0 .. destructorObjSize], &_impl_destructorCall! T);
159
140
160
141
// Allocate class
161
- T obj = emplace ! T(rawMemory[destructorObjSize .. allocSize], args);
142
+ T obj = nogc_emplace ! T(rawMemory[destructorObjSize .. allocSize], args);
162
143
return obj;
163
144
164
145
} else {
@@ -168,7 +149,7 @@ T nogc_new(T, Args...)(Args args) if (is(T == class)) {
168
149
exit(- 1 );
169
150
}
170
151
171
- return emplace ! T(rawMemory[0 .. allocSize], args);
152
+ return nogc_emplace ! T(rawMemory[0 .. allocSize], args);
172
153
}
173
154
}
174
155
@@ -269,4 +250,24 @@ void nogc_delete(T)(ref T obj_) {
269
250
}
270
251
}
271
252
}
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);
272
273
}
0 commit comments