Last active
February 4, 2020 13:08
-
-
Save antonfirsov/3b1c45742eb9ae835c333d6466e61ef6 to your computer and use it in GitHub Desktop.
Pin heap span & Forward as memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| unsafe class TemporaryHeapMemoryReference_MemoryManager : MemoryManager<byte> | |
| { | |
| private readonly unsafe byte* _ptr; | |
| private readonly int _length; | |
| public TemporaryHeapMemoryReference_MemoryManager(byte* ptr, int length) | |
| { | |
| _ptr = ptr; | |
| _length = length; | |
| } | |
| protected override void Dispose(bool disposing) | |
| { | |
| } | |
| public override Span<byte> GetSpan() | |
| { | |
| return new Span<byte>(_ptr, _length); | |
| } | |
| public override MemoryHandle Pin(int elementIndex = 0) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public override void Unpin() | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| } | |
| static unsafe void EatSpan(ReadOnlySpan<byte> span) | |
| { | |
| fixed (byte* p = &MemoryMarshal.GetReference(span)) | |
| { | |
| TemporaryHeapMemoryReference_MemoryManager mgr = new TemporaryHeapMemoryReference_MemoryManager(p, span.Length); | |
| EatMemory(mgr.Memory); | |
| } | |
| } | |
| static void EatMemory(ReadOnlyMemory<byte> memory) | |
| { | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment