|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Buffers.Binary; |
| 7 | + |
| 8 | +public class Program |
| 9 | +{ |
| 10 | + [StructLayout(LayoutKind.Sequential, Size=12)] |
| 11 | + struct S { |
| 12 | + public double d1; |
| 13 | + public int i2; |
| 14 | + } |
| 15 | + |
| 16 | + [StructLayout(LayoutKind.Sequential)] |
| 17 | + struct H { |
| 18 | + public S s1; |
| 19 | + public int i3; |
| 20 | + public byte b4; |
| 21 | + } |
| 22 | + |
| 23 | + public static int Main() |
| 24 | + { |
| 25 | + Console.WriteLine("Hello, Android!"); |
| 26 | + |
| 27 | + Console.WriteLine(Marshal.SizeOf(typeof(S))); |
| 28 | + var a2 = new S[2]; |
| 29 | + a2[1].d1 = 2.12; |
| 30 | + Console.WriteLine (a2[1].d1); |
| 31 | + var h1 = new H(); |
| 32 | + h1.s1.d1 = 4.0; |
| 33 | + h1.i3 = 15; |
| 34 | + h1.b4 = 42; |
| 35 | + unsafe { |
| 36 | + fixed (S* parr = &a2[0]) |
| 37 | + { |
| 38 | + byte* ptr = (byte*)parr; |
| 39 | + ptr += 12; |
| 40 | + Console.WriteLine ($"0x{((IntPtr)ptr):x}"); |
| 41 | + Console.WriteLine (*(ptr + 0)); |
| 42 | + Console.WriteLine (*(ptr + 1)); |
| 43 | + Console.WriteLine (*(ptr + 2)); |
| 44 | + Console.WriteLine (*(ptr + 3)); |
| 45 | + Console.WriteLine (*(ptr + 4)); |
| 46 | + Console.WriteLine (*(ptr + 5)); |
| 47 | + Console.WriteLine (*(ptr + 6)); |
| 48 | + Console.WriteLine (*(ptr + 7)); |
| 49 | + Span<byte> tmp = stackalloc byte[8]; |
| 50 | + new Span<byte>(ptr, 8).CopyTo(tmp); |
| 51 | + double d = BinaryPrimitives.ReadDoubleLittleEndian(tmp); |
| 52 | + Console.WriteLine ($"tmp = {d}"); |
| 53 | + if (d != 2.12) // if this prints 2.12 - unaligned load happened |
| 54 | + return 1; |
| 55 | + } |
| 56 | + { |
| 57 | + byte* ptr = (byte*)&h1; |
| 58 | + Console.WriteLine (*(ptr + 12)); |
| 59 | + Console.WriteLine (*(ptr + 13)); |
| 60 | + Console.WriteLine (*(ptr + 14)); |
| 61 | + Console.WriteLine (*(ptr + 15)); |
| 62 | + Console.WriteLine (*(ptr + 16)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return 42; |
| 67 | + } |
| 68 | +} |
0 commit comments