Fixed DynamicJsonBuffer::clear() not resetting allocation size (fixes #561)

This commit is contained in:
Benoit Blanchon
2017-08-30 21:51:10 +02:00
parent ac5a2676e7
commit 57defe00ee
3 changed files with 15 additions and 14 deletions

View File

@ -1,6 +1,11 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
HEAD
----
* Fixed `DynamicJsonBuffer::clear()` not resetting allocation size (issue #561)
v5.11.1 v5.11.1
------- -------

View File

@ -52,7 +52,7 @@ class DynamicJsonBufferBase
: _head(NULL), _nextBlockCapacity(initialSize) {} : _head(NULL), _nextBlockCapacity(initialSize) {}
~DynamicJsonBufferBase() { ~DynamicJsonBufferBase() {
freeAllBlocks(); clear();
} }
// Gets the number of bytes occupied in the buffer // Gets the number of bytes occupied in the buffer
@ -71,7 +71,13 @@ class DynamicJsonBufferBase
// Resets the buffer. // Resets the buffer.
// USE WITH CAUTION: this invalidates all previously allocated data // USE WITH CAUTION: this invalidates all previously allocated data
void clear() { void clear() {
freeAllBlocks(); Block* currentBlock = _head;
while (currentBlock != NULL) {
_nextBlockCapacity = currentBlock->capacity;
Block* nextBlock = currentBlock->next;
_allocator.deallocate(currentBlock);
currentBlock = nextBlock;
}
_head = 0; _head = 0;
} }
@ -144,16 +150,6 @@ class DynamicJsonBufferBase
return true; return true;
} }
void freeAllBlocks() {
Block* currentBlock = _head;
while (currentBlock != NULL) {
Block* nextBlock = currentBlock->next;
_allocator.deallocate(currentBlock);
currentBlock = nextBlock;
}
}
TAllocator _allocator; TAllocator _allocator;
Block* _head; Block* _head;
size_t _nextBlockCapacity; size_t _nextBlockCapacity;

View File

@ -46,7 +46,7 @@ TEST_CASE("DynamicJsonBuffer::alloc()") {
REQUIRE(allocatorLog.str() == "A1A2FF"); REQUIRE(allocatorLog.str() == "A1A2FF");
} }
SECTION("Keeps increasing allocation size after clear") { SECTION("Resets allocation size after clear()") {
allocatorLog.str(""); allocatorLog.str("");
{ {
DynamicJsonBufferBase<SpyingAllocator> buffer(1); DynamicJsonBufferBase<SpyingAllocator> buffer(1);
@ -55,7 +55,7 @@ TEST_CASE("DynamicJsonBuffer::alloc()") {
buffer.clear(); buffer.clear();
buffer.alloc(1); buffer.alloc(1);
} }
REQUIRE(allocatorLog.str() == "A1A2FFA4F"); REQUIRE(allocatorLog.str() == "A1A2FFA1F");
} }
SECTION("Makes a big allocation when needed") { SECTION("Makes a big allocation when needed") {