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

Commit 3e8b013

Browse files
committed
core.memory: Make GC.[profile]stats safe/nothrow/nogc
Those functions only return a struct, they do not accept any parameter, and thus should be inherently safe. It also would not make sense for GC.stats to allocate, as it would skew the metrics it returns.
1 parent 7a048cb commit 3e8b013

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/core/gc/gcinterface.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ interface GC
141141
* Retrieve statistics about garbage collection.
142142
* Useful for debugging and tuning.
143143
*/
144-
core.memory.GC.Stats stats() nothrow;
144+
core.memory.GC.Stats stats() @safe nothrow @nogc;
145145

146146
/**
147147
* Retrieve profile statistics about garbage collection.
148148
* Useful for debugging and tuning.
149149
*/
150-
core.memory.GC.ProfileStats profileStats() nothrow @safe;
150+
core.memory.GC.ProfileStats profileStats() @safe nothrow @nogc;
151151

152152
/**
153153
* add p to list of roots

src/core/internal/gc/impl/conservative/gc.d

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class ConservativeGC : GC
156156
*
157157
* Throws: InvalidMemoryOperationError on recursive locking during finalization.
158158
*/
159-
static void lockNR() @nogc nothrow
159+
static void lockNR() @safe @nogc nothrow
160160
{
161161
if (_inFinalizer)
162162
onInvalidMemoryOperationError();
@@ -1300,7 +1300,7 @@ class ConservativeGC : GC
13001300
}
13011301

13021302

1303-
core.memory.GC.Stats stats() nothrow
1303+
core.memory.GC.Stats stats() @safe nothrow @nogc
13041304
{
13051305
typeof(return) ret;
13061306

@@ -1333,8 +1333,15 @@ class ConservativeGC : GC
13331333
//
13341334
// Implementation of getStats
13351335
//
1336-
private void getStatsNoSync(out core.memory.GC.Stats stats) nothrow
1337-
{
1336+
private void getStatsNoSync(out core.memory.GC.Stats stats) @trusted nothrow @nogc
1337+
{
1338+
// This function is trusted for two reasons: `pool.pagetable` is a pointer,
1339+
// which is being sliced in the below foreach, and so is `binPageChain`,
1340+
// also sliced a bit later in this function.
1341+
// However, both usages are safe as long as the assumption that `npools`
1342+
// defines the limit for `pagetable`'s length holds true (see allocation).
1343+
// The slicing happens at __LINE__ + 4 and __LINE__ + 24.
1344+
// `@trusted` delegates are not used to prevent any performance issue.
13381345
foreach (pool; gcx.pooltable[])
13391346
{
13401347
foreach (bin; pool.pagetable[0 .. pool.npages])

src/core/internal/gc/proxy.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ extern (C)
209209
return instance.query( p );
210210
}
211211

212-
core.memory.GC.Stats gc_stats() nothrow
212+
core.memory.GC.Stats gc_stats() @safe nothrow @nogc
213213
{
214214
return instance.stats();
215215
}
216216

217-
core.memory.GC.ProfileStats gc_profileStats() nothrow @safe
217+
core.memory.GC.ProfileStats gc_profileStats() @safe nothrow @nogc
218218
{
219219
return instance.profileStats();
220220
}

src/core/memory.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private
133133
}
134134

135135
extern (C) BlkInfo_ gc_query(return scope void* p) pure nothrow;
136-
extern (C) GC.Stats gc_stats ( ) nothrow @nogc;
136+
extern (C) GC.Stats gc_stats ( ) @safe nothrow @nogc;
137137
extern (C) GC.ProfileStats gc_profileStats ( ) nothrow @nogc @safe;
138138
}
139139

@@ -766,7 +766,7 @@ extern(D):
766766
* Returns runtime stats for currently active GC implementation
767767
* See `core.memory.GC.Stats` for list of available metrics.
768768
*/
769-
static Stats stats() nothrow
769+
static Stats stats() @safe nothrow @nogc
770770
{
771771
return gc_stats();
772772
}

0 commit comments

Comments
 (0)