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

Commit d20bb68

Browse files
teodutudlang-bot
authored andcommitted
Add explicit _d_arrayappendTTrace hook
Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
1 parent e02a07c commit d20bb68

3 files changed

Lines changed: 54 additions & 23 deletions

File tree

src/core/internal/array/appending.d

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ ref Tarr _d_arrayappendT(Tarr : T[], T)(return ref scope Tarr x, scope Tarr y) @
104104
return x;
105105
}
106106

107+
/**
108+
* TraceGC wrapper around $(REF _d_arrayappendT, core,internal,array,appending).
109+
*/
110+
ref Tarr _d_arrayappendTTrace(Tarr : T[], T)(string file, int line, string funcname, return ref scope Tarr x, scope Tarr y) @trusted
111+
{
112+
version (D_TypeInfo)
113+
{
114+
import core.internal.array.utils: TraceHook, gcStatsPure, accumulatePure;
115+
mixin(TraceHook!(Tarr.stringof, "_d_arrayappendT"));
116+
117+
return _d_arrayappendT(x, y);
118+
}
119+
else
120+
assert(0, "Cannot append to array if compiling without support for runtime type information!");
121+
}
122+
107123
@safe unittest
108124
{
109125
double[] arr1;

src/core/internal/array/utils.d

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ module core.internal.array.utils;
1111

1212
import 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

src/object.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4883,6 +4883,7 @@ they are only intended to be instantiated by the compiler, not the user.
48834883
public import core.internal.entrypoint : _d_cmain;
48844884

48854885
public import core.internal.array.appending : _d_arrayappendT;
4886+
public import core.internal.array.appending : _d_arrayappendTTrace;
48864887
public import core.internal.array.appending : _d_arrayappendcTXImpl;
48874888
public import core.internal.array.comparison : __cmp;
48884889
public import core.internal.array.equality : __equals;

0 commit comments

Comments
 (0)