|
| 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 | +// Note: In below test case, we were not honoring the fact that the explicit struct size |
| 5 | +// of struct is 32 bytes while the only 2 fields it has is just 2 bytes. In such case, |
| 6 | +// we would pass partial struct value. |
| 7 | +using System; |
| 8 | +using System.Reflection.Emit; |
| 9 | +using System.Runtime.CompilerServices; |
| 10 | +using System.Runtime.InteropServices; |
| 11 | +using System.Text; |
| 12 | + |
| 13 | +[StructLayout(LayoutKind.Explicit, Size = 32)] |
| 14 | +public readonly unsafe struct SmallString |
| 15 | +{ |
| 16 | + [FieldOffset(0)] private readonly byte _length; |
| 17 | + [FieldOffset(1)] private readonly byte _firstByte; |
| 18 | + |
| 19 | + public SmallString(string value) |
| 20 | + { |
| 21 | + fixed (char* srcPtr = value) |
| 22 | + fixed (byte* destPtr = &_firstByte) |
| 23 | + { |
| 24 | + Encoding.ASCII.GetBytes(srcPtr, value.Length, destPtr, value.Length); |
| 25 | + } |
| 26 | + |
| 27 | + _length = (byte)value.Length; |
| 28 | + } |
| 29 | + |
| 30 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 31 | + public byte Dump() |
| 32 | + { |
| 33 | + fixed (byte* ptr = &_firstByte) |
| 34 | + { |
| 35 | + byte* next = ptr + 1; |
| 36 | + return *next; |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +public static class Program |
| 42 | +{ |
| 43 | + static int result = 0; |
| 44 | + public static int Main() |
| 45 | + { |
| 46 | + var value = new SmallString("foobar"); |
| 47 | + |
| 48 | + TheTest(value); |
| 49 | + |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 54 | + public static void TheTest(SmallString foo) |
| 55 | + { |
| 56 | + Execute(foo); |
| 57 | + } |
| 58 | + |
| 59 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 60 | + public static object Execute(SmallString foo) |
| 61 | + { |
| 62 | + byte value = foo.Dump(); |
| 63 | + // 111 corresponds to the ASCII code of 2nd characted of string "foobar" i.e. ASCII value of 'o'. |
| 64 | + if (value == 111) |
| 65 | + { |
| 66 | + result = 100; |
| 67 | + } |
| 68 | + return new StringBuilder(); |
| 69 | + } |
| 70 | +} |
0 commit comments