private bool TryCreateMemoryMapAccessor(long start, int size, out IDisposable accessor)
        {
            if (_lazyMemoryMap == null)
            {
                // leave the underlying stream open. It will be closed by the Dispose method.
                IDisposable newMemoryMap;

                // CreateMemoryMap might modify the stream (calls FileStream.Flush)
                lock (_streamGuard)
                {
                    newMemoryMap = MemoryMapLightUp.CreateMemoryMap(_stream);
                }

                if (newMemoryMap == null)
                {
                    accessor = null;
                    return(false);
                }

                if (Interlocked.CompareExchange(ref _lazyMemoryMap, newMemoryMap, null) != null)
                {
                    newMemoryMap.Dispose();
                }
            }

            accessor = MemoryMapLightUp.CreateViewAccessor(_lazyMemoryMap, start, size);
            return(accessor != null);
        }
Example #2
0
        private unsafe bool TryCreateMemoryMappedFileBlock(long start, int size, out MemoryMappedFileBlock block)
        {
            if (_lazyMemoryMap == null)
            {
                // leave the underlying stream open. It will be closed by the Dispose method.
                IDisposable newMemoryMap;

                // CreateMemoryMap might modify the stream (calls FileStream.Flush)
                lock (_streamGuard)
                {
                    newMemoryMap = MemoryMapLightUp.CreateMemoryMap(_stream);
                }

                if (newMemoryMap == null)
                {
                    block = null;
                    return(false);
                }

                if (Interlocked.CompareExchange(ref _lazyMemoryMap, newMemoryMap, null) != null)
                {
                    newMemoryMap.Dispose();
                }
            }

            IDisposable accessor = MemoryMapLightUp.CreateViewAccessor(_lazyMemoryMap, start, size);

            if (accessor == null)
            {
                block = null;
                return(false);
            }

            SafeBuffer safeBuffer;
            byte *     pointer = MemoryMapLightUp.AcquirePointer(accessor, out safeBuffer);

            if (pointer == null)
            {
                block = null;
                return(false);
            }

            block = new MemoryMappedFileBlock(accessor, safeBuffer, pointer, size);
            return(true);
        }
        /// <exception cref="IOException">IO error while mapping memory or not enough memory to create the mapping.</exception>
        private unsafe bool TryCreateMemoryMappedFileBlock(long start, int size, [NotNullWhen(true)] out MemoryMappedFileBlock?block)
        {
            if (_lazyMemoryMap == null)
            {
                // leave the underlying stream open. It will be closed by the Dispose method.
                IDisposable newMemoryMap;

                // CreateMemoryMap might modify the stream (calls FileStream.Flush)
                lock (_streamGuard)
                {
                    newMemoryMap = MemoryMapLightUp.CreateMemoryMap(_stream);
                }

                if (newMemoryMap == null)
                {
                    block = null;
                    return(false);
                }

                if (Interlocked.CompareExchange(ref _lazyMemoryMap, newMemoryMap, null) != null)
                {
                    newMemoryMap.Dispose();
                }
            }

            IDisposable accessor = MemoryMapLightUp.CreateViewAccessor(_lazyMemoryMap, start, size);

            if (accessor == null)
            {
                block = null;
                return(false);
            }

            if (!MemoryMapLightUp.TryGetSafeBufferAndPointerOffset(accessor, out var safeBuffer, out long offset))
            {
                block = null;
                return(false);
            }

            block = new MemoryMappedFileBlock(accessor, safeBuffer, offset, size);
            return(true);
        }
        private bool TryCreateMemoryMapAccessor(long start, int size, out IDisposable accessor)
        {
            if (lazyMemoryMap == null)
            {
                // leave the underlying stream open. It will be closed by the Dispose method.
                var newMemoryMap = MemoryMapLightUp.CreateMemoryMap(this.stream);
                if (newMemoryMap == null)
                {
                    accessor = null;
                    return(false);
                }

                if (Interlocked.CompareExchange(ref lazyMemoryMap, newMemoryMap, null) != null)
                {
                    newMemoryMap.Dispose();
                }
            }

            accessor = MemoryMapLightUp.CreateViewAccessor(lazyMemoryMap, start, size);
            return(accessor != null);
        }
 internal unsafe MemoryMappedFileBlock(IDisposable accessor, int size)
 {
     this.accessor = accessor;
     this.pointer  = MemoryMapLightUp.AcquirePointer(accessor, out safeBuffer);
     this.size     = size;
 }
Example #6
0
 internal unsafe MemoryMappedFileBlock(IDisposable accessor, int size)
 {
     _accessor = accessor;
     _pointer  = MemoryMapLightUp.AcquirePointer(accessor, out _safeBuffer);
     _size     = size;
 }