Ejemplo n.º 1
0
        /// <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(nameof(buffer));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(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]);

            Buffer.MemoryCopy(ptr, p + (index * elementSize), elementSize * count, elementSize * count);
        }
Ejemplo n.º 2
0
 /// <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));
 }