Example #1
0
        ///////////////////////////////////////////////////////////////////////
        // Listings 13-79, 13-81
        static void Main(string[] args)
        {
            Console.WriteLine(sizeof(Test1));
            Console.WriteLine(sizeof(Test2));
            //Console.WriteLine(sizeof(Test1o));
            //Console.WriteLine(sizeof(Test2o));
            Console.WriteLine(Unsafe.SizeOf <Test3>());
            Console.WriteLine(Unsafe.SizeOf <Test4>());

            TypeLayout.PrintLayout <SomeClass>();
            TypeLayout layout = TypeLayout.GetLayout <AlignedDouble>();

            Console.WriteLine($"Total size {layout.FullSize}B with {layout.Paddings}B padding.");
            foreach (var fieldBase in layout.Fields)
            {
                switch (fieldBase)
                {
                case FieldLayout field: Console.WriteLine($"{field.Offset} {field.Size} {field.FieldInfo.Name}"); break;

                case Padding padding: Console.WriteLine($"{padding.Offset} {padding.Size} Padding"); break;
                }
            }
            //TypeLayout.PrintLayout<UnalignedDoubleExplicit>();
            //var o = new AlignedDouble();
            //Console.ReadLine();
            //GC.KeepAlive(o);
            Console.ReadLine();
        }
Example #2
0
        void AssertSize <T>(int expectedSize)
        {
            Assert.Equal(expectedSize, Unsafe.SizeOf <T>());
            Assert.Equal(expectedSize, Marshal.SizeOf <T>());

            var layout = TypeLayout.GetLayout <T>();

            // check that fields are aligned
            foreach (var field in layout.Fields)
            {
                if ($"{field}".Contains("padding"))
                {
                    continue;
                }
                Assert.True(field.Offset % field.Size == 0, $"Field {field} is not aligned");
            }

            // check that fields don't overlap
            var minOffset = 0;

            foreach (var field in layout.Fields)
            {
                Assert.True(field.Offset >= minOffset, $"Field {field.Offset} overlaps");
                minOffset = field.Offset + field.Size;
            }

            // note that we aren't checking whether fields are efficiently arranged...
            // also not currently checking if the whole struct is correctly aligned, only the fields within.
        }
        public void Nullable()
        {
            TypeLayout.PrintLayout <Nullable <bool> >();
            var typeLayout = TypeLayout.GetLayout <Nullable <bool> >(includePaddings: true);

            Assert.AreEqual(Unsafe.SizeOf <Nullable <bool> >(), typeLayout.Size);
        }
        public void UnsafeStructHasEmptyPaddings()
        {
            AssertNonRecursiveWithPadding <WithNestedUnsafeStruct>();
            var typeLayout = TypeLayout.GetLayout <WithNestedUnsafeStruct>(includePaddings: true);

            Assert.That(typeLayout.Paddings, Is.EqualTo(0));
        }
Example #5
0
 public void PrivateMemoryLayout()
 {
     TypeLayout.PrintLayout <PrivateMemory <long> >(recursively: false);
     TypeLayout.PrintLayout <PrivateMemory <byte> >(recursively: false);
     TypeLayout.PrintLayout <PrivateMemory <object> >(recursively: false);
     var layout = TypeLayout.GetLayout <PrivateMemory <long> >();
 }
        public void NullableLongByteStruct()
        {
            TypeLayout.PrintLayout <Nullable <LongByteStruct> >();
            var typeLayout = TypeLayout.GetLayout <Nullable <LongByteStruct> >(includePaddings: true);
            var size       = Unsafe.SizeOf <Nullable <LongByteStruct> >();

            Assert.AreEqual(typeLayout.Size, size);
        }
Example #7
0
        public void LayoutForInstanceWithStringShouldNotCrash()
        {
            // I know, I know. This is bad to name tests like this. But this is life:)
            var layout = TypeLayout.GetLayout <WithString>();

            Assert.That(layout.Fields.Length, Is.EqualTo(1));
            Assert.That(layout.Paddings, Is.EqualTo(0));
        }
        protected void AssertNonRecursiveWithPadding <T>() where T : struct
        {
            TypeLayout.PrintLayout <T>();
            var structLayout = UnsafeLayout.GetLayout <T>(recursive: false);
            var typeLayout   = TypeLayout.GetLayout <T>(includePaddings: true);

            CollectionAssert.AreEquivalent(typeLayout.Fields, structLayout);
            Assert.AreEqual(Unsafe.SizeOf <T>(), typeLayout.Size);
        }
Example #9
0
        public void Print_WithVolatile()
        {
            AssertNonRecursive <WithVolatile>();

            TypeLayout.PrintLayout <WithVolatile>();
            var typeLayout = TypeLayout.GetLayout <WithVolatile>();

            Assert.That(typeLayout.FullSize, Is.EqualTo(16));
        }
        public void Print_WithVolatile()
        {
            Console.WriteLine(Marshal.SizeOf <WithVolatile>());

            TypeLayout.PrintLayout <WithVolatile>();

            var layout = TypeLayout.GetLayout <WithVolatile>();

            Assert.That(layout.FullSize, Is.EqualTo(16));
        }
 public void PrintClassMultipleByteWrappersLayout()
 {
     // In this case every field aligned on the pointer boundaries
     TypeLayout.PrintLayout <StructWithAutomaticLayout>();
     Assert.That(TypeLayout.GetLayout <StructWithAutomaticLayout>().Size, Is.EqualTo(24));
 }
        public void FixedBytesNoPadding()
        {
            var typeLayout = TypeLayout.GetLayout <FixedBytes>(includePaddings: true);

            Assert.That(typeLayout.Paddings, Is.EqualTo(0));
        }
Example #13
0
        public void InspectLayout(Type t)
        {
            var layout = TypeLayout.GetLayout(t);

            _output.WriteLine(layout.ToString(recursively: false));
        }
Example #14
0
 public void TypeLayoutPaddingsStruct()
 {
     TypeLayout.GetLayout <VeryVeryComplexStruct>(includePaddings: true);
 }
Example #15
0
        public void InspectLayout(Type t)
        {
            var layout = TypeLayout.GetLayout(t);

            _output.WriteLine($"{layout}");
        }
Example #16
0
 public void TypeLayoutStruct()
 {
     TypeLayout.GetLayout <VeryVeryComplexStruct>();
 }
Example #17
0
        public void NoFieldsForEmptyClass()
        {
            var layout = TypeLayout.GetLayout <EmptyClass>();

            Assert.That(layout.Fields, Is.Empty);
        }
Example #18
0
        public void SizeForEmptyClassIs3PtrSizes()
        {
            var layout = TypeLayout.GetLayout <EmptyClass>();

            Assert.That(layout.FullSize, Is.EqualTo(IntPtr.Size * 3));
        }
 static SimpleObject()
 {
     SimpleObject.TypeLayout = TypeLayout.GetLayout <SimpleObject>();
 }
Example #20
0
        public void UnsafeStructHasEmptyPaddings()
        {
            var typeLayout = TypeLayout.GetLayout <WithNestedUnsafeStruct>();

            Assert.That(typeLayout.Paddings, Is.EqualTo(0));
        }
 public void PrintStructMultipleByteWrappersLayout()
 {
     // If the layout is sequential, then structs are aligned properly with no paddings
     TypeLayout.PrintLayout <StructMultipleByteWrappers>();
     Assert.That(TypeLayout.GetLayout <StructMultipleByteWrappers>().Size, Is.EqualTo(3));
 }