@@ -1315,6 +1315,75 @@ public IntPtr AddRef()
13151315 }
13161316 } // class CleanupWorkListElement
13171317
1318+ internal unsafe struct CopyConstructorCookie
1319+ {
1320+ private void * m_source;
1321+
1322+ private nuint m_destinationOffset;
1323+
1324+ public delegate * < void * , void * , void > m_copyConstructor;
1325+
1326+ public delegate * < void * , void > m_destructor;
1327+
1328+ public CopyConstructorCookie* m_next;
1329+
1330+ [ StackTraceHidden]
1331+ public void ExecuteCopy( void * destinationBase)
1332+ {
1333+ if ( m_copyConstructor != null )
1334+ {
1335+ m_copyConstructor( ( byte * ) destinationBase + m_destinationOffset, m_source) ;
1336+ }
1337+
1338+ if ( m_destructor != null )
1339+ {
1340+ m_destructor( m_source) ;
1341+ }
1342+ }
1343+ }
1344+
1345+ internal unsafe struct CopyConstructorChain
1346+ {
1347+ public void * m_realTarget;
1348+ public CopyConstructorCookie* m_head;
1349+
1350+ public void Add( CopyConstructorCookie* cookie)
1351+ {
1352+ cookie->m_next = m_head;
1353+ m_head = cookie;
1354+ }
1355+
1356+ [ ThreadStatic]
1357+ private static CopyConstructorChain s_copyConstructorChain;
1358+
1359+ public void Install( void * realTarget)
1360+ {
1361+ m_realTarget = realTarget;
1362+ s_copyConstructorChain = this ;
1363+ }
1364+
1365+ [ StackTraceHidden]
1366+ private void ExecuteCopies( void * destinationBase)
1367+ {
1368+ for ( CopyConstructorCookie* current = m_head; current != null ; current = current->m_next)
1369+ {
1370+ current->ExecuteCopy( destinationBase) ;
1371+ }
1372+ }
1373+
1374+ [ UnmanagedCallersOnly]
1375+ [ StackTraceHidden]
1376+ public static void * ExecuteCurrentCopiesAndGetTarget( void * destinationBase)
1377+ {
1378+ void * target = s_copyConstructorChain. m_realTarget;
1379+ s_copyConstructorChain. ExecuteCopies( destinationBase) ;
1380+ // Reset this instance to ensure we don't accidentally execute the copies again.
1381+ // All of the pointers point to the stack, so we don't need to free any memory.
1382+ s_copyConstructorChain = default ;
1383+ return target;
1384+ }
1385+ }
1386+
13181387 internal static partial class StubHelpers
13191388 {
13201389 [ MethodImpl( MethodImplOptions. InternalCall) ]
0 commit comments