@@ -13,6 +13,17 @@ import core.internal.string;
1313import core.stdc.stdint ;
1414
1515
16+ // TLS storage shared for all error messages.
17+ private align (2 * size_t .sizeof) char [256 ] _store;
18+
19+ private char [] errorMessage (Args... )(scope const (char * ) format,
20+ const char [] action, Args args) @trusted
21+ {
22+ import core.stdc.stdio : snprintf;
23+ snprintf(&_store[0 ], _store.sizeof, format, &action[0 ], args);
24+ return _store;
25+ }
26+
1627@safe /* pure dmd @@@BUG11461@@@ */ nothrow :
1728
1829void enforceTypedArraysConformable (T)(const char [] action,
@@ -65,6 +76,44 @@ private void _enforceNoOverlap(const char[] action,
6576 assert (0 , msg);
6677}
6778
79+ void enforceTypedArraysConformableNogc (T)(const char [] action,
80+ const T[] a1, const T[] a2, const bool allowOverlap = false )
81+ {
82+ _enforceSameLengthNogc(action, a1.length, a2.length);
83+ if (! allowOverlap)
84+ _enforceNoOverlapNogc(action, arrayToPtr (a1), arrayToPtr(a2), T.sizeof * a1.length);
85+ }
86+
87+ void enforceRawArraysConformableNogc (const char [] action, const size_t elementSize,
88+ const void [] a1, const void [] a2, const bool allowOverlap = false )
89+ {
90+ _enforceSameLengthNogc(action, a1.length, a2.length);
91+ if (! allowOverlap)
92+ _enforceNoOverlapNogc(action, arrayToPtr (a1), arrayToPtr(a2), elementSize * a1.length);
93+ }
94+
95+ private void _enforceNoOverlapNogc (const ref char [] action,
96+ uintptr_t ptr1, uintptr_t ptr2, const size_t bytes)
97+ {
98+ const d = ptr1 > ptr2 ? ptr1 - ptr2 : ptr2 - ptr1;
99+ if (d >= bytes)
100+ return ;
101+ const overlappedBytes = bytes - d;
102+
103+ assert (0 , errorMessage(" Overlapping arrays in %s: %zu byte(s) overlap of %zu" ,
104+ action, overlappedBytes, bytes));
105+ }
106+
107+ private void _enforceSameLengthNogc (const ref char [] action,
108+ const size_t length1, const size_t length2)
109+ {
110+ if (length1 == length2)
111+ return ;
112+
113+ assert (0 , errorMessage(" Array lengths don't match for %s: %zu != %zu" ,
114+ action, length1, length2));
115+ }
116+
68117private uintptr_t arrayToPtr (const void [] array) @trusted
69118{
70119 // Ok because the user will never dereference the pointer
0 commit comments