tag/Pool: rename `TagPoolSlot` to `TagPoolItem`

This commit is contained in:
Max Kellermann 2023-09-11 20:42:56 +02:00
parent 7dff29bcd1
commit 0748f75d9b
1 changed files with 33 additions and 33 deletions

View File

@ -17,36 +17,36 @@
Mutex tag_pool_lock; Mutex tag_pool_lock;
struct TagPoolSlot { struct TagPoolItem {
IntrusiveListHook<IntrusiveHookMode::NORMAL> list_hook; IntrusiveListHook<IntrusiveHookMode::NORMAL> list_hook;
uint8_t ref = 1; uint8_t ref = 1;
TagItem item; TagItem item;
static constexpr unsigned MAX_REF = std::numeric_limits<decltype(ref)>::max(); static constexpr unsigned MAX_REF = std::numeric_limits<decltype(ref)>::max();
TagPoolSlot(TagType type, TagPoolItem(TagType type,
std::string_view value) noexcept { std::string_view value) noexcept {
item.type = type; item.type = type;
*std::copy(value.begin(), value.end(), item.value) = 0; *std::copy(value.begin(), value.end(), item.value) = 0;
} }
static TagPoolSlot *Create(TagType type, static TagPoolItem *Create(TagType type,
std::string_view value) noexcept; std::string_view value) noexcept;
}; };
TagPoolSlot * TagPoolItem *
TagPoolSlot::Create(TagType type, TagPoolItem::Create(TagType type,
std::string_view value) noexcept std::string_view value) noexcept
{ {
TagPoolSlot *dummy; TagPoolItem *dummy;
return NewVarSize<TagPoolSlot>(sizeof(dummy->item.value), return NewVarSize<TagPoolItem>(sizeof(dummy->item.value),
value.size() + 1, value.size() + 1,
type, type,
value); value);
} }
static std::array<IntrusiveList<TagPoolSlot, static std::array<IntrusiveList<TagPoolItem,
IntrusiveListMemberHookTraits<&TagPoolSlot::list_hook>, IntrusiveListMemberHookTraits<&TagPoolItem::list_hook>,
IntrusiveListOptions{.zero_initialized = true}>, IntrusiveListOptions{.zero_initialized = true}>,
16127> slots; 16127> slots;
@ -61,10 +61,10 @@ calc_hash(TagType type, std::string_view p) noexcept
return hash ^ type; return hash ^ type;
} }
static constexpr TagPoolSlot * static constexpr TagPoolItem *
tag_item_to_slot(TagItem *item) noexcept TagItemToPoolItem(TagItem *item) noexcept
{ {
return &ContainerCast(*item, &TagPoolSlot::item); return &ContainerCast(*item, &TagPoolItem::item);
} }
static inline auto & static inline auto &
@ -78,34 +78,34 @@ tag_pool_get_item(TagType type, std::string_view value) noexcept
{ {
auto &list = tag_value_list(type, value); auto &list = tag_value_list(type, value);
for (auto &slot : list) { for (auto &i : list) {
if (slot.item.type == type && if (i.item.type == type &&
value == slot.item.value && value == i.item.value &&
slot.ref < TagPoolSlot::MAX_REF) { i.ref < TagPoolItem::MAX_REF) {
assert(slot.ref > 0); assert(i.ref > 0);
++slot.ref; ++i.ref;
return &slot.item; return &i.item;
} }
} }
auto slot = TagPoolSlot::Create(type, value); auto *pool_item = TagPoolItem::Create(type, value);
list.push_front(*slot); list.push_front(*pool_item);
return &slot->item; return &pool_item->item;
} }
TagItem * TagItem *
tag_pool_dup_item(TagItem *item) noexcept tag_pool_dup_item(TagItem *item) noexcept
{ {
TagPoolSlot *slot = tag_item_to_slot(item); TagPoolItem *pool_item = TagItemToPoolItem(item);
assert(slot->ref > 0); assert(pool_item->ref > 0);
if (slot->ref < TagPoolSlot::MAX_REF) { if (pool_item->ref < TagPoolItem::MAX_REF) {
++slot->ref; ++pool_item->ref;
return item; return item;
} else { } else {
/* the reference counter overflows above MAX_REF; /* the reference counter overflows above MAX_REF;
obtain a reference to a different TagPoolSlot which obtain a reference to a different TagPoolItem which
isn't yet "full" */ isn't yet "full" */
return tag_pool_get_item(item->type, item->value); return tag_pool_get_item(item->type, item->value);
} }
@ -114,13 +114,13 @@ tag_pool_dup_item(TagItem *item) noexcept
void void
tag_pool_put_item(TagItem *item) noexcept tag_pool_put_item(TagItem *item) noexcept
{ {
TagPoolSlot *const slot = tag_item_to_slot(item); TagPoolItem *const pool_item = TagItemToPoolItem(item);
assert(slot->ref > 0); assert(pool_item->ref > 0);
--slot->ref; --pool_item->ref;
if (slot->ref > 0) if (pool_item->ref > 0)
return; return;
slot->list_hook.unlink(); pool_item->list_hook.unlink();
DeleteVarSize(slot); DeleteVarSize(pool_item);
} }