Example #1
0
        public static QuantumType[]? ToArray(IntPtr nativeData, int length)
        {
            if (nativeData == IntPtr.Zero)
            {
                return(null);
            }

            var result = new QuantumType[length];

            unsafe
            {
#if Q8
                var source = (byte *)nativeData;

                fixed(byte *destination = result)
#elif Q16
                var source = (ushort *)nativeData;

                fixed(ushort *destination = result)
#elif Q16HDRI
                var source = (float *)nativeData;
                fixed(float *destination = result)
#else
#error Not implemented!
#endif
                {
                    NativeMemory.Copy(source, destination, length);
                }
            }

            return(result);
        }
Example #2
0
    private void Resize()
    {
        int   newCapacity = data->capacity * 2;
        byte *newArr      = NativeMemory.Alloc(data->structSize * newCapacity, NativeMemory.NativeMemoryType.RawList);

        NativeMemory.Copy(newArr, data->arrayPtr, data->structSize * data->capacity);
        NativeMemory.Free(data->arrayPtr);
        data->arrayPtr = newArr;
        data->capacity = newCapacity;
    }
Example #3
0
        public void CopyEmptyBlockShouldNoOpTest()
        {
            int *source = stackalloc int[1] {
                42
            };
            int *destination = stackalloc int[1] {
                0
            };

            NativeMemory.Copy(source, destination, 0);

            Assert.Equal(0, destination[0]);
        }
Example #4
0
        public void CopyTest(int sourceSize, int destinationSize, int byteCount)
        {
            void *source      = NativeMemory.AllocZeroed((nuint)sourceSize);
            void *destination = NativeMemory.AllocZeroed((nuint)destinationSize);

            new Span <byte>(source, sourceSize).Fill(0b10101010);

            NativeMemory.Copy(source, destination, (nuint)byteCount);

            Equals(byteCount - 1, new Span <byte>(destination, destinationSize).LastIndexOf <byte>(0b10101010));

            NativeMemory.Free(source);
            NativeMemory.Free(destination);
        }
Example #5
0
        public void CopyToOverlappedMemoryTest(int size, int offset, int byteCount)
        {
            byte *source = (byte *)NativeMemory.AllocZeroed((nuint)size);

            var expectedBlock = new byte[byteCount];

            Random.Shared.NextBytes(expectedBlock);
            expectedBlock.CopyTo(new Span <byte>(source, byteCount));

            NativeMemory.Copy(source, source + offset, (nuint)byteCount);

            Assert.True(expectedBlock.AsSpan().SequenceEqual(new ReadOnlySpan <byte>(source + offset, byteCount)));

            NativeMemory.Free(source);
        }
Example #6
0
 public void CopyNullBlockShouldNoOpTest()
 {
     // This should not throw
     NativeMemory.Copy(null, null, 0);
 }
        public long Read(IntPtr data, UIntPtr count, IntPtr user_data)
        {
            var total = (long)count;

            if (total == 0)
            {
                return(0);
            }

            if (data == IntPtr.Zero)
            {
                return(0);
            }

            var destination = (byte *)data.ToPointer();

            long bytesRead = 0;

            while (total > 0)
            {
                long length = 0;
                ReadOnlySpan <byte> current;

                try
                {
                    current = _enumerator.Current.Span;

                    length = Math.Min(total, current.Length - _currentOffset);

                    fixed(byte *source = current)
                    {
                        NativeMemory.Copy(source + _currentOffset, destination, length);
                    }
                }
                catch
                {
                    return(bytesRead);
                }

                _currentOffset += length;
                _totalOffset   += length;
                bytesRead      += length;
                destination    += length;
                total          -= length;

                if (_currentOffset == current.Length)
                {
                    try
                    {
                        if (!_enumerator.MoveNext())
                        {
                            break;
                        }
                    }
                    catch
                    {
                        return(-1);
                    }

                    _currentOffset = 0;
                }
            }

            return(bytesRead);
        }