This commit is contained in:
Chris Marsh
2017-07-25 09:27:48 -07:00
parent 866e6d1104
commit 7a6172a300
10 changed files with 176 additions and 78 deletions

View File

@ -8,8 +8,9 @@
#include "rapidjson/internal/itoa.h"
// if only there was a standard library function for this
template<size_t Len>
inline size_t StringCopy(char (&dest)[Len], const char* src) {
template <size_t Len>
inline size_t StringCopy(char (&dest)[Len], const char* src)
{
if (!dest || !src || !Len) {
return 0;
}
@ -26,20 +27,30 @@ size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char*
// Commands
struct DiscordRichPresence;
size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, int nonce, int pid, const DiscordRichPresence* presence);
size_t JsonWriteRichPresenceObj(char* dest,
size_t maxLen,
int nonce,
int pid,
const DiscordRichPresence* presence);
size_t JsonWriteSubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName);
// I want to use as few allocations as I can get away with, and to do that with RapidJson, you need to supply some of
// I want to use as few allocations as I can get away with, and to do that with RapidJson, you need
// to supply some of
// your own allocators for stuff rather than use the defaults
class LinearAllocator {
public:
char* buffer_;
char* end_;
LinearAllocator() {
LinearAllocator()
{
assert(0); // needed for some default case in rapidjson, should not use
}
LinearAllocator(char* buffer, size_t size) : buffer_(buffer), end_(buffer + size) {}
LinearAllocator(char* buffer, size_t size)
: buffer_(buffer)
, end_(buffer + size)
{
}
static const bool kNeedFree = false;
void* Malloc(size_t size)
{
@ -60,14 +71,17 @@ public:
assert(!originalPtr && !originalSize);
return Malloc(newSize);
}
static void Free(void* ptr) { /* shrug */ }
static void Free(void* ptr) { /* shrug */}
};
template<size_t Size>
template <size_t Size>
class FixedLinearAllocator : public LinearAllocator {
public:
char fixedBuffer_[Size];
FixedLinearAllocator() : LinearAllocator(fixedBuffer_, Size) {}
FixedLinearAllocator()
: LinearAllocator(fixedBuffer_, Size)
{
}
static const bool kNeedFree = false;
};
@ -80,10 +94,11 @@ public:
char* current_;
DirectStringBuffer(char* buffer, size_t maxLen)
: buffer_(buffer)
, end_(buffer + maxLen)
, current_(buffer)
{}
: buffer_(buffer)
, end_(buffer + maxLen)
, current_(buffer)
{
}
void Put(char c)
{
@ -92,10 +107,7 @@ public:
}
}
void Flush() {}
size_t GetSize() const
{
return current_ - buffer_;
}
size_t GetSize() const { return current_ - buffer_; }
};
using MallocAllocator = rapidjson::CrtAllocator;
@ -104,19 +116,25 @@ using UTF8 = rapidjson::UTF8<char>;
// Writer appears to need about 16 bytes per nested object level (with 64bit size_t)
using StackAllocator = FixedLinearAllocator<2048>;
constexpr size_t WriterNestingLevels = 2048 / (2 * sizeof(size_t));
using JsonWriter = rapidjson::Writer<DirectStringBuffer, UTF8, UTF8, StackAllocator, rapidjson::kWriteNoFlags>;
using JsonWriter =
rapidjson::Writer<DirectStringBuffer, UTF8, UTF8, StackAllocator, rapidjson::kWriteNoFlags>;
using JsonDocumentBase = rapidjson::GenericDocument<UTF8, PoolAllocator, StackAllocator>;
class JsonDocument : public JsonDocumentBase
{
class JsonDocument : public JsonDocumentBase {
public:
static const int kDefaultChunkCapacity = 32 * 1024;
// json parser will use this buffer first, then allocate more if needed; I seriously doubt we send any messages that would use all of this, though.
// json parser will use this buffer first, then allocate more if needed; I seriously doubt we
// send any messages that would use all of this, though.
char parseBuffer_[32 * 1024];
MallocAllocator mallocAllocator_;
PoolAllocator poolAllocator_;
StackAllocator stackAllocator_;
JsonDocument() : JsonDocumentBase(rapidjson::kObjectType, &poolAllocator_, sizeof(stackAllocator_.fixedBuffer_), &stackAllocator_)
, poolAllocator_(parseBuffer_, sizeof(parseBuffer_), kDefaultChunkCapacity, &mallocAllocator_)
, stackAllocator_()
{}
JsonDocument()
: JsonDocumentBase(rapidjson::kObjectType,
&poolAllocator_,
sizeof(stackAllocator_.fixedBuffer_),
&stackAllocator_)
, poolAllocator_(parseBuffer_, sizeof(parseBuffer_), kDefaultChunkCapacity, &mallocAllocator_)
, stackAllocator_()
{
}
};