Re: [osg-users] OSG Arrays and custom allocators?
Hi Paul, 2012/8/13 Paul Martz pma...@skew-matrix.com Hi all -- There doesn't seem to be a way to use a custom allocator with an OSG Array type. Is that correct, or have I missed something? You're right I have a large store of array data, and want to create a new instance of an OSG Array that references that data store without invoking a data copy or per-element constructor. The typical way to do this in C++ is with a custom allocator, but as near as I can tell, OSG's Array types don't allow an allocator as a template parameter. If I can't use a custom allocator, does anyone have any ideas on how to create an OSG Array that references a pre-allocated data store? Any ideas would be appreciated, thanks. . OSG Array use an osg::MixinVectorT template that use a std::vector under the hood. I think to some solutions : 1) extend MixinVector template with a second parameter, the CustomAllocator and pass it to the std::vector used in this template. Then extend osg::TemplateArray in the same way. change typedef TemplateIndexArrayGLbyte,Array::ByteArrayType,1,GL_BYTE ByteArray; in template AllocT = defaultAlloc class ByteArray : public TemplateIndexArrayGLbyte,Array::ByteArrayType,1,GL_BYTE { ... }; Then adapt ArrayVisitor, ConstArrayVisitor and all dependency to accept a template instead of a class class ArrayVisitor { template AllocT = defaultAlloc virtual void apply(ByteArrayAllocT) {} ... }; Really big work, and there are some limitations (serialization for example). (This could be more simple with C++11 new possibility) 2) Change osg::MixinVectorT in your custom OSG implementation then use Allocator that you need and create Vec3ArrayMyAlloc and other osg::Array with your allocator, extend ArrayVisitor for your new Arrays types, and so on for all Array's dependency that your code require. 3) Change osg::MixinVectorT to don't use std::vector anymore (do the vector stuff yourself) and add in the public interface a function that allow user to change Allocator of array element at runtime. All element of the vector will be remove if you change the allocator to avoid any memory corruption. 4) You probably already dig this solution but ... If you can store your large store of array data in an std::vector, then just do a std::swap between std::vector and osg::Array. 5) In the same way of 3) define a custom allocator that could change its behaviours at runtime, but still use a std::vector. templateclass ValueT class MixinVector { typedef typename std::vectorValueT, MyAdaptableAllocator vector_type; public: ... }; Allocator could have a callback mechanism like osg::Node/osg::NodeCallback. You can do something like this in allocator to don't hurt performance in default behaviours (with if/else optimization): void MyAllocator::function() { if (callback == NULL) do stuff else callback() } Anyway there are no really good solution... HTH David -- -Paul Martz Skew Matrix Software http://www.skew-matrix.com/ __**_ osg-users mailing list osg-users@lists.**openscenegraph.org osg-users@lists.openscenegraph.org http://lists.openscenegraph.**org/listinfo.cgi/osg-users-** openscenegraph.orghttp://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] OSG Arrays and custom allocators?
Hi, Am 13.08.2012 18:07, schrieb Paul Martz: Hi all -- There doesn't seem to be a way to use a custom allocator with an OSG Array type. Is that correct, or have I missed something? I have a large store of array data, and want to create a new instance of an OSG Array that references that data store without invoking a data copy or per-element constructor. The typical way to do this in C++ is with a custom allocator, but as near as I can tell, OSG's Array types don't allow an allocator as a template parameter. If I can't use a custom allocator, does anyone have any ideas on how to create an OSG Array that references a pre-allocated data store? Maybe the osgsharedarray example can give you a hint. It is not using STL-style allocators but you can subclass an osg::Array. Any ideas would be appreciated, thanks. Hope this helps. Peter ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] OSG Arrays and custom allocators?
Hi David. Thanks for the many suggestions. Unfortunately, the code I'm working on needs to work with a released version of OSG, so mods to OSG itself are not an option. Apparently I overlooked the obvious swap() solution. I've done a quick test and I believe this will work for me. And thanks, Peter, for the pointer to the mysharedarray example. This is also a workable solution, if I encounter problems with swap(). -Paul On 8/15/2012 5:29 AM, David Callu wrote: Hi Paul, 2012/8/13 Paul Martz pma...@skew-matrix.com mailto:pma...@skew-matrix.com Hi all -- There doesn't seem to be a way to use a custom allocator with an OSG Array type. Is that correct, or have I missed something? You're right I have a large store of array data, and want to create a new instance of an OSG Array that references that data store without invoking a data copy or per-element constructor. The typical way to do this in C++ is with a custom allocator, but as near as I can tell, OSG's Array types don't allow an allocator as a template parameter. If I can't use a custom allocator, does anyone have any ideas on how to create an OSG Array that references a pre-allocated data store? Any ideas would be appreciated, thanks. . OSG Array use an osg::MixinVectorT template that use a std::vector under the hood. I think to some solutions : 1) extend MixinVector template with a second parameter, the CustomAllocator and pass it to the std::vector used in this template. Then extend osg::TemplateArray in the same way. change typedef TemplateIndexArrayGLbyte,Array::ByteArrayType,1,GL_BYTE ByteArray; in template AllocT = defaultAlloc class ByteArray : public TemplateIndexArrayGLbyte,Array::ByteArrayType,1,GL_BYTE { ... }; Then adapt ArrayVisitor, ConstArrayVisitor and all dependency to accept a template instead of a class class ArrayVisitor { template AllocT = defaultAlloc virtual void apply(ByteArrayAllocT) {} ... }; Really big work, and there are some limitations (serialization for example). (This could be more simple with C++11 new possibility) 2) Change osg::MixinVectorT in your custom OSG implementation then use Allocator that you need and create Vec3ArrayMyAlloc and other osg::Array with your allocator, extend ArrayVisitor for your new Arrays types, and so on for all Array's dependency that your code require. 3) Change osg::MixinVectorT to don't use std::vector anymore (do the vector stuff yourself) and add in the public interface a function that allow user to change Allocator of array element at runtime. All element of the vector will be remove if you change the allocator to avoid any memory corruption. 4) You probably already dig this solution but ... If you can store your large store of array data in an std::vector, then just do a std::swap between std::vector and osg::Array. 5) In the same way of 3) define a custom allocator that could change its behaviours at runtime, but still use a std::vector. templateclass ValueT class MixinVector { typedef typename std::vectorValueT, MyAdaptableAllocator vector_type; public: ... }; Allocator could have a callback mechanism like osg::Node/osg::NodeCallback. You can do something like this in allocator to don't hurt performance in default behaviours (with if/else optimization): void MyAllocator::function() { if (callback == NULL) do stuff else callback() } Anyway there are no really good solution... HTH David -- -Paul Martz Skew Matrix Software http://www.skew-matrix.com/ _ osg-users mailing list osg-users@lists.__openscenegraph.org mailto:osg-users@lists.openscenegraph.org http://lists.openscenegraph.__org/listinfo.cgi/osg-users-__openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
[osg-users] OSG Arrays and custom allocators?
Hi all -- There doesn't seem to be a way to use a custom allocator with an OSG Array type. Is that correct, or have I missed something? I have a large store of array data, and want to create a new instance of an OSG Array that references that data store without invoking a data copy or per-element constructor. The typical way to do this in C++ is with a custom allocator, but as near as I can tell, OSG's Array types don't allow an allocator as a template parameter. If I can't use a custom allocator, does anyone have any ideas on how to create an OSG Array that references a pre-allocated data store? Any ideas would be appreciated, thanks. -- -Paul Martz Skew Matrix Software http://www.skew-matrix.com/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org