|
7 | 7 | #include "CorLib.h" |
8 | 8 |
|
9 | 9 | typedef Library_corlib_native_System_Runtime_CompilerServices_RuntimeHelpers RuntimeHelpers; |
| 10 | +typedef Library_corlib_native_System_Span_1 Span_1; |
10 | 11 |
|
11 | 12 | HRESULT Library_corlib_native_System_ReadOnlySpan_1::_ctor___VOID__VOIDptr__I4(CLR_RT_StackFrame &stack) |
12 | 13 | { |
@@ -105,6 +106,47 @@ HRESULT Library_corlib_native_System_ReadOnlySpan_1::_ctor___VOID__VOIDptr__I4(C |
105 | 106 | NANOCLR_NOCLEANUP(); |
106 | 107 | } |
107 | 108 |
|
| 109 | +HRESULT Library_corlib_native_System_ReadOnlySpan_1::CopyTo___VOID__SystemSpan_1(CLR_RT_StackFrame &stack) |
| 110 | +{ |
| 111 | + NANOCLR_HEADER(); |
| 112 | + |
| 113 | + CLR_RT_HeapBlock_Array *sourceArray; |
| 114 | + CLR_RT_HeapBlock_Array *destinationArray; |
| 115 | + CLR_RT_HeapBlock *thisSpan = stack.This(); |
| 116 | + CLR_RT_HeapBlock *destinationSpan = stack.Arg1().Dereference(); |
| 117 | + |
| 118 | + // check lengths - destination must be at least as large as source |
| 119 | + if (thisSpan[FIELD___length].NumericByRefConst().u4 > |
| 120 | + destinationSpan[Span_1::FIELD___length].NumericByRefConst().u4) |
| 121 | + { |
| 122 | + NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER); |
| 123 | + } |
| 124 | + |
| 125 | + // get pointers to the arrays |
| 126 | + sourceArray = thisSpan[FIELD___array].DereferenceArray(); |
| 127 | + destinationArray = destinationSpan[Span_1::FIELD___array].DereferenceArray(); |
| 128 | + |
| 129 | + { |
| 130 | + // sanity check for empty source array |
| 131 | + if (thisSpan[FIELD___length].NumericByRefConst().s4 == 0) |
| 132 | + { |
| 133 | + NANOCLR_SET_AND_LEAVE(S_OK); |
| 134 | + } |
| 135 | + |
| 136 | + // prevent GC from moving the arrays while we copy the data |
| 137 | + CLR_RT_ProtectFromGC gc1(*sourceArray); |
| 138 | + CLR_RT_ProtectFromGC gc2(*destinationArray); |
| 139 | + |
| 140 | + // use memmove to safely handle potential overlapping memory regions |
| 141 | + memmove( |
| 142 | + destinationArray->GetElement(0), |
| 143 | + sourceArray->GetElement(0), |
| 144 | + thisSpan[FIELD___length].NumericByRefConst().s4 * sourceArray->m_sizeOfElement); |
| 145 | + } |
| 146 | + |
| 147 | + NANOCLR_NOCLEANUP(); |
| 148 | +} |
| 149 | + |
108 | 150 | HRESULT Library_corlib_native_System_ReadOnlySpan_1::NativeReadOnlySpanConstructor___VOID__SZARRAY_GENERICTYPE__I4__I4( |
109 | 151 | CLR_RT_StackFrame &stack) |
110 | 152 | { |
|
0 commit comments