Recycle removed slots

This commit is contained in:
Benoit Blanchon
2023-07-20 17:54:38 +02:00
parent 781e449e78
commit 727a1013ca
8 changed files with 94 additions and 0 deletions

View File

@@ -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

View File

@@ -59,6 +59,10 @@ class ResourceManager {
return p;
}
void freeSlot(SlotWithId id) {
variantPools_.freeSlot(id);
}
VariantSlot* getSlot(SlotId id) const {
return variantPools_.getSlot(id);
}

View File

@@ -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

View File

@@ -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