@@ -11,15 +11,15 @@ module core.internal.array.utils;
1111
1212import core.internal.traits : Parameters;
1313
14- private auto gcStatsPure () nothrow pure
14+ auto gcStatsPure () nothrow pure
1515{
1616 import core.memory : GC ;
1717
1818 auto impureBypass = cast (GC .Stats function () pure nothrow )&GC .stats;
1919 return impureBypass ();
2020}
2121
22- private ulong accumulatePure (string file, int line, string funcname, string name, ulong size) nothrow pure
22+ ulong accumulatePure (string file, int line, string funcname, string name, ulong size) nothrow pure
2323{
2424 static ulong impureBypass (string file, int line, string funcname, string name, ulong size) @nogc nothrow
2525 {
@@ -35,41 +35,32 @@ private ulong accumulatePure(string file, int line, string funcname, string name
3535}
3636
3737/**
38- * TraceGC wrapper around runtime hook `Hook`.
38+ * TraceGC wrapper generator around the runtime hook `Hook`.
3939 * Params:
40- * T = Type of hook to report to accumulate
41- * Hook = The hook to wrap
42- * errorMessage = The error message incase `version != D_TypeInfo`
43- * file = File that called `_d_HookTraceImpl`
44- * line = Line inside of `file` that called `_d_HookTraceImpl`
45- * funcname = Function that called `_d_HookTraceImpl`
46- * parameters = Parameters that will be used to call `Hook`
47- * Bugs:
48- * This function template needs be between the compiler and a much older runtime hook that bypassed safety,
49- * purity, and throwabilty checks. To prevent breaking existing code, this function template
50- * is temporarily declared `@trusted pure` until the implementation can be brought up to modern D expectations.
51- */
52- auto _d_HookTraceImpl (T, alias Hook, string errorMessage)(string file, int line, string funcname, Parameters! Hook parameters) @trusted pure
40+ * Type = The type of hook to report to accumulate
41+ * Hook = The name hook to wrap
42+ */
43+ template TraceHook (string Type, string Hook)
5344{
54- version (D_TypeInfo)
55- {
45+ const char [] TraceHook = q{
46+ import core.internal.array.utils : gcStatsPure, accumulatePure;
47+
5648 pragma (inline, false );
57- string name = T.stringof;
49+ string name = } ~ " ` " ~ Type ~ " `; " ~ q{
5850
5951 // FIXME: use rt.tracegc.accumulator when it is accessable in the future.
6052 version (tracegc)
61- {
53+ } ~ " { \n " ~ q {
6254 import core.stdc.stdio ;
6355
6456 printf(" %sTrace file = '%.*s' line = %d function = '%.*s' type = %.*s\n " ,
65- Hook.stringof. ptr,
57+ } ~ " \" " ~ Hook ~ " \" . ptr," ~ q{
6658 file.length, file.ptr,
6759 line,
6860 funcname.length, funcname.ptr,
6961 name.length, name.ptr
7062 );
71- }
72-
63+ } ~ " }\n " ~ q{
7364 ulong currentlyAllocated = gcStatsPure().allocatedInCurrentThread;
7465
7566 scope (exit)
@@ -86,6 +77,29 @@ auto _d_HookTraceImpl(T, alias Hook, string errorMessage)(string file, int line,
8677 assert (0 );
8778 }
8879 }
80+ };
81+ }
82+
83+ /**
84+ * TraceGC wrapper around runtime hook `Hook`.
85+ * Params:
86+ * T = Type of hook to report to accumulate
87+ * Hook = The hook to wrap
88+ * errorMessage = The error message incase `version != D_TypeInfo`
89+ * file = File that called `_d_HookTraceImpl`
90+ * line = Line inside of `file` that called `_d_HookTraceImpl`
91+ * funcname = Function that called `_d_HookTraceImpl`
92+ * parameters = Parameters that will be used to call `Hook`
93+ * Bugs:
94+ * This function template needs be between the compiler and a much older runtime hook that bypassed safety,
95+ * purity, and throwabilty checks. To prevent breaking existing code, this function template
96+ * is temporarily declared `@trusted pure` until the implementation can be brought up to modern D expectations.
97+ */
98+ auto _d_HookTraceImpl (T, alias Hook, string errorMessage)(string file, int line, string funcname, Parameters! Hook parameters) @trusted pure
99+ {
100+ version (D_TypeInfo)
101+ {
102+ mixin (TraceHook! (T.stringof, __traits (identifier, Hook)));
89103 return Hook (parameters);
90104 }
91105 else
0 commit comments