2014-10-01 01:20:12 +02:00
|
|
|
#include "interned_string.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
InternedString StringRegistry::acquire(StringView str)
|
|
|
|
{
|
|
|
|
auto it = m_slot_map.find(str);
|
|
|
|
if (it == m_slot_map.end())
|
|
|
|
{
|
|
|
|
size_t slot;
|
|
|
|
if (not m_free_slots.empty())
|
|
|
|
{
|
|
|
|
slot = m_free_slots.back();
|
|
|
|
m_free_slots.pop_back();
|
2014-10-28 22:54:25 +01:00
|
|
|
kak_assert(m_storage[slot].second == 0);
|
2014-10-01 01:20:12 +02:00
|
|
|
m_storage[slot] = DataAndRefCount({str.begin(), str.end()}, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
slot = m_storage.size();
|
|
|
|
m_storage.push_back(DataAndRefCount({str.begin(), str.end()}, 1));
|
|
|
|
}
|
|
|
|
// Create a new string view that point to the storage data
|
|
|
|
StringView storage_view{m_storage[slot].first.data(), (int)m_storage[slot].first.size()};
|
|
|
|
m_slot_map[storage_view] = slot;
|
|
|
|
|
2014-10-05 11:20:50 +02:00
|
|
|
return InternedString{storage_view, slot};
|
2014-10-01 01:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t slot = it->second;
|
2014-10-07 20:07:05 +02:00
|
|
|
auto& data = m_storage[slot];
|
|
|
|
++data.second;
|
|
|
|
return {{data.first.data(), (int)data.first.size()}, slot};
|
2014-10-01 01:20:12 +02:00
|
|
|
}
|
|
|
|
|
2014-10-05 11:20:50 +02:00
|
|
|
void StringRegistry::acquire(size_t slot)
|
2014-10-01 01:20:12 +02:00
|
|
|
{
|
2014-10-05 11:20:50 +02:00
|
|
|
kak_assert(slot < m_storage.size());
|
|
|
|
kak_assert(m_storage[slot].second > 0);
|
|
|
|
++m_storage[slot].second;
|
|
|
|
}
|
2014-10-01 01:20:12 +02:00
|
|
|
|
2014-10-28 21:01:27 +01:00
|
|
|
void StringRegistry::release(size_t slot) noexcept
|
2014-10-05 11:20:50 +02:00
|
|
|
{
|
2014-10-28 22:54:25 +01:00
|
|
|
kak_assert(m_storage[slot].second > 0);
|
2014-10-01 01:20:12 +02:00
|
|
|
if (--m_storage[slot].second == 0)
|
|
|
|
{
|
|
|
|
m_free_slots.push_back(slot);
|
2014-10-05 11:20:50 +02:00
|
|
|
std::vector<char>& data = m_storage[slot].first;
|
|
|
|
auto it = m_slot_map.find(StringView{data.data(), (int)data.size()});
|
|
|
|
kak_assert(it != m_slot_map.end());
|
2014-10-01 01:20:12 +02:00
|
|
|
m_slot_map.erase(it);
|
2014-10-29 14:54:35 +01:00
|
|
|
data = std::vector<char>{};
|
2014-10-01 01:20:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|