mirror of
				https://github.com/bblanchon/ArduinoJson.git
				synced 2025-11-04 08:31:36 +01:00 
			
		
		
		
	Decouple MemoryPool from VariantSlot
				
					
				
			This commit is contained in:
		@@ -12,7 +12,7 @@ inline VariantData* collectionAddElement(CollectionData* array,
 | 
			
		||||
                                         MemoryPool* pool) {
 | 
			
		||||
  if (!array)
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  auto slot = pool->allocVariant();
 | 
			
		||||
  auto slot = new (pool) VariantSlot();
 | 
			
		||||
  if (!slot)
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  array->add(slot);
 | 
			
		||||
@@ -24,7 +24,7 @@ inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key,
 | 
			
		||||
                                        MemoryPool* pool) {
 | 
			
		||||
  ARDUINOJSON_ASSERT(!key.isNull());
 | 
			
		||||
  ARDUINOJSON_ASSERT(obj != nullptr);
 | 
			
		||||
  auto slot = pool->allocVariant();
 | 
			
		||||
  auto slot = new (pool) VariantSlot();
 | 
			
		||||
  if (!slot)
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  if (key.isLinked())
 | 
			
		||||
 
 | 
			
		||||
@@ -96,7 +96,7 @@ inline void CollectionData::movePointers(ptrdiff_t variantDistance) {
 | 
			
		||||
  movePointer(head_, variantDistance);
 | 
			
		||||
  movePointer(tail_, variantDistance);
 | 
			
		||||
  for (VariantSlot* slot = head_; slot; slot = slot->next())
 | 
			
		||||
    slot->movePointers(variantDistance);
 | 
			
		||||
    slot->data()->movePointers(variantDistance);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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.
 | 
			
		||||
  // https://arduinojson.org/v6/api/JsonDocument/shrinktofit/
 | 
			
		||||
  void shrinkToFit() {
 | 
			
		||||
    pool_.shrinkToFit(data_);
 | 
			
		||||
    auto offset = pool_.shrinkToFit();
 | 
			
		||||
    data_.movePointers(offset);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Reclaims the memory leaked when removing and replacing values.
 | 
			
		||||
 
 | 
			
		||||
@@ -279,7 +279,7 @@ class JsonDeserializer {
 | 
			
		||||
          auto savedKey = stringBuilder_.save();
 | 
			
		||||
 | 
			
		||||
          // Allocate slot in object
 | 
			
		||||
          slot = pool_->allocVariant();
 | 
			
		||||
          slot = new (pool_) VariantSlot();
 | 
			
		||||
          if (!slot)
 | 
			
		||||
            return DeserializationError::NoMemory;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,22 +10,11 @@
 | 
			
		||||
#include <ArduinoJson/Polyfills/assert.hpp>
 | 
			
		||||
#include <ArduinoJson/Polyfills/mpl/max.hpp>
 | 
			
		||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
 | 
			
		||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
 | 
			
		||||
 | 
			
		||||
#include <string.h>  // memmove
 | 
			
		||||
 | 
			
		||||
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 {
 | 
			
		||||
 public:
 | 
			
		||||
  MemoryPool(size_t capa, Allocator* allocator = DefaultAllocator::instance())
 | 
			
		||||
@@ -88,11 +77,13 @@ class MemoryPool {
 | 
			
		||||
    return overflowed_;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  VariantSlot* allocVariant() {
 | 
			
		||||
    auto slot = allocRight<VariantSlot>();
 | 
			
		||||
    if (slot)
 | 
			
		||||
      slot->clear();
 | 
			
		||||
    return slot;
 | 
			
		||||
  void* allocFromPool(size_t bytes) {
 | 
			
		||||
    if (!canAlloc(bytes)) {
 | 
			
		||||
      overflowed_ = true;
 | 
			
		||||
      return 0;
 | 
			
		||||
    }
 | 
			
		||||
    right_ -= bytes;
 | 
			
		||||
    return right_;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  template <typename TAdaptedString>
 | 
			
		||||
@@ -198,10 +189,10 @@ class MemoryPool {
 | 
			
		||||
    return p;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void shrinkToFit(VariantData& variant) {
 | 
			
		||||
  ptrdiff_t shrinkToFit() {
 | 
			
		||||
    ptrdiff_t bytes_reclaimed = squash();
 | 
			
		||||
    if (bytes_reclaimed == 0)
 | 
			
		||||
      return;
 | 
			
		||||
      return 0;
 | 
			
		||||
 | 
			
		||||
    void* old_ptr = begin_;
 | 
			
		||||
    void* new_ptr = allocator_->reallocate(old_ptr, capacity());
 | 
			
		||||
@@ -210,8 +201,7 @@ class MemoryPool {
 | 
			
		||||
        static_cast<char*>(new_ptr) - static_cast<char*>(old_ptr);
 | 
			
		||||
 | 
			
		||||
    movePointers(ptr_offset);
 | 
			
		||||
    reinterpret_cast<VariantSlot&>(variant).movePointers(ptr_offset -
 | 
			
		||||
                                                         bytes_reclaimed);
 | 
			
		||||
    return ptr_offset - bytes_reclaimed;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 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) {
 | 
			
		||||
    auto buf = capa ? reinterpret_cast<char*>(allocator_->allocate(capa)) : 0;
 | 
			
		||||
    begin_ = buf;
 | 
			
		||||
 
 | 
			
		||||
@@ -495,7 +495,7 @@ class MsgPackDeserializer {
 | 
			
		||||
        // Save key in memory pool.
 | 
			
		||||
        auto savedKey = stringBuilder_.save();
 | 
			
		||||
 | 
			
		||||
        VariantSlot* slot = pool_->allocVariant();
 | 
			
		||||
        VariantSlot* slot = new (pool_) VariantSlot();
 | 
			
		||||
        if (!slot)
 | 
			
		||||
          return DeserializationError::NoMemory;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@
 | 
			
		||||
 | 
			
		||||
#include <ArduinoJson/Polyfills/attributes.hpp>
 | 
			
		||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
 | 
			
		||||
#include <ArduinoJson/Variant/VariantData.hpp>
 | 
			
		||||
#include <ArduinoJson/Variant/VariantTo.hpp>
 | 
			
		||||
#include "JsonVariantConst.hpp"
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@
 | 
			
		||||
#include <ArduinoJson/Strings/JsonString.hpp>
 | 
			
		||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
 | 
			
		||||
#include <ArduinoJson/Variant/VariantContent.hpp>
 | 
			
		||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
 | 
			
		||||
 | 
			
		||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -190,7 +190,7 @@ inline NO_INLINE VariantData* variantGetOrAddElement(VariantData* var,
 | 
			
		||||
  if (!slot)
 | 
			
		||||
    index++;
 | 
			
		||||
  while (index > 0) {
 | 
			
		||||
    slot = pool->allocVariant();
 | 
			
		||||
    slot = new (pool) VariantSlot();
 | 
			
		||||
    if (!slot)
 | 
			
		||||
      return nullptr;
 | 
			
		||||
    array->add(slot);
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
 | 
			
		||||
#include <ArduinoJson/Polyfills/integer.hpp>
 | 
			
		||||
#include <ArduinoJson/Polyfills/limits.hpp>
 | 
			
		||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
 | 
			
		||||
@@ -25,11 +26,15 @@ class VariantSlot {
 | 
			
		||||
  const char* key_;
 | 
			
		||||
 | 
			
		||||
 public:
 | 
			
		||||
  // Must be a POD!
 | 
			
		||||
  // - no constructor
 | 
			
		||||
  // - no destructor
 | 
			
		||||
  // - no virtual
 | 
			
		||||
  // - no inheritance
 | 
			
		||||
  static void* operator new(size_t size, MemoryPool* pool) noexcept {
 | 
			
		||||
    return pool->allocFromPool(size);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static void operator delete(void*, MemoryPool*) noexcept {
 | 
			
		||||
    // we cannot release memory from the pool
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  VariantSlot() : flags_(0), next_(0), key_(0) {}
 | 
			
		||||
 | 
			
		||||
  VariantData* data() {
 | 
			
		||||
    return reinterpret_cast<VariantData*>(&content_);
 | 
			
		||||
@@ -93,21 +98,20 @@ class VariantSlot {
 | 
			
		||||
  bool ownsKey() const {
 | 
			
		||||
    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) {
 | 
			
		||||
  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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user