Beispiel #1
0
        internal static uint BytesPer <T>()
        {
            System.TypedReference
                elem1 = __makeref(ArrayOfTwoElements <T> .Value[0]),
                elem2 = __makeref(ArrayOfTwoElements <T> .Value[1]);

            return((uint)((byte *)*(System.IntPtr *)(&elem2) - (byte *)*(System.IntPtr *)(&elem1)));
        }
        public unsafe static System.IntPtr AddressOf <T>(T t)
        //refember ReferenceTypes are references to the CLRHeader
        //where TOriginal : struct
        {
            System.TypedReference reference = __makeref(t);

            return(*(IntPtr *)(&reference));
        }
Beispiel #3
0
        public static bool Get(ref IStruct @this)
        {
            System.TypedReference tr = __makeref(@this);

            @this = __refvalue(tr, IStruct);

            return(true);
        }
Beispiel #4
0
        internal static bool TryRead <T>(System.IntPtr address, ref T t)
        {
            System.TypedReference tr = __makeref(t);

            *(System.IntPtr *)(&tr) = address;

            t = __refvalue(tr, T);

            return(true);
        }
Beispiel #5
0
        internal static T Read <T>(System.IntPtr address)
        {
            T obj = default(T);

            System.TypedReference tr = __makeref(obj);

            *(System.IntPtr *)(&tr) = address;

            return(__refvalue(tr, T));
        }
Beispiel #6
0
 private T GetInternal(int index)
 {
     unsafe
     {
         System.TypedReference reference = __makeref(_target);
         *(int *)*(int *)&reference = _storage[index * _elementSize];
         T result = __refvalue(reference, T);
         return(result);
     }
 }
Beispiel #7
0
        public static System.IntPtr AddressOf(object obj)
        {
            if (obj == null)
            {
                return(System.IntPtr.Zero);
            }

            System.TypedReference reference = __makeref(obj);

            System.TypedReference *pRef = &reference;

            return((System.IntPtr)pRef); //(&pRef)
        }
Beispiel #8
0
 public static bool TryAllocateAt <T>(System.IntPtr address, System.IntPtr typeHandle, out T t)
 {
     System.Runtime.InteropServices.Marshal.WriteInt32((System.IntPtr)(address), 0);
     System.Runtime.InteropServices.Marshal.WriteInt32((System.IntPtr)(address + 1 * sizeof(int)), (int)typeHandle);
     t = default(T);
     CommonIntermediateLanguage.InitBlock(address + 1 * sizeof(int), (byte)0, CommonIntermediateLanguage.SizeOf <T>());
     unsafe
     {
         System.TypedReference newObjectReference = __makeref(t);
         *(int *)(*(int *)&newObjectReference) = (address + 1 * sizeof(int)).ToInt32();
     }
     return(true);
 }
Beispiel #9
0
        //byte[] b = MakeArray<byte>(pBytes, lenBytes, sizeof(byte));

        //unsafe static void CreateUsage(string[] args)
        //{
        //    var arrayDouble = Enumerable.Range(1, 1024)
        //                                .Select(i => (double)i)
        //                                .ToArray();

        //    fixed (double* p = arrayDouble)
        //    {
        //        var array2 = Create<double>(p, arrayDouble.Length);

        //        Assert.AreEqual(arrayDouble, array2);
        //    }

        //    var arrayPoint = Enumerable.Range(1, 1024)
        //                               .Select(i => new Point(i, i * 2 + 1))
        //                               .ToArray();

        //    fixed (Point* p = arrayPoint)
        //    {
        //        var array2 = Create<Point>(p, arrayPoint.Length);

        //        Assert.AreEqual(arrayPoint, array2);
        //    }
        //}

        #endregion

        #region Other

        //https://blog.adamfurmanek.pl/blog/2016/06/25/custom-memory-allocation-in-c-part-4/

        public static object AllocateAt(int address, System.IntPtr typeHandle)
        {
            System.Runtime.InteropServices.Marshal.WriteInt32((System.IntPtr)(address), 0);
            System.Runtime.InteropServices.Marshal.WriteInt32((System.IntPtr)(address + 1 * sizeof(int)), (int)typeHandle);
            object createdObject = null;

            unsafe
            {
                System.TypedReference newObjectReference = __makeref(createdObject);
                *(int *)(*(int *)&newObjectReference) = address + 1 * sizeof(int);
            }
            return(createdObject);
        }
Beispiel #10
0
    static int Main()
    {
        int i = 0;

        System.TypedReference r = __makeref(i);
        System.Type           t = __reftype(r);
        int j = __refvalue(r, int);

        __refvalue(r, int) = 4;
        var x = (__refvalue(r, int) += 1);

        if (x != 5)
        {
            return(1);
        }

        return(0);
    }
Beispiel #11
0
            public void Add(T item)
            {
                _currentIndex++;

                unsafe
                {
                    System.TypedReference reference = __makeref(item);
                    int *itemAddress = (int *)(*(int *)*(int *)&reference - 4);

                    for (int i = 1; i < _elementSize; ++i)
                    {
                        _storage[_currentIndex * _elementSize + i] = *itemAddress;
                        itemAddress = itemAddress + 1;
                    }

                    reference = __makeref(_storage);
                    _storage[_currentIndex * _elementSize] = *(int *)*(int *)&reference + _currentIndex * _elementSize * 4 + 16;
                    _elements[_currentIndex] = GetInternal(_currentIndex);
                }
            }
Beispiel #12
0
        static object Read(System.IntPtr address)
        {
            //Create memory for a boxed object
            object box = default(object);

            //Make a TypedReference to the object created
            System.TypedReference tr = __makeref(box);

            //This is equivalent to shooting yourself in the foot
            //but it's the only high-perf solution in some cases
            //it sets the first field of the TypedReference (which is a pointer)
            //to the address you give it, then it dereferences the value.
            //Better be 10000% sure that your type T is unmanaged/blittable...
            *(System.IntPtr *)(&tr) = address;

            //Using __refvalue
            //return __refvalue(tr, object);

            //Convert from the TypedReference back to object
            return(System.TypedReference.ToObject(tr));
        }
Beispiel #13
0
        public static System.IntPtr AddressOf <T>(ref T t)
        {
            System.TypedReference reference = __makeref(t);

            return(**(System.IntPtr * *)(&reference));
        }
Beispiel #14
0
        public unsafe static System.IntPtr AddressOf <T>(T t)
        {
            System.TypedReference reference = __makeref(t);

            return(*(IntPtr *)(&reference));
        }
Beispiel #15
0
 public virtual void SetValueDirect(System.TypedReference obj, object value)
 {
 }
Beispiel #16
0
 public virtual object GetValueDirect(System.TypedReference obj)
 {
 }
Beispiel #17
0
        internal TypeReference(string type)
        {
            System.TypedReference typedRefeence = __makeref(type);

            Reference = (System.IntPtr)(&typedRefeence);
        }