This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 411
Add templated _d_arraycatnTX with tests #2648
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /** | ||
| This module contains support for controlling dynamic arrays' concatenation | ||
| Copyright: Copyright Digital Mars 2000 - 2019. | ||
| License: Distributed under the | ||
| $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). | ||
| (See accompanying file LICENSE) | ||
| Source: $(DRUNTIMESRC rt/_array/_concatenation.d) | ||
| */ | ||
| module rt.array.concatenation; | ||
|
|
||
| /// See $(REF _d_arraycatnTX, rt,lifetime) | ||
| private extern (C) void[] _d_arraycatnTX(const TypeInfo ti, byte[][] arrs) pure nothrow; | ||
|
|
||
| // This wrapper is needed because a externDFunc cannot be cast()ed directly. | ||
| private void accumulate(string file, uint line, string funcname, string type, ulong sz) @nogc | ||
| { | ||
| import core.internal.traits : externDFunc; | ||
|
|
||
| alias func = externDFunc!("rt.profilegc.accumulate", void function(string file, uint line, string funcname, string type, ulong sz) @nogc); | ||
| return func(file, line, funcname, type, sz); | ||
| } | ||
|
|
||
| /// Implementation of `_d_arraycatnTX` and `_d_arraycatnTXTrace` | ||
| template _d_arraycatnTXImpl(Tarr : ResultArrT[], ResultArrT : T[], T) | ||
| { | ||
| /** | ||
| * Concatenating the arrays inside of `arrs`. | ||
| * `_d_arraycatnTX([a, b, c])` means `a ~ b ~ c`. | ||
| * Params: | ||
| * arrs = Array containing arrays that will be concatenated. | ||
| * Returns: | ||
| * A newly allocated array that contains all the elements from all the arrays in `arrs`. | ||
| * Bugs: | ||
| * This function template was ported from a much older runtime hook that bypassed safety, | ||
| * purity, and throwabilty checks. To prevent breaking existing code, this function template | ||
| * is temporarily declared `@trusted pure nothrow` until the implementation can be brought up to modern D expectations. | ||
| */ | ||
| ResultArrT _d_arraycatnTX(scope const Tarr arrs) @trusted pure nothrow | ||
| { | ||
| pragma(inline, false); | ||
| version (D_TypeInfo) | ||
| { | ||
| auto ti = typeid(ResultArrT); | ||
|
|
||
| byte[][] arrs2 = (cast(byte[]*)arrs.ptr)[0 .. arrs.length]; | ||
| void[] result = ._d_arraycatnTX(ti, arrs2); | ||
| return (cast(T*)result.ptr)[0 .. result.length]; | ||
| } | ||
| else | ||
| assert(0, "Cannot concatenate arrays if compiling without support for runtime type information!"); | ||
| } | ||
|
|
||
| /** | ||
| * TraceGC wrapper around $(REF _d_arraycatnTX, rt,array,concat). | ||
| * Bugs: | ||
| * This function template was ported from a much older runtime hook that bypassed safety, | ||
| * purity, and throwabilty checks. To prevent breaking existing code, this function template | ||
| * is temporarily declared `@trusted pure nothrow` until the implementation can be brought up to modern D expectations. | ||
| */ | ||
| ResultArrT _d_arraycatnTXTrace(string file, int line, string funcname, scope const Tarr arrs) @trusted pure nothrow | ||
| { | ||
| pragma(inline, false); | ||
| version (D_TypeInfo) | ||
| { | ||
| import core.memory : GC; | ||
| auto accumulate = cast(void function(string file, uint line, string funcname, string type, ulong sz) @nogc nothrow pure)&accumulate; | ||
| auto gcStats = cast(GC.Stats function() nothrow pure)&GC.stats; | ||
|
|
||
| string name = ResultArrT.stringof; | ||
|
|
||
| // FIXME: use rt.tracegc.accumulator when it is accessable in the future. | ||
| version (tracegc) | ||
| { | ||
| import core.stdc.stdio; | ||
|
|
||
| printf("%s file = '%.*s' line = %d function = '%.*s' type = %.*s\n", | ||
| __FUNCTION__.ptr, | ||
| file.length, file.ptr, | ||
| line, | ||
| funcname.length, funcname.ptr, | ||
| name.length, name.ptr | ||
| ); | ||
| } | ||
|
|
||
| ulong currentlyAllocated = gcStats().allocatedInCurrentThread; | ||
|
|
||
| scope(exit) | ||
| { | ||
| ulong size = gcStats().allocatedInCurrentThread - currentlyAllocated; | ||
| if (size > 0) | ||
| accumulate(file, line, funcname, name, size); | ||
| } | ||
| return _d_arraycatnTX(arrs); | ||
| } | ||
| else | ||
| assert(0, "Cannot concatenate arrays if compiling without support for runtime type information!"); | ||
| } | ||
| } | ||
|
|
||
| @safe unittest | ||
| { | ||
| int counter; | ||
| struct S | ||
| { | ||
| int val; | ||
| this(this) | ||
| { | ||
| counter++; | ||
| } | ||
| } | ||
|
|
||
| S[][] arr = [[S(0), S(1), S(2), S(3)], [S(4), S(5), S(6), S(7)]]; | ||
| S[] result = _d_arraycatnTXImpl!(typeof(arr))._d_arraycatnTX(arr); | ||
|
|
||
| assert(counter == 8); | ||
| assert(result == [S(0), S(1), S(2), S(3), S(4), S(5), S(6), S(7)]); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.