Rename SlotWithId to Slot

This commit is contained in:
Benoit Blanchon
2024-08-25 15:04:39 +02:00
parent 362201241f
commit 5dd203bca4
6 changed files with 28 additions and 30 deletions

View File

@ -97,17 +97,15 @@ class CollectionData {
} }
protected: protected:
void appendOne(SlotWithId<VariantData> slot, void appendOne(Slot<VariantData> slot, const ResourceManager* resources);
const ResourceManager* resources); void appendPair(Slot<VariantData> key, Slot<VariantData> value,
void appendPair(SlotWithId<VariantData> key, SlotWithId<VariantData> value,
const ResourceManager* resources); const ResourceManager* resources);
void removeOne(iterator it, ResourceManager* resources); void removeOne(iterator it, ResourceManager* resources);
void removePair(iterator it, ResourceManager* resources); void removePair(iterator it, ResourceManager* resources);
private: private:
SlotWithId<VariantData> getPreviousSlot(VariantData*, Slot<VariantData> getPreviousSlot(VariantData*, const ResourceManager*) const;
const ResourceManager*) const;
}; };
inline const VariantData* collectionToVariant( inline const VariantData* collectionToVariant(

View File

@ -30,7 +30,7 @@ inline CollectionData::iterator CollectionData::createIterator(
return iterator(resources->getVariant(head_), head_); return iterator(resources->getVariant(head_), head_);
} }
inline void CollectionData::appendOne(SlotWithId<VariantData> slot, inline void CollectionData::appendOne(Slot<VariantData> slot,
const ResourceManager* resources) { const ResourceManager* resources) {
if (tail_ != NULL_SLOT) { if (tail_ != NULL_SLOT) {
auto tail = resources->getVariant(tail_); auto tail = resources->getVariant(tail_);
@ -42,8 +42,8 @@ inline void CollectionData::appendOne(SlotWithId<VariantData> slot,
} }
} }
inline void CollectionData::appendPair(SlotWithId<VariantData> key, inline void CollectionData::appendPair(Slot<VariantData> key,
SlotWithId<VariantData> value, Slot<VariantData> value,
const ResourceManager* resources) { const ResourceManager* resources) {
key->setNext(value.id()); key->setNext(value.id());
@ -63,22 +63,22 @@ inline void CollectionData::clear(ResourceManager* resources) {
auto currId = next; auto currId = next;
auto slot = resources->getVariant(next); auto slot = resources->getVariant(next);
next = slot->next(); next = slot->next();
resources->freeVariant(SlotWithId<VariantData>(slot, currId)); resources->freeVariant({slot, currId});
} }
head_ = NULL_SLOT; head_ = NULL_SLOT;
tail_ = NULL_SLOT; tail_ = NULL_SLOT;
} }
inline SlotWithId<VariantData> CollectionData::getPreviousSlot( inline Slot<VariantData> CollectionData::getPreviousSlot(
VariantData* target, const ResourceManager* resources) const { VariantData* target, const ResourceManager* resources) const {
auto prev = SlotWithId<VariantData>(); auto prev = Slot<VariantData>();
auto currentId = head_; auto currentId = head_;
while (currentId != NULL_SLOT) { while (currentId != NULL_SLOT) {
auto currentSlot = resources->getVariant(currentId); auto currentSlot = resources->getVariant(currentId);
if (currentSlot == target) if (currentSlot == target)
break; break;
prev = SlotWithId<VariantData>(currentSlot, currentId); prev = Slot<VariantData>(currentSlot, currentId);
currentId = currentSlot->next(); currentId = currentSlot->next();
} }
return prev; return prev;

View File

@ -15,15 +15,15 @@ using SlotCount = SlotId;
const SlotId NULL_SLOT = SlotId(-1); const SlotId NULL_SLOT = SlotId(-1);
template <typename T> template <typename T>
class SlotWithId { class Slot {
public: public:
SlotWithId() : slot_(nullptr), id_(NULL_SLOT) {} Slot() : ptr_(nullptr), id_(NULL_SLOT) {}
SlotWithId(T* slot, SlotId id) : slot_(slot), id_(id) { Slot(T* p, SlotId id) : ptr_(p), id_(id) {
ARDUINOJSON_ASSERT((slot == nullptr) == (id == NULL_SLOT)); ARDUINOJSON_ASSERT((p == nullptr) == (id == NULL_SLOT));
} }
explicit operator bool() const { explicit operator bool() const {
return slot_ != nullptr; return ptr_ != nullptr;
} }
SlotId id() const { SlotId id() const {
@ -31,16 +31,16 @@ class SlotWithId {
} }
T* ptr() const { T* ptr() const {
return slot_; return ptr_;
} }
T* operator->() const { T* operator->() const {
ARDUINOJSON_ASSERT(slot_ != nullptr); ARDUINOJSON_ASSERT(ptr_ != nullptr);
return slot_; return ptr_;
} }
private: private:
T* slot_; T* ptr_;
SlotId id_; SlotId id_;
}; };
@ -62,7 +62,7 @@ class MemoryPool {
usage_ = 0; usage_ = 0;
} }
SlotWithId<T> allocSlot() { Slot<T> allocSlot() {
if (!slots_) if (!slots_)
return {}; return {};
if (usage_ >= capacity_) if (usage_ >= capacity_)

View File

@ -79,7 +79,7 @@ class MemoryPoolList {
return *this; return *this;
} }
SlotWithId<T> allocSlot(Allocator* allocator) { Slot<T> allocSlot(Allocator* allocator) {
// try to allocate from free list // try to allocate from free list
if (freeList_ != NULL_SLOT) { if (freeList_ != NULL_SLOT) {
return allocFromFreeList(); return allocFromFreeList();
@ -100,7 +100,7 @@ class MemoryPoolList {
return allocFromLastPool(); return allocFromLastPool();
} }
void freeSlot(SlotWithId<T> slot) { void freeSlot(Slot<T> slot) {
reinterpret_cast<FreeSlot*>(slot.ptr())->next = freeList_; reinterpret_cast<FreeSlot*>(slot.ptr())->next = freeList_;
freeList_ = slot.id(); freeList_ = slot.id();
} }
@ -149,7 +149,7 @@ class MemoryPoolList {
} }
private: private:
SlotWithId<T> allocFromFreeList() { Slot<T> allocFromFreeList() {
ARDUINOJSON_ASSERT(freeList_ != NULL_SLOT); ARDUINOJSON_ASSERT(freeList_ != NULL_SLOT);
auto id = freeList_; auto id = freeList_;
auto slot = getSlot(freeList_); auto slot = getSlot(freeList_);
@ -157,7 +157,7 @@ class MemoryPoolList {
return {slot, id}; return {slot, id};
} }
SlotWithId<T> allocFromLastPool() { Slot<T> allocFromLastPool() {
ARDUINOJSON_ASSERT(count_ > 0); ARDUINOJSON_ASSERT(count_ > 0);
auto poolIndex = SlotId(count_ - 1); auto poolIndex = SlotId(count_ - 1);
auto slot = pools_[poolIndex].allocSlot(); auto slot = pools_[poolIndex].allocSlot();

View File

@ -49,9 +49,9 @@ class ResourceManager {
return overflowed_; return overflowed_;
} }
SlotWithId<VariantData> allocVariant(); Slot<VariantData> allocVariant();
void freeVariant(SlotWithId<VariantData> slot); void freeVariant(Slot<VariantData> slot);
VariantData* getVariant(SlotId id) const; VariantData* getVariant(SlotId id) const;

View File

@ -10,7 +10,7 @@
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
inline SlotWithId<VariantData> ResourceManager::allocVariant() { inline Slot<VariantData> ResourceManager::allocVariant() {
auto p = variantPools_.allocSlot(allocator_); auto p = variantPools_.allocSlot(allocator_);
if (!p) { if (!p) {
overflowed_ = true; overflowed_ = true;
@ -19,7 +19,7 @@ inline SlotWithId<VariantData> ResourceManager::allocVariant() {
return {new (p.ptr()) VariantData, p.id()}; return {new (p.ptr()) VariantData, p.id()};
} }
inline void ResourceManager::freeVariant(SlotWithId<VariantData> variant) { inline void ResourceManager::freeVariant(Slot<VariantData> variant) {
variant->setNull(this); variant->setNull(this);
variantPools_.freeSlot(variant); variantPools_.freeSlot(variant);
} }