CommunityToolkit / CommunityToolkit/dotnet

SafeBufferMemoryManager

Open
#1,197 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

Exposing a MemoryManager that would make access to memory mapped files easier would help projects interacting with them.

### API breakdown

```csharp
public sealed class SafeBufferMemoryManager : MemoryManager
{
public SafeBufferMemoryManager(SafeBuffer buffer);
public override Span GetSpan();
public override MemoryHandle Pin(int elementIndex = 0);
public override void Unpin();
protected override void Dispose(bool disposing);
}

public static class MemoryMappedFileExtensions
{
public static SafeBufferMemoryManager CreateMemoryManager(this MemoryMappedFile file);
public static SafeBufferMemoryManager CreateMemoryManager(this MemoryMappedFile file, long offset, int size);
public static SafeBufferMemoryManager CreateMemoryManager(this MemoryMappedFile file, long offset, int size, MemoryMappedFileAccess access);
}
```

### Usage example

```csharp
using MemoryMappedFile file = ...;
using SafeBufferMemoryManager memoryManager = file.CreateMemoryManager();
SomeApi(memoryManager.Memory.Span);
```

### Breaking change?

No

### Alternatives

https://github.com/dotnet/runtime/issues/37227

### 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.