Csound Csound-dev Csound-tekno Search About

[Csnd] csnd::AuxMem vector of vectors from c++ plugin API

Date2019-12-11 20:36
FromRichard Knight
Subject[Csnd] csnd::AuxMem vector of vectors from c++ plugin API

Hi

I'm trying to use csnd::AuxMem like this but guessing it won't work as the way it appears to wrap AUXCH, and I'm getting segfaults when trying to allocate. Is that the case (otherwise I may be doing something wrong elsewhere) ?

csnd::AuxMem< csnd::AuxMem< csnd::AuxMem<MYFLT> > > data;

 

If so, are there any ways of doing something similar / and/or could I use std::vector and somehow have that safely memory managed by Csound ?
I'm presuming I'll just be best to use a regular MYFLT pointer and work out the vector boundaries etc in a kind of traditional way.

thanks
Richard


Date2019-12-11 20:56
FromVictor Lazzarini
SubjectRe: [Csnd] csnd::AuxMem vector of vectors from c++ plugin API
You can't do that, because AuxMem
just wraps Csound's memory mechanism.
However, you can just allocate n x m items
then access each item a[i + j*m] etc

std::vector should be avoided because you will need to dispose and reallocate memory yourself to avoid leaks. Also a csound plugin opcode class is PLOD and won't be able to initialise any C++ members so you would need to use pointers instead. All of this 
introduces complexity and issues that you don't want to be dealing with.

Csound is in C; using  CPOF has limitations imposed by the fact that the opcodes are instantiated in C so any use of C++ libs and templates have to be carefully considered.

Prof. Victor Lazzarini
Maynooth University
Ireland

On 11 Dec 2019, at 20:36, Richard Knight <richard@1bpm.net> wrote:

Hi

I'm trying to use csnd::AuxMem like this but guessing it won't work as the way it appears to wrap AUXCH, and I'm getting segfaults when trying to allocate. Is that the case (otherwise I may be doing something wrong elsewhere) ?

csnd::AuxMem< csnd::AuxMem< csnd::AuxMem<MYFLT> > > data;

 

If so, are there any ways of doing something similar / and/or could I use std::vector and somehow have that safely memory managed by Csound ?
I'm presuming I'll just be best to use a regular MYFLT pointer and work out the vector boundaries etc in a kind of traditional way.

thanks
Richard

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2019-12-11 21:08
FromRichard Knight
SubjectRe: [Csnd] csnd::AuxMem vector of vectors from c++ plugin API

Thanks for your quick response and detailed explanation.

On Wed, 11 Dec 2019 20:56:34 +0000, Victor Lazzarini wrote:

You can't do that, because AuxMem
just wraps Csound's memory mechanism.
However, you can just allocate n x m items
then access each item a[i + j*m] etc
std::vector should be avoided because you will need to dispose and reallocate memory yourself to avoid leaks. Also a csound plugin opcode class is PLOD and won't be able to initialise any C++ members so you would need to use pointers instead. All of this 
introduces complexity and issues that you don't want to be dealing with.
Csound is in C; using  CPOF has limitations imposed by the fact that the opcodes are instantiated in C so any use of C++ libs and templates have to be carefully considered.

Prof. Victor Lazzarini
Maynooth University
Ireland

On 11 Dec 2019, at 20:36, Richard Knight <richard@1bpm.net> wrote:

Hi

I'm trying to use csnd::AuxMem like this but guessing it won't work as the way it appears to wrap AUXCH, and I'm getting segfaults when trying to allocate. Is that the case (otherwise I may be doing something wrong elsewhere) ?

csnd::AuxMem< csnd::AuxMem< csnd::AuxMem > > data;

 

If so, are there any ways of doing something similar / and/or could I use std::vector and somehow have that safely memory managed by Csound ?
I'm presuming I'll just be best to use a regular MYFLT pointer and work out the vector boundaries etc in a kind of traditional way.

thanks
Richard

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

-- 


http://rk.1bpm.net/

Date2019-12-11 21:30
FromMichael Gogins
SubjectRe: [Csnd] csnd::AuxMem vector of vectors from c++ plugin API
You could use include/OpcodeBase.hpp. This used in a number of opcodes
by me, including ampmidid, doppler, and the signal flow graph opcodes.
You can indeed use std::vector, std::map, and so on.

It is tricky, because as Victor says, memory management is an issue.
The opcodes I mentioned deal with memory management by using the
regular C++ allocator, and then deallocating collections in the
csoundModuleDestroy function. This in turn may involve creating a
state object a pointer to which is stored as a Csound global variable,
and other complexities.

