stack.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef RAPIDJSON_INTERNAL_STACK_H_
27 #define RAPIDJSON_INTERNAL_STACK_H_
28 
29 namespace rapidjson {
30 namespace internal {
31 
33 // Stack
34 
36 
38 template <typename Allocator>
39 class Stack {
40 public:
41  Stack(Allocator* allocator, size_t stack_capacity) : allocator_(allocator), own_allocator_(0), stack_(0), stack_top_(0), stack_end_(0), stack_capacity_(stack_capacity) {
43  if (!allocator_)
45  stack_top_ = stack_ = (char*)allocator_->Malloc(stack_capacity_);
47  }
48 
49  ~Stack() {
50  Allocator::Free(stack_);
51  delete own_allocator_; // Only delete if it is owned by the stack
52  }
53 
54  void Clear() { /*stack_top_ = 0;*/ stack_top_ = stack_; }
55 
56  template<typename T>
57  T* Push(size_t count = 1) {
58  // Expand the stack if needed
59  if (stack_top_ + sizeof(T) * count >= stack_end_) {
60  size_t new_capacity = stack_capacity_ * 2;
61  size_t size = GetSize();
62  size_t new_size = GetSize() + sizeof(T) * count;
63  if (new_capacity < new_size)
64  new_capacity = new_size;
65  stack_ = (char*)allocator_->Realloc(stack_, stack_capacity_, new_capacity);
66  stack_capacity_ = new_capacity;
67  stack_top_ = stack_ + size;
69  }
70  T* ret = (T*)stack_top_;
71  stack_top_ += sizeof(T) * count;
72  return ret;
73  }
74 
75  template<typename T>
76  T* Pop(size_t count) {
77  RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
78  stack_top_ -= count * sizeof(T);
79  return (T*)stack_top_;
80  }
81 
82  template<typename T>
83  T* Top() {
84  RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
85  return (T*)(stack_top_ - sizeof(T));
86  }
87 
88  template<typename T>
89  T* Bottom() { return (T*)stack_; }
90 
92  size_t GetSize() const { return stack_top_ - stack_; }
93  size_t GetCapacity() const { return stack_capacity_; }
94 
95 private:
98  char *stack_;
99  char *stack_top_;
100  char *stack_end_;
102 };
103 
104 } // namespace internal
105 } // namespace rapidjson
106 
107 #endif // RAPIDJSON_STACK_H_
Concept for allocating, resizing and freeing memory block.
A type-unsafe stack for storing different types of data.
Definition: stack.h:39
T * Bottom()
Definition: stack.h:89
char * stack_
Definition: stack.h:98
~Stack()
Definition: stack.h:49
size_t GetCapacity() const
Definition: stack.h:93
Stack(Allocator *allocator, size_t stack_capacity)
Definition: stack.h:41
void Clear()
Definition: stack.h:54
T * Pop(size_t count)
Definition: stack.h:76
T * Push(size_t count=1)
Definition: stack.h:57
Allocator * own_allocator_
Definition: stack.h:97
size_t GetSize() const
Definition: stack.h:92
size_t stack_capacity_
Definition: stack.h:101
char * stack_top_
Definition: stack.h:99
T * Top()
Definition: stack.h:83
char * stack_end_
Definition: stack.h:100
Allocator & GetAllocator()
Definition: stack.h:91
Allocator * allocator_
Definition: stack.h:96
Definition: document.h:38
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:105