[C++-sig] from-python converter for aligned class

Václav Šmilauer eudoxos at arcig.cz
Sun Feb 12 18:49:21 CET 2012


I wrote a custom to-python converter for an aligned struct (It is a 
128-bit aligned vector type from the http://eigen.tuxfamily.org 
library). I followed 
http://www.boost.org/doc/libs/1_39_0/libs/python/doc/v2/faq.html#custom_string), 
the converter looks like this:

     class AlignedType{ /*... */} __attribute__((aligned(16)));
     struct converter{
        converter(){ 
py::converter::registry::push_back(&convertible,&construct,py::type_id<AlignedType>());
        static void* convertible(PyObject* obj_ptr){ /*...*/ }
        static void construct(PyObject* obj_ptr, 
py::converter::rvalue_from_python_stage1_data* data){
           void* 
storage=((py::converter::rvalue_from_python_storage<VT>*)(data))->storage.bytes;
           // !! this creates AlignedType instance at a possibly 
unaligned address !!
           new (storage) AlignedType;
           data->convertible=storage;
        }
     };

I am getting crashes due to mis-alignment of the resulting object, 
because it is created at a possible unaligned address. Is there a way 
around this? Can I allocate another chunk of memory, or enforce the 
alignment before it is allocated?

Best regards, Vaclav




More information about the Cplusplus-sig mailing list