diff --git a/Src/IronPython.Modules/mmap.cs b/Src/IronPython.Modules/mmap.cs index 232f173a6..f711790f7 100644 --- a/Src/IronPython.Modules/mmap.cs +++ b/Src/IronPython.Modules/mmap.cs @@ -1228,10 +1228,11 @@ public IPythonBuffer GetBuffer(BufferFlags flags = BufferFlags.Simple) { return new MmapBuffer(this, flags); } - private sealed class MmapBuffer : IPythonBuffer { + private sealed unsafe class MmapBuffer : IPythonBuffer { private readonly MmapDefault _mmap; private readonly BufferFlags _flags; private SafeMemoryMappedViewHandle? _handle; + private byte* _pointer = null; public MmapBuffer(MmapDefault mmap, BufferFlags flags) { mmap.EnsureOpen(); @@ -1265,30 +1266,27 @@ public MmapBuffer(MmapDefault mmap, BufferFlags flags) { public unsafe ReadOnlySpan AsReadOnlySpan() { if (_handle is null) throw new ObjectDisposedException(nameof(MmapBuffer)); - byte* pointer = null; - _handle.AcquirePointer(ref pointer); - return new ReadOnlySpan(pointer, ItemCount); + if (_pointer is null) _handle.AcquirePointer(ref _pointer); + return new ReadOnlySpan(_pointer, ItemCount); } public unsafe Span AsSpan() { if (_handle is null) throw new ObjectDisposedException(nameof(MmapBuffer)); if (IsReadOnly) throw new InvalidOperationException("object is not writable"); - byte* pointer = null; - _handle.AcquirePointer(ref pointer); - return new Span(pointer, ItemCount); + if (_pointer is null) _handle.AcquirePointer(ref _pointer); + return new Span(_pointer, ItemCount); } public unsafe MemoryHandle Pin() { if (_handle is null) throw new ObjectDisposedException(nameof(MmapBuffer)); - byte* pointer = null; - _handle.AcquirePointer(ref pointer); - return new MemoryHandle(pointer); + if (_pointer is null) _handle.AcquirePointer(ref _pointer); + return new MemoryHandle(_pointer); } public void Dispose() { var handle = Interlocked.Exchange(ref _handle, null); if (handle is null) return; - handle.Dispose(); + if (_pointer is not null) handle.ReleasePointer(); _mmap.CloseWorker(); }