| 1 |
/** |
| 2 |
This module contains utility functions to help the implementation of the runtime hook |
| 3 |
|
| 4 |
Copyright: Copyright Digital Mars 2000 - 2019. |
| 5 |
License: Distributed under the |
| 6 |
$(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 7 |
(See accompanying file LICENSE) |
| 8 |
Source: $(DRUNTIMESRC core/internal/_array/_utils.d) |
| 9 |
*/ |
| 10 |
module core.internal.array.utils; |
| 11 |
|
| 12 |
import core.internal.traits : Parameters; |
| 13 |
import core.memory : GC; |
| 14 |
|
| 15 |
alias BlkAttr = GC.BlkAttr; |
| 16 |
|
| 17 |
auto gcStatsPure() nothrow pure |
| 18 |
{ |
| 19 |
import core.memory : GC; |
| 20 |
auto impureBypass = cast(GC.Stats function() pure nothrow)&GC.stats; |
| 21 |
return impureBypass(); |
| 22 |
} |
| 23 |
|
| 24 |
ulong accumulatePure(string file, int line, string funcname, string name, ulong size) nothrow pure |
| 25 |
{ |
| 26 |
static ulong impureBypass(string file, int line, string funcname, string name, ulong size) @nogc nothrow |
| 27 |
{ |
| 28 |
import core.internal.traits : externDFunc; |
| 29 |
|
| 30 |
alias accumulate = externDFunc!("rt.profilegc.accumulate", void function(string file, uint line, string funcname, string type, ulong sz) @nogc nothrow); |
| 31 |
accumulate(file, line, funcname, name, size); |
| 32 |
return size; |
| 33 |
} |
| 34 |
|
| 35 |
auto func = cast(ulong function(string file, int line, string funcname, string name, ulong size) @nogc nothrow pure)&impureBypass; |
| 36 |
return func(file, line, funcname, name, size); |
| 37 |
} |
| 38 |
|
| 39 |
version (D_ProfileGC) |
| 40 |
{ |
| 41 |
/** |
| 42 |
* TraceGC wrapper generator around the runtime hook `Hook`. |
| 43 |
* Params: |
| 44 |
* Type = The type of hook to report to accumulate |
| 45 |
* Hook = The name hook to wrap |
| 46 |
*/ |
| 47 |
template TraceHook(string Type, string Hook) |
| 48 |
{ |
| 49 |
const char[] TraceHook = q{ |
| 50 |
import core.internal.array.utils : gcStatsPure, accumulatePure; |
| 51 |
|
| 52 |
pragma(inline, false); |
| 53 |
string name = } ~ "`" ~ Type ~ "`;" ~ q{ |
| 54 |
|
| 55 |
// FIXME: use rt.tracegc.accumulator when it is accessable in the future. |
| 56 |
ulong currentlyAllocated = gcStatsPure().allocatedInCurrentThread; |
| 57 |
|
| 58 |
scope(exit) |
| 59 |
{ |
| 60 |
ulong size = gcStatsPure().allocatedInCurrentThread - currentlyAllocated; |
| 61 |
if (size > 0) |
| 62 |
if (!accumulatePure(file, line, funcname, name, size)) { |
| 63 |
// This 'if' and 'assert' is needed to force the compiler to not remove the call to |
| 64 |
// `accumulatePure`. It really want to do that while optimizing as the function is |
| 65 |
// `pure` and it does not influence the result of this hook. |
| 66 |
|
| 67 |
// `accumulatePure` returns the value of `size`, which can never be zero due to the |
| 68 |
// previous 'if'. So this assert will never be triggered. |
| 69 |
assert(0); |
| 70 |
} |
| 71 |
} |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* TraceGC wrapper around runtime hook `Hook`. |
| 77 |
* Params: |
| 78 |
* T = Type of hook to report to accumulate |
| 79 |
* Hook = The hook to wrap |
| 80 |
* errorMessage = The error message incase `version != D_TypeInfo` |
| 81 |
* file = File that called `_d_HookTraceImpl` |
| 82 |
* line = Line inside of `file` that called `_d_HookTraceImpl` |
| 83 |
* funcname = Function that called `_d_HookTraceImpl` |
| 84 |
* parameters = Parameters that will be used to call `Hook` |
| 85 |
* Bugs: |
| 86 |
* This function template needs be between the compiler and a much older runtime hook that bypassed safety, |
| 87 |
* purity, and throwabilty checks. To prevent breaking existing code, this function template |
| 88 |
* is temporarily declared `@trusted pure` until the implementation can be brought up to modern D expectations. |
| 89 |
*/ |
| 90 |
auto _d_HookTraceImpl(T, alias Hook, string errorMessage)(Parameters!Hook parameters, string file = __FILE__, int line = __LINE__, string funcname = __FUNCTION__) @trusted pure |
| 91 |
{ |
| 92 |
version (D_TypeInfo) |
| 93 |
{ |
| 94 |
mixin(TraceHook!(T.stringof, __traits(identifier, Hook))); |
| 95 |
return Hook(parameters); |
| 96 |
} |
| 97 |
else |
| 98 |
assert(0, errorMessage); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Check if the function `F` is calleable in a `nothrow` scope. |
| 104 |
* Params: |
| 105 |
* F = Function that does not take any parameters |
| 106 |
* Returns: |
| 107 |
* if the function is callable in a `nothrow` scope. |
| 108 |
*/ |
| 109 |
enum isNoThrow(alias F) = is(typeof(() nothrow { F(); })); |
| 110 |
|
| 111 |
/** |
| 112 |
* Check if the type `T`'s postblit is called in nothrow, if it exist |
| 113 |
* Params: |
| 114 |
* T = Type to check |
| 115 |
* Returns: |
| 116 |
* if the postblit is callable in a `nothrow` scope, if it exist. |
| 117 |
* if it does not exist, return true. |
| 118 |
*/ |
| 119 |
template isPostblitNoThrow(T) { |
| 120 |
static if (__traits(isStaticArray, T)) |
| 121 |
enum isPostblitNoThrow = isPostblitNoThrow!(typeof(T.init[0])); |
| 122 |
else static if (__traits(hasMember, T, "__xpostblit") && |
| 123 |
// Bugzilla 14746: Check that it's the exact member of S. |
| 124 |
__traits(isSame, T, __traits(parent, T.init.__xpostblit))) |
| 125 |
enum isPostblitNoThrow = isNoThrow!(T.init.__xpostblit); |
| 126 |
else |
| 127 |
enum isPostblitNoThrow = true; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Allocate a memory block with appendable capabilities for array usage. |
| 132 |
* |
| 133 |
* Params: |
| 134 |
* arrSize = size of the allocated array in bytes |
| 135 |
* Returns: |
| 136 |
* `void[]` matching requested size on success, `null` on failure. |
| 137 |
*/ |
| 138 |
void[] __arrayAlloc(T)(size_t arrSize) @trusted |
| 139 |
{ |
| 140 |
import core.lifetime : TypeInfoSize; |
| 141 |
import core.internal.traits : hasIndirections; |
| 142 |
|
| 143 |
enum typeInfoSize = TypeInfoSize!T; |
| 144 |
BlkAttr attr = BlkAttr.APPENDABLE; |
| 145 |
|
| 146 |
/* `extern(C++)` classes don't have a classinfo pointer in their vtable, |
| 147 |
* so the GC can't finalize them. |
| 148 |
*/ |
| 149 |
static if (typeInfoSize) |
| 150 |
attr |= BlkAttr.FINALIZE; |
| 151 |
static if (!hasIndirections!T) |
| 152 |
attr |= BlkAttr.NO_SCAN; |
| 153 |
|
| 154 |
auto ptr = GC.malloc(arrSize, attr, typeid(T)); |
| 155 |
if (ptr) |
| 156 |
return ptr[0 .. arrSize]; |
| 157 |
return null; |
| 158 |
} |