| 1 |
/* Copyright (c) 2023, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
/// @addtogroup Replication |
| 25 |
/// @{ |
| 26 |
/// |
| 27 |
/// @file allocator.h |
| 28 |
/// |
| 29 |
/// @brief Allocator class that uses a polymorphic Memory_resource to |
| 30 |
/// allocate memory. |
| 31 |
|
| 32 |
#ifndef MYSQL_RESOURCE_ALLOCATOR_H_ |
| 33 |
#define MYSQL_RESOURCE_ALLOCATOR_H_ |
| 34 |
|
| 35 |
#include <cassert> // assert |
| 36 |
#include <limits> // std::numeric_limits |
| 37 |
|
| 38 |
#include "memory_resource.h" |
| 39 |
|
| 40 |
namespace mysqlns::resource { |
| 41 |
|
| 42 |
/// @brief Allocator using a Memory_resource to do the allocator. |
| 43 |
/// |
| 44 |
/// A library that allocates memory should allow the user to pass a |
| 45 |
/// Memory_resource object which defaults to a default-constructed |
| 46 |
/// instance, Memory_resource(). Internally it should create the |
| 47 |
/// Allocator<T> classes it needs (possibly several, for different |
| 48 |
/// classes T), using the given Memory_resource object. Users of the |
| 49 |
/// library *outside* the server should just use the default |
| 50 |
/// Memory_resource. Users of the library *inside* the server should |
| 51 |
/// setup a PSI key and pass the result from @c memory_resource(Key) |
| 52 |
/// to the library. |
| 53 |
template <class T> |
| 54 |
class Allocator { |
| 55 |
public: |
| 56 |
using value_type = T; |
| 57 |
using size_type = std::size_t; |
| 58 |
using difference_type = std::ptrdiff_t; |
| 59 |
|
| 60 |
/// Construct a new Allocator using the given Memory_resource. |
| 61 |
/// |
| 62 |
/// @param memory_resource The memory resource. By default, this |
| 63 |
/// uses a default-constructed Memory_resource, so it uses |
| 64 |
/// std::malloc and std::free for allocations. |
| 65 |
explicit Allocator(Memory_resource memory_resource = Memory_resource()) |
| 66 |
: m_memory_resource(std::move(memory_resource)) {} |
| 67 |
|
| 68 |
/// Implicit conversion from other instance. |
| 69 |
/// |
| 70 |
/// This is required by Windows implementation of |
| 71 |
/// @c std::vector<Allocator>. |
| 72 |
template <class U> |
| 73 |
explicit Allocator(const Allocator<U> &other) |
| 74 |
: m_memory_resource(other.get_memory_resource()) {} |
| 75 |
|
| 76 |
/// Use the Memory_resource to allocate the given number of elements |
| 77 |
/// of type T. |
| 78 |
/// |
| 79 |
/// @param n The number of elements. |
| 80 |
/// |
| 81 |
/// @return The new pointer. |
| 82 |
/// |
| 83 |
/// @throws std::bad_alloc on out of memory conditions. |
| 84 |
[[nodiscard]] constexpr T *allocate(size_type n) { |
| 85 |
T *p = static_cast<T *>(m_memory_resource.allocate(n * sizeof(value_type))); |
| 86 |
if (p == nullptr) throw std::bad_alloc(); |
| 87 |
return p; |
| 88 |
} |
| 89 |
|
| 90 |
/// Use the Memory_resource to deallocate the given pointer. |
| 91 |
/// |
| 92 |
/// @param p The pointer to deallocate. |
| 93 |
/// |
| 94 |
/// @param size Unused. |
| 95 |
constexpr void deallocate(T *p, [[maybe_unused]] size_type size) { |
| 96 |
m_memory_resource.deallocate(p); |
| 97 |
} |
| 98 |
|
| 99 |
/// Return a Deleter function for objects allocated by this class. |
| 100 |
/// |
| 101 |
/// Such a Deleter must be specified when constructing a smart |
| 102 |
/// pointer to an object created by this Allocator, for example: |
| 103 |
/// |
| 104 |
/// @code |
| 105 |
/// Allocator<T> allocator(some_memory_resource); |
| 106 |
/// T *obj = allocator.allocate(1); |
| 107 |
/// std::shared_ptr<T> ptr(obj, allocator.get_deleter()); |
| 108 |
/// @endcode |
| 109 |
/// |
| 110 |
/// @retval Deleter function that takes a `T*` as argument and |
| 111 |
/// uses the Memory_resource to deallocate it. |
| 112 |
std::function<void(T *)> get_deleter() { |
| 113 |
auto deallocator = m_memory_resource.get_deallocator(); |
| 114 |
// Capture by value so we get a self-contained object that may |
| 115 |
// outlive this Allocator and the Memory_resource if needed. |
| 116 |
return [=](T *p) { |
| 117 |
p->~T(); |
| 118 |
deallocator(p); |
| 119 |
}; |
| 120 |
} |
| 121 |
|
| 122 |
/// Return the underlying Memory_resource object. |
| 123 |
Memory_resource get_memory_resource() const { return m_memory_resource; } |
| 124 |
|
| 125 |
private: |
| 126 |
/// The underlying Memory_resource object. |
| 127 |
const Memory_resource m_memory_resource; |
| 128 |
}; |
| 129 |
|
| 130 |
/// Compare two Allocator objects for equality. |
| 131 |
template <class T> |
| 132 |
bool operator==([[maybe_unused]] const Allocator<T> &a1, |
| 133 |
[[maybe_unused]] const Allocator<T> &a2) { |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
/// Compare two Allocator objects for inequality. |
| 138 |
template <class T> |
| 139 |
bool operator!=([[maybe_unused]] const Allocator<T> &a1, |
| 140 |
[[maybe_unused]] const Allocator<T> &a2) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
} // namespace mysqlns::resource |
| 145 |
|
| 146 |
/// @} (end of group Replication) |
| 147 |
|
| 148 |
#endif // MYSQL_RESOURCE_ALLOCATOR_H_ |