stack.h
Go to the documentation of this file.
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 
15 #ifndef RAPIDJSON_INTERNAL_STACK_H_
16 #define RAPIDJSON_INTERNAL_STACK_H_
17 
18 #include "../allocators.h"
19 #include "swap.h"
20 #include <cstddef>
21 
22 #if defined(__clang__)
23 RAPIDJSON_DIAG_PUSH
24 RAPIDJSON_DIAG_OFF(c++ 98 - compat)
25 #endif
26 
28 namespace internal
29 {
31 // Stack
32 
34 
36 template <typename Allocator>
37 class Stack
38 {
39 public:
40  // Optimization note: Do not allocate memory for stack_ in constructor.
41  // Do it lazily when first Push() -> Expand() -> Resize().
42  Stack(Allocator* allocator, size_t stackCapacity)
43  : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity)
44  {
45  }
46 
47 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
48  Stack(Stack&& rhs)
49  : allocator_(rhs.allocator_)
50  , ownAllocator_(rhs.ownAllocator_)
51  , stack_(rhs.stack_)
52  , stackTop_(rhs.stackTop_)
53  , stackEnd_(rhs.stackEnd_)
54  , initialCapacity_(rhs.initialCapacity_)
55  {
56  rhs.allocator_ = 0;
57  rhs.ownAllocator_ = 0;
58  rhs.stack_ = 0;
59  rhs.stackTop_ = 0;
60  rhs.stackEnd_ = 0;
61  rhs.initialCapacity_ = 0;
62  }
63 #endif
64 
66  {
67  Destroy();
68  }
69 
70 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
71  Stack& operator=(Stack&& rhs)
72  {
73  if (&rhs != this)
74  {
75  Destroy();
76 
77  allocator_ = rhs.allocator_;
78  ownAllocator_ = rhs.ownAllocator_;
79  stack_ = rhs.stack_;
80  stackTop_ = rhs.stackTop_;
81  stackEnd_ = rhs.stackEnd_;
82  initialCapacity_ = rhs.initialCapacity_;
83 
84  rhs.allocator_ = 0;
85  rhs.ownAllocator_ = 0;
86  rhs.stack_ = 0;
87  rhs.stackTop_ = 0;
88  rhs.stackEnd_ = 0;
89  rhs.initialCapacity_ = 0;
90  }
91  return *this;
92  }
93 #endif
94 
95  void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT
96  {
97  internal::Swap(allocator_, rhs.allocator_);
98  internal::Swap(ownAllocator_, rhs.ownAllocator_);
99  internal::Swap(stack_, rhs.stack_);
100  internal::Swap(stackTop_, rhs.stackTop_);
101  internal::Swap(stackEnd_, rhs.stackEnd_);
102  internal::Swap(initialCapacity_, rhs.initialCapacity_);
103  }
104 
105  void Clear()
106  {
107  stackTop_ = stack_;
108  }
109 
110  void ShrinkToFit()
111  {
112  if (Empty())
113  {
114  // If the stack is empty, completely deallocate the memory.
115  Allocator::Free(stack_); // NOLINT (+clang-analyzer-unix.Malloc)
116  stack_ = 0;
117  stackTop_ = 0;
118  stackEnd_ = 0;
119  }
120  else
121  Resize(GetSize());
122  }
123 
124  // Optimization note: try to minimize the size of this function for force inline.
125  // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
126  template <typename T>
127  RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1)
128  {
129  // Expand the stack if needed
130  if (RAPIDJSON_UNLIKELY(static_cast<std::ptrdiff_t>(sizeof(T) * count) > (stackEnd_ - stackTop_)))
131  Expand<T>(count);
132  }
133 
134  template <typename T>
135  RAPIDJSON_FORCEINLINE T* Push(size_t count = 1)
136  {
137  Reserve<T>(count);
138  return PushUnsafe<T>(count);
139  }
140 
141  template <typename T>
142  RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1)
143  {
145  RAPIDJSON_ASSERT(static_cast<std::ptrdiff_t>(sizeof(T) * count) <= (stackEnd_ - stackTop_));
146  T* ret = reinterpret_cast<T*>(stackTop_);
147  stackTop_ += sizeof(T) * count;
148  return ret;
149  }
150 
151  template <typename T>
152  T* Pop(size_t count)
153  {
154  RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
155  stackTop_ -= count * sizeof(T);
156  return reinterpret_cast<T*>(stackTop_);
157  }
158 
159  template <typename T>
160  T* Top()
161  {
162  RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
163  return reinterpret_cast<T*>(stackTop_ - sizeof(T));
164  }
165 
166  template <typename T>
167  const T* Top() const
168  {
169  RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
170  return reinterpret_cast<T*>(stackTop_ - sizeof(T));
171  }
172 
173  template <typename T>
174  T* End()
175  {
176  return reinterpret_cast<T*>(stackTop_);
177  }
178 
179  template <typename T>
180  const T* End() const
181  {
182  return reinterpret_cast<T*>(stackTop_);
183  }
184 
185  template <typename T>
186  T* Bottom()
187  {
188  return reinterpret_cast<T*>(stack_);
189  }
190 
191  template <typename T>
192  const T* Bottom() const
193  {
194  return reinterpret_cast<T*>(stack_);
195  }
196 
197  bool HasAllocator() const
198  {
199  return allocator_ != 0;
200  }
201 
202  Allocator& GetAllocator()
203  {
205  return *allocator_;
206  }
207 
208  bool Empty() const
209  {
210  return stackTop_ == stack_;
211  }
212  size_t GetSize() const
213  {
214  return static_cast<size_t>(stackTop_ - stack_);
215  }
216  size_t GetCapacity() const
217  {
218  return static_cast<size_t>(stackEnd_ - stack_);
219  }
220 
221 private:
222  template <typename T>
223  void Expand(size_t count)
224  {
225  // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
226  size_t newCapacity;
227  if (stack_ == 0)
228  {
229  if (!allocator_)
230  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
231  newCapacity = initialCapacity_;
232  }
233  else
234  {
235  newCapacity = GetCapacity();
236  newCapacity += (newCapacity + 1) / 2;
237  }
238  size_t newSize = GetSize() + sizeof(T) * count;
239  if (newCapacity < newSize)
240  newCapacity = newSize;
241 
242  Resize(newCapacity);
243  }
244 
245  void Resize(size_t newCapacity)
246  {
247  const size_t size = GetSize(); // Backup the current size
248  stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
249  stackTop_ = stack_ + size;
250  stackEnd_ = stack_ + newCapacity;
251  }
252 
253  void Destroy()
254  {
255  Allocator::Free(stack_);
256  RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
257  }
258 
259  // Prohibit copy constructor & assignment operator.
260  Stack(const Stack&);
261  Stack& operator=(const Stack&);
262 
263  Allocator* allocator_;
264  Allocator* ownAllocator_;
265  char* stack_;
266  char* stackTop_;
267  char* stackEnd_;
269 };
270 
271 } // namespace internal
273 
274 #if defined(__clang__)
275 RAPIDJSON_DIAG_POP
276 #endif
277 
278 #endif // RAPIDJSON_STACK_H_
char * stack_
Definition: stack.h:265
Allocator * ownAllocator_
Definition: stack.h:264
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:416
RAPIDJSON_FORCEINLINE T * PushUnsafe(size_t count=1)
Definition: stack.h:142
void Expand(size_t count)
Definition: stack.h:223
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
const T * Top() const
Definition: stack.h:167
void Resize(size_t newCapacity)
Definition: stack.h:245
A type-unsafe stack for storing different types of data.
Definition: stack.h:37
bool Empty() const
Definition: stack.h:208
void Clear()
Definition: stack.h:105
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
Stack & operator=(const Stack &)
RAPIDJSON_FORCEINLINE T * Push(size_t count=1)
Definition: stack.h:135
void Destroy()
Definition: stack.h:253
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:649
char * stackEnd_
Definition: stack.h:267
T * End()
Definition: stack.h:174
RAPIDJSON_FORCEINLINE void Reserve(size_t count=1)
Definition: stack.h:127
size_t GetSize() const
Definition: stack.h:212
size_t initialCapacity_
Definition: stack.h:268
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
Definition: swap.h:33
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:653
Allocator * allocator_
Definition: stack.h:263
Allocator & GetAllocator()
Definition: stack.h:202
T * Bottom()
Definition: stack.h:186
bool HasAllocator() const
Definition: stack.h:197
char * stackTop_
Definition: stack.h:266
void ShrinkToFit()
Definition: stack.h:110
size_t GetCapacity() const
Definition: stack.h:216
Stack(Allocator *allocator, size_t stackCapacity)
Definition: stack.h:42
const T * End() const
Definition: stack.h:180
T * Pop(size_t count)
Definition: stack.h:152
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:495
T * Top()
Definition: stack.h:160
void Swap(Stack &rhs) RAPIDJSON_NOEXCEPT
Definition: stack.h:95
const T * Bottom() const
Definition: stack.h:192


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:54