/// <summary> /// Writes a number of elements to a memory location from the provided buffer starting at the specified index. /// </summary> /// <typeparam name="T">The structure type</typeparam> /// <param name="destination">The destination memory location.</param> /// <param name="buffer">The source buffer.</param> /// <param name="index">The start index within <paramref name="buffer"/>.</param> /// <param name="count">The number of elements to write.</param> public static unsafe void WriteArray <T>(IntPtr destination, T[] buffer, int index, int count) { uint elementSize = (uint)SizeOf <T>(); if (buffer == null) { throw new ArgumentNullException("buffer"); } if (count < 0) { throw new ArgumentOutOfRangeException("count"); } if (index < 0) { throw new ArgumentOutOfRangeException("index"); } if (buffer.Length - index < count) { throw new ArgumentException("Invalid offset into array specified by index and count"); } void *ptr = destination.ToPointer(); byte *p = (byte *)FastStructure.GetPtr <T>(ref buffer[0]); #if NETCORE Buffer.MemoryCopy(p + (index * elementSize), ptr, elementSize * count, elementSize * count); #else UnsafeNativeMethods.CopyMemoryPtr(ptr, p + (index * elementSize), (uint)(elementSize * count)); #endif }
/// <summary> /// Writes the generic value type <typeparamref name="T"/> to the location specified by a pointer. This is achieved by emitting a <see cref="DynamicMethod"/> that copies the value from the referenced structure into the specified memory location. /// <para>There is no exact equivalent possible in C#, the closest possible (generates the same IL) is the following code:</para> /// <code> /// unsafe void WriteToPointer(ref SharedHeader dest, ref SharedHeader src) /// { /// dest = src; /// } /// </code> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pointer"></param> /// <param name="structure"></param> public static unsafe void StructureToPtr <T>(ref T structure, IntPtr pointer) { FastStructure <T> .StructureToPtr(ref structure, pointer); }
/// <summary> /// Loads the generic value type <typeparamref name="T"/> from a pointer. This is achieved by emitting a <see cref="DynamicMethod"/> that returns the value in the memory location as a <typeparamref name="T"/>. /// <para>The equivalent non-generic C# code:</para> /// <code> /// unsafe MyStruct ReadFromPointer(byte* pointer) /// { /// return *(MyStruct*)pointer; /// } /// </code> /// </summary> /// <typeparam name="T">Any value/structure type</typeparam> /// <param name="pointer">Unsafe pointer to memory to load the value from</param> /// <returns>The newly loaded value</returns> public static unsafe T PtrToStructure <T>(IntPtr pointer) { return(FastStructure <T> .PtrToStructure(pointer)); }
/// <summary> /// Retrieve a pointer to the passed generic structure type. This is achieved by emitting a <see cref="DynamicMethod"/> to retrieve a pointer to the structure. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="structure"></param> /// <returns>A pointer to the provided structure in memory.</returns> /// <see cref="FastStructure{T}.GetPtr"/> public static unsafe void *GetPtr <T>(ref T structure) { return(FastStructure <T> .GetPtr(ref structure)); }