MySQL 8.4.0
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/// @file allocator.h
25///
26/// @brief Allocator class that uses a polymorphic Memory_resource to
27/// allocate memory.
28
29#ifndef MYSQL_BINLOG_EVENT_RESOURCE_ALLOCATOR_H
30#define MYSQL_BINLOG_EVENT_RESOURCE_ALLOCATOR_H
31
32#include <cassert> // assert
33#include <limits> // std::numeric_limits
34
36
37/// @addtogroup GroupLibsMysqlBinlogEvent
38/// @{
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
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)
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) {
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.
124
125 private:
126 /// The underlying Memory_resource object.
128};
129
130/// Compare two Allocator objects for equality.
131template <class T>
132bool 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.
138template <class T>
139bool operator!=([[maybe_unused]] const Allocator<T> &a1,
140 [[maybe_unused]] const Allocator<T> &a2) {
141 return true;
142}
143
144} // namespace mysql::binlog::event::resource
145
146/// @}
147
148#endif // MYSQL_BINLOG_EVENT_RESOURCE_ALLOCATOR_H
Allocator using a Memory_resource to do the allocator.
Definition: allocator.h:54
std::size_t size_type
Definition: allocator.h:57
const Memory_resource m_memory_resource
The underlying Memory_resource object.
Definition: allocator.h:127
Memory_resource get_memory_resource() const
Return the underlying Memory_resource object.
Definition: allocator.h:123
T value_type
Definition: allocator.h:56
Allocator(Memory_resource memory_resource=Memory_resource())
Construct a new Allocator using the given Memory_resource.
Definition: allocator.h:65
std::ptrdiff_t difference_type
Definition: allocator.h:58
Allocator(const Allocator< U > &other)
Implicit conversion from other instance.
Definition: allocator.h:73
std::function< void(T *)> get_deleter()
Return a Deleter function for objects allocated by this class.
Definition: allocator.h:112
constexpr T * allocate(size_type n)
Use the Memory_resource to allocate the given number of elements of type T.
Definition: allocator.h:84
constexpr void deallocate(T *p, size_type size)
Use the Memory_resource to deallocate the given pointer.
Definition: allocator.h:95
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:1235
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 equality.
Definition: allocator.h:132
bool operator!=(const Allocator< T > &a1, const Allocator< T > &a2)
Compare two Allocator objects for inequality.
Definition: allocator.h:139
size_t size(const char *const c)
Definition: base64.h:46
Definition: gcs_xcom_synode.h:64
int n
Definition: xcom_base.cc:509