Hi Paul,
2012/8/13 Paul Martz <[email protected]> > 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::MixinVector<T> 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 TemplateIndexArray<GLbyte,Array::ByteArrayType,1,GL_BYTE> ByteArray; in template <AllocT = defaultAlloc> class ByteArray : public TemplateIndexArray<GLbyte,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(ByteArray<AllocT>&) {} ... }; Really big work, and there are some limitations (serialization for example). (This could be more simple with C++11 new possibility) 2) Change osg::MixinVector<T> 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::MixinVector<T> 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. template<class ValueT> class MixinVector { typedef typename std::vector<ValueT, 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 <[email protected]> > http://lists.openscenegraph.**org/listinfo.cgi/osg-users-** > openscenegraph.org<http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org> >
_______________________________________________ osg-users mailing list [email protected] http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

