1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-27 17:28:41 +00:00

Fix bugs in ModifySafeMap (#14276)

This commit is contained in:
sfan5 2024-01-20 15:37:30 +01:00 committed by GitHub
parent e9233bc169
commit 8cbd629010
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 206 additions and 10 deletions

View file

@ -376,10 +376,16 @@ public:
assert(false);
return;
}
if (m_iterating)
m_new.emplace(key, value);
else
m_values.emplace(key, value);
if (m_iterating) {
auto it = m_values.find(key);
if (it != m_values.end()) {
it->second = V();
m_garbage++;
}
m_new[key] = value;
} else {
m_values[key] = value;
}
}
void put(const K &key, V &&value) {
@ -387,10 +393,16 @@ public:
assert(false);
return;
}
if (m_iterating)
m_new.emplace(key, std::move(value));
else
m_values.emplace(key, std::move(value));
if (m_iterating) {
auto it = m_values.find(key);
if (it != m_values.end()) {
it->second = V();
m_garbage++;
}
m_new[key] = std::move(value);
} else {
m_values[key] = std::move(value);
}
}
V take(const K &key) {
@ -405,7 +417,8 @@ public:
auto it = m_values.find(key);
if (it == m_values.end())
return ret;
ret = std::move(it->second);
if (!ret)
ret = std::move(it->second);
if (m_iterating) {
it->second = V();
m_garbage++;
@ -516,7 +529,8 @@ private:
std::map<K, V> m_values;
std::map<K, V> m_new;
unsigned int m_iterating = 0;
size_t m_garbage = 0; // upper bound for null-placeholders in m_values
// approximate amount of null-placeholders in m_values, reliable for != 0 tests
size_t m_garbage = 0;
static constexpr size_t GC_MIN_SIZE = 30;
};