If you just have arrays, by all means use the Csound allocator and
just use regular arithmetic to access elements of arrays.

If you have a complex data type that can't be reduced to arrays or
that is shared between different opcodes and/or instances of opcodes,
have a look at signalflowgraph.cpp.

Regards,
Mike



-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com

On Wed, Dec 11, 2019 at 4:08 PM Richard Knight  wrote:
>
> Thanks for your quick response and detailed explanation.
>
> On Wed, 11 Dec 2019 20:56:34 +0000, Victor Lazzarini wrote:
>
> You can't do that, because AuxMem
> just wraps Csound's memory mechanism.
> However, you can just allocate n x m items
> then access each item a[i + j*m] etc
> std::vector should be avoided because you will need to dispose and reallocate memory yourself to avoid leaks. Also a csound plugin opcode class is PLOD and won't be able to initialise any C++ members so you would need to use pointers instead. All of this
> introduces complexity and issues that you don't want to be dealing with.
> Csound is in C; using  CPOF has limitations imposed by the fact that the opcodes are instantiated in C so any use of C++ libs and templates have to be carefully considered.
>
> Prof. Victor Lazzarini
> Maynooth University
> Ireland
>
> On 11 Dec 2019, at 20:36, Richard Knight  wrote:
>
> Hi
>
> I'm trying to use csnd::AuxMem like this but guessing it won't work as the way it appears to wrap AUXCH, and I'm getting segfaults when trying to allocate. Is that the case (otherwise I may be doing something wrong elsewhere) ?
>
> csnd::AuxMem< csnd::AuxMem< csnd::AuxMem > > data;
>
>
>
> If so, are there any ways of doing something similar / and/or could I use std::vector and somehow have that safely memory managed by Csound ?
> I'm presuming I'll just be best to use a regular MYFLT pointer and work out the vector boundaries etc in a kind of traditional way.
>
> thanks
> Richard
>
> Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
>
> Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
>
> --
>
>
> http://rk.1bpm.net/
>
> Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2019-12-12 01:00
FromRichard Knight
SubjectRe: [Csnd] csnd::AuxMem vector of vectors from c++ plugin API

Thanks - signalflowgraph.cpp does look like a useful reference.

I am using some complex types which are shared between opcodes (in a different project to the original question). At the moment I've been allocating and passing a pointer to a struct with Create/QueryGlobalVariable during opcode initialisations, and then using the csnd::Plugin deinit() function to handle deallocation. I've not come across obvious problems with this, but based on your comments and looking at OpcodeBase.hpp/signalflowgraph.cpp, maybe it warrants a review, particularly also in regard to mutexes, which might be something I've overlooked.

A brief example of what I'm doing - creating some database opcodes, with one allocating/opening the connection (and closing/de-allocating in deinit() ) and then others for performing queries referring to that via a handle/QueryGlobalVariable to get a pointer to a struct which contains a pointer to the connection data.

Richard

On Wed, 11 Dec 2019 16:30:14 -0500, Michael Gogins wrote:

You could use include/OpcodeBase.hpp. This used in a number of opcodes
by me, including ampmidid, doppler, and the signal flow graph opcodes.
You can indeed use std::vector, std::map, and so on.

It is tricky, because as Victor says, memory management is an issue.
The opcodes I mentioned deal with memory management by using the
regular C++ allocator, and then deallocating collections in the
csoundModuleDestroy function. This in turn may involve creating a
state object a pointer to which is stored as a Csound global variable,
and other complexities.

If you just have arrays, by all means use the Csound allocator and
just use regular arithmetic to access elements of arrays.

If you have a complex data type that can't be reduced to arrays or
that is shared between different opcodes and/or instances of opcodes,
have a look at signalflowgraph.cpp.

Regards,
Mike



-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com

