From 2148806957fe9cdb7d462848165b48e14f9f98a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Loriot?= Date: Thu, 3 Apr 2025 11:56:06 +0200 Subject: [PATCH 1/2] workaround possible optimisation that would alter the value of the erase_counter The code is still not legal but works in practise. fix similar to 5853673267 --- .../include/CGAL/Compact_container.h | 30 +++++++++++++++++++ .../CGAL/Concurrent_compact_container.h | 21 +++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/STL_Extension/include/CGAL/Compact_container.h b/STL_Extension/include/CGAL/Compact_container.h index 073300c8a82..25b72e80877 100644 --- a/STL_Extension/include/CGAL/Compact_container.h +++ b/STL_Extension/include/CGAL/Compact_container.h @@ -177,6 +177,8 @@ namespace internal { template static void set_erase_counter(Element &, unsigned int) {} template + static void restore_erase_counter(Element*, unsigned int) {} + template static void increment_erase_counter(Element &) {} }; @@ -192,12 +194,24 @@ namespace internal { return e.erase_counter(); } + template + static unsigned int erase_counter(Element* e) + { + return e->erase_counter(); + } + template static void set_erase_counter(Element &e, unsigned int c) { e.set_erase_counter(c); } + template + static void restore_erase_counter(Element* e, unsigned int c) + { + e->set_erase_counter(c); + } + template static void increment_erase_counter(Element &e) { @@ -392,13 +406,18 @@ public: iterator emplace(const Args&... args) { + typedef internal::Erase_counter_strategy< + internal::has_increment_erase_counter::value> EraseCounterStrategy; + if (free_list == nullptr) allocate_new_block(); pointer ret = free_list; free_list = clean_pointee(ret); std::size_t ts; + const auto ec = EraseCounterStrategy::erase_counter(ret); CGAL_USE(ts); + if constexpr (Time_stamper::has_timestamp) { ts = ret->time_stamp(); } @@ -407,6 +426,7 @@ public: ret->set_time_stamp(ts); } Time_stamper::set_time_stamp(ret, time_stamp); + EraseCounterStrategy::restore_erase_counter(ret, ec); CGAL_assertion(type(ret) == USED); ++size_; return iterator(ret, 0); @@ -414,6 +434,9 @@ public: iterator insert(const T &t) { + typedef internal::Erase_counter_strategy< + internal::has_increment_erase_counter::value> EraseCounterStrategy; + if (free_list == nullptr) allocate_new_block(); @@ -421,6 +444,8 @@ public: free_list = clean_pointee(ret); std::size_t ts; CGAL_USE(ts); + const auto ec = EraseCounterStrategy::erase_counter(ret); + if constexpr (Time_stamper::has_timestamp) { ts = ret->time_stamp(); } @@ -429,6 +454,8 @@ public: ret->set_time_stamp(ts); } Time_stamper::set_time_stamp(ret, time_stamp); + EraseCounterStrategy::restore_erase_counter(ret, ec); + CGAL_assertion(type(ret) == USED); ++size_; return iterator(ret, 0); @@ -455,6 +482,7 @@ public: CGAL_precondition(type(&*x) == USED); EraseCounterStrategy::increment_erase_counter(*x); + const auto ec = EraseCounterStrategy::erase_counter(*x); std::size_t ts; CGAL_USE(ts); if constexpr (Time_stamper::has_timestamp) { @@ -464,6 +492,8 @@ public: if constexpr (Time_stamper::has_timestamp) { x->set_time_stamp(ts); } + EraseCounterStrategy::restore_erase_counter(&*x, ec); + put_on_free_list(&*x); --size_; diff --git a/STL_Extension/include/CGAL/Concurrent_compact_container.h b/STL_Extension/include/CGAL/Concurrent_compact_container.h index 7f5533f92b7..f02a518db00 100644 --- a/STL_Extension/include/CGAL/Concurrent_compact_container.h +++ b/STL_Extension/include/CGAL/Concurrent_compact_container.h @@ -80,6 +80,8 @@ namespace CCC_internal { template static void set_erase_counter(Element &, unsigned int) {} template + static void restore_erase_counter(Element*, unsigned int) {} + template static void increment_erase_counter(Element &) {} }; @@ -95,12 +97,24 @@ namespace CCC_internal { return e.erase_counter(); } + template + static unsigned int erase_counter(Element* e) + { + return e->erase_counter(); + } + template static void set_erase_counter(Element &e, unsigned int c) { e.set_erase_counter(c); } + template + static void restore_erase_counter(Element* e, unsigned int c) + { + e->set_erase_counter(c); + } + template static void increment_erase_counter(Element &e) { @@ -350,7 +364,7 @@ public: CCC_internal::has_increment_erase_counter::value> EraseCounterStrategy; FreeList * fl = get_free_list(); pointer ret = init_insert(fl); - auto erase_counter = EraseCounterStrategy::erase_counter(*ret);; + auto erase_counter = EraseCounterStrategy::erase_counter(*ret); new (ret) value_type(args...); EraseCounterStrategy::set_erase_counter(*ret, erase_counter); return finalize_insert(ret, fl); @@ -390,9 +404,10 @@ private: CGAL_precondition(type(x) == USED); EraseCounterStrategy::increment_erase_counter(*x); - + auto erase_counter = EraseCounterStrategy::erase_counter(*x); + auto ptr = &*x; std::allocator_traits::destroy(m_alloc, &*x); - + EraseCounterStrategy::set_erase_counter(ptr, erase_counter); put_on_free_list(&*x, fl); } public: From b6163a5e7d7e72e6694216bfcb22e3dab1b12d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Loriot?= Date: Wed, 21 May 2025 09:24:05 +0200 Subject: [PATCH 2/2] missing overload --- STL_Extension/include/CGAL/Concurrent_compact_container.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/STL_Extension/include/CGAL/Concurrent_compact_container.h b/STL_Extension/include/CGAL/Concurrent_compact_container.h index f02a518db00..9d686046f45 100644 --- a/STL_Extension/include/CGAL/Concurrent_compact_container.h +++ b/STL_Extension/include/CGAL/Concurrent_compact_container.h @@ -109,6 +109,12 @@ namespace CCC_internal { e.set_erase_counter(c); } + template + static void set_erase_counter(Element* e, unsigned int c) + { + e->set_erase_counter(c); + } + template static void restore_erase_counter(Element* e, unsigned int c) {