MySQL 8.0.37
Source Code Documentation
allocator.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, 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
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.
53template <class T>
54class Allocator {
55 public:
56 using value_type = T;
57 using size_type = std::size_t;
58 using difference_type = std::ptrdiff_t;
59
61 using const_pointer = const value_type *;
62
63 using reference = T &;
64 using const_reference = const T &;
65
66 /// Construct a new Allocator using the given Memory_resource.
67 ///
68 /// @param memory_resource The memory resource. By default, this
69 /// uses a default-constructed Memory_resource, so it uses
70 /// std::malloc and std::free for allocations.
71 explicit Allocator(Memory_resource memory_resource = Memory_resource())
72 : m_memory_resource(std::move(memory_resource)) {}
73
74 /// Implicit conversion from other instance.
75 ///
76 /// This is required by Windows implementation of
77 /// @c std::vector<Allocator>.
78 template <class U>
79 explicit Allocator(const Allocator<U> &other)
81
82 /// Use the Memory_resource to allocate the given number of elements
83 /// of type T.
84 ///
85 /// @param n The number of elements.
86 ///
87 /// @param hint Unused.
88 ///
89 /// @return The new pointer.
90 ///
91 /// @throws std::bad_alloc on out of memory conditions.
92 pointer allocate(size_type n, const_pointer hint [[maybe_unused]] = nullptr) {
93 pointer p = static_cast<pointer>(
95 if (p == nullptr) throw std::bad_alloc();
96 return p;
97 }
98
99 /// Use the Memory_resource to deallocate the given pointer.
100 ///
101 /// @param p The pointer to deallocate.
102 ///
103 /// @param size Unused.
104 void deallocate(pointer p, [[maybe_unused]] size_type size) {
106 }
107
108 /// Return a Deleter function for objects allocated by this class.
109 ///
110 /// Such a Deleter must be specified when constructing a smart
111 /// pointer to an object created by this Allocator, for example:
112 ///
113 /// @code
114 /// Allocator<T> allocator(some_memory_resource);
115 /// T *obj = allocator.allocate(1);
116 /// std::shared_ptr<T> ptr(obj, allocator.get_deleter());
117 /// @endcode
118 ///
119 /// @retval Deleter function that takes a `pointer` as argument and
120 /// uses the Memory_resource to deallocate it.
121 std::function<void(pointer)> get_deleter() {
122 auto deallocator = m_memory_resource.get_deallocator();
123 // Capture by value so we get a self-contained object that may
124 // outlive this Allocator and the Memory_resource if needed.
125 return [=](pointer p) {
126 p->~T();
127 deallocator(p);
128 };
129 }
130
131 /// In-place construct a new object of the given type.
132 ///
133 /// @param p Pointer to memory area where the new object will be
134 /// constructed.
135 ///
136 /// @param args Arguments to pass to the constructor.
137 ///
138 /// @throws std::bad_alloc on out of memory conditions.
139 template <class U, class... Args>
140 [[deprecated("std::allocator::construct is deprecated in C++17")]] void
141 construct(U *p, Args &&... args) {
142 assert(p != nullptr);
143 ::new ((void *)p) U(std::forward<Args>(args)...);
144 // Constructor may throw an exception, which we propagate to the
145 // caller. This may happen if the constructor itself allocates
146 // memory (e.g. for members) using @c allocate.
147 }
148
149 /// Destroy an object but do not deallocate the memory it occupies.
150 ///
151 /// @param p Pointer to the object.
152 [[deprecated("std::allocator::destroy is deprecated in C++17")]] void destroy(
153 pointer p) {
154 assert(p != nullptr);
155 try {
156 p->~T();
157 } catch (...) {
158 assert(false); // Destructor should not throw an exception
159 }
160 }
161
162 /// The maximum number of T objects that fit in memory.
163 [[deprecated("std::allocator::max_size is deprecated in C++17")]] size_type
164 max_size() const {
165 return std::numeric_limits<size_type>::max() / sizeof(T);
166 }
167
168 /// Rebind the allocator as requried by the Allocator named requirement.
169 template <class U>
170 struct rebind {
172 };
173
174 /// Return the underlying Memory_resource object.
176
177 private:
178 /// The underlying Memory_resource object.
180};
181
182/// Compare two Allocator objects for equality.
183template <class T>
184bool operator==([[maybe_unused]] const Allocator<T> &a1,
185 [[maybe_unused]] const Allocator<T> &a2) {
186 return true;
187}
188
189/// Compare two Allocator objects for inequality.
190template <class T>
191bool operator!=([[maybe_unused]] const Allocator<T> &a1,
192 [[maybe_unused]] const Allocator<T> &a2) {
193 return true;
194}
195
196} // namespace mysqlns::resource
197
198/// @} (end of group Replication)
199
200#endif // MYSQL_RESOURCE_ALLOCATOR_H_
Allocator using a Memory_resource to do the allocator.
Definition: allocator.h:54
value_type * pointer
Definition: allocator.h:60
std::size_t size_type
Definition: allocator.h:57
void construct(U *p, Args &&... args)
In-place construct a new object of the given type.
Definition: allocator.h:141
const Memory_resource m_memory_resource
The underlying Memory_resource object.
Definition: allocator.h:179
T & reference
Definition: allocator.h:63
void destroy(pointer p)
Destroy an object but do not deallocate the memory it occupies.
Definition: allocator.h:152
const value_type * const_pointer
Definition: allocator.h:61
Allocator(Memory_resource memory_resource=Memory_resource())
Construct a new Allocator using the given Memory_resource.
Definition: allocator.h:71
std::ptrdiff_t difference_type
Definition: allocator.h:58
size_type max_size() const
The maximum number of T objects that fit in memory.
Definition: allocator.h:164
pointer allocate(size_type n, const_pointer hint=nullptr)
Use the Memory_resource to allocate the given number of elements of type T.
Definition: allocator.h:92
Allocator(const Allocator< U > &other)
Implicit conversion from other instance.
Definition: allocator.h:79
Memory_resource get_memory_resource() const
Return the underlying Memory_resource object.
Definition: allocator.h:175
const T & const_reference
Definition: allocator.h:64
void deallocate(pointer p, size_type size)
Use the Memory_resource to deallocate the given pointer.
Definition: allocator.h:104
T value_type
Definition: allocator.h:56
std::function< void(pointer)> get_deleter()
Return a Deleter function for objects allocated by this class.
Definition: allocator.h:121
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
void deallocate(Ptr_t p) const
Deallocate memory using the provided deallocator.
Definition: memory_resource.h:118
Deallocator_t get_deallocator() const
Return the deallocator.
Definition: memory_resource.h:121
void * allocate(Size_t n) const
Allocate memory using the provided allocator.
Definition: memory_resource.h:113
const char * p
Definition: ctype-mb.cc:1237
#define U
Definition: ctype-tis620.cc:75
Class that wraps resources in a polymorphic manner.
Definition: allocator.h:40
bool operator!=(const Allocator< T > &a1, const Allocator< T > &a2)
Compare two Allocator objects for inequality.
Definition: allocator.h:191
bool operator==(const Allocator< T > &a1, const Allocator< T > &a2)
Compare two Allocator objects for equality.
Definition: allocator.h:184
Definition: gcs_xcom_synode.h:64
Rebind the allocator as requried by the Allocator named requirement.
Definition: allocator.h:170
Allocator< U > other
Definition: allocator.h:171
Definition: dtoa.cc:595
int n
Definition: xcom_base.cc:509