 |
Blamite Game Engine - blam!
00398.09.22.23.2015.blamite
The core library for the Blamite Game Engine.
|
Go to the documentation of this file.
15 #ifndef RAPIDJSON_ALLOCATORS_H_
16 #define RAPIDJSON_ALLOCATORS_H_
23 #if RAPIDJSON_HAS_CXX11
24 #include <type_traits>
70 #ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
71 #define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024)
91 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
128 template <
typename BaseAllocator = CrtAllocator>
140 ChunkHeader *chunkHead;
141 BaseAllocator* ownBaseAllocator;
146 static const size_t SIZEOF_SHARED_DATA =
RAPIDJSON_ALIGN(
sizeof(SharedData));
147 static const size_t SIZEOF_CHUNK_HEADER =
RAPIDJSON_ALIGN(
sizeof(ChunkHeader));
149 static inline ChunkHeader *GetChunkHead(SharedData *shared)
151 return reinterpret_cast<ChunkHeader*
>(
reinterpret_cast<uint8_t*
>(shared) + SIZEOF_SHARED_DATA);
153 static inline uint8_t *GetChunkBuffer(SharedData *shared)
155 return reinterpret_cast<uint8_t*
>(shared->chunkHead) + SIZEOF_CHUNK_HEADER;
170 chunk_capacity_(chunkSize),
171 baseAllocator_(baseAllocator ? baseAllocator :
RAPIDJSON_NEW(BaseAllocator)()),
172 shared_(static_cast<SharedData*>(baseAllocator_ ? baseAllocator_->
Malloc(SIZEOF_SHARED_DATA + SIZEOF_CHUNK_HEADER) : 0))
177 shared_->ownBaseAllocator = 0;
180 shared_->ownBaseAllocator = baseAllocator_;
182 shared_->chunkHead = GetChunkHead(shared_);
183 shared_->chunkHead->capacity = 0;
184 shared_->chunkHead->size = 0;
185 shared_->chunkHead->next = 0;
186 shared_->ownBuffer =
true;
187 shared_->refcount = 1;
200 MemoryPoolAllocator(
void *buffer,
size_t size,
size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
201 chunk_capacity_(chunkSize),
202 baseAllocator_(baseAllocator),
203 shared_(static_cast<SharedData*>(AlignBuffer(buffer, size)))
206 shared_->chunkHead = GetChunkHead(shared_);
207 shared_->chunkHead->capacity = size - SIZEOF_SHARED_DATA - SIZEOF_CHUNK_HEADER;
208 shared_->chunkHead->size = 0;
209 shared_->chunkHead->next = 0;
210 shared_->ownBaseAllocator = 0;
211 shared_->ownBuffer =
false;
212 shared_->refcount = 1;
216 chunk_capacity_(rhs.chunk_capacity_),
217 baseAllocator_(rhs.baseAllocator_),
226 ++rhs.shared_->refcount;
228 baseAllocator_ = rhs.baseAllocator_;
229 chunk_capacity_ = rhs.chunk_capacity_;
230 shared_ = rhs.shared_;
234 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
236 chunk_capacity_(rhs.chunk_capacity_),
237 baseAllocator_(rhs.baseAllocator_),
247 baseAllocator_ = rhs.baseAllocator_;
248 chunk_capacity_ = rhs.chunk_capacity_;
249 shared_ = rhs.shared_;
263 if (shared_->refcount > 1) {
268 BaseAllocator *
a = shared_->ownBaseAllocator;
269 if (shared_->ownBuffer) {
270 baseAllocator_->Free(shared_);
279 ChunkHeader* c = shared_->chunkHead;
283 shared_->chunkHead = c->next;
284 baseAllocator_->Free(c);
286 shared_->chunkHead->size = 0;
295 for (ChunkHeader* c = shared_->chunkHead; c != 0; c = c->next)
296 capacity += c->capacity;
303 size_t Size() const RAPIDJSON_NOEXCEPT {
306 for (ChunkHeader* c = shared_->chunkHead; c != 0; c = c->next)
316 return shared_->refcount > 1;
326 if (
RAPIDJSON_UNLIKELY(shared_->chunkHead->size + size > shared_->chunkHead->capacity))
327 if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
330 void *buffer = GetChunkBuffer(shared_) + shared_->chunkHead->size;
331 shared_->chunkHead->size += size;
336 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
337 if (originalPtr == 0)
348 if (originalSize >= newSize)
352 if (originalPtr == GetChunkBuffer(shared_) + shared_->chunkHead->size - originalSize) {
353 size_t increment =
static_cast<size_t>(newSize - originalSize);
354 if (shared_->chunkHead->size + increment <= shared_->chunkHead->capacity) {
355 shared_->chunkHead->size += increment;
361 if (
void* newBuffer =
Malloc(newSize)) {
363 std::memcpy(newBuffer, originalPtr, originalSize);
371 static void Free(
void *ptr) RAPIDJSON_NOEXCEPT { (void)ptr; }
377 return shared_ == rhs.shared_;
389 bool AddChunk(
size_t capacity) {
391 shared_->ownBaseAllocator = baseAllocator_ =
RAPIDJSON_NEW(BaseAllocator)();
392 if (ChunkHeader* chunk =
static_cast<ChunkHeader*
>(baseAllocator_->Malloc(SIZEOF_CHUNK_HEADER + capacity))) {
393 chunk->capacity = capacity;
395 chunk->next = shared_->chunkHead;
396 shared_->chunkHead = chunk;
403 static inline void* AlignBuffer(
void* buf,
size_t &size)
406 const uintptr_t mask =
sizeof(
void*) - 1;
409 const uintptr_t abuf = (ubuf + mask) & ~mask;
411 buf =
reinterpret_cast<void*
>(abuf);
417 size_t chunk_capacity_;
418 BaseAllocator* baseAllocator_;
423 template<
typename,
typename =
void>
433 template<
typename T,
typename A>
434 inline T*
Realloc(A&
a, T* old_p,
size_t old_n,
size_t new_n)
437 return static_cast<T*
>(
a.Realloc(old_p, old_n *
sizeof(T), new_n *
sizeof(T)));
440 template<
typename T,
typename A>
443 return Realloc<T, A>(
a,
NULL, 0, n);
446 template<
typename T,
typename A>
447 inline void Free(A&
a, T *p,
size_t n = 1)
449 static_cast<void>(Realloc<T, A>(
a, p, n, 0));
454 RAPIDJSON_DIAG_OFF(effc++)
457 template <
typename T,
typename BaseAllocator = CrtAllocator>
461 typedef std::allocator<T> allocator_type;
462 #if RAPIDJSON_HAS_CXX11
463 typedef std::allocator_traits<allocator_type> traits_type;
465 typedef allocator_type traits_type;
478 baseAllocator_(rhs.baseAllocator_)
484 baseAllocator_(rhs.baseAllocator_)
487 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
489 allocator_type(std::move(rhs)),
490 baseAllocator_(std::move(rhs.baseAllocator_))
493 #if RAPIDJSON_HAS_CXX11
494 using propagate_on_container_move_assignment = std::true_type;
495 using propagate_on_container_swap = std::true_type;
519 #if RAPIDJSON_HAS_CXX11
521 typedef typename std::add_lvalue_reference<value_type>::type &
reference;
522 typedef typename std::add_lvalue_reference<typename std::add_const<value_type>::type>::type &
const_reference;
526 return std::addressof(r);
530 return std::addressof(r);
535 return traits_type::max_size(*
this);
538 template <
typename ...Args>
541 traits_type::construct(*
this, p, std::forward<Args>(args)...);
545 traits_type::destroy(*
this, p);
548 #else // !RAPIDJSON_HAS_CXX11
555 return allocator_type::address(r);
559 return allocator_type::address(r);
564 return allocator_type::max_size();
569 allocator_type::construct(p, r);
573 allocator_type::destroy(p);
576 #endif // !RAPIDJSON_HAS_CXX11
578 template <
typename U>
581 return RAPIDJSON_NAMESPACE::Malloc<U>(baseAllocator_, n);
583 template <
typename U>
586 RAPIDJSON_NAMESPACE::Free<U>(baseAllocator_, p, n);
591 return allocate<value_type>(n);
595 deallocate<value_type>(p, n);
598 #if RAPIDJSON_HAS_CXX11
599 using is_always_equal = std::is_empty<BaseAllocator>;
605 return baseAllocator_ == rhs.baseAllocator_;
618 return baseAllocator_.Malloc(size);
620 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize)
622 return baseAllocator_.Realloc(originalPtr, originalSize, newSize);
624 static void Free(
void *ptr) RAPIDJSON_NOEXCEPT
630 template <
typename,
typename>
633 BaseAllocator baseAllocator_;
636 #if !RAPIDJSON_HAS_CXX17 // std::allocator<void> deprecated in C++17
637 template <
typename BaseAllocator>
641 typedef std::allocator<void> allocator_type;
653 baseAllocator_(rhs.baseAllocator_)
659 baseAllocator_(rhs.baseAllocator_)
665 baseAllocator_(baseAllocator)
679 template <
typename,
typename>
682 BaseAllocator baseAllocator_;
692 #endif // RAPIDJSON_ENCODINGS_H_
static const bool kNeedFree
Tell users that no need to call Free() with this allocator. (concept Allocator)
Definition: allocators.h:161
~MemoryPoolAllocator() RAPIDJSON_NOEXCEPT
Destructor.
Definition: allocators.h:258
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
#define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
User-defined kDefaultChunkCapacity definition.
Definition: allocators.h:71
allocator_type::reference reference
Definition: allocators.h:550
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
StdAllocator(const StdAllocator &rhs) RAPIDJSON_NOEXCEPT
Definition: allocators.h:651
StdAllocator() RAPIDJSON_NOEXCEPT
Definition: allocators.h:646
static const bool kRefCounted
Definition: allocators.h:615
bool operator==(const CrtAllocator &) const RAPIDJSON_NOEXCEPT
Definition: allocators.h:101
StdAllocator() RAPIDJSON_NOEXCEPT
Definition: allocators.h:471
pointer address(reference r) const RAPIDJSON_NOEXCEPT
Definition: allocators.h:553
StdAllocator(const BaseAllocator &allocator) RAPIDJSON_NOEXCEPT
Definition: allocators.h:499
Type
Type of JSON value.
Definition: rapidjson.h:729
#define SIZE_MAX
Definition: stdint.h:117
static void Free(void *ptr) RAPIDJSON_NOEXCEPT
Frees a memory block (concept Allocator)
Definition: allocators.h:371
common definitions and configuration
const_pointer address(const_reference r) const RAPIDJSON_NOEXCEPT
Definition: allocators.h:557
static void Free(void *ptr) RAPIDJSON_NOEXCEPT
Definition: allocators.h:624
static const bool kRefCounted
Tell users that this allocator is reference counted on copy.
Definition: allocators.h:162
~StdAllocator() RAPIDJSON_NOEXCEPT
Definition: allocators.h:668
unsigned char uint8_t
Definition: stdint.h:15
bool operator==(const StdAllocator< U, BaseAllocator > &rhs) const RAPIDJSON_NOEXCEPT
Definition: allocators.h:603
T * Realloc(A &a, T *old_p, size_t old_n, size_t new_n)
Definition: allocators.h:434
traits_type::size_type size_type
Definition: allocators.h:512
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
Definition: allocators.h:169
StdAllocator< U, BaseAllocator > other
Definition: allocators.h:509
U * allocate(size_type n=1, const void *=0)
Definition: allocators.h:579
Blam::LinearAllocator allocator
– TO BE FILLED IN BY VERTIGO –
Definition: main.cpp:76
static const bool kNeedFree
Definition: allocators.h:84
void deallocate(pointer p, size_type n=1)
Definition: allocators.h:593
bool operator!=(const StdAllocator< U, BaseAllocator > &rhs) const RAPIDJSON_NOEXCEPT
Definition: allocators.h:608
MemoryPoolAllocator & operator=(const MemoryPoolAllocator &rhs) RAPIDJSON_NOEXCEPT
Definition: allocators.h:223
#define RAPIDJSON_REALLOC(ptr, new_size)
! customization point for global realloc
Definition: rapidjson.h:700
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:437
Definition: allocators.h:508
friend class StdAllocator
Definition: allocators.h:631
Definition: allocators.h:458
size_t Capacity() const RAPIDJSON_NOEXCEPT
Computes the total capacity of allocated memory chunks.
Definition: allocators.h:292
StdAllocator(const StdAllocator &rhs) RAPIDJSON_NOEXCEPT
Definition: allocators.h:476
allocator_type::value_type value_type
Definition: allocators.h:676
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1249
StdAllocator(const StdAllocator< U, BaseAllocator > &rhs) RAPIDJSON_NOEXCEPT
Definition: allocators.h:482
BaseAllocator BaseAllocatorType
Definition: allocators.h:469
size_type max_size() const RAPIDJSON_NOEXCEPT
Definition: allocators.h:562
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
Definition: allocators.h:336
Default memory allocator used by the parser and DOM.
Definition: allocators.h:129
MemoryPoolAllocator(const MemoryPoolAllocator &rhs) RAPIDJSON_NOEXCEPT
Definition: allocators.h:215
size_t Size() const RAPIDJSON_NOEXCEPT
Computes the memory blocks allocated.
Definition: allocators.h:303
allocator_type::const_reference const_reference
Definition: allocators.h:551
static const bool kNeedFree
rapidjson Allocator concept
Definition: allocators.h:614
StdAllocator(const BaseAllocator &baseAllocator) RAPIDJSON_NOEXCEPT
Definition: allocators.h:663
traits_type::pointer pointer
Definition: allocators.h:516
#define RAPIDJSON_MALLOC(size)
! customization point for global malloc
Definition: rapidjson.h:696
void Free(A &a, T *p, size_t n=1)
Definition: allocators.h:447
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:712
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:716
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:320
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
Definition: allocators.h:200
#define RAPIDJSON_FREE(ptr)
! customization point for global free
Definition: rapidjson.h:704
static void Free(void *ptr) RAPIDJSON_NOEXCEPT
Definition: allocators.h:99
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Definition: allocators.h:91
BaseAllocator BaseAllocatorType
Definition: allocators.h:644
traits_type::value_type value_type
Definition: allocators.h:515
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1249
StdAllocator(const StdAllocator< U, BaseAllocator > &rhs) RAPIDJSON_NOEXCEPT
Definition: allocators.h:657
T * Malloc(A &a, size_t n=1)
Definition: allocators.h:441
Definition: allocators.h:424
void * Malloc(size_t size)
Definition: allocators.h:616
_W64 unsigned int uintptr_t
Definition: stdint.h:52
#define RAPIDJSON_NOEXCEPT_ASSERT(x)
Assertion (in non-throwing contexts).
Definition: rapidjson.h:687
C-runtime library allocator.
Definition: allocators.h:82
~StdAllocator() RAPIDJSON_NOEXCEPT
Definition: allocators.h:504
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:307
pointer allocate(size_type n=1, const void *=0)
Definition: allocators.h:589
Definition: allocators.h:422
bool operator==(const MemoryPoolAllocator &rhs) const RAPIDJSON_NOEXCEPT
Compare (equality) with another MemoryPoolAllocator.
Definition: allocators.h:374
bool Shared() const RAPIDJSON_NOEXCEPT
Whether the allocator is shared.
Definition: allocators.h:314
void deallocate(U *p, size_type n=1)
Definition: allocators.h:584
void * Malloc(size_t size)
Definition: allocators.h:85
void Clear() RAPIDJSON_NOEXCEPT
Deallocates all memory chunks, excluding the first/user one.
Definition: allocators.h:276
bool operator!=(const MemoryPoolAllocator &rhs) const RAPIDJSON_NOEXCEPT
Compare (inequality) with another MemoryPoolAllocator.
Definition: allocators.h:380
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:507
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Definition: allocators.h:620
bool operator!=(const CrtAllocator &) const RAPIDJSON_NOEXCEPT
Definition: allocators.h:104
void destroy(pointer p)
Definition: allocators.h:571
traits_type::difference_type difference_type
Definition: allocators.h:513
traits_type::const_pointer const_pointer
Definition: allocators.h:517
void construct(pointer p, const_reference r)
Definition: allocators.h:567
StdAllocator< U, BaseAllocator > other
Definition: allocators.h:673