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
sizenuintalignmentnuinthandleAllocationHandleallocationOptionAllocationOption
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
buffervoid*A pointer to the memory location that holds the elements of the array.
sizenuintThe 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
IsCreated
public readonly bool IsCreated { get; }
Property Value
Size
public readonly nuint Size { get; }
Property Value
Methods
AsSpan<T>()
Converts into a Span for efficient memory access.
public readonly Span<T> AsSpan<T>() where T : unmanaged
Returns
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
startintThe 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.
lengthintThe number of elements to include in the span. Must be greater than or equal to zero and the range defined by
startandlengthmust not exceed the bounds of the collection.
Returns
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
sourceReadOnlySpan<T>The typed span containing the elements to be copied.
Type Parameters
TSpecifies 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
sourceReadOnlySpan<T>The typed span containing the elements to be copied.
sourceIndexuintThe starting element index in the source span from which to begin copying.
destinationOffsetuintThe byte offset in the destination collection where the data will be placed.
lengthuintThe number of elements to copy from the source span.
Type Parameters
TSpecifies 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
destinationSpan<T>The typed span where the elements will be placed.
sourceOffsetuintThe byte offset in the source collection from which to start copying.
destinationIndexuintThe element index in the destination span where copying will begin.
lengthuintThe number of elements of type T to copy.
Type Parameters
TSpecifies 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
destinationSpan<T>The typed span where the data will be copied to.
Type Parameters
CTSpecifies 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
indexnuint
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
newSizeuintSpecifies the new size to which the collection should be adjusted.
optionAllocationOptionSpecifies allocation options that may affect how memory is managed during the resize operation.