/// <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) where T : struct { 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> /// Reads a number of elements from a memory location into the provided buffer starting at the specified index. /// </summary> /// <typeparam name="T">The structure type</typeparam> /// <param name="buffer">The destination buffer.</param> /// <param name="source">The source memory location.</param> /// <param name="index">The start index within <paramref name="buffer"/>.</param> /// <param name="count">The number of elements to read.</param> public static unsafe void ReadArray <T>(T[] buffer, IntPtr source, int index, int count) where T : struct { 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 = source.ToPointer(); byte *p = (byte *)FastStructure.GetPtr <T>(ref buffer[0]); UnsafeNativeMethods.CopyMemoryPtr(p + (index * elementSize), ptr, (uint)(elementSize * count)); }
/// <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) where T : struct { return(FastStructure <T> .GetPtr(ref structure)); }