CommunityToolkit / CommunityToolkit/dotnet

PointerMemoryManager

Open
#1,198 0 comments 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

`PointerMemoryManager` is a common concept among projects, allowing to pass native memory to async apis that take Memory.

### API breakdown

```csharp
public sealed class PointerMemoryManager : MemoryManager
{
public PointerMemoryManager(T* pointer, int size);
public override Span GetSpan();
public override MemoryHandle Pin(int elementIndex = 0);
public override void Unpin();
protected override void Dispose(bool disposing);
}
```

### Usage example

```csharp
byte* ptr = SomeApi(out int length);
using PointerMemoryManager manager = new(ptr, length);
await SomeAsyncApi(manager.Memory);
```

### Breaking change?

No

### Alternatives

Define it in your project.

### Additional context

_No response_

### Help us help you

No, just wanted to propose this

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.