stack.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_);
  113. RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_);
  114. T* ret = reinterpret_cast<T*>(stackTop_);
  115. stackTop_ += sizeof(T) * count;
  116. return ret;
  117. }
  118. template<typename T>
  119. T* Pop(size_t count) {
  120. RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
  121. stackTop_ -= count * sizeof(T);
  122. return reinterpret_cast<T*>(stackTop_);
  123. }
  124. template<typename T>
  125. T* Top() {
  126. RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
  127. return reinterpret_cast<T*>(stackTop_ - sizeof(T));
  128. }
  129. template<typename T>
  130. const T* Top() const {
  131. RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
  132. return reinterpret_cast<T*>(stackTop_ - sizeof(T));
  133. }
  134. template<typename T>
  135. T* End() { return reinterpret_cast<T*>(stackTop_); }
  136. template<typename T>
  137. const T* End() const { return reinterpret_cast<T*>(stackTop_); }
  138. template<typename T>
  139. T* Bottom() { return reinterpret_cast<T*>(stack_); }
  140. template<typename T>
  141. const T* Bottom() const { return reinterpret_cast<T*>(stack_); }
  142. bool HasAllocator() const {
  143. return allocator_ != 0;
  144. }
  145. Allocator& GetAllocator() {
  146. RAPIDJSON_ASSERT(allocator_);
  147. return *allocator_;
  148. }
  149. bool Empty() const { return stackTop_ == stack_; }
  150. size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); }
  151. size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); }
  152. private:
  153. template<typename T>
  154. void Expand(size_t count) {
  155. // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
  156. size_t newCapacity;
  157. if (stack_ == 0) {
  158. if (!allocator_)
  159. ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
  160. newCapacity = initialCapacity_;
  161. } else {
  162. newCapacity = GetCapacity();
  163. newCapacity += (newCapacity + 1) / 2;
  164. }
  165. size_t newSize = GetSize() + sizeof(T) * count;
  166. if (newCapacity < newSize)
  167. newCapacity = newSize;
  168. Resize(newCapacity);
  169. }
  170. void Resize(size_t newCapacity) {
  171. const size_t size = GetSize(); // Backup the current size
  172. stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
  173. stackTop_ = stack_ + size;
  174. stackEnd_ = stack_ + newCapacity;
  175. }
  176. void Destroy() {
  177. Allocator::Free(stack_);
  178. RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
  179. }
  180. // Prohibit copy constructor & assignment operator.
  181. Stack(const Stack&);
  182. Stack& operator=(const Stack&);
  183. Allocator* allocator_;
  184. Allocator* ownAllocator_;
  185. char *stack_;
  186. char *stackTop_;
  187. char *stackEnd_;
  188. size_t initialCapacity_;
  189. };
  190. } // namespace internal
  191. RAPIDJSON_NAMESPACE_END
  192. #if defined(__clang__)
  193. RAPIDJSON_DIAG_POP
  194. #endif
  195. #endif // RAPIDJSON_STACK_H_