sequence_stack.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // sequence_stack.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_SEQUENCE_STACK_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_SEQUENCE_STACK_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. # pragma warning(push)
  13. # pragma warning(disable : 4127) // conditional expression constant
  14. #endif
  15. #include <algorithm>
  16. #include <functional>
  17. namespace boost { namespace xpressive { namespace detail
  18. {
  19. struct fill_t {} const fill = {};
  20. //////////////////////////////////////////////////////////////////////////
  21. // sequence_stack
  22. //
  23. // For storing a stack of sequences of type T, where each sequence
  24. // is guaranteed to be stored in contiguous memory.
  25. template<typename T>
  26. struct sequence_stack
  27. {
  28. struct allocate_guard_t;
  29. friend struct allocate_guard_t;
  30. struct allocate_guard_t
  31. {
  32. std::size_t i;
  33. T *p;
  34. bool dismissed;
  35. ~allocate_guard_t()
  36. {
  37. if(!this->dismissed)
  38. sequence_stack::deallocate(this->p, this->i);
  39. }
  40. };
  41. private:
  42. static T *allocate(std::size_t size, T const &t)
  43. {
  44. allocate_guard_t guard = {0, (T *)::operator new(size * sizeof(T)), false};
  45. for(; guard.i < size; ++guard.i)
  46. ::new((void *)(guard.p + guard.i)) T(t);
  47. guard.dismissed = true;
  48. return guard.p;
  49. }
  50. static void deallocate(T *p, std::size_t i)
  51. {
  52. while(i-- > 0)
  53. (p+i)->~T();
  54. ::operator delete(p);
  55. }
  56. struct chunk
  57. {
  58. chunk(std::size_t size, T const &t, std::size_t count, chunk *back, chunk *next)
  59. : begin_(allocate(size, t))
  60. , curr_(begin_ + count)
  61. , end_(begin_ + size)
  62. , back_(back)
  63. , next_(next)
  64. {
  65. if(this->back_)
  66. this->back_->next_ = this;
  67. if(this->next_)
  68. this->next_->back_ = this;
  69. }
  70. ~chunk()
  71. {
  72. deallocate(this->begin_, this->size());
  73. }
  74. std::size_t size() const
  75. {
  76. return static_cast<std::size_t>(this->end_ - this->begin_);
  77. }
  78. T *const begin_, *curr_, *const end_;
  79. chunk *back_, *next_;
  80. private:
  81. chunk &operator =(chunk const &);
  82. };
  83. chunk *current_chunk_;
  84. // Cache these for faster access
  85. T *begin_;
  86. T *curr_;
  87. T *end_;
  88. T *grow_(std::size_t count, T const &t)
  89. {
  90. if(this->current_chunk_)
  91. {
  92. // write the cached value of current into the expr.
  93. // OK to do this even if later statements throw.
  94. this->current_chunk_->curr_ = this->curr_;
  95. // Do we have a expr with enough available memory already?
  96. if(this->current_chunk_->next_ && count <= this->current_chunk_->next_->size())
  97. {
  98. this->current_chunk_ = this->current_chunk_->next_;
  99. this->curr_ = this->current_chunk_->curr_ = this->current_chunk_->begin_ + count;
  100. this->end_ = this->current_chunk_->end_;
  101. this->begin_ = this->current_chunk_->begin_;
  102. std::fill_n(this->begin_, count, t);
  103. return this->begin_;
  104. }
  105. // grow exponentially
  106. std::size_t new_size = (std::max)(
  107. count
  108. , static_cast<std::size_t>(static_cast<double>(this->current_chunk_->size()) * 1.5)
  109. );
  110. // Create a new expr and insert it into the list
  111. this->current_chunk_ = new chunk(new_size, t, count, this->current_chunk_, this->current_chunk_->next_);
  112. }
  113. else
  114. {
  115. // first chunk is 256
  116. std::size_t new_size = (std::max)(count, static_cast<std::size_t>(256U));
  117. // Create a new expr and insert it into the list
  118. this->current_chunk_ = new chunk(new_size, t, count, 0, 0);
  119. }
  120. this->begin_ = this->current_chunk_->begin_;
  121. this->curr_ = this->current_chunk_->curr_;
  122. this->end_ = this->current_chunk_->end_;
  123. return this->begin_;
  124. }
  125. void unwind_chunk_()
  126. {
  127. // write the cached value of curr_ into current_chunk_
  128. this->current_chunk_->curr_ = this->begin_;
  129. // make the previous chunk the current
  130. this->current_chunk_ = this->current_chunk_->back_;
  131. // update the cache
  132. this->begin_ = this->current_chunk_->begin_;
  133. this->curr_ = this->current_chunk_->curr_;
  134. this->end_ = this->current_chunk_->end_;
  135. }
  136. bool in_current_chunk(T *ptr) const
  137. {
  138. return !std::less<void*>()(ptr, this->begin_) && std::less<void*>()(ptr, this->end_);
  139. }
  140. public:
  141. sequence_stack()
  142. : current_chunk_(0)
  143. , begin_(0)
  144. , curr_(0)
  145. , end_(0)
  146. {
  147. }
  148. ~sequence_stack()
  149. {
  150. this->clear();
  151. }
  152. // walk to the front of the linked list
  153. void unwind()
  154. {
  155. if(this->current_chunk_)
  156. {
  157. while(this->current_chunk_->back_)
  158. {
  159. this->current_chunk_->curr_ = this->current_chunk_->begin_;
  160. this->current_chunk_ = this->current_chunk_->back_;
  161. }
  162. this->begin_ = this->curr_ = this->current_chunk_->curr_ = this->current_chunk_->begin_;
  163. this->end_ = this->current_chunk_->end_;
  164. }
  165. }
  166. void clear()
  167. {
  168. // walk to the front of the list
  169. this->unwind();
  170. // delete the list
  171. for(chunk *next; this->current_chunk_; this->current_chunk_ = next)
  172. {
  173. next = this->current_chunk_->next_;
  174. delete this->current_chunk_;
  175. }
  176. this->begin_ = this->curr_ = this->end_ = 0;
  177. }
  178. T *push_sequence(std::size_t count, T const &t)
  179. {
  180. // This is the ptr to return
  181. T *ptr = this->curr_;
  182. // Advance the high-water mark
  183. this->curr_ += count;
  184. // Check to see if we have overflowed this buffer
  185. if(std::less<void*>()(this->end_, this->curr_))
  186. {
  187. // oops, back this out.
  188. this->curr_ = ptr;
  189. // allocate a new block and return a ptr to the new memory
  190. return this->grow_(count, t);
  191. }
  192. return ptr;
  193. }
  194. T *push_sequence(std::size_t count, T const &t, fill_t)
  195. {
  196. T *ptr = this->push_sequence(count, t);
  197. std::fill_n(ptr, count, t);
  198. return ptr;
  199. }
  200. void unwind_to(T *ptr)
  201. {
  202. while(!this->in_current_chunk(ptr))
  203. {
  204. // completely unwind the current chunk, move to the previous chunk
  205. this->unwind_chunk_();
  206. }
  207. this->current_chunk_->curr_ = this->curr_ = ptr;
  208. }
  209. // shrink-to-fit: remove any unused nodes in the chain
  210. void conserve()
  211. {
  212. if(this->current_chunk_)
  213. {
  214. for(chunk *next; this->current_chunk_->next_; this->current_chunk_->next_ = next)
  215. {
  216. next = this->current_chunk_->next_->next_;
  217. delete this->current_chunk_->next_;
  218. }
  219. }
  220. }
  221. };
  222. }}} // namespace boost::xpressive::detail
  223. #if defined(_MSC_VER)
  224. # pragma warning(pop)
  225. #endif
  226. #endif