forked from bblanchon/ArduinoJson
ResourceManager: replace allocFromPool() with allocVariant()
This commit is contained in:
@@ -12,7 +12,7 @@ inline VariantData* collectionAddElement(CollectionData* array,
|
||||
ResourceManager* resources) {
|
||||
if (!array)
|
||||
return nullptr;
|
||||
auto slot = new (resources) VariantSlot();
|
||||
auto slot = resources->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
array->add(slot);
|
||||
@@ -24,7 +24,7 @@ inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(!key.isNull());
|
||||
ARDUINOJSON_ASSERT(obj != nullptr);
|
||||
auto slot = new (resources) VariantSlot();
|
||||
auto slot = resources->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
if (key.isLinked())
|
||||
|
||||
@@ -279,7 +279,7 @@ class JsonDeserializer {
|
||||
auto savedKey = stringBuilder_.save();
|
||||
|
||||
// Allocate slot in object
|
||||
slot = new (resources_) VariantSlot();
|
||||
slot = resources_->allocVariant();
|
||||
if (!slot)
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantSlot;
|
||||
|
||||
class ResourceManager {
|
||||
public:
|
||||
ResourceManager(size_t capa,
|
||||
@@ -71,15 +73,7 @@ class ResourceManager {
|
||||
return overflowed_;
|
||||
}
|
||||
|
||||
void* allocFromPool(size_t bytes) {
|
||||
if (!canAlloc(bytes)) {
|
||||
overflowed_ = true;
|
||||
return 0;
|
||||
}
|
||||
auto p = pool_ + poolUsage_;
|
||||
poolUsage_ += bytes;
|
||||
return p;
|
||||
}
|
||||
VariantSlot* allocVariant();
|
||||
|
||||
template <typename TAdaptedString>
|
||||
StringNode* saveString(TAdaptedString str) {
|
||||
@@ -171,10 +165,6 @@ class ResourceManager {
|
||||
deallocAllStrings();
|
||||
}
|
||||
|
||||
bool canAlloc(size_t bytes) const {
|
||||
return poolUsage_ + bytes <= poolCapacity_;
|
||||
}
|
||||
|
||||
// Workaround for missing placement new
|
||||
void* operator new(size_t, void* p) {
|
||||
return p;
|
||||
|
||||
@@ -495,7 +495,7 @@ class MsgPackDeserializer {
|
||||
// Save key in memory pool.
|
||||
auto savedKey = stringBuilder_.save();
|
||||
|
||||
VariantSlot* slot = new (resources_) VariantSlot();
|
||||
VariantSlot* slot = resources_->allocVariant();
|
||||
if (!slot)
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
|
||||
@@ -234,7 +234,7 @@ class VariantData {
|
||||
if (!slot)
|
||||
index++;
|
||||
while (index > 0) {
|
||||
slot = new (resources) VariantSlot();
|
||||
slot = resources->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
array->add(slot);
|
||||
|
||||
@@ -26,13 +26,12 @@ class VariantSlot {
|
||||
const char* key_;
|
||||
|
||||
public:
|
||||
static void* operator new(size_t size, ResourceManager* resources) noexcept {
|
||||
return resources->allocFromPool(size);
|
||||
// Placement new
|
||||
static void* operator new(size_t, void* p) noexcept {
|
||||
return p;
|
||||
}
|
||||
|
||||
static void operator delete(void*, ResourceManager*) noexcept {
|
||||
// we cannot release memory from the pool
|
||||
}
|
||||
static void operator delete(void*, void*) noexcept {}
|
||||
|
||||
VariantSlot() : flags_(0), next_(0), key_(0) {}
|
||||
|
||||
@@ -118,4 +117,14 @@ constexpr size_t sizeofObject(size_t n) {
|
||||
return n * sizeof(VariantSlot);
|
||||
}
|
||||
|
||||
inline VariantSlot* ResourceManager::allocVariant() {
|
||||
if (poolUsage_ + sizeof(VariantSlot) > poolCapacity_) {
|
||||
overflowed_ = true;
|
||||
return 0;
|
||||
}
|
||||
auto p = pool_ + poolUsage_;
|
||||
poolUsage_ += sizeof(VariantSlot);
|
||||
return new (p) VariantSlot;
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
Reference in New Issue
Block a user