pub struct NDArrayPool { /* private fields */ }Expand description
NDArray factory with free-list reuse and memory tracking.
Mimics C++ ADCore’s NDArrayPool: on alloc, checks the free list for a buffer with sufficient capacity. On release, returns the buffer to the free list for future reuse. The free list is sorted by capacity (descending) and excess entries are dropped when max_memory is exceeded.
Implementations§
Source§impl NDArrayPool
impl NDArrayPool
pub fn new(max_memory: usize) -> Self
Sourcepub fn alloc(
&self,
dims: Vec<NDDimension>,
data_type: NDDataType,
) -> ADResult<NDArray>
pub fn alloc( &self, dims: Vec<NDDimension>, data_type: NDDataType, ) -> ADResult<NDArray>
Allocate an NDArray. Tries to reuse a free-list entry with sufficient capacity.
Memory accounting tracks the exact requested byte count (data_size,
equivalent to C++ dataSize), NOT the allocator-rounded Vec::capacity.
allocated_bytes is the sum of the data_size of every live + free array,
so it matches what C++ getMemorySize() reports and the max_memory
limit is enforced exactly.
Sourcepub fn alloc_copy(&self, source: &NDArray) -> ADResult<NDArray>
pub fn alloc_copy(&self, source: &NDArray) -> ADResult<NDArray>
Allocate a copy of an existing NDArray (new unique_id, data cloned). Tries the free list first (via alloc()), then copies data from source.
Sourcepub fn release(&self, array: NDArray)
pub fn release(&self, array: NDArray)
Return an array to the free list for future reuse.
The array must have been allocated from this pool. C++
(NDArrayPool.cpp:352) verifies pArray->pNDArrayPool == this and refuses
otherwise; the Rust port checks pool_id and drops a foreign array
without touching this pool’s free list or accounting.
Sourcepub fn empty_free_list(&self)
pub fn empty_free_list(&self)
Clear all entries from the free list.
pub fn allocated_bytes(&self) -> u64
pub fn num_alloc_buffers(&self) -> u32
pub fn num_free_buffers(&self) -> u32
pub fn max_memory(&self) -> usize
Sourcepub fn alloc_handle(
pool: &Arc<Self>,
dims: Vec<NDDimension>,
data_type: NDDataType,
) -> ADResult<NDArrayHandle>
pub fn alloc_handle( pool: &Arc<Self>, dims: Vec<NDDimension>, data_type: NDDataType, ) -> ADResult<NDArrayHandle>
Allocate an NDArray wrapped in a pool-aware handle. On final drop, the array is returned to this pool’s free list.
Sourcepub fn copy(
&self,
src: &NDArray,
out: Option<NDArray>,
copy_data: bool,
copy_dimensions: bool,
copy_data_type: bool,
) -> ADResult<NDArray>
pub fn copy( &self, src: &NDArray, out: Option<NDArray>, copy_data: bool, copy_dimensions: bool, copy_data_type: bool, ) -> ADResult<NDArray>
Copy src into a (possibly existing) output array.
Mirrors C++ NDArrayPool::copy(pIn, pOut, copyData, copyDimensions, copyDataType):
outisNone: a fresh array is allocated through this pool with the source dimensions/type.copy_dimensions: copydimsfrom source.copy_data_type: the output buffer takes the source data type.copy_data: copy the pixel/codec bytes.
Attributes are always cleared on the output then copied from the source.
Sourcepub fn pre_allocate_buffers(
&self,
template_array: &NDArray,
count: usize,
) -> ADResult<()>
pub fn pre_allocate_buffers( &self, template_array: &NDArray, count: usize, ) -> ADResult<()>
Allocate count copies of template_array, then immediately release
them back to the free list (C++ asynNDArrayDriver::preAllocateBuffers).
The net effect is that the pool’s free list is warmed with count
reusable buffers sized for the template array.
Sourcepub fn convert_type(
&self,
src: &NDArray,
target_type: NDDataType,
) -> ADResult<NDArray>
pub fn convert_type( &self, src: &NDArray, target_type: NDDataType, ) -> ADResult<NDArray>
Convert data type only (no dimension changes). Allocates the output through the pool, converts data, copies metadata.
Sourcepub fn convert(
&self,
src: &NDArray,
dims_out: &[NDDimension],
target_type: NDDataType,
) -> ADResult<NDArray>
pub fn convert( &self, src: &NDArray, dims_out: &[NDDimension], target_type: NDDataType, ) -> ADResult<NDArray>
Full convert with dimension changes: extract sub-region, bin, reverse.
dims_out specifies offset/size/binning/reverse for each dimension.
Allocates from pool with output dimensions.
Matches the C++ NDArrayPool::convert() semantics:
- Output size for each dim =
dims_out[i].size / dims_out[i].binning - Source pixels are summed (not averaged) across each binning window
- Reverse flips the output along that dimension
- Cumulative offset:
out.dims[i].offset = src.dims[i].offset + dims_out[i].offset - Cumulative binning:
out.dims[i].binning = src.dims[i].binning * dims_out[i].binning