8 #ifndef SAWYER_STACK_ALLOCATOR
9 #define SAWYER_STACK_ALLOCATOR
11 #include <Sawyer/Sawyer.h>
26 const size_t elmtsPerBlock_;
27 std::list<T*> blocks_;
29 std::list<T*> freeBlocks_;
33 : elmtsPerBlock_(elmtsPerBlock), available_(0) {}
37 BOOST_FOREACH (T *ptr, blocks_)
45 BOOST_FOREACH (T *ptr, freeBlocks_)
55 if (nElmts > available_) {
56 ASSERT_require(nElmts <= elmtsPerBlock_);
59 return blocks_.front() + (elmtsPerBlock_ - available_);
67 return blocks_.front() + (elmtsPerBlock_ - available_--);
75 while (!blocks_.empty() && (ptr < blocks_.front() || ptr >= blocks_.front() + elmtsPerBlock_))
77 ASSERT_always_forbid2(blocks_.empty(),
"bad address or previously reverted");
78 available_ = elmtsPerBlock_ - (ptr - blocks_.front());
82 void allocateBlock() {
83 if (freeBlocks_.empty()) {
84 blocks_.insert(blocks_.begin(),
new T[elmtsPerBlock_]);
86 blocks_.insert(blocks_.begin(), freeBlocks_.front());
87 freeBlocks_.erase(freeBlocks_.begin());
89 available_ = elmtsPerBlock_;
93 ASSERT_forbid(blocks_.empty());
94 freeBlocks_.insert(freeBlocks_.begin(), blocks_.front());
95 blocks_.erase(blocks_.begin());