ZeroBuf  0.2.0
Zero-copy, zero-serialize, zero-hassle protocol buffers
Allocator.h
1 
2 /* Copyright (c) 2015-2016, Human Brain Project
3  * Stefan.Eilemann@epfl.ch
4  * grigori.chevtchenko@epfl.ch
5  */
6 
7 #ifndef ZEROBUF_ALLOCATOR_H
8 #define ZEROBUF_ALLOCATOR_H
9 
10 #include <zerobuf/types.h>
11 
12 namespace zerobuf
13 {
20 class Allocator
21 {
22 public:
23  Allocator() {}
24  virtual ~Allocator() {}
25 
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 /*threshold*/ )
31  { throw std::runtime_error( "Compaction not implemented" ); }
32  virtual bool isMovable() const { return false; } // allocation is moveable
33 
42  virtual uint8_t* updateAllocation( size_t /*index*/, bool /*copy*/,
43  size_t /*newSize*/ )
44  { throw std::runtime_error( "Dynamic allocation not implemented" ); }
45 
46 
47  template< class T > T* getItemPtr( const size_t offset )
48  { return reinterpret_cast< T* >( getData() + offset ); }
49 
50  template< class T > const T* getItemPtr( const size_t offset ) const
51  { return reinterpret_cast< const T* >( getData() + offset ); }
52 
53  template< class T > T& getItem( const size_t offset )
54  { return *getItemPtr< T >( offset ); }
55 
56  template< class T > T getItem( const size_t offset ) const
57  { return *getItemPtr< T >( offset ); }
58 
59  template< class T > T* getDynamic( const size_t index )
60  { return reinterpret_cast< T* >( getData() + _getOffset( index )); }
61 
62  template< class T > const T* getDynamic( const size_t index ) const
63  { return reinterpret_cast< const T* >( getData() + _getOffset(index)); }
64 
65  uint64_t getDynamicOffset( const size_t index ) const
66  { return _getOffset( index ); }
67 
68  size_t getDynamicSize( const size_t index ) const
69  { return _getSize( index ); }
70 
71  void check( const size_t numDynamics ) const
72  {
73  for( size_t i = 0; i < numDynamics; ++i )
74  _checkIndex( i );
75  }
76 
77 protected:
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 ); }
86 
87  void _checkIndex( const size_t i ) const
88  {
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" );
101  }
102 };
103 }
104 
105 #endif
Zero-copy, zero-serialize, zero-hassle protocol buffers.
Definition: Allocator.h:12
virtual uint8_t * updateAllocation(size_t, bool, size_t)
Update allocation of the dynamic elem at index to have newSize bytes.
Definition: Allocator.h:42
Allocator base class and interface.
Definition: Allocator.h:20