7 #ifndef ZEROBUF_ALLOCATOR_H 8 #define ZEROBUF_ALLOCATOR_H 10 #include <zerobuf/types.h> 26 virtual uint8_t* getData() = 0;
27 virtual const uint8_t* getData()
const = 0;
28 virtual size_t getSize()
const = 0;
29 virtual void copyBuffer(
const void* data,
size_t size ) = 0;
30 virtual void compact(
float )
31 {
throw std::runtime_error(
"Compaction not implemented" ); }
32 virtual bool isMovable()
const {
return false; }
44 {
throw std::runtime_error(
"Dynamic allocation not implemented" ); }
47 template<
class T > T* getItemPtr(
const size_t offset )
48 {
return reinterpret_cast< T*
>( getData() + offset ); }
50 template<
class T >
const T* getItemPtr(
const size_t offset )
const 51 {
return reinterpret_cast< const T*
>( getData() + offset ); }
53 template<
class T > T& getItem(
const size_t offset )
54 {
return *getItemPtr< T >( offset ); }
56 template<
class T > T getItem(
const size_t offset )
const 57 {
return *getItemPtr< T >( offset ); }
59 template<
class T > T* getDynamic(
const size_t index )
60 {
return reinterpret_cast< T*
>( getData() + _getOffset( index )); }
62 template<
class T >
const T* getDynamic(
const size_t index )
const 63 {
return reinterpret_cast< const T*
>( getData() + _getOffset(index)); }
65 uint64_t getDynamicOffset(
const size_t index )
const 66 {
return _getOffset( index ); }
68 size_t getDynamicSize(
const size_t index )
const 69 {
return _getSize( index ); }
71 void check(
const size_t numDynamics )
const 73 for(
size_t i = 0; i < numDynamics; ++i )
78 uint64_t& _getOffset(
const size_t i )
79 { _checkIndex( i );
return getItem< uint64_t >( 4 + i * 16 ); }
80 uint64_t _getOffset(
const size_t i )
const 81 { _checkIndex( i );
return getItem< uint64_t >( 4 + i * 16 ); }
82 uint64_t& _getSize(
const size_t i )
83 { _checkIndex( i );
return getItem< uint64_t >( 4 + 8 + i * 16 ); }
84 uint64_t _getSize(
const size_t i )
const 85 { _checkIndex( i );
return getItem< uint64_t >( 4 + 8 + i * 16 ); }
87 void _checkIndex(
const size_t i )
const 89 const uint64_t offset = getItem< uint64_t >( 4 + i * 16 );
90 const uint64_t size = getItem< uint64_t >( 4 + 8 + i * 16 );
91 if( offset + size > getSize( ))
92 throw std::runtime_error(
93 "Internal allocator error: dynamic #" + std::to_string( i ) +
94 " at " + std::to_string( offset ) +
" size " +
95 std::to_string( size ) +
" exceeds allocation size " +
96 std::to_string( getSize( )));
97 if( offset != 0 && offset < 4 + (i+1) * 16 )
98 throw std::runtime_error(
99 "Internal allocator error: dynamic #" + std::to_string( i ) +
100 " at " + std::to_string(offset) +
" is within static section" );
Zero-copy, zero-serialize, zero-hassle protocol buffers.
virtual uint8_t * updateAllocation(size_t, bool, size_t)
Update allocation of the dynamic elem at index to have newSize bytes.
Allocator base class and interface.