@@ -138,7 +138,7 @@ private
138138 }
139139
140140 extern (C ) BlkInfo_ gc_query( void * p ) pure nothrow ;
141- extern (C ) GC .Stats gc_stats ( ) nothrow @nogc ;
141+ extern (C ) GC .Stats gc_stats ( ulong fields ) nothrow @nogc ;
142142
143143 extern (C ) void gc_addRoot( in void * p ) nothrow @nogc ;
144144 extern (C ) void gc_addRange( in void * p, size_t sz, const TypeInfo ti = null ) nothrow @nogc ;
@@ -170,6 +170,12 @@ struct GC
170170 size_t freeSize;
171171 }
172172
173+ /**
174+ * Auto-generated bitflag enum with values of identical names as `Stats`
175+ * struct. Optionally used to specify exact stats to calculate.
176+ */
177+ mixin (generateFieldEnum! Stats);
178+
173179 /**
174180 * Enables automatic garbage collection behavior if collections have
175181 * previously been suspended by a call to disable. This function is
@@ -674,10 +680,16 @@ struct GC
674680 /**
675681 * Returns runtime stats for currently active GC implementation
676682 * See `core.memory.GC.Stats` for list of available metrics.
683+ *
684+ * Params:
685+ * fields = optional bit flag argument which specifies which stats need
686+ * to be calculated. By default equals to "all fields". If some field
687+ * was not requested via bit flag, its value in returned `Stats` struct
688+ * will be undefined.
677689 */
678- static Stats stats () nothrow
690+ static Stats stats (ulong fields = ulong .max ) nothrow
679691 {
680- return gc_stats ();
692+ return gc_stats (fields );
681693 }
682694
683695 /**
@@ -1184,4 +1196,49 @@ unittest
11841196 assert (GC .addrOf(y.ptr) == null );
11851197}
11861198
1199+ /**
1200+ For a given struct `S` generated bitflag enum with a value for each of
1201+ struct fields.
1202+ */
1203+ private string generateFieldEnum (alias S)()
1204+ {
1205+ import core.internal.string ;
1206+
1207+ string code = " enum " ~ __traits(identifier, S) ~ " Fields\n {\n " ;
1208+ ulong shift = 0 ;
1209+ char [3 ] buf;
1210+
1211+ foreach (idx, _; S.init.tupleof)
1212+ {
1213+ auto init = " 1UL << " ~ unsignedToTempString(shift, buf);
1214+ code ~= __traits(identifier, S.tupleof[idx]) ~ " = " ~ init ~ " ,\n " ;
1215+ ++ shift;
1216+ }
1217+
1218+ code ~= " }" ;
11871219
1220+ return code;
1221+ }
1222+
1223+ unittest
1224+ {
1225+ static struct Dummy
1226+ {
1227+ int a, b, c;
1228+ }
1229+
1230+ enum code = generateFieldEnum! Dummy();
1231+
1232+ static assert (code == " enum DummyFields
1233+ {
1234+ a = 1UL << 0,
1235+ b = 1UL << 1,
1236+ c = 1UL << 2,
1237+ }" );
1238+
1239+ mixin (code);
1240+
1241+ assert (DummyFields.a == 1 );
1242+ assert (DummyFields.b == 2 );
1243+ assert (DummyFields.c == 4 );
1244+ }
0 commit comments