From 5fec1f80ff0a1e63c122dd1c84ea932b31da0267 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 3 Sep 2023 13:54:52 +0800 Subject: [PATCH] Fix compilation errors. --- include/libimp/uninitialized.h | 15 +++++++++++++++ include/libpmr/small_storage.h | 16 +++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/libimp/uninitialized.h b/include/libimp/uninitialized.h index 335d001..7837bf9 100644 --- a/include/libimp/uninitialized.h +++ b/include/libimp/uninitialized.h @@ -83,6 +83,21 @@ void destroy(ForwardIt first, ForwardIt last) noexcept { #endif } +/** + * \brief Destroys a number of objects in a range. + * \see https://en.cppreference.com/w/cpp/memory/destroy_n +*/ +template +ForwardIt destroy_n(ForwardIt first, Size n) noexcept { +#if defined(LIBIMP_CPP_17) + return std::destroy_n(first, n); +#else + for (; n > 0; (void) ++first, --n) + destroy(std::addressof(*first)); + return first; +#endif +} + /** * \brief Constructs objects by default-initialization * in an uninitialized area of memory, defined by a start and a count. diff --git a/include/libpmr/small_storage.h b/include/libpmr/small_storage.h index 79c613c..068bab9 100644 --- a/include/libpmr/small_storage.h +++ b/include/libpmr/small_storage.h @@ -207,6 +207,7 @@ struct holder_info { std::size_t sizeof_type; std::size_t count; void (*copy)(allocator const &, void const *s, void *d); + void (*dest)(void *p, std::size_t n) noexcept; }; template @@ -260,6 +261,9 @@ public: if (dst.value_ptr_ == nullptr) return; dst.info_ = src.info_; }; + info_.dest = [](void *p, std::size_t n) noexcept { + ::LIBIMP::destroy_n(static_cast(p), n); + }; } bool valid() const noexcept override { @@ -292,23 +296,21 @@ public: void move_to(allocator const &, void *p) noexcept override { auto *des = ::LIBIMP::construct(p); - if (value_ptr_ == nullptr) { - return; - } + if (!valid()) return; std::swap(value_ptr_, des->value_ptr_); std::swap(info_, des->info_); } void copy_to(allocator const &alloc, void *p) const noexcept(false) override { auto *des = ::LIBIMP::construct(p); - if (value_ptr_ == nullptr) { - return; - } + if (!valid()) return; info_.copy(alloc, this, des); } void destroy(allocator const &alloc) noexcept override { - alloc.deallocate(::LIBIMP::destroy(value_ptr_), info_.sizeof_type * info_.count); + if (!valid()) return; + info_.dest(value_ptr_, count()); + alloc.deallocate(value_ptr_, sizeof_heap()); } };