Class NativeTensorArrayFromManagedArray
Represents an area of managed memory that's exposed as if it's native memory.
Implements
Inherited Members
Namespace: Unity.InferenceEngine
Assembly: Unity.InferenceEngine.dll
Syntax
[MovedFrom("Unity.Sentis")]
public class NativeTensorArrayFromManagedArray : NativeTensorArray, IDisposable
Remarks
Pins the managed array so it can be used where a NativeTensorArray is expected, avoiding copies. The managed array must remain allocated for the lifetime of this instance. Dispose() does not free the managed array; it only releases the pin.
Additional resources
Examples
Wrap a managed float array for use with tensor operations without copying.
var data = new float[] { 1.0f, 2.0f, 3.0f, 4.0f };
using var tensorArray = new NativeTensorArrayFromManagedArray(data, 0, sizeof(float), data.Length);
float value = tensorArray.Get<float>(2);
// value is 3.0f; data is pinned, not copied
Constructors
NativeTensorArrayFromManagedArray(Array, int, int, int)
Creates a new wrapper that pins a managed array and exposes a view of it as a native tensor array.
Declaration
public NativeTensorArrayFromManagedArray(Array srcData, int srcElementOffset, int srcElementSize, int numDestElement)
Parameters
| Type | Name | Description |
|---|---|---|
| Array | srcData | The backing data as a managed array. |
| int | srcElementOffset | The element offset into |
| int | srcElementSize | The size in bytes of each element in |
| int | numDestElement | The number of elements to expose. |
Remarks
Pins srcData and exposes a view of numDestElement elements. Each element is srcElementSize bytes; the backing buffer pads to k_DataItemSize bytes per element.
Examples
var floats = new float[] { 1.0f, 2.0f, 3.0f, 4.0f };
var tensorArray = new NativeTensorArrayFromManagedArray(floats, 0, sizeof(float), floats.Length);
float value = tensorArray.Get<float>(0);
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when |
| InvalidOperationException | Thrown when the source buffer is too small to account for padding to |
NativeTensorArrayFromManagedArray(byte[], int, int)
Creates a new wrapper that pins a byte array and exposes a view of it as a native tensor array.
Declaration
public NativeTensorArrayFromManagedArray(byte[] srcData, int srcOffset, int numDestElement)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | srcData | The backing data as a byte array. |
| int | srcOffset | The byte offset into |
| int | numDestElement | The number of elements (1 byte each) to expose. |
Remarks
Pins srcData and exposes a view of numDestElement elements (1 byte each) starting at srcOffset. The source is padded to k_DataItemSize bytes per element.
Examples
var bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
var tensorArray = new NativeTensorArrayFromManagedArray(bytes, 2, 4);
// Exposes elements at indices 2, 3, 4, 5
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when |
| InvalidOperationException | Thrown when the source buffer is too small to account for padding. |
Properties
RawPtr
The raw pointer of the backing data.
Declaration
public override void* RawPtr { get; }
Property Value
| Type | Description |
|---|---|
| void* |
Overrides
Remarks
Returns a pointer to the first element. Throws InvalidOperationException if the array has been disposed.
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when the array has been disposed. |
Methods
Dispose()
Disposes of the array and any associated memory.
Declaration
public override void Dispose()
Overrides
Remarks
Releases the pin on the managed array. Does not free the managed array itself.
Examples
var data = new float[] { 1.0f, 2.0f, 3.0f };
var tensorArray = new NativeTensorArrayFromManagedArray(data, 0, sizeof(float), data.Length);
tensorArray.Dispose();
// data remains allocated; only the pin is released