Skip to content

Commit

Permalink
Release pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
slozier committed Jan 11, 2025
1 parent e068943 commit 2260cdf
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions Src/IronPython.Modules/mmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1265,30 +1266,27 @@ public MmapBuffer(MmapDefault mmap, BufferFlags flags) {

public unsafe ReadOnlySpan<byte> AsReadOnlySpan() {
if (_handle is null) throw new ObjectDisposedException(nameof(MmapBuffer));
byte* pointer = null;
_handle.AcquirePointer(ref pointer);
return new ReadOnlySpan<byte>(pointer, ItemCount);
if (_pointer is null) _handle.AcquirePointer(ref _pointer);
return new ReadOnlySpan<byte>(_pointer, ItemCount);
}

public unsafe Span<byte> 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<byte>(pointer, ItemCount);
if (_pointer is null) _handle.AcquirePointer(ref _pointer);
return new Span<byte>(_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();
}

Expand Down

0 comments on commit 2260cdf

Please sign in to comment.