1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System
.Runtime
.InteropServices
;
7 namespace System
.Buffers
10 /// A handle for the memory.
12 public unsafe struct MemoryHandle
: IDisposable
14 private void* _pointer
;
15 private GCHandle _handle
;
16 private IPinnable
? _pinnable
;
19 /// Creates a new memory handle for the memory.
21 /// <param name="pointer">pointer to memory</param>
22 /// <param name="pinnable">reference to manually managed object, or default if there is no memory manager</param>
23 /// <param name="handle">handle used to pin array buffers</param>
25 public MemoryHandle(void* pointer
, GCHandle handle
= default, IPinnable
? pinnable
= default)
33 /// Returns the pointer to memory, where the memory is assumed to be pinned and hence the address won't change.
36 public void* Pointer
=> _pointer
;
39 /// Frees the pinned handle and releases IPinnable.
43 if (_handle
.IsAllocated
)
48 if (_pinnable
!= null)