Beispiel #1
0
        public override unsafe int Read(byte[] buffer, int offset, int count)
        {
            fixed(void *pBuffer = buffer)
            {
                var bufferPointer = new IntPtr(pBuffer);
                var sourcePointer = mMemory.GetPointer();

                var length = Math.Min(count, mMemory.Size - mCurrentOffset);

                UnmanagedBlock.Copy(sourcePointer, bufferPointer, (int)mCurrentOffset, offset, length);
                mCurrentOffset += length;

                return((int)length);
            }
        }
Beispiel #2
0
        public MemoryWindow([NotNull] IMemoryBlock memoryBlock, int offset, long size)
        {
            if (memoryBlock == null)
            {
                throw new ArgumentNullException(nameof(memoryBlock));
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (size > memoryBlock.Size - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(size), "The window size is larger than the size of the target block.");
            }

            Pointer = memoryBlock.GetPointer() + offset;
            Size    = size;

            mReadable = memoryBlock is IReadableMemory;
            mWritable = memoryBlock is IWritableMemory;

            if (!mReadable && !mWritable)
            {
                // neither readable nor writable!? must be some unknown memory restriction...
                mReadable = mWritable = true;
            }
        }
Beispiel #3
0
        public ReadOnlyMemoryWindow([NotNull] IMemoryBlock memoryBlock)
        {
            if (memoryBlock == null)
            {
                throw new ArgumentNullException(nameof(memoryBlock));
            }

            Pointer = memoryBlock.GetPointer();
            Size    = memoryBlock.Size;
        }
Beispiel #4
0
        public MemoryWindow([NotNull] IMemoryBlock memoryBlock)
        {
            if (memoryBlock == null)
            {
                throw new ArgumentNullException(nameof(memoryBlock));
            }

            Pointer = memoryBlock.GetPointer();
            Size    = memoryBlock.Size;

            mReadable = memoryBlock is IReadableMemory;
            mWritable = memoryBlock is IWritableMemory;
        }
Beispiel #5
0
        public T this[int index]
        {
            get
            {
                if (!mReadable)
                {
                    throw new AccessViolationException("The underlying memory block does not allow reading.");
                }

                if (index < 0 || index >= mLength)
                {
                    throw new IndexOutOfRangeException();
                }

                var offset  = index * mSizeOfT;
                var address = mMemory.GetPointer() + offset;

                return(Marshal.PtrToStructure <T>(address));
            }
            set
            {
                if (!mWritable)
                {
                    throw new AccessViolationException("The underlying memory block does not allow writing.");
                }

                if (index < 0 || index >= mLength)
                {
                    throw new IndexOutOfRangeException();
                }

                var offset  = index * mSizeOfT;
                var address = mMemory.GetPointer() + offset;

                Marshal.StructureToPtr(value, address, true);
            }
        }