Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit b99629e

Browse files
committed
addRange/addRoot lazy init
1 parent 7952d94 commit b99629e

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

src/gc/impl/proto/gc.d

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,27 @@ private
3131

3232
class ProtoGC : GC
3333
{
34-
__gshared Array!Root roots;
35-
__gshared Array!Range ranges;
34+
Array!Root roots;
35+
Array!Range ranges;
36+
37+
// Call this function when initializing the real GC
38+
// upon ProtoGC term. This function should be called
39+
// after the real GC is in place.
40+
void term()
41+
{
42+
// Transfer all ranges
43+
foreach (ref r; ranges)
44+
{
45+
// Range(p, p + sz, cast() ti)
46+
gc_addRange(r.pbot, r.ptop - r.pbot, r.ti);
47+
}
48+
49+
// Transfer all roots
50+
foreach (ref r; roots)
51+
{
52+
gc_addRoot(r.proot);
53+
}
54+
}
3655

3756
this()
3857
{
@@ -141,14 +160,24 @@ class ProtoGC : GC
141160
return typeof(return).init;
142161
}
143162

163+
144164
void addRoot(void* p) nothrow @nogc
145165
{
146-
gc_init_nothrow();
147-
gc_addRoot(p);
166+
roots.insertBack(Root(p));
148167
}
149168

150169
void removeRoot(void* p) nothrow @nogc
151170
{
171+
foreach (ref r; roots)
172+
{
173+
if (r is p)
174+
{
175+
r = roots.back;
176+
roots.popBack();
177+
return;
178+
}
179+
}
180+
assert(false);
152181
}
153182

154183
@property RootIterator rootIter() return @nogc
@@ -158,17 +187,31 @@ class ProtoGC : GC
158187

159188
private int rootsApply(scope int delegate(ref Root) nothrow dg)
160189
{
190+
foreach (ref r; roots)
191+
{
192+
if (auto result = dg(r))
193+
return result;
194+
}
161195
return 0;
162196
}
163197

164198
void addRange(void* p, size_t sz, const TypeInfo ti = null) nothrow @nogc
165199
{
166-
gc_init_nothrow();
167-
gc_addRange(p, sz, ti);
200+
ranges.insertBack(Range(p, p + sz, cast() ti));
168201
}
169202

170203
void removeRange(void* p) nothrow @nogc
171204
{
205+
foreach (ref r; ranges)
206+
{
207+
if (r.pbot is p)
208+
{
209+
r = ranges.back;
210+
ranges.popBack();
211+
return;
212+
}
213+
}
214+
assert(false);
172215
}
173216

174217
@property RangeIterator rangeIter() return @nogc
@@ -178,6 +221,11 @@ class ProtoGC : GC
178221

179222
private int rangesApply(scope int delegate(ref Range) nothrow dg)
180223
{
224+
foreach (ref r; ranges)
225+
{
226+
if (auto result = dg(r))
227+
return result;
228+
}
181229
return 0;
182230
}
183231

src/gc/proxy.d

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ extern (C)
3838
{
3939
void gc_init()
4040
{
41-
import core.atomic : atomicLoad, atomicStore, MemoryOrder;
42-
4341
instanceLock.lock();
4442
if (!isInstanceInit)
4543
{
@@ -48,7 +46,7 @@ extern (C)
4846
ManualGC.initialize(instance);
4947
ConservativeGC.initialize(instance);
5048

51-
if (instance == protoInstance)
49+
if (instance is protoInstance)
5250
{
5351
import core.stdc.stdio : fprintf, stderr;
5452
import core.stdc.stdlib : exit;
@@ -61,6 +59,8 @@ extern (C)
6159
assert(0);
6260
}
6361

62+
// Transfer all ranges and roots to the real GC.
63+
(cast(ProtoGC) protoInstance).term();
6464
isInstanceInit = true;
6565
}
6666
instanceLock.unlock();
@@ -89,11 +89,14 @@ extern (C)
8989
// NOTE: Due to popular demand, this has been re-enabled. It still has
9090
// the problems mentioned above though, so I guess we'll see.
9191

92-
instance.collectNoStack(); // not really a 'collect all' -- still scans
93-
// static data area, roots, and ranges.
92+
if (isInstanceInit)
93+
{
94+
instance.collectNoStack(); // not really a 'collect all' -- still scans
95+
// static data area, roots, and ranges.
9496

95-
ManualGC.finalize(instance);
96-
ConservativeGC.finalize(instance);
97+
ManualGC.finalize(instance);
98+
ConservativeGC.finalize(instance);
99+
}
97100
}
98101

99102
void gc_enable()

src/rt/dmain2.d

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ extern (C) void _d_monitor_staticctor();
5858
extern (C) void _d_monitor_staticdtor();
5959
extern (C) void _d_critical_init();
6060
extern (C) void _d_critical_term();
61-
extern (C) void protogc_init();
6261
extern (C) void gc_init();
6362
extern (C) void gc_term();
6463
extern (C) void thread_init() @nogc;

test/exceptions/src/unknown_gc.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import core.memory;
2+
13
extern(C) __gshared string[] rt_options = [ "gcopt=gc:unknowngc" ];
24

35
void main()
46
{
7+
// GC initialized upon first call -> Unknown GC error is thrown
8+
GC.enable();
59
}

test/nogc.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extern(C) __gshared string[] rt_options = [ "gcopt=gc:conservative" ];
1+
extern(C) __gshared string[] rt_options = [ "gcopt=gc:non-existing" ];
22

33
void main() @nogc
44
{

0 commit comments

Comments
 (0)