7 #ifndef ZEROBUF_ALLOCATOR_H 8 #define ZEROBUF_ALLOCATOR_H 10 #include <zerobuf/types.h> 25 virtual uint8_t* getData() = 0;
26 virtual const uint8_t* getData()
const = 0;
27 virtual size_t getSize()
const = 0;
28 virtual void copyBuffer(
const void* data,
size_t size) = 0;
29 virtual void compact(
float )
31 throw std::runtime_error(
"Compaction not implemented");
33 virtual bool isMovable()
const {
return false; }
34 virtual bool isMutable()
const {
return true; }
47 throw std::runtime_error(
"Dynamic allocation not implemented");
51 T* getItemPtr(
const size_t offset)
53 return reinterpret_cast<T*
>(getData() + offset);
57 const T* getItemPtr(
const size_t offset)
const 59 return reinterpret_cast<const T*
>(getData() + offset);
63 T& getItem(
const size_t offset)
65 return *getItemPtr<T>(offset);
69 T getItem(
const size_t offset)
const 71 return *getItemPtr<T>(offset);
75 T* getDynamic(
const size_t index)
77 return reinterpret_cast<T*
>(getData() + _getOffset(index));
81 const T* getDynamic(
const size_t index)
const 83 return reinterpret_cast<const T*
>(getData() + _getOffset(index));
86 uint64_t getDynamicOffset(
const size_t index)
const 88 return _getOffset(index);
91 size_t getDynamicSize(
const size_t index)
const {
return _getSize(index); }
92 void check(
const size_t numDynamics)
const 94 for (
size_t i = 0; i < numDynamics; ++i)
99 uint64_t& _getOffset(
const size_t i)
102 return getItem<uint64_t>(4 + i * 16);
104 uint64_t _getOffset(
const size_t i)
const 107 return getItem<uint64_t>(4 + i * 16);
109 uint64_t& _getSize(
const size_t i)
112 return getItem<uint64_t>(4 + 8 + i * 16);
114 uint64_t _getSize(
const size_t i)
const 117 return getItem<uint64_t>(4 + 8 + i * 16);
120 void _checkIndex(
const size_t i)
const 122 const uint64_t offset = getItem<uint64_t>(4 + i * 16);
123 const uint64_t size = getItem<uint64_t>(4 + 8 + i * 16);
124 if (offset + size > getSize())
125 throw std::runtime_error(
126 "Internal allocator error: dynamic #" + std::to_string(i) +
127 " at " + std::to_string(offset) +
" size " +
128 std::to_string(size) +
" exceeds allocation size " +
129 std::to_string(getSize()));
130 if (offset != 0 && offset < 4 + (i + 1) * 16)
131 throw std::runtime_error(
132 "Internal allocator error: dynamic #" + std::to_string(i) +
133 " 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.