Add ArenaStlAllocator, and use arena throughout CommitRequest
This commit is contained in:
@@ -423,3 +423,55 @@ private:
|
||||
/// Current offset within the current block's data area
|
||||
size_t current_offset_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief STL-compatible allocator that uses ArenaAllocator for memory
|
||||
* management.
|
||||
* @tparam T The type of objects to allocate
|
||||
*/
|
||||
template <typename T> class ArenaStlAllocator {
|
||||
public:
|
||||
using value_type = T;
|
||||
using pointer = T *;
|
||||
using const_pointer = const T *;
|
||||
using reference = T &;
|
||||
using const_reference = const T &;
|
||||
using size_type = std::size_t;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
|
||||
template <typename U> struct rebind {
|
||||
using other = ArenaStlAllocator<U>;
|
||||
};
|
||||
|
||||
explicit ArenaStlAllocator(ArenaAllocator *arena) noexcept : arena_(arena) {}
|
||||
|
||||
template <typename U>
|
||||
ArenaStlAllocator(const ArenaStlAllocator<U> &other) noexcept
|
||||
: arena_(other.arena_) {}
|
||||
|
||||
T *allocate(size_type n) {
|
||||
if (n == 0)
|
||||
return nullptr;
|
||||
return static_cast<T *>(arena_->allocate(n * sizeof(T), alignof(T)));
|
||||
}
|
||||
|
||||
void deallocate(T *ptr, size_type n) noexcept {
|
||||
// Arena allocator doesn't support individual deallocation
|
||||
(void)ptr;
|
||||
(void)n;
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
bool operator==(const ArenaStlAllocator<U> &other) const noexcept {
|
||||
return arena_ == other.arena_;
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
bool operator!=(const ArenaStlAllocator<U> &other) const noexcept {
|
||||
return arena_ != other.arena_;
|
||||
}
|
||||
|
||||
ArenaAllocator *arena_;
|
||||
|
||||
template <typename U> friend class ArenaStlAllocator;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user