mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 04:52:22 +02:00
Decouple MemoryPool
from VariantSlot
This commit is contained in:
@ -3,19 +3,20 @@
|
|||||||
// MIT License
|
// MIT License
|
||||||
|
|
||||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||||
|
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
#include "Allocators.hpp"
|
#include "Allocators.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson::detail;
|
using namespace ArduinoJson::detail;
|
||||||
|
|
||||||
TEST_CASE("MemoryPool::allocVariant()") {
|
TEST_CASE("new (pool) VariantSlot()") {
|
||||||
SECTION("Returns different pointer") {
|
SECTION("Returns different pointer") {
|
||||||
MemoryPool pool(4096);
|
MemoryPool pool(4096);
|
||||||
|
|
||||||
VariantSlot* s1 = pool.allocVariant();
|
VariantSlot* s1 = new (&pool) VariantSlot();
|
||||||
REQUIRE(s1 != 0);
|
REQUIRE(s1 != 0);
|
||||||
VariantSlot* s2 = pool.allocVariant();
|
VariantSlot* s2 = new (&pool) VariantSlot();
|
||||||
REQUIRE(s2 != 0);
|
REQUIRE(s2 != 0);
|
||||||
|
|
||||||
REQUIRE(s1 != s2);
|
REQUIRE(s1 != s2);
|
||||||
@ -24,27 +25,27 @@ TEST_CASE("MemoryPool::allocVariant()") {
|
|||||||
SECTION("Returns aligned pointers") {
|
SECTION("Returns aligned pointers") {
|
||||||
MemoryPool pool(4096);
|
MemoryPool pool(4096);
|
||||||
|
|
||||||
REQUIRE(isAligned(pool.allocVariant()));
|
REQUIRE(isAligned(new (&pool) VariantSlot()));
|
||||||
REQUIRE(isAligned(pool.allocVariant()));
|
REQUIRE(isAligned(new (&pool) VariantSlot()));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Returns zero if capacity is 0") {
|
SECTION("Returns zero if capacity is 0") {
|
||||||
MemoryPool pool(0);
|
MemoryPool pool(0);
|
||||||
|
|
||||||
REQUIRE(pool.allocVariant() == 0);
|
REQUIRE(new (&pool) VariantSlot() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Returns zero if buffer is null") {
|
SECTION("Returns zero if buffer is null") {
|
||||||
MemoryPool pool(4096, FailingAllocator::instance());
|
MemoryPool pool(4096, FailingAllocator::instance());
|
||||||
|
|
||||||
REQUIRE(pool.allocVariant() == 0);
|
REQUIRE(new (&pool) VariantSlot() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Returns zero if capacity is insufficient") {
|
SECTION("Returns zero if capacity is insufficient") {
|
||||||
MemoryPool pool(sizeof(VariantSlot));
|
MemoryPool pool(sizeof(VariantSlot));
|
||||||
|
|
||||||
pool.allocVariant();
|
new (&pool) VariantSlot();
|
||||||
|
|
||||||
REQUIRE(pool.allocVariant() == 0);
|
REQUIRE(new (&pool) VariantSlot() == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||||
|
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
using namespace ArduinoJson::detail;
|
using namespace ArduinoJson::detail;
|
||||||
@ -14,7 +15,7 @@ TEST_CASE("MemoryPool::clear()") {
|
|||||||
MemoryPool pool(poolCapacity);
|
MemoryPool pool(poolCapacity);
|
||||||
|
|
||||||
SECTION("Discards allocated variants") {
|
SECTION("Discards allocated variants") {
|
||||||
pool.allocVariant();
|
new (&pool) VariantSlot();
|
||||||
|
|
||||||
pool.clear();
|
pool.clear();
|
||||||
REQUIRE(pool.size() == 0);
|
REQUIRE(pool.size() == 0);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// MIT License
|
// MIT License
|
||||||
|
|
||||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||||
|
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
using namespace ArduinoJson::detail;
|
using namespace ArduinoJson::detail;
|
||||||
@ -24,10 +25,10 @@ TEST_CASE("MemoryPool::size()") {
|
|||||||
const size_t variantCount = pool.capacity() / sizeof(VariantSlot);
|
const size_t variantCount = pool.capacity() / sizeof(VariantSlot);
|
||||||
|
|
||||||
for (size_t i = 0; i < variantCount; i++)
|
for (size_t i = 0; i < variantCount; i++)
|
||||||
pool.allocVariant();
|
new (&pool) VariantSlot();
|
||||||
size_t size = pool.size();
|
size_t size = pool.size();
|
||||||
|
|
||||||
pool.allocVariant();
|
new (&pool) VariantSlot();
|
||||||
|
|
||||||
REQUIRE(size == pool.size());
|
REQUIRE(size == pool.size());
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ inline VariantData* collectionAddElement(CollectionData* array,
|
|||||||
MemoryPool* pool) {
|
MemoryPool* pool) {
|
||||||
if (!array)
|
if (!array)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
auto slot = pool->allocVariant();
|
auto slot = new (pool) VariantSlot();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
array->add(slot);
|
array->add(slot);
|
||||||
@ -24,7 +24,7 @@ inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key,
|
|||||||
MemoryPool* pool) {
|
MemoryPool* pool) {
|
||||||
ARDUINOJSON_ASSERT(!key.isNull());
|
ARDUINOJSON_ASSERT(!key.isNull());
|
||||||
ARDUINOJSON_ASSERT(obj != nullptr);
|
ARDUINOJSON_ASSERT(obj != nullptr);
|
||||||
auto slot = pool->allocVariant();
|
auto slot = new (pool) VariantSlot();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
if (key.isLinked())
|
if (key.isLinked())
|
||||||
|
@ -96,7 +96,7 @@ inline void CollectionData::movePointers(ptrdiff_t variantDistance) {
|
|||||||
movePointer(head_, variantDistance);
|
movePointer(head_, variantDistance);
|
||||||
movePointer(tail_, variantDistance);
|
movePointer(tail_, variantDistance);
|
||||||
for (VariantSlot* slot = head_; slot; slot = slot->next())
|
for (VariantSlot* slot = head_; slot; slot = slot->next())
|
||||||
slot->movePointers(variantDistance);
|
slot->data()->movePointers(variantDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -86,7 +86,8 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
// Reduces the capacity of the memory pool to match the current usage.
|
// Reduces the capacity of the memory pool to match the current usage.
|
||||||
// https://arduinojson.org/v6/api/JsonDocument/shrinktofit/
|
// https://arduinojson.org/v6/api/JsonDocument/shrinktofit/
|
||||||
void shrinkToFit() {
|
void shrinkToFit() {
|
||||||
pool_.shrinkToFit(data_);
|
auto offset = pool_.shrinkToFit();
|
||||||
|
data_.movePointers(offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reclaims the memory leaked when removing and replacing values.
|
// Reclaims the memory leaked when removing and replacing values.
|
||||||
|
@ -279,7 +279,7 @@ class JsonDeserializer {
|
|||||||
auto savedKey = stringBuilder_.save();
|
auto savedKey = stringBuilder_.save();
|
||||||
|
|
||||||
// Allocate slot in object
|
// Allocate slot in object
|
||||||
slot = pool_->allocVariant();
|
slot = new (pool_) VariantSlot();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
|
@ -10,22 +10,11 @@
|
|||||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||||
#include <ArduinoJson/Polyfills/mpl/max.hpp>
|
#include <ArduinoJson/Polyfills/mpl/max.hpp>
|
||||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
|
||||||
|
|
||||||
#include <string.h> // memmove
|
#include <string.h> // memmove
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
// Returns the size (in bytes) of an array with n elements.
|
|
||||||
constexpr size_t sizeofArray(size_t n) {
|
|
||||||
return n * sizeof(VariantSlot);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the size (in bytes) of an object with n members.
|
|
||||||
constexpr size_t sizeofObject(size_t n) {
|
|
||||||
return n * sizeof(VariantSlot);
|
|
||||||
}
|
|
||||||
|
|
||||||
class MemoryPool {
|
class MemoryPool {
|
||||||
public:
|
public:
|
||||||
MemoryPool(size_t capa, Allocator* allocator = DefaultAllocator::instance())
|
MemoryPool(size_t capa, Allocator* allocator = DefaultAllocator::instance())
|
||||||
@ -88,11 +77,13 @@ class MemoryPool {
|
|||||||
return overflowed_;
|
return overflowed_;
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantSlot* allocVariant() {
|
void* allocFromPool(size_t bytes) {
|
||||||
auto slot = allocRight<VariantSlot>();
|
if (!canAlloc(bytes)) {
|
||||||
if (slot)
|
overflowed_ = true;
|
||||||
slot->clear();
|
return 0;
|
||||||
return slot;
|
}
|
||||||
|
right_ -= bytes;
|
||||||
|
return right_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
@ -198,10 +189,10 @@ class MemoryPool {
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
void shrinkToFit(VariantData& variant) {
|
ptrdiff_t shrinkToFit() {
|
||||||
ptrdiff_t bytes_reclaimed = squash();
|
ptrdiff_t bytes_reclaimed = squash();
|
||||||
if (bytes_reclaimed == 0)
|
if (bytes_reclaimed == 0)
|
||||||
return;
|
return 0;
|
||||||
|
|
||||||
void* old_ptr = begin_;
|
void* old_ptr = begin_;
|
||||||
void* new_ptr = allocator_->reallocate(old_ptr, capacity());
|
void* new_ptr = allocator_->reallocate(old_ptr, capacity());
|
||||||
@ -210,8 +201,7 @@ class MemoryPool {
|
|||||||
static_cast<char*>(new_ptr) - static_cast<char*>(old_ptr);
|
static_cast<char*>(new_ptr) - static_cast<char*>(old_ptr);
|
||||||
|
|
||||||
movePointers(ptr_offset);
|
movePointers(ptr_offset);
|
||||||
reinterpret_cast<VariantSlot&>(variant).movePointers(ptr_offset -
|
return ptr_offset - bytes_reclaimed;
|
||||||
bytes_reclaimed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -251,20 +241,6 @@ class MemoryPool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
T* allocRight() {
|
|
||||||
return reinterpret_cast<T*>(allocRight(sizeof(T)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void* allocRight(size_t bytes) {
|
|
||||||
if (!canAlloc(bytes)) {
|
|
||||||
overflowed_ = true;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
right_ -= bytes;
|
|
||||||
return right_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void allocPool(size_t capa) {
|
void allocPool(size_t capa) {
|
||||||
auto buf = capa ? reinterpret_cast<char*>(allocator_->allocate(capa)) : 0;
|
auto buf = capa ? reinterpret_cast<char*>(allocator_->allocate(capa)) : 0;
|
||||||
begin_ = buf;
|
begin_ = buf;
|
||||||
|
@ -495,7 +495,7 @@ class MsgPackDeserializer {
|
|||||||
// Save key in memory pool.
|
// Save key in memory pool.
|
||||||
auto savedKey = stringBuilder_.save();
|
auto savedKey = stringBuilder_.save();
|
||||||
|
|
||||||
VariantSlot* slot = pool_->allocVariant();
|
VariantSlot* slot = new (pool_) VariantSlot();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <ArduinoJson/Polyfills/attributes.hpp>
|
#include <ArduinoJson/Polyfills/attributes.hpp>
|
||||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||||
|
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||||
#include <ArduinoJson/Variant/VariantTo.hpp>
|
#include <ArduinoJson/Variant/VariantTo.hpp>
|
||||||
#include "JsonVariantConst.hpp"
|
#include "JsonVariantConst.hpp"
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
#include <ArduinoJson/Strings/JsonString.hpp>
|
||||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||||
#include <ArduinoJson/Variant/VariantContent.hpp>
|
#include <ArduinoJson/Variant/VariantContent.hpp>
|
||||||
|
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ inline NO_INLINE VariantData* variantGetOrAddElement(VariantData* var,
|
|||||||
if (!slot)
|
if (!slot)
|
||||||
index++;
|
index++;
|
||||||
while (index > 0) {
|
while (index > 0) {
|
||||||
slot = pool->allocVariant();
|
slot = new (pool) VariantSlot();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
array->add(slot);
|
array->add(slot);
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||||
#include <ArduinoJson/Polyfills/integer.hpp>
|
#include <ArduinoJson/Polyfills/integer.hpp>
|
||||||
#include <ArduinoJson/Polyfills/limits.hpp>
|
#include <ArduinoJson/Polyfills/limits.hpp>
|
||||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||||
@ -25,11 +26,15 @@ class VariantSlot {
|
|||||||
const char* key_;
|
const char* key_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Must be a POD!
|
static void* operator new(size_t size, MemoryPool* pool) noexcept {
|
||||||
// - no constructor
|
return pool->allocFromPool(size);
|
||||||
// - no destructor
|
}
|
||||||
// - no virtual
|
|
||||||
// - no inheritance
|
static void operator delete(void*, MemoryPool*) noexcept {
|
||||||
|
// we cannot release memory from the pool
|
||||||
|
}
|
||||||
|
|
||||||
|
VariantSlot() : flags_(0), next_(0), key_(0) {}
|
||||||
|
|
||||||
VariantData* data() {
|
VariantData* data() {
|
||||||
return reinterpret_cast<VariantData*>(&content_);
|
return reinterpret_cast<VariantData*>(&content_);
|
||||||
@ -93,21 +98,20 @@ class VariantSlot {
|
|||||||
bool ownsKey() const {
|
bool ownsKey() const {
|
||||||
return (flags_ & OWNED_KEY_BIT) != 0;
|
return (flags_ & OWNED_KEY_BIT) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
|
||||||
next_ = 0;
|
|
||||||
flags_ = 0;
|
|
||||||
key_ = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void movePointers(ptrdiff_t variantDistance) {
|
|
||||||
if (flags_ & COLLECTION_MASK)
|
|
||||||
content_.asCollection.movePointers(variantDistance);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline VariantData* slotData(VariantSlot* slot) {
|
inline VariantData* slotData(VariantSlot* slot) {
|
||||||
return reinterpret_cast<VariantData*>(slot);
|
return reinterpret_cast<VariantData*>(slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the size (in bytes) of an array with n elements.
|
||||||
|
constexpr size_t sizeofArray(size_t n) {
|
||||||
|
return n * sizeof(VariantSlot);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the size (in bytes) of an object with n members.
|
||||||
|
constexpr size_t sizeofObject(size_t n) {
|
||||||
|
return n * sizeof(VariantSlot);
|
||||||
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
Reference in New Issue
Block a user