From 866e6d1104a5128748601c9a148281b026e1eb28 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Tue, 25 Jul 2017 09:06:48 -0700 Subject: [PATCH] Clean up this allocation junk --- src/discord-rpc.cpp | 6 +----- src/rpc_connection.cpp | 5 +---- src/serialization.h | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index e0eb5d9..0f7be50 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -83,12 +83,8 @@ extern "C" void Discord_UpdateConnection() else { // reads - // 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]; for (;;) { - PoolAllocator pa(parseBuffer, sizeof(parseBuffer)); - StackAllocator sa; - JsonDocument message(rapidjson::kObjectType, &pa, sizeof(sa.fixedBuffer_), &sa); + JsonDocument message; if (!Connection->Read(message)) { break; diff --git a/src/rpc_connection.cpp b/src/rpc_connection.cpp index 50fc07c..ececc3e 100644 --- a/src/rpc_connection.cpp +++ b/src/rpc_connection.cpp @@ -34,10 +34,7 @@ void RpcConnection::Open() } if (state == State::SentHandshake) { - char parseBuffer[32 * 1024]; - PoolAllocator pa(parseBuffer, sizeof(parseBuffer)); - StackAllocator sa; - JsonDocument message(rapidjson::kObjectType, &pa, sizeof(sa.fixedBuffer_), &sa); + JsonDocument message; if (Read(message)) { auto cmd = message.FindMember("cmd"); if (cmd == message.MemberEnd() || !cmd->value.IsString()) { diff --git a/src/serialization.h b/src/serialization.h index 9ddffc3..1d6f1bf 100644 --- a/src/serialization.h +++ b/src/serialization.h @@ -99,11 +99,24 @@ public: }; using MallocAllocator = rapidjson::CrtAllocator; -extern MallocAllocator MallocAllocatorInst; using PoolAllocator = rapidjson::MemoryPoolAllocator; using UTF8 = rapidjson::UTF8; // 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; -using JsonDocument = rapidjson::GenericDocument>; +using JsonDocumentBase = rapidjson::GenericDocument; +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. + 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_() + {} +};