2
0

stack.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "../rapidjson.h"
  17. #include "swap.h"
  18. RAPIDJSON_NAMESPACE_BEGIN
  19. namespace internal {
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // Stack
  22. //! A type-unsafe stack for storing different types of data.
  23. /*! \tparam Allocator Allocator for allocating stack memory.
  24. */
  25. template <typename Allocator>
  26. class Stack {
  27. public:
  28. // Optimization note: Do not allocate memory for stack_ in constructor.
  29. // Do it lazily when first Push() -> Expand() -> Resize().
  30. Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
  31. RAPIDJSON_ASSERT(stackCapacity > 0);
  32. }
  33. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  34. Stack(Stack&& rhs)
  35. : allocator_(rhs.allocator_),
  36. ownAllocator_(rhs.ownAllocator_),
  37. stack_(rhs.stack_),
  38. stackTop_(rhs.stackTop_),
  39. stackEnd_(rhs.stackEnd_),
  40. initialCapacity_(rhs.initialCapacity_)
  41. {
  42. rhs.allocator_ = 0;
  43. rhs.ownAllocator_ = 0;
  44. rhs.stack_ = 0;
  45. rhs.stackTop_ = 0;
  46. rhs.stackEnd_ = 0;
  47. rhs.initialCapacity_ = 0;
  48. }
  49. #endif
  50. ~Stack() {
  51. Destroy();
  52. }
  53. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  54. Stack& operator=(Stack&& rhs) {
  55. if (&rhs != this)
  56. {
  57. Destroy();
  58. allocator_ = rhs.allocator_;
  59. ownAllocator_ = rhs.ownAllocator_;
  60. stack_ = rhs.stack_;
  61. stackTop_ = rhs.stackTop_;
  62. stackEnd_ = rhs.stackEnd_;
  63. initialCapacity_ = rhs.initialCapacity_;
  64. rhs.allocator_ = 0;
  65. rhs.ownAllocator_ = 0;
  66. rhs.stack_ = 0;
  67. rhs.stackTop_ = 0;
  68. rhs.stackEnd_ = 0;
  69. rhs.initialCapacity_ = 0;
  70. }
  71. return *this;
  72. }
  73. #endif
  74. void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT {
  75. internal::Swap(allocator_, rhs.allocator_);
  76. internal::Swap(ownAllocator_, rhs.ownAllocator_);
  77. internal::Swap(stack_, rhs.stack_);
  78. internal::Swap(stackTop_, rhs.stackTop_);
  79. internal::Swap(stackEnd_, rhs.stackEnd_);
  80. internal::Swap(initialCapacity_, rhs.initialCapacity_);
  81. }
  82. void Clear() { stackTop_ = stack_; }
  83. void ShrinkToFit() {
  84. if (Empty()) {
  85. // If the stack is empty, completely deallocate the memory.
  86. Allocator::Free(stack_);
  87. stack_ = 0;
  88. stackTop_ = 0;
  89. stackEnd_ = 0;
  90. }
  91. else
  92. Resize(GetSize());
  93. }
  94. // Optimization note: try to minimize the size of this function for force inline.
  95. // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
  96. template<typename T>
  97. RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
  98. // Expand the stack if needed
  99. if (stackTop_ + sizeof(T) * count >= stackEnd_)
  100. Expand<T>(count);
  101. T* ret = reinterpret_cast<T*>(stackTop_);
  102. stackTop_ += sizeof(T) * count;
  103. return ret;
  104. }
  105. template<typename T>
  106. T* Pop(size_t count) {
  107. RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
  108. stackTop_ -= count * sizeof(T);
  109. return reinterpret_cast<T*>(stackTop_);
  110. }
  111. template<typename T>
  112. T* Top() {
  113. RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
  114. return reinterpret_cast<T*>(stackTop_ - sizeof(T));
  115. }
  116. template<typename T>
  117. T* Bottom() { return (T*)stack_; }
  118. bool HasAllocator() const {
  119. return allocator_ != 0;
  120. }
  121. Allocator& GetAllocator() {
  122. RAPIDJSON_ASSERT(allocator_);
  123. return *allocator_;
  124. }
  125. bool Empty() const { return stackTop_ == stack_; }
  126. size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); }
  127. size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); }
  128. private:
  129. template<typename T>
  130. void Expand(size_t count) {
  131. // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
  132. size_t newCapacity;
  133. if (stack_ == 0) {
  134. if (!allocator_)
  135. ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
  136. newCapacity = initialCapacity_;
  137. } else {
  138. newCapacity = GetCapacity();
  139. newCapacity += (newCapacity + 1) / 2;
  140. }
  141. size_t newSize = GetSize() + sizeof(T) * count;
  142. if (newCapacity < newSize)
  143. newCapacity = newSize;
  144. Resize(newCapacity);
  145. }
  146. void Resize(size_t newCapacity) {
  147. const size_t size = GetSize(); // Backup the current size
  148. stack_ = (char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity);
  149. stackTop_ = stack_ + size;
  150. stackEnd_ = stack_ + newCapacity;
  151. }
  152. void Destroy() {
  153. Allocator::Free(stack_);
  154. RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
  155. }
  156. // Prohibit copy constructor & assignment operator.
  157. Stack(const Stack&);
  158. Stack& operator=(const Stack&);
  159. Allocator* allocator_;
  160. Allocator* ownAllocator_;
  161. char *stack_;
  162. char *stackTop_;
  163. char *stackEnd_;
  164. size_t initialCapacity_;
  165. };
  166. } // namespace internal
  167. RAPIDJSON_NAMESPACE_END
  168. #endif // RAPIDJSON_STACK_H_