Table of Contents

Struct MemoryBlock

Namespace
Misaki.HighPerformance.LowLevel.Buffer
Assembly
Misaki.HighPerformance.LowLevel.dll
public struct MemoryBlock : IDisposable
Implements
Inherited Members

Constructors

MemoryBlock()

public MemoryBlock()

MemoryBlock(nuint, nuint, AllocationHandle, AllocationOption)

public MemoryBlock(nuint size, nuint alignment, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)

Parameters

size nuint
alignment nuint
handle AllocationHandle
allocationOption AllocationOption

MemoryBlock(void*, nuint)

Initializes an UnsafeArray with a pointer to a buffer and a count of elements. This does not copy the data.

public MemoryBlock(void* buffer, nuint size)

Parameters

buffer void*

A pointer to the memory location that holds the elements of the array.

size nuint

The total size of the data.

Remarks

When using this constructor, the user is responsible for managing the memory pointed to by the buffer. Disposing of the UnsafeArray does not free the memory and only release the reference. The memory should be freed manually when no longer needed. Use MemoryBlock(nuint, nuint, AllocationHandle, AllocationOption) constructor and MemCpy(void*, void*, nuint) if you are not sure what you are doing.

Properties

Alignment

public readonly nuint Alignment { get; }

Property Value

nuint

IsCreated

public readonly bool IsCreated { get; }

Property Value

bool

Size

public readonly nuint Size { get; }

Property Value

nuint

Methods

AsSpan<T>()

Converts into a Span for efficient memory access.

public readonly Span<T> AsSpan<T>() where T : unmanaged

Returns

Span<T>

A Span<T> that provides a view over the elements of the UnsafeCollection.

Type Parameters

T

AsSpan<T>(int, int)

Creates a span over a contiguous region of elements in the specified unsafe collection, starting at the given index and covering the specified number of elements.

public readonly Span<T> AsSpan<T>(int start, int length) where T : unmanaged

Parameters

start int

The zero-based index of the first element in the collection to include in the span. Must be greater than or equal to zero and less than the number of elements in the collection.

length int

The number of elements to include in the span. Must be greater than or equal to zero and the range defined by start and length must not exceed the bounds of the collection.

Returns

Span<T>

A Span<T> representing the specified region of the collection.

Type Parameters

T

Clear()

Clears the contents of the memory block by setting all bytes to zero.

public readonly void Clear()

CopyFrom<T>(ReadOnlySpan<T>)

Copies elements from a typed source span to an untyped destination collection.

public readonly void CopyFrom<T>(ReadOnlySpan<T> source) where T : unmanaged

Parameters

source ReadOnlySpan<T>

The typed span containing the elements to be copied.

Type Parameters

T

Specifies the type of elements in the source span, which must be unmanaged.

Exceptions

ArgumentException

Thrown when the destination collection is smaller than the source span data size.

CopyFrom<T>(ReadOnlySpan<T>, uint, uint, uint)

Copies a range of elements from a typed source span to an untyped destination collection at a specified byte offset.

public readonly void CopyFrom<T>(ReadOnlySpan<T> source, uint sourceIndex, uint destinationOffset, uint length) where T : unmanaged

Parameters

source ReadOnlySpan<T>

The typed span containing the elements to be copied.

sourceIndex uint

The starting element index in the source span from which to begin copying.

destinationOffset uint

The byte offset in the destination collection where the data will be placed.

length uint

The number of elements to copy from the source span.

Type Parameters

T

Specifies the type of elements in the source span, which must be unmanaged.

Exceptions

ArgumentException

Thrown when the specified range exceeds the bounds of the source span or destination collection.

CopyTo<T>(Span<T>, uint, uint, uint)

Copies a range of bytes from an untyped source collection to a destination span, interpreting the bytes as elements of type T.

public readonly void CopyTo<T>(Span<T> destination, uint sourceOffset, uint destinationIndex, uint length) where T : unmanaged

Parameters

destination Span<T>

The typed span where the elements will be placed.

sourceOffset uint

The byte offset in the source collection from which to start copying.

destinationIndex uint

The element index in the destination span where copying will begin.

length uint

The number of elements of type T to copy.

Type Parameters

T

Specifies the type of elements in the destination span, which must be unmanaged.

Exceptions

ArgumentException

Thrown when the specified range exceeds the bounds of the source collection or destination span.

CopyTo<C, T>(Span<T>)

Copies elements from an untyped source collection to a destination span of a specific type.

public readonly void CopyTo<C, T>(Span<T> destination) where T : unmanaged

Parameters

destination Span<T>

The typed span where the data will be copied to.

Type Parameters

C
T

Specifies the type of elements in the destination span, which must be unmanaged.

Exceptions

ArgumentException

Thrown when the source collection size exceeds the destination span capacity.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetElementAt<T>(nuint)

public readonly ref T GetElementAt<T>(nuint index) where T : unmanaged

Parameters

index nuint

Returns

T

Type Parameters

T

GetUnsafePtr()

Gets an pointer to the memory block, allowing for direct memory access and manipulation.

public readonly void* GetUnsafePtr()

Returns

void*

A pointer to the memory block.

Resize(uint, AllocationOption)

Changes the size of a memory block to the specified value.

public void Resize(uint newSize, AllocationOption option = AllocationOption.None)

Parameters

newSize uint

Specifies the new size to which the collection should be adjusted.

option AllocationOption

Specifies allocation options that may affect how memory is managed during the resize operation.