2 * Part of the D programming language runtime library.
3 * http://www.digitalmars.com
4 * Written by Walter Bright
5 * Placed in the Public Domain
14 debug(PRINTF) import stdc.stdio;
18 * Does array assignment (not construction) from another
19 * array of the same element type.
20 * ti is the element type.
21 * Handles overlapping copies.
23 extern (C) void[] _d_arrayassign(TypeInfo ti, void[] from, void[] to)
25 debug(PRINTF) printf("_d_arrayassign(from = %p,%d, to = %p,%d) size = %d\n", from.ptr, from.length, to.ptr, to.length, ti.tsize());
27 if (to.length != from.length)
30 string msg = "lengths don't match for array copy,"c;
31 msg ~= tmp.intToString(to.length) ~ " = " ~ tmp.intToString(from.length);
32 throw new Exception(msg);
35 auto element_size = ti.tsize();
37 /* Need a temporary buffer tmp[] big enough to hold one element
41 if (element_size > buf.sizeof)
42 tmp = alloca(element_size)[0 .. element_size];
47 if (to.ptr <= from.ptr)
49 foreach (i; 0 .. to.length)
51 void* pto = to.ptr + i * element_size;
52 void* pfrom = from.ptr + i * element_size;
53 memcpy(tmp.ptr, pto, element_size);
54 memcpy(pto, pfrom, element_size);
61 for (int i = to.length; i--; )
63 void* pto = to.ptr + i * element_size;
64 void* pfrom = from.ptr + i * element_size;
65 memcpy(tmp.ptr, pto, element_size);
66 memcpy(pto, pfrom, element_size);
75 * Does array initialization (not assignment) from another
76 * array of the same element type.
77 * ti is the element type.
79 extern (C) void[] _d_arrayctor(TypeInfo ti, void[] from, void[] to)
81 debug(PRINTF) printf("_d_arrayctor(from = %p,%d, to = %p,%d) size = %d\n", from.ptr, from.length, to.ptr, to.length, ti.tsize());
83 if (to.length != from.length)
86 string msg = "lengths don't match for array initialization,"c;
87 msg ~= tmp.intToString(to.length) ~ " = " ~ tmp.intToString(from.length);
88 throw new Exception(msg);
91 auto element_size = ti.tsize();
96 for (i = 0; i < to.length; i++)
98 // Copy construction is defined as bit copy followed by postblit.
99 memcpy(to.ptr + i * element_size, from.ptr + i * element_size, element_size);
100 ti.postblit(to.ptr + i * element_size);
105 /* Destroy, in reverse order, what we've constructed so far
109 ti.destroy(to.ptr + i * element_size);
119 * Do assignment to an array.
120 * p[0 .. count] = value;
122 extern (C) void* _d_arraysetassign(void* p, void* value, int count, TypeInfo ti)
126 auto element_size = ti.tsize();
128 //Need a temporary buffer tmp[] big enough to hold one element
131 if (element_size > buf.sizeof)
133 tmp = alloca(element_size)[0 .. element_size];
138 foreach (i; 0 .. count)
140 memcpy(tmp.ptr, p, element_size);
141 memcpy(p, value, element_size);
150 * Do construction of an array.
151 * ti[count] p = value;
153 extern (C) void* _d_arraysetctor(void* p, void* value, int count, TypeInfo ti)
156 auto element_size = ti.tsize();
160 foreach (i; 0 .. count)
162 // Copy construction is defined as bit copy followed by postblit.
163 memcpy(p, value, element_size);
170 // Destroy, in reverse order, what we've constructed so far