/// <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]); UnsafeNativeMethods.CopyMemoryPtr(ptr, p + (index * elementSize), (uint)(elementSize * count)); }
/// <summary> /// Writes the structure array buffer to the next available node for writing /// </summary> /// <param name="source">Reference to the buffer to write</param> /// <param name="startIndex">The index within the buffer to start writing from</param> /// <param name="timeout">The maximum number of milliseconds to wait for a node to become available for writing (default 1000ms)</param> /// <returns>The number of elements written</returns> /// <remarks>The maximum number of elements that can be written is the minimum of the length of <paramref name="source"/> subtracted by <paramref name="startIndex"/> and <see cref="NodeBufferSize"/> divided by <code>FastStructure.SizeOf>T<()</code>.</remarks> public virtual int Write <T>(T[] source, int startIndex = 0, int timeout = 1000) where T : struct { // Grab a node for writing Node *node = GetNodeForWriting(timeout); if (node == null) { return(0); } // Write the data using the FastStructure class (much faster than the MemoryMappedViewAccessor WriteArray<T> method) int count = Math.Min(source.Length - startIndex, NodeBufferSize / FastStructure.SizeOf <T>()); base.WriteArray <T>(source, startIndex, count, node->Offset); node->AmountWritten = count * FastStructure.SizeOf <T>(); // Writing is complete, make node readable PostNode(node); return(count); }
/// <summary> /// Reads the next available node for reading into the specified structure array /// </summary> /// <typeparam name="T">The structure type to be read</typeparam> /// <param name="destination">Reference to the buffer</param> /// <param name="startIndex">The index within the destination to start writing to.</param> /// <param name="timeout">The maximum number of milliseconds to wait for a node to become available for reading (default 1000ms)</param> /// <returns>The number of elements read into destination</returns> /// <remarks>The maximum number of elements that can be read is the minimum of the length of <paramref name="destination"/> subtracted by <paramref name="startIndex"/> and <see cref="Node.AmountWritten"/> divided by <code>FastStructure.SizeOf>T<()</code>.</remarks> public virtual int Read <T>(T[] destination, int startIndex = 0, int timeout = 1000) where T : struct { Node *node = GetNodeForReading(timeout); if (node == null) { return(0); } // Copy the data using the FastStructure class (much faster than the MemoryMappedViewAccessor ReadArray<T> method) int count = Math.Min(destination.Length - startIndex, node->AmountWritten / FastStructure.SizeOf <T>()); base.ReadArray <T>(destination, startIndex, count, node->Offset); // Return the node for further writing ReturnNode(node); return(count); }
/// <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="destination">The destination buffer.</param> /// <param name="index">The start index within <paramref name="destination"/>.</param> /// <param name="count">The number of elements to read.</param> /// <param name="bufferPosition">The source offset within the buffer region of the shared memory.</param> protected virtual void ReadArray <T>(T[] destination, int index, int count, long bufferPosition) where T : struct { FastStructure.ReadArray <T>(destination, (IntPtr)(BufferStartPtr + bufferPosition), index, count); }
/// <summary> /// Reads an array of <typeparamref name="T"/> from the buffer. /// </summary> /// <typeparam name="T">A structure type</typeparam> /// <param name="destination">Array that will contain the values read from the buffer. The length of this array controls the number of elements to read.</param> /// <param name="bufferPosition">The offset within the buffer region of the shared memory to read from.</param> protected virtual void Read <T>(T[] destination, long bufferPosition = 0) where T : struct { FastStructure.ReadArray <T>(destination, (IntPtr)(BufferStartPtr + bufferPosition), 0, destination.Length); }
/// <summary> /// Writes an array of <typeparamref name="T"/> into the buffer /// </summary> /// <typeparam name="T">A structure type</typeparam> /// <param name="source">The source data to be written to the buffer</param> /// <param name="index">The start index within <paramref name="source"/>.</param> /// <param name="count">The number of elements to write.</param> /// <param name="bufferPosition">The offset within the buffer region of the shared memory to write to.</param> protected virtual void WriteArray <T>(T[] source, int index, int count, long bufferPosition = 0) where T : struct { FastStructure.WriteArray <T>((IntPtr)(BufferStartPtr + bufferPosition), source, index, count); }
/// <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) where T : struct { 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) where T : struct { 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) where T : struct { return(FastStructure <T> .GetPtr(ref structure)); }