Beispiel #1
0
        public void Test()
        {
            int valType = 0xFF;

            Assert.That(new IntPtr(&valType), Is.EqualTo(Unsafe.AddressOf(ref valType)));

            string s = "foo";

            Assert.That(new IntPtr(CSUnsafe.AsPointer(ref s)), Is.EqualTo(Unsafe.AddressOf(ref s)));

            IntPtr sChars = Unsafe.AddressOfHeap(ref s, OffsetType.StringData);

            Assert.That(Marshal.ReadInt16(sChars), Is.EqualTo(s[0]));

            int[]  arr     = { 1, 2, 3 };
            IntPtr arrData = Unsafe.AddressOfHeap(ref arr, OffsetType.ArrayData);

            Assert.That(Marshal.ReadInt32(arrData), Is.EqualTo(arr[0]));

            Dummy  d     = new Dummy(100, "bar");
            IntPtr dData = Unsafe.AddressOfHeap(ref d, OffsetType.Fields);

            // Largest field is first in memory
            Assert.That(Marshal.ReadInt32(dData, IntPtr.Size), Is.EqualTo(100));
        }
Beispiel #2
0
        public unsafe static bool IsNegativeZero(ref double d)
        {
            //Make a pointer to the pointer, which when dereferenced can access the result.
            System.IntPtr value = Unsafe.AddressOf(ref d);

            //Check for -0.0
            return((*((long *)((int *)&value))).Equals(Common.Binary.NegativeZeroBits));
        }
Beispiel #3
0
 public TypedInformation(T t)
     : base(new Classes.T.T()
 {
     Reference = Unsafe.AddressOf(ref t)
 })
 {
     TypeReference = new TypeReference <T>(t);
 }
Beispiel #4
0
        public static ArrayObject **GetArrayObject <T>(ref T t) where T : class
        {
            if (!typeof(T).IsArray)
            {
                TypeException.Throw <Array, T>();
            }

            return((ArrayObject **)Unsafe.AddressOf(ref t));
        }
Beispiel #5
0
        /// <summary>
        /// Manually reads a CLR MethodTable (TypeHandle)
        /// </summary>
        /// <returns>A pointer to the object type's MethodTable</returns>
        public static MethodTable *ReadMethodTable <T>(ref T t)
        {
            // Value types do not have a MethodTable ptr, but they do have a TypeHandle.
            if (typeof(T).IsValueType)
            {
                return(MethodTableOf <T>());
            }

            // We need to get the heap pointer manually because of type constraints
            var ptr  = (Marshal.ReadIntPtr(Unsafe.AddressOf(ref t)));
            var @out = *(MethodTable **)ptr;

            return(@out);

            //return (*((HeapObject**) Unsafe.AddressOf(ref t)))->MethodTable;
        }
Beispiel #6
0
        /// <summary>
        /// Asserts that a Pointer points to the correct object address during GC pressure
        /// </summary>
        internal static void AssertPressure <TPointer, TValue>(Pointer <TPointer> ptr, ref TValue t)
        {
            int passes = 0;

            while (passes++ < MaxPasses)
            {
                object[] oArr = new object[MaxObjects];
                for (int i = 0; i < oArr.Length; i++)
                {
                    oArr[i] = new object();
                }


                Assert.That(ptr.Value, Is.EqualTo(t));
                Assert.That(ptr.Address, Is.EqualTo(Unsafe.AddressOf(ref t)));
            }
        }
Beispiel #7
0
        /// <summary>
        /// Asserts that the heap and stack pointers of a reference type don't change at all
        /// during GC compaction. This test will pass if the parameter is pinned.
        /// </summary>
        internal static void AssertPinning <T>(ref T t) where T : class
        {
            (IntPtr stackPtr, IntPtr heap)mem = (Unsafe.AddressOf(ref t), Unsafe.AddressOfHeap(ref t));


            int passes = 0;

            while (passes++ < MaxPasses)
            {
                object[] oArr = new object[MaxObjects];
                for (int i = 0; i < oArr.Length; i++)
                {
                    oArr[i] = new object();
                }

                // pass when reference is pinned
                Assert.That(mem.stackPtr, Is.EqualTo(Unsafe.AddressOf(ref t)));
                Assert.That(mem.heap, Is.EqualTo(Unsafe.AddressOfHeap(ref t)));
            }
        }
Beispiel #8
0
 public unsafe bool Equals(Extensions other)
 {
     return(object.ReferenceEquals(this, NilExtensions) ? object.ReferenceEquals(other, NilExtensions) : Unsafe.AddressOf(this) == Unsafe.AddressOf(other));
 }
Beispiel #9
0
 //fixme
 private EEClassLayoutInfo *GetLayoutInfo()
 {
     return(&((LayoutEEClass *)(Unsafe.AddressOf(ref this)))->m_LayoutInfo);            //
 }
Beispiel #10
0
        public static HeapObject **GetHeapObject <T>(ref T t) where T : class
        {
            HeapObject **h = (HeapObject **)Unsafe.AddressOf(ref t);

            return(h);
        }
Beispiel #11
0
 public static StringObject **GetStringObject(ref string s)
 {
     return((StringObject **)Unsafe.AddressOf(ref s));
 }
Beispiel #12
0
 /// <summary>
 /// <c>PTR_HOST_MEMBER_TADDR</c>
 /// </summary>
 internal static Pointer <byte> HostMemberOffset <T>(ref T value, long ofs, Pointer <byte> fieldValue)
     where T : struct
 {
     return(Unsafe.AddressOf(ref value).Add((long)fieldValue).Add(ofs).Cast());
 }
Beispiel #13
0
 protected internal AddressInfo(ref T t)
 {
     Address = Unsafe.AddressOf(ref t);
 }