[Csnd-dev] Re-allocating memory in a running opcode
Date | 2017-01-13 22:51 |
From | Ed Costello |
Subject | [Csnd-dev] Re-allocating memory in a running opcode |
Hi,
I am writing an opcode that buffers incoming data in frames which are used for further processing. I would also like it so the amount of frames buffered can be changed at k-rate during runtime but I would also like to make it so there doesn't have to be a hard limit on the maximum amount of frames that can be buffered at initialisation. It seems then that I will need to re-allocated my buffer during performance. I am not very experienced in multi-threaded programming and was wondering what the best way to go about this is, would I have to write logic using pthreads and perhaps atomics to do the reallocation in a different thread? Then signal the main process when the allocation has successfully completed? As a small aside Apple's GCD seems to have a very nice API for doing processes in a background thread then notifying the main thread which makes this process a lot easier, an example would be something I found on Stackoverflow like:
Is there an equivalent to this in C++ perhaps which would do what I require in a similar fashion and would not block the main process thread? Thanks Ed |
Date | 2017-01-13 23:55 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] Re-allocating memory in a running opcode |
Whatever you do, don't allocate it in the processing thread. Start a thread using the Csound threading interface, allocate memory and then swap the pointers (with a
lock protecting it), signal the change if needed, then deallocate the old
pointer and finish the thread. You might not need to complicate it any further.
Victor Lazzarini Dean of Arts, Celtic Studies, and Philosophy
Maynooth University
Ireland
|
Date | 2017-01-14 00:18 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] Re-allocating memory in a running opcode |
Btw if you are using AuxAlloc, you don't free, keep the pointer somewhere in case you want to use that block again. If you are using a lot of memory, then use csound->Malloc() and Free(), and add a dealloc callback to free memory at the end too.
Victor Lazzarini Dean of Arts, Celtic Studies, and Philosophy
Maynooth University
Ireland
|
Date | 2017-01-14 12:51 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] Re-allocating memory in a running opcode |
Yes, look at futures in the standard c++11 library. Regards, Mike On Jan 13, 2017 5:51 PM, "Ed Costello" <phasereset@gmail.com> wrote:
|
Date | 2017-01-14 17:25 |
From | Ed Costello |
Subject | Re: [Csnd-dev] Re-allocating memory in a running opcode |
Yeah I figured that using Csounds allocator would be best practice, I will look into using Csounds threading interface and C++ futures also. I have been using std::vector in my code, would it be even more convenient just to use the vector resize method in another thread? Ed On 14 January 2017 at 12:51, Michael Gogins <michael.gogins@gmail.com> wrote:
|
Date | 2017-01-14 17:32 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] Re-allocating memory in a running opcode |
If you use std::vector() you really need to re-implement new/delete to use the Csound memory allocation mechanism as Mike said, because otherwise things might not tie in very well with the Csound engine. Anything like resizing has to happen in another thread. Resize will also need to use csound->ReAlloc (if you are using csound->Malloc). Not quite sure whether it’s worth the trouble. ======================== Prof. Victor Lazzarini Dean of Arts, Celtic Studies, and Philosophy, Maynooth University, Maynooth, Co Kildare, Ireland Tel: 00 353 7086936 Fax: 00 353 1 7086952 > On 14 Jan 2017, at 17:25, Ed Costello |
Date | 2017-01-14 17:48 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] Re-allocating memory in a running opcode |
The standard C++ library does not parametrize thread launching facilities in the same way that it parametrizes memory allocators and deallocators. So when using |
Date | 2017-01-14 17:54 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] Re-allocating memory in a running opcode |
Vector allocation and deallocation can be done either by overriding operators new and delete, or by providing a custom implementation of std::allocator as a template argument to the std::vector declaration. I don't think std::vector<>::resize() would have to use csound->ReAlloc, but I'm not 100% sure. Regards, Mike ----------------------------------------------------- Michael Gogins Irreducible Productions http://michaelgogins.tumblr.com Michael dot Gogins at gmail dot com On Sat, Jan 14, 2017 at 12:32 PM, Victor Lazzarini |
Date | 2017-01-14 18:02 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] Re-allocating memory in a running opcode |
If the allocator uses csound->Malloc(), the only sane way to grow the memory is to use csound->ReAlloc() (unless you want to reimplement it using csound->Malloc() and Free()) , so resize() would need to do that if the memory needs to be expanded. ======================== Prof. Victor Lazzarini Dean of Arts, Celtic Studies, and Philosophy, Maynooth University, Maynooth, Co Kildare, Ireland Tel: 00 353 7086936 Fax: 00 353 1 7086952 > On 14 Jan 2017, at 17:54, Michael Gogins |