stack.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef RAPIDJSON_INTERNAL_STACK_H_
  15. #define RAPIDJSON_INTERNAL_STACK_H_
  16. #include "../allocators.h"
  17. #include "swap.h"
  18. #if defined(__clang__)
  19. RAPIDJSON_DIAG_PUSH
  20. RAPIDJSON_DIAG_OFF(c++98-compat)
  21. #endif
  22. RAPIDJSON_NAMESPACE_BEGIN
  23. namespace internal {
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // Stack
  26. //! A type-unsafe stack for storing different types of data.
  27. /*! \tparam Allocator Allocator for allocating stack memory.
  28. */
  29. template <typename Allocator>
  30. class Stack {
  31. public:
  32. // Optimization note: Do not allocate memory for stack_ in constructor.
  33. // Do it lazily when first Push() -> Expand() -> Resize().
  34. Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
  35. }
  36. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  37. Stack(Stack&& rhs)
  38. : allocator_(rhs.allocator_),
  39. ownAllocator_(rhs.ownAllocator_),
  40. stack_(rhs.stack_),
  41. stackTop_(rhs.stackTop_),
  42. stackEnd_(rhs.stackEnd_),
  43. initialCapacity_(rhs.initialCapacity_)
  44. {
  45. rhs.allocator_ = 0;
  46. rhs.ownAllocator_ = 0;
  47. rhs.stack_ = 0;
  48. rhs.stackTop_ = 0;
  49. rhs.stackEnd_ = 0;
  50. rhs.initialCapacity_ = 0;
  51. }
  52. #endif
  53. ~Stack() {
  54. Destroy();
  55. }
  56. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  57. Stack& operator=(Stack&& rhs) {
  58. if (&rhs != this)
  59. {
  60. Destroy();
  61. allocator_ = rhs.allocator_;
  62. ownAllocator_ = rhs.ownAllocator_;
  63. stack_ = rhs.stack_;
  64. stackTop_ = rhs.stackTop_;
  65. stackEnd_ = rhs.stackEnd_;
  66. initialCapacity_ = rhs.initialCapacity_;
  67. rhs.allocator_ = 0;
  68. rhs.ownAllocator_ = 0;
  69. rhs.stack_ = 0;
  70. rhs.stackTop_ = 0;
  71. rhs.stackEnd_ = 0;
  72. rhs.initialCapacity_ = 0;
  73. }
  74. return *this;
  75. }
  76. #endif
  77. void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT {
  78. internal::Swap(allocator_, rhs.allocator_);
  79. internal::Swap(ownAllocator_, rhs.ownAllocator_);
  80. internal::Swap(stack_, rhs.stack_);
  81. internal::Swap(stackTop_, rhs.stackTop_);
  82. internal::Swap(stackEnd_, rhs.stackEnd_);
  83. internal::Swap(initialCapacity_, rhs.initialCapacity_);
  84. }
  85. void Clear() { stackTop_ = stack_; }
  86. void ShrinkToFit() {
  87. if (Empty()) {
  88. // If the stack is empty, completely deallocate the memory.
  89. Allocator::Free(stack_);
  90. stack_ = 0;
  91. stackTop_ = 0;
  92. stackEnd_ = 0;
  93. }
  94. else
  95. Resize(GetSize());
  96. }
  97. // Optimization note: try to minimize the size of this function for force inline.
  98. // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
  99. template<typename T>
  100. RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) {
  101. // Expand the stack if needed
  102. if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_))
  103. Expand<T>(count);
  104. }
  105. template<typename T>
  106. RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
  107. Reserve<T>(count);
  108. return PushUnsafe<T>(count);
  109. }
  110. template<typename T>
  111. RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) {
  112. RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_);
  113. T* ret = reinterpret_cast<T*>(stackTop_);
  114. stackTop_ += sizeof(T) * count;
  115. return ret;
  116. }
  117. template<typename T>
  118. T* Pop(size_t count) {
  119. RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
  120. stackTop_ -= count * sizeof(T);
  121. return reinterpret_cast<T*>(stackTop_);
  122. }
  123. template<typename T>
  124. T* Top() {
  125. RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
  126. return reinterpret_cast<T*>(stackTop_ - sizeof(T));
  127. }
  128. template<typename T>
  129. const T* Top() const {
  130. RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
  131. return reinterpret_cast<T*>(stackTop_ - sizeof(T));
  132. }
  133. template<typename T>
  134. T* End() { return reinterpret_cast<T*>(stackTop_); }
  135. template<typename T>
  136. const T* End() const { return reinterpret_cast<T*>(stackTop_); }
  137. template<typename T>
  138. T* Bottom() { return reinterpret_cast<T*>(stack_); }
  139. template<typename T>
  140. const T* Bottom() const { return reinterpret_cast<T*>(stack_); }
  141. bool HasAllocator() const {
  142. return allocator_ != 0;
  143. }
  144. Allocator& GetAllocator() {
  145. RAPIDJSON_ASSERT(allocator_);
  146. return *allocator_;
  147. }
  148. bool Empty() const { return stackTop_ == stack_; }
  149. size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); }
  150. size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); }
  151. private:
  152. template<typename T>
  153. void Expand(size_t count) {
  154. // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
  155. size_t newCapacity;
  156. if (stack_ == 0) {
  157. if (!allocator_)
  158. ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
  159. newCapacity = initialCapacity_;
  160. } else {
  161. newCapacity = GetCapacity();
  162. newCapacity += (newCapacity + 1) / 2;
  163. }
  164. size_t newSize = GetSize() + sizeof(T) * count;
  165. if (newCapacity < newSize)
  166. newCapacity = newSize;
  167. Resize(newCapacity);
  168. }
  169. void Resize(size_t newCapacity) {
  170. const size_t size = GetSize(); // Backup the current size
  171. stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
  172. stackTop_ = stack_ + size;
  173. stackEnd_ = stack_ + newCapacity;
  174. }
  175. void Destroy() {
  176. Allocator::Free(stack_);
  177. RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
  178. }
  179. // Prohibit copy constructor & assignment operator.
  180. Stack(const Stack&);
  181. Stack& operator=(const Stack&);
  182. Allocator* allocator_;
  183. Allocator* ownAllocator_;
  184. char *stack_;
  185. char *stackTop_;
  186. char *stackEnd_;
  187. size_t initialCapacity_;
  188. };
  189. } // namespace internal
  190. RAPIDJSON_NAMESPACE_END
  191. #if defined(__clang__)
  192. RAPIDJSON_DIAG_POP
  193. #endif
  194. #endif // RAPIDJSON_STACK_H_