Skip to content

Commit 67dfbbb

Browse files
committed
storage: make storage safer with reentrant destructors
1 parent c391b08 commit 67dfbbb

2 files changed

Lines changed: 6 additions & 17 deletions

File tree

‎TODO‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ TODO:
2121
* archetype-like a-l� EnTT support (see my own notes)
2222
* organizer: view/storage only based model, no registry
2323
* explore "runtime" mode for hashed string where the source is copied internally
24-
* storage: shrink_to_fit does not work with reentrant destructor?
2524
* finish the imgui viewer/editor!
2625
* entity io
26+
* storage.cpp update_from_destructor triggers natvis errors

‎src/entt/entity/storage.hpp‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -255,22 +255,10 @@ class basic_storage: public basic_sparse_set<Entity, typename stl::allocator_tra
255255
return it;
256256
}
257257

258-
void shrink_to_size(const stl::size_t sz) {
259-
const auto from = (sz + traits_type::page_size - 1u) / traits_type::page_size;
258+
void shrink_to_size() {
259+
const auto from = (base_type::size() + traits_type::page_size - 1u) / traits_type::page_size;
260260
allocator_type allocator{get_allocator()};
261261

262-
if constexpr(!stl::is_trivially_destructible_v<element_type>) {
263-
for(auto pos = sz, length = base_type::size(); pos < length; ++pos) {
264-
if constexpr(traits_type::in_place_delete) {
265-
if(base_type::data()[pos] != tombstone) {
266-
alloc_traits::destroy(allocator, stl::addressof(element_at(pos)));
267-
}
268-
} else {
269-
alloc_traits::destroy(allocator, stl::addressof(element_at(pos)));
270-
}
271-
}
272-
}
273-
274262
for(auto pos = from, last = payload.size(); pos < last; ++pos) {
275263
alloc_traits::deallocate(allocator, payload[pos], traits_type::page_size);
276264
}
@@ -458,7 +446,8 @@ class basic_storage: public basic_sparse_set<Entity, typename stl::allocator_tra
458446
/*! @brief Default destructor. */
459447
// NOLINTNEXTLINE(bugprone-exception-escape)
460448
~basic_storage() override {
461-
shrink_to_size(0u);
449+
pop_all();
450+
shrink_to_size();
462451
}
463452

464453
/**
@@ -523,7 +512,7 @@ class basic_storage: public basic_sparse_set<Entity, typename stl::allocator_tra
523512
/*! @brief Requests the removal of unused capacity. */
524513
void shrink_to_fit() override {
525514
base_type::shrink_to_fit();
526-
shrink_to_size(base_type::size());
515+
shrink_to_size();
527516
}
528517

529518
/**

0 commit comments

Comments
 (0)