From ba3617c22f464b8059b88f203cb4607d696d1ca9 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 9 Nov 2015 21:37:40 +0100 Subject: [PATCH] Added parameter to `DynamicJsonBuffer` constructor to set initial size (issue #152) --- CHANGELOG.md | 8 +++++++- include/ArduinoJson/Internals/BlockJsonBuffer.hpp | 11 ++++++----- src/JsonVariant.cpp | 8 ++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dfbd6bd..1a4e683e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,16 @@ ArduinoJson: change log ======================= +v5.0.6 +------ + +* Added parameter to `DynamicJsonBuffer` constructor to set initial size (issue #152) +* Fixed warning about library category in Arduino 1.6.6 (issue #147) + v5.0.5 ------ -* Add overload `JsonObjectSuscript::set(value, decimals)` (issue #143) +* Added overload `JsonObjectSuscript::set(value, decimals)` (issue #143) * Use `float` instead of `double` to reduce the size of `JsonVariant` (issue #134) v5.0.4 diff --git a/include/ArduinoJson/Internals/BlockJsonBuffer.hpp b/include/ArduinoJson/Internals/BlockJsonBuffer.hpp index 0581a2e5..55662b4f 100644 --- a/include/ArduinoJson/Internals/BlockJsonBuffer.hpp +++ b/include/ArduinoJson/Internals/BlockJsonBuffer.hpp @@ -31,7 +31,8 @@ class BlockJsonBuffer : public JsonBuffer { }; public: - BlockJsonBuffer() : _head(NULL) {} + BlockJsonBuffer(size_t initialSize = 256) + : _head(NULL), _nextBlockSize(initialSize) {} ~BlockJsonBuffer() { Block* currentBlock = _head; @@ -55,8 +56,6 @@ class BlockJsonBuffer : public JsonBuffer { } private: - static const size_t FIRST_BLOCK_CAPACITY = 32; - bool canAllocInHead(size_t bytes) const { return _head != NULL && _head->size + bytes <= _head->capacity; } @@ -68,10 +67,11 @@ class BlockJsonBuffer : public JsonBuffer { } void* allocInNewBlock(size_t bytes) { - size_t capacity = FIRST_BLOCK_CAPACITY; + size_t capacity = _nextBlockSize; if (_head != NULL) capacity = _head->capacity * 2; if (bytes > capacity) capacity = bytes; if (!addNewBlock(capacity)) return NULL; + _nextBlockSize *= 2; return allocInHead(bytes); } @@ -86,8 +86,9 @@ class BlockJsonBuffer : public JsonBuffer { return true; } - Block* _head; TAllocator _allocator; + Block* _head; + size_t _nextBlockSize; }; } } diff --git a/src/JsonVariant.cpp b/src/JsonVariant.cpp index 5066ebd8..375e2096 100644 --- a/src/JsonVariant.cpp +++ b/src/JsonVariant.cpp @@ -20,22 +20,22 @@ template static TFloat parse(const char *); template <> -FORCE_INLINE float parse(const char *s) { +float parse(const char *s) { return static_cast(strtod(s, NULL)); } template <> -FORCE_INLINE double parse(const char *s) { +double parse(const char *s) { return strtod(s, NULL); } template <> -FORCE_INLINE long parse(const char *s) { +long parse(const char *s) { return strtol(s, NULL, 10); } template <> -FORCE_INLINE int parse(const char *s) { +int parse(const char *s) { return atoi(s); }