Csound Csound-dev Csound-tekno Search About

[Csnd-dev] Shared-memory arrays (views / slices)

Date2025-02-18 10:48
FromEduardo Moguillansky
Subject[Csnd-dev] Shared-memory arrays (views / slices)
Hi all,

I would like to make a plädoyer for shared arrays, meaning arrays which do not own their memory. This is the case, for example, when slicing an array, or querying a row of a 2D array, etc., where it is useful to have a reference to the memory of the array, either to be able to modify it or to prevent a full copy of the entire array.

I implemented something of the sort (https://csound-plugins.github.io/csound-plugins/opcodes/ref.html), where taking a reference from an array would move the memory to a global space, making the original array (and any subsequent call to deref) a reference to that memory (the deinit callback of the opcode would prevent the array from freeing its memory).

This mechanism makes it possible to pass arrays around (between instruments, for example), put arrays inside of other arrays, etc. But the approach would be safer if it was possible to mark an array as not owning its memory so that: 1. any attempt to reshape/resize it would result in an error, 2. there would be no need to have a deinit callback intercept the cleanup of the array.

cheers,
Eduardo