Added parameter to DynamicJsonBuffer constructor to set initial size (issue #152)

This commit is contained in:
Benoit Blanchon
2015-11-09 21:37:40 +01:00
parent b7d9bb2765
commit ba3617c22f
3 changed files with 17 additions and 10 deletions

View File

@ -1,10 +1,16 @@
ArduinoJson: change log 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 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) * Use `float` instead of `double` to reduce the size of `JsonVariant` (issue #134)
v5.0.4 v5.0.4

View File

@ -31,7 +31,8 @@ class BlockJsonBuffer : public JsonBuffer {
}; };
public: public:
BlockJsonBuffer() : _head(NULL) {} BlockJsonBuffer(size_t initialSize = 256)
: _head(NULL), _nextBlockSize(initialSize) {}
~BlockJsonBuffer() { ~BlockJsonBuffer() {
Block* currentBlock = _head; Block* currentBlock = _head;
@ -55,8 +56,6 @@ class BlockJsonBuffer : public JsonBuffer {
} }
private: private:
static const size_t FIRST_BLOCK_CAPACITY = 32;
bool canAllocInHead(size_t bytes) const { bool canAllocInHead(size_t bytes) const {
return _head != NULL && _head->size + bytes <= _head->capacity; return _head != NULL && _head->size + bytes <= _head->capacity;
} }
@ -68,10 +67,11 @@ class BlockJsonBuffer : public JsonBuffer {
} }
void* allocInNewBlock(size_t bytes) { void* allocInNewBlock(size_t bytes) {
size_t capacity = FIRST_BLOCK_CAPACITY; size_t capacity = _nextBlockSize;
if (_head != NULL) capacity = _head->capacity * 2; if (_head != NULL) capacity = _head->capacity * 2;
if (bytes > capacity) capacity = bytes; if (bytes > capacity) capacity = bytes;
if (!addNewBlock(capacity)) return NULL; if (!addNewBlock(capacity)) return NULL;
_nextBlockSize *= 2;
return allocInHead(bytes); return allocInHead(bytes);
} }
@ -86,8 +86,9 @@ class BlockJsonBuffer : public JsonBuffer {
return true; return true;
} }
Block* _head;
TAllocator _allocator; TAllocator _allocator;
Block* _head;
size_t _nextBlockSize;
}; };
} }
} }

View File

@ -20,22 +20,22 @@ template <typename TFloat>
static TFloat parse(const char *); static TFloat parse(const char *);
template <> template <>
FORCE_INLINE float parse<float>(const char *s) { float parse<float>(const char *s) {
return static_cast<float>(strtod(s, NULL)); return static_cast<float>(strtod(s, NULL));
} }
template <> template <>
FORCE_INLINE double parse<double>(const char *s) { double parse<double>(const char *s) {
return strtod(s, NULL); return strtod(s, NULL);
} }
template <> template <>
FORCE_INLINE long parse<long>(const char *s) { long parse<long>(const char *s) {
return strtol(s, NULL, 10); return strtol(s, NULL, 10);
} }
template <> template <>
FORCE_INLINE int parse<int>(const char *s) { int parse<int>(const char *s) {
return atoi(s); return atoi(s);
} }