#ifndef RUBY_PARSER_POOL_HH #define RUBY_PARSER_POOL_HH #include #include #include template class pool { public: pool() : _slab(new(slab)) {} template T *alloc(Args&&... args) { if (_slab->is_full()) { push_slab(); } return _slab->alloc(std::forward(args)...); } ~pool() { delete _slab; for (auto &p: _history) { delete p; } } protected: class slab { typename std::aligned_storage::type data[N]; std::size_t _size = 0; public: inline bool is_full() const { return _size >= N; } template T *alloc(Args&&... args) { assert(!is_full()); T *p = reinterpret_cast(data+_size); new(p) T(std::forward(args)...); ++_size; return p; } ~slab() { for (std::size_t pos = 0; pos < _size; ++pos) { reinterpret_cast(data+pos)->~T(); } } }; using slab_t = slab*; std::vector _history; slab *_slab; void push_slab() { slab *newb = new(slab); _history.emplace_back(_slab); _slab = newb; } }; #endif