/* Copyright (c) 2020, 2024, Oracle and/or its affiliates. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is designed to work with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have either included with the program or referenced in the documentation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef MEMORY_UNIQUE_PTR_INCLUDED #define MEMORY_UNIQUE_PTR_INCLUDED #include #include #include #include #include #include #include #include #include #include #include "my_sys.h" #include "mysql/service_mysql_alloc.h" // my_malloc #include "sql/memory/aligned_atomic.h" // memory::cache_line_size #include "sql/memory/ref_ptr.h" // memory::Ref_ptr namespace memory { namespace traits { /** Tests for the existence of `allocate(size_t)` in order to disambiguate if `T` is an allocator class. */ template auto test_for_allocate(int T::*) -> decltype(std::declval().allocate(std::declval()), std::true_type{}); template std::false_type test_for_allocate(...); } // namespace traits /** Struct that allows for checking if `T` fulfills the Allocator named requirements. */ template struct is_allocator : decltype(traits::test_for_allocate(nullptr)) {}; /** Allocator class for instrumenting allocated memory with Performance Schema keys. */ template class PFS_allocator { using value_type = T; using size_type = size_t; /** Constructor for the class that takes the PFS key to be used. @param key The PFS key to be used. */ PFS_allocator(PSI_memory_key key); /** Copy constructor. @param rhs The object to copy from. */ template PFS_allocator(PFS_allocator const &rhs) noexcept; /** Move constructor. @param rhs The object to move from. */ template PFS_allocator(PFS_allocator &&rhs) noexcept; /** Retrieves the PFS for `this` allocator object. @return The PFS key. */ PSI_memory_key key() const; /** Allocate `n` bytes and return a pointer to the beginning of the allocated memory. @param n The size of the memory to allocate. @return A pointer to the beginning of the allocated memory. */ T *allocate(std::size_t n); /** Deallocates the `n` bytes stored in the memory pointer `p` is pointing to. @param p The pointer to the beginning of the memory to deallocate. @param n The size of the memory to deallocate. */ void deallocate(T *p, std::size_t n) noexcept; /** In-place constructs an object of class `U` in the memory pointed by `p`. @param p The pointer to the beginning of the memory to construct the object in. @param args The parameters to be used with the `U` constructor. */ template void construct(U *p, Args &&... args); /** In-place invokes the destructor for class `T` on object pointed by `p`. @param p The object pointer to invoke the destructor on. */ void destroy(T *p); /** The maximum size available to allocate. @return The maximum size available to allocate. */ size_type max_size() const; private: /** The PFS key to be used to allocate memory */ PSI_memory_key m_key; }; /** Smart pointer to hold a unique pointer to a heap allocated memory of type `T`, constructed using a specific allocator. Template parameters are as follows: - `T` is the type of the pointer to allocate. It may be an array type. - `A` the allocator to use. If none is passed, `std::nullptr_t` is passed and regular `new` and `delete` are used to construct the memory. */ template class Unique_ptr { public: using type = typename std::remove_extent::type; using pointer = type *; using reference = type &; /** Default class constructor, only to be used with no specific allocator. */ template < typename D = T, typename B = A, std::enable_if_t::value> * = nullptr> Unique_ptr(); /** Class constructor, to be used with specific allocators, passing the allocator object to be used. @param alloc The allocator instance to be used. */ template < typename D = T, typename B = A, std::enable_if_t::value> * = nullptr> Unique_ptr(A &alloc); /** Class constructor, to be used with specific allocators and when `T` is an array type, passing the allocator object to be used and the size of the array. @param alloc The allocator instance to be used. @param size The size of the array to allocate. */ template ::value && std::is_array::value> * = nullptr> Unique_ptr(A &alloc, size_t size); /** Class constructor, to be used with no specific allocators and when `T` is an array type, passing the allocator object to be used and the size of the array. @param size The size of the array to allocate. */ template ::value && std::is_array::value> * = nullptr> Unique_ptr(size_t size); /** Class constructor, to be used with specific allocators and when `T` is not an array type, passing the allocator object to be used and the parameters to be used with `T` object constructor. @param alloc The allocator instance to be used. @param args The parameters to be used with `T` object constructor. */ template ::value && !std::is_array::value> * = nullptr> Unique_ptr(A &alloc, Args &&... args); /** Class constructor, to be used with no specific allocators and when `T` is not an array type, passing the parameters to be used with `T` object constructor. @param args The parameters to be used with `T` object constructor. */ template ::value && !std::is_array::value> * = nullptr> Unique_ptr(Args &&... args); // Deleted copy constructor Unique_ptr(Unique_ptr const &rhs) = delete; /** Move constructor. @param rhs The object to move data from. */ Unique_ptr(Unique_ptr &&rhs); /** Destructor for the class. */ virtual ~Unique_ptr(); // Deleted copy operator Unique_ptr &operator=(Unique_ptr const &rhs) = delete; /** Move operator. @param rhs The object to move data from. */ Unique_ptr &operator=(Unique_ptr &&rhs); /** Arrow operator to access the underlying object of type `T`. @return A pointer to the underlying object of type `T`. */ template ::value> * = nullptr> pointer operator->() const; /** Star operator to access the underlying object of type `T`. @return A reference to the underlying object of type `T`. */ reference operator*() const; /** Subscript operator, to access an array element when `T` is of array type. @param index The index of the element to retrieve the value for. @return A reference to the value stored at index. */ template ::value> * = nullptr> reference operator[](size_t index) const; /** Casting operator to bool. @return `true` if the underlying pointer is instantiated, `false` otherwise. */ operator bool() const; /** Releases the ownership of the underlying allocated memory and returns a pointer to the beginning of that memory. This smart pointer will no longer manage the underlying memory. @return the pointer to the allocated and no longer managed memory. */ template < typename B = A, std::enable_if_t::value> * = nullptr> pointer release(); /** Releases the ownership of the underlying allocated memory and returns a pointer to the beginning of that memory. This smart pointer will no longer manage the underlying memory. @return the pointer to the allocated and no longer managed memory. */ template < typename B = A, std::enable_if_t::value> * = nullptr> pointer release(); /** Returns a pointer to the underlying allocated memory. @return A pointer to the underlying allocated memory */ pointer get() const; /** The size of the memory allocated, in bytes. @return The size of the memory allocated, in bytes */ size_t size() const; /** Will resize the allocated memory to `new_size`. If the configure allocator supports this operation, the allocator is used. If not, a new memory chunk is allocated and the memory is copied. @param new_size The new desired size for the memory. @return The reference to `this` object, for chaining purposed. */ template < typename D = T, typename B = A, std::enable_if_t::value && std::is_same::value> * = nullptr> Unique_ptr &reserve(size_t new_size); /** Will resize the allocated memory to `new_size`. If the configure allocator supports this operation, the allocator is used. If not, a new memory chunk is allocated and the memory is copied. @param new_size The new desired size for the memory. @return The reference to `this` object, for chaining purposed. */ template < typename D = T, typename B = A, std::enable_if_t::value && !std::is_same::value> * = nullptr> Unique_ptr &reserve(size_t new_size); /** Returns the used allocator instance, if any. @return The reference to the allocator object. */ A &allocator() const; private: /** The pointer to the underlying allocated memory */ alignas(std::max_align_t) pointer m_underlying{nullptr}; /** The allocator to be used to allocate memory */ memory::Ref_ptr m_allocator; /** The size of the allocated memory */ size_t m_size{0}; /** Clears the underlying pointer and size. */ void reset(); /** Deallocates the underlying allocated memory. */ template ::value && std::is_array::value> * = nullptr> void destroy(); /** Deallocates the underlying allocated memory. */ template ::value && !std::is_array::value> * = nullptr> void destroy(); /** Deallocates the underlying allocated memory. */ template ::value && std::is_array::value> * = nullptr> void destroy(); /** Deallocates the underlying allocated memory. */ template ::value && !std::is_array::value> * = nullptr> void destroy(); /** Clones the underlying memory and returns a pointer to the clone memory. @return A pointer to the cloned underlying memory. */ template ::value> * = nullptr> pointer clone() const; /** Clones the underlying memory and returns a pointer to the clone memory. @return A pointer to the cloned underlying memory. */ template ::value> * = nullptr> pointer clone() const; }; /** In-place constructs a new unique pointer with no specific allocator and with array type `T`. @param size The size of the array to allocate. @return A new instance of unique pointer. */ template ::value> * = nullptr> Unique_ptr make_unique(size_t size); /** In-place constructs a new unique pointer with a specific allocator and with array type `T`. @param alloc A reference to the allocator object to use. @param size The size of the array to allocate. @return A new instance of unique pointer. */ template ::value> * = nullptr> Unique_ptr make_unique(A &alloc, size_t size); /** In-place constructs a new unique pointer with a specific allocator and with non-array type `T`. @param alloc A reference to the allocator object to use. @param args The parameters to be used in constructing the instance of `T`. @return A new instance of unique pointer. */ template ::value && memory::is_allocator::value> * = nullptr> Unique_ptr make_unique(A &alloc, Args &&... args); /** In-place constructs a new unique pointer with no specific allocator and with non-array type `T`. @param args The parameters to be used in constructing the instance of `T`. @return A new instance of unique pointer. */ template ::value> * = nullptr> Unique_ptr make_unique(Args &&... args); } // namespace memory // global scope template bool operator==(const memory::PFS_allocator &lhs, const memory::PFS_allocator &rhs) { return lhs.key() == rhs.key(); } template bool operator!=(const memory::PFS_allocator &lhs, const memory::PFS_allocator &rhs) { return lhs.key() != rhs.key(); } template bool operator==(memory::Unique_ptr const &lhs, memory::Unique_ptr const &rhs) { return static_cast(lhs.get()) == static_cast(rhs.get()); } template bool operator!=(memory::Unique_ptr const &lhs, memory::Unique_ptr const &rhs) { return !(lhs == rhs); } template bool operator==(memory::Unique_ptr const &lhs, std::nullptr_t) { return lhs.get() == nullptr; } template bool operator!=(memory::Unique_ptr const &lhs, std::nullptr_t) { return !(lhs == nullptr); } // global scope template memory::PFS_allocator::PFS_allocator(PSI_memory_key key) : m_key{key} {} template template memory::PFS_allocator::PFS_allocator(PFS_allocator const &rhs) noexcept : m_key{rhs.m_key} {} template template memory::PFS_allocator::PFS_allocator(PFS_allocator &&rhs) noexcept : m_key{rhs.m_key} { rhs.m_key = 0; } template PSI_memory_key memory::PFS_allocator::key() const { return this->m_key; } template T *memory::PFS_allocator::allocate(std::size_t n) { if (n <= std::numeric_limits::max() / sizeof(T)) { if (auto p = static_cast(my_malloc(this->m_key, (n * sizeof(T)), MYF(MY_WME | ME_FATALERROR)))) return p; } throw std::bad_alloc(); } template void memory::PFS_allocator::deallocate(T *p, std::size_t) noexcept { my_free(p); } template template void memory::PFS_allocator::construct(U *p, Args &&... args) { assert(p != nullptr); try { ::new ((void *)p) U(std::forward(args)...); } catch (...) { assert(false); // Constructor should not throw an exception. } } template void memory::PFS_allocator::destroy(T *p) { assert(p != nullptr); try { p->~T(); } catch (...) { assert(false); // Destructor should not throw an exception } } template size_t memory::PFS_allocator::max_size() const { return std::numeric_limits::max() / sizeof(T); } template template ::value> *> memory::Unique_ptr::Unique_ptr() : m_underlying{nullptr}, m_size{0} {} template template ::value> *> memory::Unique_ptr::Unique_ptr(A &alloc) : m_underlying{nullptr}, m_allocator{alloc}, m_size{0} {} template template ::value && std::is_array::value> *> memory::Unique_ptr::Unique_ptr(A &alloc, size_t size) : m_underlying{nullptr}, m_allocator{alloc}, m_size{size} { this->m_underlying = this->m_allocator->allocate(this->m_size); } template template ::value && std::is_array::value> *> memory::Unique_ptr::Unique_ptr(size_t size) : m_underlying{new type[size]}, m_size{size} {} template template ::value && !std::is_array::value> *> memory::Unique_ptr::Unique_ptr(A &alloc, Args &&... args) : m_underlying{nullptr}, m_allocator{alloc}, m_size{sizeof(T)} { this->m_underlying = this->m_allocator->allocate(this->m_size); this->m_allocator->construct(this->m_underlying, std::forward(args)...); } template template ::value && !std::is_array::value> *> memory::Unique_ptr::Unique_ptr(Args &&... args) : m_underlying{new T{std::forward(args)...}}, m_size{sizeof(T)} {} template memory::Unique_ptr::Unique_ptr(memory::Unique_ptr &&rhs) : m_underlying{rhs.m_underlying}, m_allocator{rhs.m_allocator}, m_size{rhs.m_size} { rhs.reset(); } template memory::Unique_ptr::~Unique_ptr() { this->destroy(); } template typename memory::Unique_ptr &memory::Unique_ptr::operator=( memory::Unique_ptr &&rhs) { this->m_underlying = rhs.m_underlying; this->m_allocator = rhs.m_allocator; this->m_size = rhs.m_size; rhs.reset(); return (*this); } template template ::value> *> typename memory::Unique_ptr::pointer memory::Unique_ptr::operator->() const { return this->m_underlying; } template typename memory::Unique_ptr::reference memory::Unique_ptr::operator*() const { return (*this->m_underlying); } template template ::value> *> typename memory::Unique_ptr::reference memory::Unique_ptr::operator[](size_t index) const { return this->m_underlying[index]; } template memory::Unique_ptr::operator bool() const { return this->m_underlying != nullptr; } template template ::value> *> typename memory::Unique_ptr::pointer memory::Unique_ptr::release() { pointer to_return = this->m_underlying; this->reset(); return to_return; } template template ::value> *> typename memory::Unique_ptr::pointer memory::Unique_ptr::release() { pointer to_return = this->m_allocator->release(this->m_underlying); if (to_return != this->m_underlying) { to_return = this->clone(); this->destroy(); } else { this->reset(); } return to_return; } template typename memory::Unique_ptr::pointer memory::Unique_ptr::get() const { return this->m_underlying; } template size_t memory::Unique_ptr::size() const { return this->m_size; } template template ::value && std::is_same::value> *> typename memory::Unique_ptr &memory::Unique_ptr::reserve( size_t new_size) { pointer old_ptr = this->m_underlying; this->m_underlying = new type[new_size]; if (this->m_size != 0) { std::copy(old_ptr, old_ptr + std::min(this->m_size, new_size), this->m_underlying); } this->m_size = new_size; delete[] old_ptr; return (*this); } template template ::value && !std::is_same::value> *> typename memory::Unique_ptr &memory::Unique_ptr::reserve( size_t new_size) { if (this->m_allocator->can_resize()) { this->m_underlying = this->m_allocator->resize(this->m_underlying, this->m_size, new_size); } else { pointer old_ptr = this->m_underlying; this->m_underlying = this->m_allocator->allocate(new_size); if (this->m_size != 0) { std::copy(old_ptr, old_ptr + std::min(this->m_size, new_size), this->m_underlying); this->m_allocator->deallocate(old_ptr, this->m_size); } } this->m_size = new_size; return (*this); } template A &memory::Unique_ptr::allocator() const { return *this->m_allocator; } template void memory::Unique_ptr::reset() { this->m_underlying = nullptr; this->m_size = 0; } template template ::value && std::is_array::value> *> void memory::Unique_ptr::destroy() { if (this->m_underlying != nullptr) { delete[] this->m_underlying; this->reset(); } } template template ::value && !std::is_array::value> *> void memory::Unique_ptr::destroy() { if (this->m_underlying != nullptr) { delete this->m_underlying; this->reset(); } } template template ::value && std::is_array::value> *> void memory::Unique_ptr::destroy() { if (this->m_underlying != nullptr) { this->m_allocator->deallocate(this->m_underlying, this->m_size); this->reset(); } } template template ::value && !std::is_array::value> *> void memory::Unique_ptr::destroy() { if (this->m_underlying != nullptr) { this->m_allocator->destroy(this->m_underlying); this->m_allocator->deallocate(this->m_underlying, this->m_size); this->reset(); } } template template ::value> *> typename memory::Unique_ptr::pointer memory::Unique_ptr::clone() const { pointer to_return = new type[this->m_size]; std::copy(this->m_underlying, this->m_underlying + this->m_size, to_return); return to_return; } template template ::value> *> typename memory::Unique_ptr::pointer memory::Unique_ptr::clone() const { pointer to_return = new type(*this->m_underlying); return to_return; } #ifndef IN_DOXYGEN // Doxygen doesn't understand this construction. template ::value> *> memory::Unique_ptr memory::make_unique(size_t size) { return memory::Unique_ptr{size}; } template ::value> *> memory::Unique_ptr memory::make_unique(A &alloc, size_t size) { return std::move(memory::Unique_ptr{alloc, size}); } template ::value && memory::is_allocator::value> *> memory::Unique_ptr memory::make_unique(A &alloc, Args &&... args) { return std::move( memory::Unique_ptr{alloc, std::forward(args)...}); } template ::value> *> memory::Unique_ptr memory::make_unique(Args &&... args) { return memory::Unique_ptr{std::forward(args)...}; } #endif #endif // MEMORY_UNIQUE_PTR_INCLUDED