1 #ifndef RAPIDJSON_INTERNAL_STACK_H_
2 #define RAPIDJSON_INTERNAL_STACK_H_
13 template <
typename Allocator>
16 Stack(
Allocator* allocator,
size_t stack_capacity) : allocator_(allocator), own_allocator_(0), stack_(0), stack_top_(0), stack_end_(0), stack_capacity_(stack_capacity) {
19 own_allocator_ = allocator_ =
new Allocator();
20 stack_top_ = stack_ = (
char*)allocator_->Malloc(stack_capacity_);
21 stack_end_ = stack_ + stack_capacity_;
25 Allocator::Free(stack_);
26 delete own_allocator_;
29 void Clear() { stack_top_ = stack_; }
32 T*
Push(
size_t count = 1) {
34 if (stack_top_ +
sizeof(T) * count >= stack_end_) {
35 size_t new_capacity = stack_capacity_ * 2;
37 size_t new_size =
GetSize() +
sizeof(T) * count;
38 if (new_capacity < new_size)
39 new_capacity = new_size;
40 stack_ = (
char*)allocator_->Realloc(stack_, stack_capacity_, new_capacity);
41 stack_capacity_ = new_capacity;
42 stack_top_ = stack_ + size;
43 stack_end_ = stack_ + stack_capacity_;
45 T* ret = (T*)stack_top_;
46 stack_top_ +=
sizeof(T) * count;
51 T*
Pop(
size_t count) {
53 stack_top_ -= count *
sizeof(T);
54 return (T*)stack_top_;
60 return (T*)(stack_top_ -
sizeof(T));
67 size_t GetSize()
const {
return stack_top_ - stack_; }
76 size_t stack_capacity_;
82 #endif // RAPIDJSON_STACK_H_
size_t GetCapacity() const
A type-unsafe stack for storing different types of data.
#define RAPIDJSON_ASSERT(x)
Assertion.
Stack(Allocator *allocator, size_t stack_capacity)
Concept for allocating, resizing and freeing memory block.
Allocator & GetAllocator()