Esempio n. 1
0
        public static IntPtr AllocateArray(RuntimeTypeHandle handle, uint elementSize, uint elements)
        {
            // An array has the following memory layout:
            //   - IntPtr TypeDef
            //   - IntPtr SyncBlock
            //   - int length
            //   - ElementType[length] elements
            //   - Padding

            uint allocationSize = ((uint)(IntPtr.Size) * 3) + (elements * elementSize);

            allocationSize = (allocationSize + 3) & ~3u;                // Align to 4-bytes boundary

            var memory = GC.AllocateObject(allocationSize);

            Intrinsic.Store(memory, 0, handle.Value);

            if (IntPtr.Size == 4)
            {
                Intrinsic.Store32(memory, IntPtr.Size, 0);
                Intrinsic.Store32(memory, IntPtr.Size * 2, elements);
            }
            else
            {
                Intrinsic.Store64(memory, IntPtr.Size, 0);
                Intrinsic.Store64(memory, IntPtr.Size * 2, elements);
            }

            return(memory);
        }
Esempio n. 2
0
        public static IntPtr Box16(RuntimeTypeHandle handle, ushort value)
        {
            var memory = AllocateObject(handle, IntPtr.Size);

            Intrinsic.Store(memory, 0, handle.Value);
            Intrinsic.Store16(memory, IntPtr.Size * 2, value);

            return(memory);
        }
Esempio n. 3
0
        public static IntPtr BoxR8(RuntimeTypeHandle handle, double value)
        {
            var memory = AllocateObject(handle, IntPtr.Size * 2);

            Intrinsic.Store(memory, 0, handle.Value);
            Intrinsic.StoreR8(memory, IntPtr.Size * 2, value);

            return(memory);
        }
Esempio n. 4
0
        public static IntPtr AllocateObject(RuntimeTypeHandle handle, uint classSize)
        {
            // An object has the following memory layout:
            //   - IntPtr TypeDef
            //   - IntPtr SyncBlock
            //   - 0 .. n object data fields

            var memory = GC.AllocateObject((2 * (uint)(IntPtr.Size)) + classSize);

            Intrinsic.Store(memory, 0, handle.Value);

            if (IntPtr.Size == 4)
            {
                Intrinsic.Store32(memory, IntPtr.Size, 0);
            }
            else
            {
                Intrinsic.Store64(memory, IntPtr.Size, 0);
            }

            return(memory);
        }