任务:shared_ptr,T=buffer,其中buffer有动态字节数(uint8_t[]结尾);
allocate_shared 是否保证将保留此订单:[shared_ptr data][object][additional butebuffer]
因为其他方式不起作用,例如:[object][shared_ptr data][additional butebuffer]
其他实现思路?
template <class T>
class addon_allocator : public boost::noncopyable
{
public :
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public :
template<typename U> struct rebind
{
typedef addon_allocator<U> other;
};
public :
explicit addon_allocator( std::size_t size )
:m_size( size )
{
}
~addon_allocator()
{
}
addon_allocator(const addon_allocator<T>& other)
:m_size(other.m_size)
{
}
template <class U>
explicit addon_allocator(const addon_allocator<U>& other)
:m_size(other.get_addon_size())
{
}
pointer address(reference r)
{
return &r;
}
const_pointer address(const_reference r)
{
return &r;
}
pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0)
{
assert( cnt == 1 );
return reinterpret_cast<pointer>(
::operator new(sizeof(T) + m_size)
);
}
void deallocate(pointer p, size_type)
{
::operator delete(p);
}
std::size_t get_addon_size() const
{
return m_size;
}
private:
const std::size_t m_size;
};
class buffer : public boost::noncopyable
{
public:
buffer( std::size_t size )
:m_size(size)
{
}
const std::size_t m_size;
uint8_t m_data[];
static boost::shared_ptr<buffer> create(std::size_t size)
{
boost::allocate_shared<buffer>(
addon_allocator<buffer>(size),
size
);
}
};