/// <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. MemoryMappedFile newMemoryMap; // CreateMemoryMap might modify the stream (calls FileStream.Flush) lock (_streamGuard) { try { newMemoryMap = MemoryMappedFile.CreateFromFile( fileStream: (FileStream)_stream, mapName: null, capacity: 0, access: MemoryMappedFileAccess.Read, inheritability: HandleInheritability.None, leaveOpen: true); } catch (UnauthorizedAccessException e) { throw new IOException(e.Message, e); } } if (newMemoryMap == null) { block = null; return(false); } if (Interlocked.CompareExchange(ref _lazyMemoryMap, newMemoryMap, null) != null) { newMemoryMap.Dispose(); } } MemoryMappedViewAccessor accessor; lock (_streamGuard) { accessor = _lazyMemoryMap.CreateViewAccessor(start, size, MemoryMappedFileAccess.Read); } if (accessor == null) { block = null; return(false); } block = new MemoryMappedFileBlock(accessor, accessor.SafeMemoryMappedViewHandle, accessor.PointerOffset, size); return(true); }
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 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; }