bug #1200: backport aligned_allocator from 3.3

This commit is contained in:
Gael Guennebaud 2016-04-13 14:56:49 +02:00
parent b4669f9036
commit a432b017fb

View File

@ -659,99 +659,60 @@ template<typename T> class aligned_stack_memory_handler
/****************************************************************************/ /****************************************************************************/
/** \class aligned_allocator /** \class aligned_allocator
* \ingroup Core_Module * \ingroup Core_Module
* *
* \brief STL compatible allocator to use with with 16 byte aligned types * \brief STL compatible allocator to use with with 16 byte aligned types
* *
* Example: * Example:
* \code * \code
* // Matrix4f requires 16 bytes alignment: * // Matrix4f requires 16 bytes alignment:
* std::map< int, Matrix4f, std::less<int>, * std::map< int, Matrix4f, std::less<int>,
* aligned_allocator<std::pair<const int, Matrix4f> > > my_map_mat4; * aligned_allocator<std::pair<const int, Matrix4f> > > my_map_mat4;
* // Vector3f does not require 16 bytes alignment, no need to use Eigen's allocator: * // Vector3f does not require 16 bytes alignment, no need to use Eigen's allocator:
* std::map< int, Vector3f > my_map_vec3; * std::map< int, Vector3f > my_map_vec3;
* \endcode * \endcode
* *
* \sa \ref TopicStlContainers. * \sa \blank \ref TopicStlContainers.
*/ */
template<class T> template<class T>
class aligned_allocator class aligned_allocator : public std::allocator<T>
{ {
public: public:
typedef size_t size_type; typedef size_t size_type;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
typedef T* pointer; typedef T* pointer;
typedef const T* const_pointer; typedef const T* const_pointer;
typedef T& reference; typedef T& reference;
typedef const T& const_reference; typedef const T& const_reference;
typedef T value_type; typedef T value_type;
template<class U> template<class U>
struct rebind struct rebind
{ {
typedef aligned_allocator<U> other; typedef aligned_allocator<U> other;
}; };
pointer address( reference value ) const aligned_allocator() : std::allocator<T>() {}
{
return &value;
}
const_pointer address( const_reference value ) const aligned_allocator(const aligned_allocator& other) : std::allocator<T>(other) {}
{
return &value;
}
aligned_allocator() template<class U>
{ aligned_allocator(const aligned_allocator<U>& other) : std::allocator<T>(other) {}
}
aligned_allocator( const aligned_allocator& ) ~aligned_allocator() {}
{
}
template<class U> pointer allocate(size_type num, const void* /*hint*/ = 0)
aligned_allocator( const aligned_allocator<U>& ) {
{ internal::check_size_for_overflow<T>(num);
} return static_cast<pointer>( internal::aligned_malloc(num * sizeof(T)) );
}
~aligned_allocator() void deallocate(pointer p, size_type /*num*/)
{ {
} internal::aligned_free(p);
}
size_type max_size() const
{
return (std::numeric_limits<size_type>::max)();
}
pointer allocate( size_type num, const void* hint = 0 )
{
EIGEN_UNUSED_VARIABLE(hint);
internal::check_size_for_overflow<T>(num);
return static_cast<pointer>( internal::aligned_malloc( num * sizeof(T) ) );
}
void construct( pointer p, const T& value )
{
::new( p ) T( value );
}
void destroy( pointer p )
{
p->~T();
}
void deallocate( pointer p, size_type /*num*/ )
{
internal::aligned_free( p );
}
bool operator!=(const aligned_allocator<T>& ) const
{ return false; }
bool operator==(const aligned_allocator<T>& ) const
{ return true; }
}; };
//---------- Cache sizes ---------- //---------- Cache sizes ----------