|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using InlineIL; |
| 5 | + |
| 6 | +namespace Silk.NET.Core; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Represents an untyped constant pointer. |
| 10 | +/// </summary> |
| 11 | +public readonly unsafe ref struct Any2D |
| 12 | +{ |
| 13 | +#pragma warning disable CS0649 |
| 14 | + private readonly ref byte InteriorRef; |
| 15 | +#pragma warning restore CS0649 |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Creates a <see cref="Any"/> pointing at the given <see cref="ConstPtr{T}"/>. |
| 19 | + /// </summary> |
| 20 | + /// <param name="ref">The reference to the pointee <see cref="ConstPtr{T}"/>.</param> |
| 21 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 22 | + public Any2D(ref Any @ref) |
| 23 | + { |
| 24 | + IL.Emit.Ldarg_0(); |
| 25 | + IL.Emit.Ldarg_1(); |
| 26 | + IL.Emit.Stfld(FieldRef.Field(TypeRef.Type(typeof(Any2D)), nameof(InteriorRef))); |
| 27 | + IL.Emit.Ret(); |
| 28 | + throw IL.Unreachable(); |
| 29 | + } |
| 30 | + |
| 31 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 32 | + private Any2D(ref byte interior) => InteriorRef = ref interior; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The underlying reference. |
| 36 | + /// </summary> |
| 37 | + public ref Any Ref |
| 38 | + { |
| 39 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 40 | + get |
| 41 | + { |
| 42 | + // Would use the delegate* trick but this isn't optimised in JIT yet or necessarily safe |
| 43 | + IL.Emit.Ldarg_0(); |
| 44 | + IL.Emit.Ldfld(FieldRef.Field(TypeRef.Type(typeof(Any2D)), |
| 45 | + nameof(InteriorRef))); |
| 46 | + IL.Emit.Ret(); |
| 47 | + throw IL.Unreachable(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the item at the given offset from this pointer. |
| 53 | + /// </summary> |
| 54 | + /// <param name="index">The index.</param> |
| 55 | + public ref Any this[nuint index] |
| 56 | + { |
| 57 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 58 | + get |
| 59 | + { |
| 60 | + IL.Emit.Ldarg_0(); |
| 61 | + IL.Emit.Ldfld(FieldRef.Field(TypeRef.Type(typeof(Any2D)), |
| 62 | + nameof(InteriorRef))); |
| 63 | + IL.Emit.Ldarg_1(); |
| 64 | + IL.Emit.Sizeof<nuint>(); |
| 65 | + IL.Emit.Mul(); |
| 66 | + IL.Emit.Add(); |
| 67 | + IL.Emit.Ret(); |
| 68 | + throw IL.Unreachable(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // TODO strings? |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Gets the underlying reference. |
| 76 | + /// </summary> |
| 77 | + /// <returns>The underlying reference.</returns> |
| 78 | + /// <remarks> |
| 79 | + /// This function allows a <see cref="ConstPtr{T}"/> to be used in a <c>fixed</c> statement. |
| 80 | + /// </remarks> |
| 81 | + public ref void* GetPinnableReference() |
| 82 | + { |
| 83 | + IL.Emit.Ldarg_0(); |
| 84 | + IL.Emit.Ldfld(FieldRef.Field(TypeRef.Type(typeof(Any2D)), |
| 85 | + nameof(InteriorRef))); |
| 86 | + IL.Emit.Ret(); |
| 87 | + throw IL.Unreachable(); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Creates a <see cref="Any2D"/> from a raw pointer. |
| 92 | + /// </summary> |
| 93 | + /// <param name="raw">The raw pointer.</param> |
| 94 | + /// <returns>The pointer.</returns> |
| 95 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 96 | + public static implicit operator Any2D(void** raw) |
| 97 | + { |
| 98 | + IL.DeclareLocals(new LocalVar("ret", TypeRef.Type(typeof(Any2D)))); |
| 99 | + IL.Emit.Ldloca_S("ret"); |
| 100 | + IL.Emit.Initobj(TypeRef.Type(typeof(Any2D))); |
| 101 | + IL.Emit.Ldloca_S("ret"); |
| 102 | + IL.Emit.Ldarg_0(); |
| 103 | + IL.Emit.Stfld( |
| 104 | + FieldRef.Field(TypeRef.Type(typeof(Any2D)), nameof(InteriorRef))); |
| 105 | + IL.Emit.Ldloc_S("ret"); |
| 106 | + IL.Emit.Ret(); |
| 107 | + throw IL.Unreachable(); |
| 108 | + } |
| 109 | + |
| 110 | + // TODO arrays? |
| 111 | + |
| 112 | + // TODO strings? |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Creates a <see cref="Any2D"/> from an array of pointers. |
| 116 | + /// </summary> |
| 117 | + /// <param name="array">The array.</param> |
| 118 | + /// <returns>The pointer.</returns> |
| 119 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 120 | + [SuppressMessage("ReSharper", "EntityNameCapturedOnly.Local")] |
| 121 | + public static implicit operator Any2D(void*[] array) |
| 122 | + { |
| 123 | + IL.Emit.Ldarg_0(); |
| 124 | + IL.Emit.Ldc_I4_0(); |
| 125 | + IL.Emit.Ldelema(TypeRef.Type(typeof(void).MakePointerType())); |
| 126 | + IL.Emit.Newobj(MethodRef.Constructor(TypeRef.Type(typeof(Any2D)), |
| 127 | + TypeRef.Type(typeof(Any).MakeByRefType()))); |
| 128 | + IL.Emit.Ret(); |
| 129 | + throw IL.Unreachable(); |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Creates a null <see cref="Any2D"/>. |
| 134 | + /// </summary> |
| 135 | + /// <param name="_"><see cref="DSL.nullptr"/></param> |
| 136 | + /// <returns>A null pointer.</returns> |
| 137 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 138 | + public static implicit operator Any2D(NullPtr _) |
| 139 | + { |
| 140 | + IL.DeclareLocals(new LocalVar("ret", TypeRef.Type(typeof(Any2D)))); |
| 141 | + IL.Emit.Ldloca_S("ret"); |
| 142 | + IL.Emit.Initobj(TypeRef.Type(typeof(Any2D))); |
| 143 | + IL.Emit.Ldloca_S("ret"); |
| 144 | + IL.Emit.Ldc_I4_0(); |
| 145 | + IL.Emit.Conv_U(); |
| 146 | + IL.Emit.Stfld( |
| 147 | + FieldRef.Field(TypeRef.Type(typeof(Any2D)), nameof(InteriorRef))); |
| 148 | + IL.Emit.Ldloc_S("ret"); |
| 149 | + IL.Emit.Ret(); |
| 150 | + throw IL.Unreachable(); |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Unsafely gets a raw pointer from a <see cref="Any2D"/>. |
| 155 | + /// </summary> |
| 156 | + /// <param name="ptr">The <see cref="Any2D"/>.</param> |
| 157 | + /// <returns>The raw pointer.</returns> |
| 158 | + /// <remarks> |
| 159 | + /// This is unsafe if the reference is a managed reference. You should prefer pinning using <c>fixed</c> instead. |
| 160 | + /// </remarks> |
| 161 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 162 | + public static explicit operator void**(Any2D ptr) |
| 163 | + { |
| 164 | + IL.Emit.Ldarg_0(); |
| 165 | + IL.Emit.Ldfld( |
| 166 | + FieldRef.Field(TypeRef.Type(typeof(Any2D)), nameof(InteriorRef))); |
| 167 | + IL.Emit.Ret(); |
| 168 | + throw IL.Unreachable(); |
| 169 | + } |
| 170 | + |
| 171 | + /// <inheritdoc /> |
| 172 | + public override bool Equals(object? obj) => false; |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// Determines whether the given pointer points to the same location as this pointer. |
| 176 | + /// </summary> |
| 177 | + /// <param name="other">The other pointer.</param> |
| 178 | + /// <returns>Whether the pointers are equal.</returns> |
| 179 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 180 | + public bool Equals(Any2D other) => |
| 181 | + Unsafe.AreSame(ref Unsafe.AsRef(in InteriorRef), ref Unsafe.AsRef(in other.InteriorRef)); |
| 182 | + |
| 183 | + /// <inheritdoc /> |
| 184 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 185 | + public override int GetHashCode() => HashCode.Combine((nint)Unsafe.AsPointer(ref Unsafe.AsRef(in InteriorRef))); |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// Determines whether the given pointers point to the same location. |
| 189 | + /// </summary> |
| 190 | + /// <param name="left">The first pointer.</param> |
| 191 | + /// <param name="right">The second pointer.</param> |
| 192 | + /// <returns>Whether the pointers are equal.</returns> |
| 193 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 194 | + public static bool operator ==(Any2D left, Any2D right) => left.Equals(right); |
| 195 | + |
| 196 | + /// <summary> |
| 197 | + /// Determines whether the given pointers point do not to the same location. |
| 198 | + /// </summary> |
| 199 | + /// <param name="left">The first pointer.</param> |
| 200 | + /// <param name="right">The second pointer.</param> |
| 201 | + /// <returns>Whether the pointers are not equal.</returns> |
| 202 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 203 | + public static bool operator !=(Any2D left, Any2D right) => !left.Equals(right); |
| 204 | + |
| 205 | + /// <summary> |
| 206 | + /// Determines whether the given pointer is not null. |
| 207 | + /// </summary> |
| 208 | + /// <param name="left">The pointer.</param> |
| 209 | + /// <param name="_"><see cref="DSL.nullptr"/></param> |
| 210 | + /// <returns>Whether the pointer is not null.</returns> |
| 211 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 212 | + public static bool operator !=(Any2D left, NullPtr _) => !(left == _); |
| 213 | + |
| 214 | + /// <summary> |
| 215 | + /// Determines whether the given pointer is null. |
| 216 | + /// </summary> |
| 217 | + /// <param name="left">The pointer.</param> |
| 218 | + /// <param name="_"><see cref="DSL.nullptr"/></param> |
| 219 | + /// <returns>Whether the pointer is null.</returns> |
| 220 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 221 | + public static bool operator ==(Any2D left, NullPtr _) => |
| 222 | + Unsafe.IsNullRef(ref Unsafe.AsRef(in left.InteriorRef)); |
| 223 | +} |
0 commit comments