21 #ifndef RAPIDJSON_INTERNAL_STACK_H_
22 #define RAPIDJSON_INTERNAL_STACK_H_
33 template <
typename Allocator>
36 Stack(Allocator* allocator,
size_t stack_capacity) : allocator_(allocator), own_allocator_(0), stack_(0), stack_top_(0), stack_end_(0), stack_capacity_(stack_capacity) {
39 own_allocator_ = allocator_ =
new Allocator();
40 stack_top_ = stack_ = (
char*)allocator_->Malloc(stack_capacity_);
41 stack_end_ = stack_ + stack_capacity_;
45 Allocator::Free(stack_);
46 delete own_allocator_;
49 void Clear() { stack_top_ = stack_; }
54 RAPIDJSON_FORCEINLINE T* Push(
size_t count = 1) {
56 if (stack_top_ +
sizeof(T) * count >= stack_end_)
59 T* ret =
reinterpret_cast<T*
>(stack_top_);
60 stack_top_ +=
sizeof(T) * count;
65 T* Pop(
size_t count) {
67 stack_top_ -= count *
sizeof(T);
68 return reinterpret_cast<T*
>(stack_top_);
74 return reinterpret_cast<T*
>(stack_top_ -
sizeof(T));
78 T* Bottom() {
return (T*)stack_; }
80 Allocator& GetAllocator() {
return *allocator_; }
81 bool Empty()
const {
return stack_top_ == stack_; }
82 size_t GetSize()
const {
return static_cast<size_t>(stack_top_ - stack_); }
83 size_t GetCapacity()
const {
return stack_capacity_; }
87 void Expand(
size_t count) {
88 size_t new_capacity = stack_capacity_ * 2;
89 size_t size = GetSize();
90 size_t new_size = GetSize() +
sizeof(T) * count;
91 if (new_capacity < new_size)
92 new_capacity = new_size;
93 stack_ = (
char*)allocator_->Realloc(stack_, stack_capacity_, new_capacity);
94 stack_capacity_ = new_capacity;
95 stack_top_ = stack_ + size;
96 stack_end_ = stack_ + stack_capacity_;
101 Stack& operator=(
const Stack&);
103 Allocator* allocator_;
104 Allocator* own_allocator_;
108 size_t stack_capacity_;
114 #endif // RAPIDJSON_STACK_H_
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:175