On Wed, Dec 11, 2019 at 4:08 PM Richard Knight <richard@1bpm.net> wrote:
Thanks for your quick response and detailed explanation. On Wed, 11 Dec 2019 20:56:34 +0000, Victor Lazzarini wrote: You can't do that, because AuxMem just wraps Csound's memory mechanism. However, you can just allocate n x m items then access each item a[i + j*m] etc std::vector should be avoided because you will need to dispose and reallocate memory yourself to avoid leaks. Also a csound plugin opcode class is PLOD and won't be able to initialise any C++ members so you would need to use pointers instead. All of this introduces complexity and issues that you don't want to be dealing with. Csound is in C; using CPOF has limitations imposed by the fact that the opcodes are instantiated in C so any use of C++ libs and templates have to be carefully considered. Prof. Victor Lazzarini Maynooth University Ireland On 11 Dec 2019, at 20:36, Richard Knight <richard@1bpm.net> wrote: Hi I'm trying to use csnd::AuxMem like this but guessing it won't work as the way it appears to wrap AUXCH, and I'm getting segfaults when trying to allocate. Is that the case (otherwise I may be doing something wrong elsewhere) ? csnd::AuxMem< csnd::AuxMem< csnd::AuxMem > > data; If so, are there any ways of doing something similar / and/or could I use std::vector and somehow have that safely memory managed by Csound ? I'm presuming I'll just be best to use a regular MYFLT pointer and work out the vector boundaries etc in a kind of traditional way. thanks Richard Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here -- http://rk.1bpm.net/ Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
-- 


http://rk.1bpm.net/

Date2019-12-12 06:39
FromVictor Lazzarini
SubjectRe: [Csnd] csnd::AuxMem vector of vectors from c++ plugin API
If you use deinit() at least you should be protected against leaks.

Prof. Victor Lazzarini
Maynooth University
Ireland

On 12 Dec 2019, at 01:00, Richard Knight <richard@1bpm.net> wrote:

Thanks - signalflowgraph.cpp does look like a useful reference.

I am using some complex types which are shared between opcodes (in a different project to the original question). At the moment I've been allocating and passing a pointer to a struct with Create/QueryGlobalVariable during opcode initialisations, and then using the csnd::Plugin deinit() function to handle deallocation. I've not come across obvious problems with this, but based on your comments and looking at OpcodeBase.hpp/signalflowgraph.cpp, maybe it warrants a review, particularly also in regard to mutexes, which might be something I've overlooked.

A brief example of what I'm doing - creating some database opcodes, with one allocating/opening the connection (and closing/de-allocating in deinit() ) and then others for performing queries referring to that via a handle/QueryGlobalVariable to get a pointer to a struct which contains a pointer to the connection data.

Richard

On Wed, 11 Dec 2019 16:30:14 -0500, Michael Gogins wrote:

You could use include/OpcodeBase.hpp. This used in a number of opcodes
by me, including ampmidid, doppler, and the signal flow graph opcodes.
You can indeed use std::vector, std::map, and so on.

It is tricky, because as Victor says, memory management is an issue.
The opcodes I mentioned deal with memory management by using the
regular C++ allocator, and then deallocating collections in the
csoundModuleDestroy function. This in turn may involve creating a
state object a pointer to which is stored as a Csound global variable,
and other complexities.

If you just have arrays, by all means use the Csound allocator and
just use regular arithmetic to access elements of arrays.

If you have a complex data type that can't be reduced to arrays or
that is shared between different opcodes and/or instances of opcodes,
have a look at signalflowgraph.cpp.

Regards,
Mike



-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com

On Wed, Dec 11, 2019 at 4:08 PM Richard Knight <richard@1bpm.net> wrote:
Thanks for your quick response and detailed explanation. On Wed, 11 Dec 2019 20:56:34 +0000, Victor Lazzarini wrote: You can't do that, because AuxMem just wraps Csound's memory mechanism. However, you can just allocate n x m items then access each item a[i + j*m] etc std::vector should be avoided because you will need to dispose and reallocate memory yourself to avoid leaks. Also a csound plugin opcode class is PLOD and won't be able to initialise any C++ members so you would need to use pointers instead. All of this introduces complexity and issues that you don't want to be dealing with. Csound is in C; using CPOF has limitations imposed by the fact that the opcodes are instantiated in C so any use of C++ libs and templates have to be carefully considered. Prof. Victor Lazzarini Maynooth University Ireland On 11 Dec 2019, at 20:36, Richard Knight <richard@1bpm.net> wrote: Hi I'm trying to use csnd::AuxMem like this but guessing it won't work as the way it appears to wrap AUXCH, and I'm getting segfaults when trying to allocate. Is that the case (otherwise I may be doing something wrong elsewhere) ? csnd::AuxMem< csnd::AuxMem< csnd::AuxMem > > data; If so, are there any ways of doing something similar / and/or could I use std::vector and somehow have that safely memory managed by Csound ? I'm presuming I'll just be best to use a regular MYFLT pointer and work out the vector boundaries etc in a kind of traditional way. thanks Richard Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here -- http://rk.1bpm.net/ Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here