Rename SlotWithId::slot() and VariantWithId::data() to ptr()

This commit is contained in:
Benoit Blanchon
2024-08-25 14:51:59 +02:00
parent 4327f72140
commit 2be24eded8
9 changed files with 28 additions and 28 deletions

View File

@ -14,11 +14,11 @@ TEST_CASE("ResourceManager::allocVariant()") {
ResourceManager resources;
auto s1 = resources.allocVariant();
REQUIRE(s1.data() != nullptr);
REQUIRE(s1.ptr() != nullptr);
auto s2 = resources.allocVariant();
REQUIRE(s2.data() != nullptr);
REQUIRE(s2.ptr() != nullptr);
REQUIRE(s1.data() != s2.data());
REQUIRE(s1.ptr() != s2.ptr());
}
SECTION("Returns the same slot after calling freeVariant()") {
@ -42,8 +42,8 @@ TEST_CASE("ResourceManager::allocVariant()") {
SECTION("Returns aligned pointers") {
ResourceManager resources;
REQUIRE(isAligned(resources.allocVariant().data()));
REQUIRE(isAligned(resources.allocVariant().data()));
REQUIRE(isAligned(resources.allocVariant().ptr()));
REQUIRE(isAligned(resources.allocVariant().ptr()));
}
SECTION("Returns null if pool list allocation fails") {
@ -51,7 +51,7 @@ TEST_CASE("ResourceManager::allocVariant()") {
auto variant = resources.allocVariant();
REQUIRE(variant.id() == NULL_SLOT);
REQUIRE(variant.data() == nullptr);
REQUIRE(variant.ptr() == nullptr);
}
SECTION("Returns null if pool allocation fails") {
@ -61,7 +61,7 @@ TEST_CASE("ResourceManager::allocVariant()") {
auto variant = resources.allocVariant();
REQUIRE(variant.id() == NULL_SLOT);
REQUIRE(variant.data() == nullptr);
REQUIRE(variant.ptr() == nullptr);
}
SECTION("Try overflow pool counter") {
@ -75,7 +75,7 @@ TEST_CASE("ResourceManager::allocVariant()") {
for (SlotId i = 0; i < NULL_SLOT; i++) {
auto slot = resources.allocVariant();
REQUIRE(slot.id() == i); // or != NULL_SLOT
REQUIRE(slot.data() != nullptr);
REQUIRE(slot.ptr() != nullptr);
}
REQUIRE(resources.overflowed() == false);
@ -84,7 +84,7 @@ TEST_CASE("ResourceManager::allocVariant()") {
for (int i = 0; i < 10; i++) {
auto slot = resources.allocVariant();
REQUIRE(slot.id() == NULL_SLOT);
REQUIRE(slot.data() == nullptr);
REQUIRE(slot.ptr() == nullptr);
}
REQUIRE(resources.overflowed() == true);

View File

@ -29,8 +29,8 @@ TEST_CASE("ResourceManager::swap()") {
swap(a, b);
REQUIRE(a1.data() == b.getVariant(a1.id()));
REQUIRE(b1.data() == a.getVariant(b1.id()));
REQUIRE(a1.ptr() == b.getVariant(a1.id()));
REQUIRE(b1.ptr() == a.getVariant(b1.id()));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()) * 2,
@ -47,8 +47,8 @@ TEST_CASE("ResourceManager::swap()") {
auto b1 = b.allocVariant();
swap(a, b);
REQUIRE(a1.data() == b.getVariant(a1.id()));
REQUIRE(b1.data() == a.getVariant(b1.id()));
REQUIRE(a1.ptr() == b.getVariant(a1.id()));
REQUIRE(b1.ptr() == a.getVariant(b1.id()));
REQUIRE(spy.log() ==
AllocatorLog{
@ -68,8 +68,8 @@ TEST_CASE("ResourceManager::swap()") {
auto b1 = b.allocVariant();
swap(a, b);
REQUIRE(a1.data() == b.getVariant(a1.id()));
REQUIRE(b1.data() == a.getVariant(b1.id()));
REQUIRE(a1.ptr() == b.getVariant(a1.id()));
REQUIRE(b1.ptr() == a.getVariant(b1.id()));
REQUIRE(spy.log() ==
AllocatorLog{
@ -91,7 +91,7 @@ TEST_CASE("ResourceManager::swap()") {
swap(a, b);
REQUIRE(a1.data() == b.getVariant(a1.id()));
REQUIRE(b1.data() == a.getVariant(b1.id()));
REQUIRE(a1.ptr() == b.getVariant(a1.id()));
REQUIRE(b1.ptr() == a.getVariant(b1.id()));
}
}

View File

@ -25,7 +25,7 @@ inline VariantData* ArrayData::addElement(ResourceManager* resources) {
if (!slot)
return nullptr;
CollectionData::appendOne(slot, resources);
return slot.data();
return slot.ptr();
}
inline VariantData* ArrayData::getOrAddElement(size_t index,
@ -62,7 +62,7 @@ inline bool ArrayData::addValue(T&& value, ResourceManager* resources) {
auto slot = resources->allocVariant();
if (!slot)
return false;
JsonVariant variant(slot.data(), resources);
JsonVariant variant(slot.ptr(), resources);
if (!variant.set(detail::forward<T>(value))) {
resources->freeVariant(slot);
return false;

View File

@ -30,7 +30,7 @@ class SlotWithId {
return id_;
}
VariantData* slot() const {
VariantData* ptr() const {
return slot_;
}

View File

@ -74,7 +74,7 @@ inline SlotWithId MemoryPoolList::allocFromFreeList() {
}
inline void MemoryPoolList::freeSlot(SlotWithId slot) {
reinterpret_cast<FreeSlot*>(slot.slot())->next = freeList_;
reinterpret_cast<FreeSlot*>(slot.ptr())->next = freeList_;
freeList_ = slot.id();
}

View File

@ -145,7 +145,7 @@ class MemoryPoolList {
auto slot = pools_[poolIndex].allocSlot();
if (!slot)
return {};
return {slot.slot(),
return {slot.ptr(),
SlotId(poolIndex * ARDUINOJSON_POOL_CAPACITY + slot.id())};
}

View File

@ -16,7 +16,7 @@ inline VariantWithId ResourceManager::allocVariant() {
overflowed_ = true;
return {};
}
return {new (p.slot()) VariantData, p.id()};
return {new (p.ptr()) VariantData, p.id()};
}
inline void ResourceManager::freeVariant(VariantWithId variant) {

View File

@ -65,7 +65,7 @@ inline VariantData* ObjectData::addMember(TAdaptedString key,
CollectionData::appendPair(keySlot, valueSlot, resources);
return valueSlot.data();
return valueSlot.ptr();
}
// Returns the size (in bytes) of an object with n members.

View File

@ -540,13 +540,13 @@ class VariantWithId : public SlotWithId {
VariantWithId(VariantData* data, SlotId id)
: SlotWithId(reinterpret_cast<VariantData*>(data), id) {}
VariantData* data() {
return reinterpret_cast<VariantData*>(slot());
VariantData* ptr() {
return reinterpret_cast<VariantData*>(SlotWithId::ptr());
}
VariantData* operator->() {
ARDUINOJSON_ASSERT(data() != nullptr);
return data();
ARDUINOJSON_ASSERT(ptr() != nullptr);
return ptr();
}
};