Debian Codegraph @

libphobos/libdruntime/core/internal/array/appending.d

raw
1 /**
2 This module contains support for controlling dynamic arrays' appending
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/_appending.d)
9 */
10 module core.internal.array.appending;
11
12 /// See $(REF _d_arrayappendcTX, rt,lifetime,_d_arrayappendcTX)
13 private extern (C) byte[] _d_arrayappendcTX(const TypeInfo ti, ref return scope byte[] px, size_t n) @trusted pure nothrow;
14
15 private enum isCopyingNothrow(T) = __traits(compiles, (ref T rhs) nothrow { T lhs = rhs; });
16
17 /**
18 * Extend an array `px` by `n` elements.
19 * Caller must initialize those elements.
20 * Params:
21 * px = the array that will be extended, taken as a reference
22 * n = how many new elements to extend it with
23 * Returns:
24 * The new value of `px`
25 * Bugs:
26 * This function template was ported from a much older runtime hook that bypassed safety,
27 * purity, and throwabilty checks. To prevent breaking existing code, this function template
28 * is temporarily declared `@trusted pure` until the implementation can be brought up to modern D expectations.
29 */
30 ref Tarr _d_arrayappendcTX(Tarr : T[], T)(return ref scope Tarr px, size_t n) @trusted
31 {
32 // needed for CTFE: https://github.com/dlang/druntime/pull/3870#issuecomment-1178800718
33 version (DigitalMars) pragma(inline, false);
34 version (D_TypeInfo)
35 {
36 auto ti = typeid(Tarr);
37
38 // _d_arrayappendcTX takes the `px` as a ref byte[], but its length
39 // should still be the original length
40 auto pxx = (cast(byte*)px.ptr)[0 .. px.length];
41 ._d_arrayappendcTX(ti, pxx, n);
42 px = (cast(T*)pxx.ptr)[0 .. pxx.length];
43
44 return px;
45 }
46 else
47 assert(0, "Cannot append to array if compiling without support for runtime type information!");
48 }
49
50 version (D_ProfileGC)
51 {
52 /**
53 * TraceGC wrapper around $(REF _d_arrayappendT, core,internal,array,appending).
54 */
55 ref Tarr _d_arrayappendcTXTrace(Tarr : T[], T)(return ref scope Tarr px, size_t n, string file = __FILE__, int line = __LINE__, string funcname = __FUNCTION__) @trusted
56 {
57 version (D_TypeInfo)
58 {
59 import core.internal.array.utils: TraceHook, gcStatsPure, accumulatePure;
60 mixin(TraceHook!(Tarr.stringof, "_d_arrayappendcTX"));
61
62 return _d_arrayappendcTX(px, n);
63 }
64 else
65 static assert(0, "Cannot append to array if compiling without support for runtime type information!");
66 }
67 }
68
69 /// Implementation of `_d_arrayappendT`
70 ref Tarr _d_arrayappendT(Tarr : T[], T)(return ref scope Tarr x, scope Tarr y) @trusted
71 {
72 version (DigitalMars) pragma(inline, false);
73
74 import core.stdc.string : memcpy;
75 import core.internal.traits : hasElaborateCopyConstructor, Unqual;
76
77 enum hasPostblit = __traits(hasPostblit, T);
78 auto length = x.length;
79
80 _d_arrayappendcTX(x, y.length);
81
82 // Only call `copyEmplace` if `T` has a copy ctor and no postblit.
83 static if (hasElaborateCopyConstructor!T && !hasPostblit)
84 {
85 import core.lifetime : copyEmplace;
86
87 foreach (i, ref elem; y)
88 copyEmplace(elem, x[length + i]);
89 }
90 else
91 {
92 if (y.length)
93 {
94 // blit all elements at once
95 auto xptr = cast(Unqual!T *)&x[length];
96 immutable size = T.sizeof;
97
98 memcpy(xptr, cast(Unqual!T *)&y[0], y.length * size);
99
100 // call postblits if they exist
101 static if (hasPostblit)
102 {
103 auto eptr = xptr + y.length;
104 for (auto ptr = xptr; ptr < eptr; ptr++)
105 ptr.__xpostblit();
106 }
107 }
108 }
109
110 return x;
111 }
112
113 version (D_ProfileGC)
114 {
115 /**
116 * TraceGC wrapper around $(REF _d_arrayappendT, core,internal,array,appending).
117 */
118 ref Tarr _d_arrayappendTTrace(Tarr : T[], T)(return ref scope Tarr x, scope Tarr y, string file = __FILE__, int line = __LINE__, string funcname = __FUNCTION__) @trusted
119 {
120 version (D_TypeInfo)
121 {
122 import core.internal.array.utils: TraceHook, gcStatsPure, accumulatePure;
123 mixin(TraceHook!(Tarr.stringof, "_d_arrayappendT"));
124
125 return _d_arrayappendT(x, y);
126 }
127 else
128 static assert(0, "Cannot append to array if compiling without support for runtime type information!");
129 }
130 }
131
132 @safe unittest
133 {
134 double[] arr1;
135 foreach (i; 0 .. 4)
136 _d_arrayappendT(arr1, [cast(double)i]);
137 assert(arr1 == [0.0, 1.0, 2.0, 3.0]);
138 }
139
140 @safe unittest
141 {
142 int blitted;
143 struct Item
144 {
145 this(this)
146 {
147 blitted++;
148 }
149 }
150
151 Item[] arr1 = [Item(), Item()];
152 Item[] arr2 = [Item(), Item()];
153 Item[] arr1_org = [Item(), Item()];
154 arr1_org ~= arr2;
155 _d_arrayappendT(arr1, arr2);
156
157 // postblit should have triggered on at least the items in arr2
158 assert(blitted >= arr2.length);
159 }
160
161 @safe nothrow unittest
162 {
163 int blitted;
164 struct Item
165 {
166 this(this) nothrow
167 {
168 blitted++;
169 }
170 }
171
172 Item[][] arr1 = [[Item()]];
173 Item[][] arr2 = [[Item()]];
174
175 _d_arrayappendT(arr1, arr2);
176
177 // no postblit should have happened because arr{1,2} contain dynamic arrays
178 assert(blitted == 0);
179 }
180
181 @safe nothrow unittest
182 {
183 int copied;
184 struct Item
185 {
186 this(const scope ref Item) nothrow
187 {
188 copied++;
189 }
190 }
191
192 Item[1][] arr1 = [[Item()]];
193 Item[1][] arr2 = [[Item()]];
194
195 _d_arrayappendT(arr1, arr2);
196 // copy constructor should have been invoked because arr{1,2} contain static arrays
197 assert(copied >= arr2.length);
198 }
199
200 @safe nothrow unittest
201 {
202 string str;
203 _d_arrayappendT(str, "a");
204 _d_arrayappendT(str, "b");
205 _d_arrayappendT(str, "c");
206 assert(str == "abc");
207 }