PinnedMemory 2.0.0

dotnet add package PinnedMemory --version 2.0.0                
NuGet\Install-Package PinnedMemory -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PinnedMemory" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PinnedMemory --version 2.0.0                
#r "nuget: PinnedMemory, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PinnedMemory as a Cake Addin
#addin nuget:?package=PinnedMemory&version=2.0.0

// Install PinnedMemory as a Cake Tool
#tool nuget:?package=PinnedMemory&version=2.0.0                

PinnedMemory

License: MIT nuget

PinnedMemory is a cross-platform, high-performance solution for creating, accessing, and managing pinned and locked memory for Windows, macOS, and Linux operating systems in .NET Core. It provides automatic memory pinning and optional locking for sensitive operations, helping you prevent garbage collection relocations and offering enhanced performance in low-level memory manipulation scenarios.

Install

From a command prompt:

dotnet add package PinnedMemory
Install-Package PinnedMemory

You can also search for the package via your NuGet UI or website:

NuGet: PinnedMemory

Features

  • Cross-platform memory management for Windows, Linux, and macOS.
  • Supports several primitive types (e.g., byte, int, float, etc.).
  • Offers zeroing, locking, and unlocking of memory for security and performance.
  • Prevents garbage collection from relocating memory by pinning the array in memory.
  • Efficient cloning and pooling of arrays using ArrayPool<T>.
  • Optimized for performance with aggressive inlining and reduced allocations.

Usage

Basic Example

using (var pin = new PinnedMemory<byte>(new byte[3]))
{
    pin[0] = 65;
    pin[1] = 61;
    pin[2] = 77;
}

Writing to Memory

using (var pin = new PinnedMemory<byte>(new byte[3]))
{
    pin.Write(0, 65);
    pin.Write(1, 61);
    pin.Write(2, 77);
}

Reading from Memory

using (var pin = new PinnedMemory<byte>(new byte[] {65, 61, 77}, false))
{
    var byte1 = pin[0];
    var byte2 = pin[1];
    var byte3 = pin[2];
}
using (var pin = new PinnedMemory<byte>(new byte[] {65, 61, 77}, false))
{
    var byte1 = pin.Read(0);
    var byte2 = pin.Read(1);
    var byte3 = pin.Read(2);
}

Cloning Pinned Memory

using (var pin = new PinnedMemory<byte>(new byte[] {65, 61, 77}, false))
{
    var clone = pin.Clone();
    var clonedArray = clone.ToArray();
}

Zeroing, Locking, and Unlocking Memory

using (var pin = new PinnedMemory<byte>(new byte[3], zero: true, locked: true))
{
    // Memory is automatically zeroed and locked.
    // Perform secure operations.
}

Supported Types

The following primitive types are supported in PinnedMemory:

  • sbyte
  • byte
  • char
  • short
  • ushort
  • int
  • uint
  • long
  • ulong
  • float
  • double
  • decimal
  • bool

API Reference

Constructor

PinnedMemory(T[] value, bool zero = true, bool locked = true, SystemType type = SystemType.Unknown)
  • value: The array to pin in memory.
  • zero: Optional. If true, the memory will be zeroed out after allocation.
  • locked: Optional. If true, the memory will be locked in RAM to prevent paging.
  • type: Optional. Specifies the OS platform (SystemType.Windows, SystemType.Linux, SystemType.Osx). If Unknown, it is detected automatically.

Properties

  • T this[int i]: Indexer for accessing elements in the pinned array.
  • int Length: Returns the length of the pinned array.

Methods

  • T[] Read(): Returns the entire pinned array.
  • T Read(int index): Reads the value at the specified index.
  • void Write(int index, T value): Writes the value at the specified index.
  • PinnedMemory<T> Clone(): Clones the pinned memory array and returns a new PinnedMemory<T> object.
  • T[] ToArray(): Returns a copy of the pinned memory as an array.
  • void Dispose(): Frees the pinned memory, zeroes it out, and unlocks it if locked.

License

This library is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on PinnedMemory:

Package Downloads
Blake2b.NetCore

Implementation of the cryptographic hash function BLAKE2b. Optimized for PinnedMemory, and 64-bit platform.

SecureRandom.NetCore

Implementation of a cryptographic pseudorandom number generator (CPRNG) using Blake2b. Optimized for PinnedMemory.

ChaCha20.NetCore

Implementation of chacha20 cipher, designed by D. J. Bernstein. Optimized for PinnedMemory

Argon2.NetCore

Implementation of Argon2 key derivation function designed by Alex Biryukov, Daniel Dinu, and Dmitry Khovratovich. Optimized for PinnedMemory.

AeadChaCha20Poly1305.NetCore

Implementation of AEAD_CHACHA20_POLY1305 an authenticated encryption with additional data algorithm using ChaCha20, and Poly1305 designed by D. J. Bernstein. Optimized for PinnedMemory, and .NET core.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 106 10/14/2024
1.0.1 9,256 7/11/2020
1.0.0 436 7/11/2020