forked from bblanchon/ArduinoJson
VariantPoolList: handle SlotId
overflow
This commit is contained in:
@ -10,6 +10,11 @@ add_executable(ResourceManagerTests
|
||||
StringBuilder.cpp
|
||||
)
|
||||
|
||||
add_compile_definitions(ResourceManagerTests
|
||||
ARDUINOJSON_SLOT_ID_SIZE=1 # require less RAM for overflow tests
|
||||
ARDUINOJSON_POOL_CAPACITY=16
|
||||
)
|
||||
|
||||
add_test(ResourceManager ResourceManagerTests)
|
||||
|
||||
set_tests_properties(ResourceManager
|
||||
|
@ -66,4 +66,30 @@ TEST_CASE("ResourceManager::allocSlot()") {
|
||||
REQUIRE(variant.id() == NULL_SLOT);
|
||||
REQUIRE(static_cast<VariantSlot*>(variant) == nullptr);
|
||||
}
|
||||
|
||||
SECTION("Try overflow pool counter") {
|
||||
ResourceManager resources;
|
||||
|
||||
// this test assumes SlotId is 8-bit; otherwise it consumes a lot of memory
|
||||
// tyhe GitHub Workflow gets killed
|
||||
REQUIRE(NULL_SLOT == 255);
|
||||
|
||||
// fill all the pools
|
||||
for (SlotId i = 0; i < NULL_SLOT; i++) {
|
||||
auto slot = resources.allocSlot();
|
||||
REQUIRE(slot.id() == i); // or != NULL_SLOT
|
||||
REQUIRE(static_cast<VariantSlot*>(slot) != nullptr);
|
||||
}
|
||||
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
|
||||
// now all allocations should fail
|
||||
for (int i = 0; i < 10; i++) {
|
||||
auto slot = resources.allocSlot();
|
||||
REQUIRE(slot.id() == NULL_SLOT);
|
||||
REQUIRE(static_cast<VariantSlot*>(slot) == nullptr);
|
||||
}
|
||||
|
||||
REQUIRE(resources.overflowed() == true);
|
||||
}
|
||||
}
|
||||
|
@ -108,11 +108,16 @@ class VariantPoolList {
|
||||
if (count_ == capacity_ && !increaseCapacity(allocator))
|
||||
return nullptr;
|
||||
auto pool = &pools_[count_++];
|
||||
pool->create(ARDUINOJSON_POOL_CAPACITY, allocator);
|
||||
SlotCount poolCapacity = ARDUINOJSON_POOL_CAPACITY;
|
||||
if (count_ == maxPools) // last pool is smaller because of NULL_SLOT
|
||||
poolCapacity--;
|
||||
pool->create(poolCapacity, allocator);
|
||||
return pool;
|
||||
}
|
||||
|
||||
bool increaseCapacity(Allocator* allocator) {
|
||||
if (count_ == maxPools)
|
||||
return false;
|
||||
void* newPools;
|
||||
PoolCount newCapacity;
|
||||
if (pools_) {
|
||||
@ -134,6 +139,10 @@ class VariantPoolList {
|
||||
PoolCount count_ = 0;
|
||||
PoolCount capacity_ = 0;
|
||||
SlotId freeList_ = NULL_SLOT;
|
||||
|
||||
public:
|
||||
static const PoolCount maxPools =
|
||||
PoolCount(NULL_SLOT / ARDUINOJSON_POOL_CAPACITY + 1);
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
Reference in New Issue
Block a user