Skip to content

Commit 2260cdf

Browse files
committed
Release pointer
1 parent e068943 commit 2260cdf

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

Src/IronPython.Modules/mmap.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,10 +1228,11 @@ public IPythonBuffer GetBuffer(BufferFlags flags = BufferFlags.Simple) {
12281228
return new MmapBuffer(this, flags);
12291229
}
12301230

1231-
private sealed class MmapBuffer : IPythonBuffer {
1231+
private sealed unsafe class MmapBuffer : IPythonBuffer {
12321232
private readonly MmapDefault _mmap;
12331233
private readonly BufferFlags _flags;
12341234
private SafeMemoryMappedViewHandle? _handle;
1235+
private byte* _pointer = null;
12351236

12361237
public MmapBuffer(MmapDefault mmap, BufferFlags flags) {
12371238
mmap.EnsureOpen();
@@ -1265,30 +1266,27 @@ public MmapBuffer(MmapDefault mmap, BufferFlags flags) {
12651266

12661267
public unsafe ReadOnlySpan<byte> AsReadOnlySpan() {
12671268
if (_handle is null) throw new ObjectDisposedException(nameof(MmapBuffer));
1268-
byte* pointer = null;
1269-
_handle.AcquirePointer(ref pointer);
1270-
return new ReadOnlySpan<byte>(pointer, ItemCount);
1269+
if (_pointer is null) _handle.AcquirePointer(ref _pointer);
1270+
return new ReadOnlySpan<byte>(_pointer, ItemCount);
12711271
}
12721272

12731273
public unsafe Span<byte> AsSpan() {
12741274
if (_handle is null) throw new ObjectDisposedException(nameof(MmapBuffer));
12751275
if (IsReadOnly) throw new InvalidOperationException("object is not writable");
1276-
byte* pointer = null;
1277-
_handle.AcquirePointer(ref pointer);
1278-
return new Span<byte>(pointer, ItemCount);
1276+
if (_pointer is null) _handle.AcquirePointer(ref _pointer);
1277+
return new Span<byte>(_pointer, ItemCount);
12791278
}
12801279

12811280
public unsafe MemoryHandle Pin() {
12821281
if (_handle is null) throw new ObjectDisposedException(nameof(MmapBuffer));
1283-
byte* pointer = null;
1284-
_handle.AcquirePointer(ref pointer);
1285-
return new MemoryHandle(pointer);
1282+
if (_pointer is null) _handle.AcquirePointer(ref _pointer);
1283+
return new MemoryHandle(_pointer);
12861284
}
12871285

12881286
public void Dispose() {
12891287
var handle = Interlocked.Exchange(ref _handle, null);
12901288
if (handle is null) return;
1291-
handle.Dispose();
1289+
if (_pointer is not null) handle.ReleasePointer();
12921290
_mmap.CloseWorker();
12931291
}
12941292

0 commit comments

Comments
 (0)