1 #ifndef RAPIDJSON_RAPIDJSON_H_
2 #define RAPIDJSON_RAPIDJSON_H_
15 #ifndef RAPIDJSON_NO_INT64DEFINE
17 typedef __int64 int64_t;
18 typedef unsigned __int64 uint64_t;
22 #endif // RAPIDJSON_NO_INT64TYPEDEF
26 #define RAPIDJSON_LITTLEENDIAN 0
27 #define RAPIDJSON_BIGENDIAN 1
34 #ifndef RAPIDJSON_ENDIAN
36 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
37 #define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
39 #define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
40 #endif // __BYTE_ORDER__
42 #define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN // Assumes little endian otherwise.
44 #endif // RAPIDJSON_ENDIAN
55 #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
56 #define RAPIDJSON_SIMD
62 #ifndef RAPIDJSON_NO_SIZETYPEDEFINE
78 #ifndef RAPIDJSON_ASSERT
80 #define RAPIDJSON_ASSERT(x) assert(x)
81 #endif // RAPIDJSON_ASSERT
86 #define RAPIDJSON_MULTILINEMACRO_BEGIN do {
87 #define RAPIDJSON_MULTILINEMACRO_END \
135 void*
Malloc(
size_t size) {
return malloc(size); }
136 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) { (
void)originalSize;
return realloc(originalPtr, newSize); }
137 static void Free(
void *ptr) { free(ptr); }
159 template <
typename BaseAllocator = CrtAllocator>
169 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
172 ownBaseAllocator_ = baseAllocator_ =
new BaseAllocator();
173 AddChunk(chunk_capacity_);
186 MemoryPoolAllocator(
char *buffer,
size_t size,
size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
187 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
191 chunkHead_ = (ChunkHeader*)buffer;
192 chunkHead_->capacity = size -
sizeof(ChunkHeader);
193 chunkHead_->size = 0;
194 chunkHead_->next = 0;
202 delete ownBaseAllocator_;
207 while(chunkHead_ != 0 && chunkHead_ != (ChunkHeader *)userBuffer_) {
208 ChunkHeader* next = chunkHead_->next;
209 baseAllocator_->Free(chunkHead_);
219 for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
220 capacity += c->capacity;
229 for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
236 size = (size + 3) & ~3;
238 if (chunkHead_->size + size > chunkHead_->capacity)
239 AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
241 char *buffer = (
char *)(chunkHead_ + 1) + chunkHead_->size;
243 chunkHead_->size += size;
249 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
250 if (originalPtr == 0)
254 if (originalSize >= newSize)
258 if (originalPtr == (
char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
259 size_t increment = newSize - originalSize;
260 increment = (increment + 3) & ~3;
261 if (chunkHead_->size + increment <= chunkHead_->capacity) {
262 chunkHead_->size += increment;
269 void* newBuffer =
Malloc(newSize);
271 return memcpy(newBuffer, originalPtr, originalSize);
281 void AddChunk(
size_t capacity) {
282 ChunkHeader* chunk = (ChunkHeader*)baseAllocator_->Malloc(
sizeof(ChunkHeader) + capacity);
283 chunk->capacity = capacity;
285 chunk->next = chunkHead_;
289 static const int kDefaultChunkCapacity = 64 * 1024;
300 ChunkHeader *chunkHead_;
301 size_t chunk_capacity_;
303 BaseAllocator* baseAllocator_;
304 BaseAllocator* ownBaseAllocator_;
334 template<
typename CharType =
char>
339 if (codepoint <= 0x7F)
340 *buffer++ = codepoint & 0xFF;
341 else if (codepoint <= 0x7FF) {
342 *buffer++ = 0xC0 | ((codepoint >> 6) & 0xFF);
343 *buffer++ = 0x80 | ((codepoint & 0x3F));
345 else if (codepoint <= 0xFFFF) {
346 *buffer++ = 0xE0 | ((codepoint >> 12) & 0xFF);
347 *buffer++ = 0x80 | ((codepoint >> 6) & 0x3F);
348 *buffer++ = 0x80 | (codepoint & 0x3F);
352 *buffer++ = 0xF0 | ((codepoint >> 18) & 0xFF);
353 *buffer++ = 0x80 | ((codepoint >> 12) & 0x3F);
354 *buffer++ = 0x80 | ((codepoint >> 6) & 0x3F);
355 *buffer++ = 0x80 | (codepoint & 0x3F);
369 template<
typename CharType =
wchar_t>
374 if (codepoint <= 0xFFFF) {
376 *buffer++ =
static_cast<Ch>(codepoint);
380 unsigned v = codepoint - 0x10000;
381 *buffer++ =
static_cast<Ch>((v >> 10) + 0xD800);
382 *buffer++ = (v & 0x3FF) + 0xDC00;
396 template<
typename CharType =
unsigned>
402 *buffer++ = codepoint;
446 template<
typename Stream,
typename Ch>
449 for (
size_t i = 0; i < n; i++)
459 template <
typename Encoding>
461 typedef typename Encoding::Ch
Ch;
486 template <
typename Encoding>
488 typedef typename Encoding::Ch
Ch;
525 #endif // RAPIDJSON_RAPIDJSON_H_
~MemoryPoolAllocator()
Destructor.
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
unsigned SizeType
Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
const Ch * head_
Original head of the string.
const Ch * src_
Current read position.
static Ch * Encode(Ch *buffer, unsigned codepoint)
MemoryPoolAllocator(char *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
GenericInsituStringStream(Ch *src)
static const bool kNeedFree
C-runtime library allocator.
GenericInsituStringStream< UTF8<> > InsituStringStream
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
static Ch * Encode(Ch *buffer, unsigned codepoint)
#define RAPIDJSON_ASSERT(x)
Assertion.
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
GenericStringStream< UTF8<> > StringStream
Concept for reading and writing characters.
static void Free(void *ptr)
double double double *typedef void(CALLCONV AGdll_TYPE)(double *
void * Malloc(size_t size)
size_t Size()
Computes the memory blocks allocated.
static const bool kNeedFree
Tell users that no need to call Free() with this allocator. (concept Allocator)
GenericStringStream(const Ch *src)
size_t Capacity()
Computes the total capacity of allocated memory chunks.
Default memory allocator used by the parser and DOM.
static void Free(void *)
Frees a memory block (concept Allocator)
static Ch * Encode(Ch *buffer, unsigned codepoint)
A read-write string stream.
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.