VariantPoolList: reduce the size of capacity_ and count_

This commit is contained in:
Benoit Blanchon
2023-07-20 18:13:07 +02:00
parent 8fcaebb44a
commit 8be0d57d24

View File

@ -9,6 +9,8 @@
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
using PoolCount = SlotId;
class VariantPoolList { class VariantPoolList {
public: public:
VariantPoolList() = default; VariantPoolList() = default;
@ -63,7 +65,7 @@ class VariantPoolList {
void clear(Allocator* allocator) { void clear(Allocator* allocator) {
if (!pools_) if (!pools_)
return; return;
for (size_t i = 0; i < count_; i++) for (PoolCount i = 0; i < count_; i++)
pools_[i].destroy(allocator); pools_[i].destroy(allocator);
allocator->deallocate(pools_); allocator->deallocate(pools_);
pools_ = nullptr; pools_ = nullptr;
@ -73,7 +75,7 @@ class VariantPoolList {
SlotCount usage() const { SlotCount usage() const {
SlotCount total = 0; SlotCount total = 0;
for (size_t i = 0; i < count_; i++) for (PoolCount i = 0; i < count_; i++)
total = SlotCount(total + pools_[i].usage()); total = SlotCount(total + pools_[i].usage());
return total; return total;
} }
@ -112,9 +114,9 @@ class VariantPoolList {
bool increaseCapacity(Allocator* allocator) { bool increaseCapacity(Allocator* allocator) {
void* newPools; void* newPools;
size_t newCapacity; PoolCount newCapacity;
if (pools_) { if (pools_) {
newCapacity = capacity_ * 2; newCapacity = PoolCount(capacity_ * 2);
newPools = newPools =
allocator->reallocate(pools_, newCapacity * sizeof(VariantPool)); allocator->reallocate(pools_, newCapacity * sizeof(VariantPool));
} else { } else {
@ -129,8 +131,8 @@ class VariantPoolList {
} }
VariantPool* pools_ = nullptr; VariantPool* pools_ = nullptr;
size_t count_ = 0; PoolCount count_ = 0;
size_t capacity_ = 0; PoolCount capacity_ = 0;
SlotId freeList_ = NULL_SLOT; SlotId freeList_ = NULL_SLOT;
}; };