Skip to content

[API Proposal]: RuntimeHelpers.AllocateTypeAssociatedMemory overload with alignment #118897

@MichalPetryka

Description

@MichalPetryka

Background and motivation

RuntimeHelpers.AllocateTypeAssociatedMemory is an API that's very useful for allocating private, permanent buffers for lookup tables and such.

It doesn't however provide a way to specify the alignment which makes the users that require it for correctness or performance have to manually ensure that via for example overallocating today.
As such, I propose that an overload that allows it to be specified would be added.

API Proposal

namespace System.Runtime.CompilerServices;

public static class RuntimeHelpers
{
    public static IntPtr AllocateTypeAssociatedMemory(Type type, int size, int alignment);
}

API Usage

public static class SimdAlgorithm
{
    public static readonly byte* SimdLookupTable = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(SimdAlgorithm), 4096, 64);
}

Alternative Designs

The API could return void* instead and take parameters as nuint, I decided to match the existing overload for now though.

Risks

Matching IntPtr return might be problematic with the new unsafe guidelines.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions