Exemplo n.º 1
0
        private static ClassDisposeHandle CreateAction <TAction, TArg0>(TArg0 arg0, IntPtr pMethod, out TAction action)
            where TAction : Delegate
            where TArg0 : class
        {
            MethodTable *pMT = MethodTable.GetMethodTable <TAction>();

            ObjectHeader *pThis     = TypedReferenceHelper.ClassToPointer(arg0);
            ObjectHeader *objHeader = (ObjectHeader *)RawMemoryManager.Allocate(pMT->BaseSize);

            try
            {
                objHeader->SyncBlock   = 0;
                objHeader->MethodTable = pMT;

                Byte *objBody = (Byte *)(objHeader + 1);
                *(MethodTable ***)objBody = &pThis->MethodTable;     // Delegate._target
                *(IntPtr *)(objBody + 2 * sizeof(IntPtr)) = pMethod; // Delegate._methodPtr

                action = TypedReferenceHelper.PointerToObject <TAction>(objHeader);
            }
            catch
            {
                RawMemoryManager.Free(pMT->BaseSize, objHeader);
                throw;
            }

            return(new ClassDisposeHandle(pMT->BaseSize, objHeader));
        }
Exemplo n.º 2
0
        public unsafe static T PointerToObject <T>(ObjectHeader *objectHeader) where T : class
        {
            MethodTable **       ppMT           = &(objectHeader->MethodTable);
            PublicTypedReference typedReference = new PublicTypedReference
            {
                Value = &ppMT,
                Type  = (void *)typeof(T).TypeHandle.Value,
            };

            return(__refvalue(*(TypedReference *)&typedReference, T));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Similar with FormatterServices.GetUninitializedObject. Allocate and return zeroed T-typed object.
        /// </summary>
        public static ClassDisposeHandle UninitializedAllocation(Type t, out Object result)
        {
            MethodTable *mt   = MethodTable.GetMethodTable(t);
            int          size = mt->BaseSize;

            ObjectHeader *objHeader = (ObjectHeader *)RawMemoryManager.Allocate(size);

            objHeader->SyncBlock   = 0;
            objHeader->MethodTable = mt;
            RawMemoryManager.FillMemory(objHeader + 1, 0, size - sizeof(ObjectHeader));

            result = TypedReferenceHelper.PointerToObject <Object>(objHeader);
            return(new ClassDisposeHandle(size, objHeader));
        }
Exemplo n.º 4
0
        public static ClassDisposeHandle Box <T>(T val, out Object boxed) where T : struct
        {
            MethodTable *mt   = MethodTable.GetMethodTable <T>();
            int          size = mt->BaseSize;

            ObjectHeader *objHeader = (ObjectHeader *)RawMemoryManager.Allocate(size);

            void *src = TypedReferenceHelper.StructToPointer(ref val);

            objHeader->SyncBlock   = 0;
            objHeader->MethodTable = mt;
            RawMemoryManager.MemCpy(objHeader + 1, src, mt->DataSize);

            boxed = TypedReferenceHelper.PointerToObject <Object>(objHeader);
            return(new ClassDisposeHandle(size, objHeader));
        }
Exemplo n.º 5
0
        public static ClassDisposeHandle AllocateSZArray <T>(int size, out T[] array)
        {
            MethodTable *pElementMT  = MethodTable.GetMethodTable <T>();
            MethodTable *pArrayMT    = MethodTable.GetMethodTable <T[]>();
            int          elementSize = pElementMT->IsClass ? sizeof(IntPtr) : pElementMT->DataSize;

            int memSize = sizeof(ObjectHeader) + sizeof(SZArrayHeader) + elementSize * size;
            int align   = sizeof(IntPtr) - 1;

            memSize = (memSize + align) & (~align);

            void *         addr          = RawMemoryManager.Allocate(memSize);
            ObjectHeader * objHeader     = (ObjectHeader *)addr;
            SZArrayHeader *szArrayHeader = (SZArrayHeader *)(objHeader + 1);

            objHeader->MethodTable       = pArrayMT;
            szArrayHeader->NumComponents = size;

            array = TypedReferenceHelper.PointerToObject <T[]>(objHeader);
            return(new ClassDisposeHandle(memSize, objHeader));
        }
Exemplo n.º 6
0
 public static MethodTable **GetMethodTablePointer(ObjectHeader *headerPtr)
 {
     return((MethodTable **)((Int32 *)headerPtr + 1));
 }