mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-11-11 21:10:03 +01:00
Recycle removed slots
This commit is contained in:
@@ -133,6 +133,7 @@ inline void CollectionData::releaseSlot(iterator it,
|
||||
if (it.ownsKey())
|
||||
resources->dereferenceString(it.key());
|
||||
it->setNull(resources);
|
||||
resources->freeSlot(SlotWithId(it.slot_, it.currentId_));
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -59,6 +59,10 @@ class ResourceManager {
|
||||
return p;
|
||||
}
|
||||
|
||||
void freeSlot(SlotWithId id) {
|
||||
variantPools_.freeSlot(id);
|
||||
}
|
||||
|
||||
VariantSlot* getSlot(SlotId id) const {
|
||||
return variantPools_.getSlot(id);
|
||||
}
|
||||
|
||||
@@ -61,4 +61,17 @@ inline size_t VariantPool::slotsToBytes(SlotCount n) {
|
||||
return n * sizeof(VariantSlot);
|
||||
}
|
||||
|
||||
inline SlotWithId VariantPoolList::allocFromFreeList() {
|
||||
ARDUINOJSON_ASSERT(freeList_ != NULL_SLOT);
|
||||
auto id = freeList_;
|
||||
auto slot = getSlot(freeList_);
|
||||
freeList_ = slot->next();
|
||||
return {new (slot) VariantSlot, id};
|
||||
}
|
||||
|
||||
inline void VariantPoolList::freeSlot(SlotWithId slot) {
|
||||
slot->setNext(freeList_);
|
||||
freeList_ = slot.id();
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -29,6 +29,11 @@ class VariantPoolList {
|
||||
}
|
||||
|
||||
SlotWithId allocSlot(Allocator* allocator) {
|
||||
// try to allocate from free list
|
||||
if (freeList_ != NULL_SLOT) {
|
||||
return allocFromFreeList();
|
||||
}
|
||||
|
||||
// try to allocate from last pool (other pools are full)
|
||||
if (pools_) {
|
||||
auto slot = allocFromLastPool();
|
||||
@@ -44,6 +49,8 @@ class VariantPoolList {
|
||||
return allocFromLastPool();
|
||||
}
|
||||
|
||||
void freeSlot(SlotWithId slot);
|
||||
|
||||
VariantSlot* getSlot(SlotId id) const {
|
||||
auto poolIndex = SlotId(id / ARDUINOJSON_POOL_CAPACITY);
|
||||
auto indexInPool = SlotId(id % ARDUINOJSON_POOL_CAPACITY);
|
||||
@@ -82,6 +89,8 @@ class VariantPoolList {
|
||||
}
|
||||
|
||||
private:
|
||||
SlotWithId allocFromFreeList();
|
||||
|
||||
SlotWithId allocFromLastPool() {
|
||||
ARDUINOJSON_ASSERT(pools_ != nullptr);
|
||||
auto poolIndex = SlotId(count_ - 1);
|
||||
@@ -121,6 +130,7 @@ class VariantPoolList {
|
||||
VariantPool* pools_ = nullptr;
|
||||
size_t count_ = 0;
|
||||
size_t capacity_ = 0;
|
||||
SlotId freeList_ = NULL_SLOT;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
Reference in New Issue
Block a user