MySQL 9.1.0
Source Code Documentation
memory_resource.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
25///
26/// @brief Class that wraps resources in a polymorphic manner.
27
28#ifndef MYSQL_ALLOCATORS_MEMORY_RESOURCE_H
29#define MYSQL_ALLOCATORS_MEMORY_RESOURCE_H
30
31#include <cstdlib>
32#include <functional>
33
34/// @addtogroup GroupLibsMysqlAllocators
35/// @{
36
37namespace mysql::allocators {
38
39/// Polymorphism-free memory resource class with custom allocator and
40/// deallocator functions.
41///
42/// This is used as the "back-end" for Allocator objects. The
43/// allocator and deallocator functions are member objects of type
44/// std::function, specified as constructor arguments.
45///
46/// Example: a class that needs to allocate memory, and should have a
47/// way to use a specified PSI key, can be implemented as follows:
48///
49/// - For each type the class needs to allocate, it has one member
50/// object of type Allocator. The constructor takes a
51/// Memory_resource parameter, which defaults to a
52/// default-constructed Memory_resource. The constructor passes the
53/// Memory_resource to the Allocator constructors. The class calls
54/// the Allocator member functions for its memory management.
55///
56/// - API clients that don't need a PSI key don't need to pass a
57/// Memory_resource to the constructor; they use the default value
58/// for the parameter. API clients that need a PSI key, pass a
59/// Memory_resource object where the allocate/deallocate functions
60/// are PSI-enabled. Such a Memory_resource object can be obtained
61/// from the factory function @c psi_memory_resource in
62/// sql/psi_memory_resource.h.
63///
64/// Notes on design choices:
65///
66/// We avoid using polymorphism to configure the behavior of the
67/// allocate/deallocate member functions. Classes that use
68/// polymorphic memory resources would have to take a reference or
69/// pointer to the memory resource as argument, so either the class
70/// can't own the memory resource or it has to be dynamically
71/// allocated. If the class can't own the memory resource, it
72/// complicates the usage since the caller has to ensure the
73/// Memory_resource outlives the class that uses it. If the
74/// Memory_resource has to be allocated dynamically, that allocation
75/// cannot use a custom Memory_resource, which may defeat the purpose
76/// of using custom Memory_resources to track all allocations.
77///
78/// We also avoid static polymorphism, since every user of a
79/// statically polymorphic Memory_resource would have to be a
80/// template, which has two problems: first, two classes with the same
81/// behavior but different allocation strategies would be different
82/// types. Second, any class that allocates memory and has a
83/// requirement that the API client can configure the Memory_resource,
84/// has to be templated and therefore be defined in a header.
85///
86/// With the current solution, the caller just creates a
87/// Memory_resource object and passes it to the allocator.
89 public:
90 using Size_t = std::size_t;
91 using Ptr_t = void *;
92 using Allocator_t = std::function<Ptr_t(Size_t)>;
93 using Deallocator_t = std::function<void(Ptr_t)>;
94
95 /// Construct a new Memory_resource that uses the given allocator
96 /// and deallocator.
97 ///
98 /// @param allocator The allocator, e.g. std::malloc or my_malloc.
99 ///
100 /// @param deallocator The deallocator, e.g. std::free or my_free.
101 Memory_resource(const Allocator_t &allocator,
102 const Deallocator_t &deallocator)
103 : m_allocator(allocator), m_deallocator(deallocator) {}
104
105 /// Construct a new Memory_resource that uses std::malloc and std::free.
107
108 /// Allocate memory using the provided allocator.
109 ///
110 /// @param n The size.
111 ///
112 /// @return The new memory.
113 void *allocate(Size_t n) const { return m_allocator(n); }
114
115 /// Deallocate memory using the provided deallocator.
116 ///
117 /// @param p The pointer.
118 void deallocate(Ptr_t p) const { m_deallocator(p); }
119
120 /// Return the deallocator.
122
123 private:
124 /// The allocator object.
126
127 /// The deallocator object.
129};
130
131} // namespace mysql::allocators
132
133/// @}
134
135#endif // MYSQL_ALLOCATORS_MEMORY_RESOURCE_H
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
void * Ptr_t
Definition: memory_resource.h:91
const Allocator_t m_allocator
The allocator object.
Definition: memory_resource.h:125
void * allocate(Size_t n) const
Allocate memory using the provided allocator.
Definition: memory_resource.h:113
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
std::function< void(Ptr_t)> Deallocator_t
Definition: memory_resource.h:93
Memory_resource(const Allocator_t &allocator, const Deallocator_t &deallocator)
Construct a new Memory_resource that uses the given allocator and deallocator.
Definition: memory_resource.h:101
Memory_resource()
Construct a new Memory_resource that uses std::malloc and std::free.
Definition: memory_resource.h:106
std::function< Ptr_t(Size_t)> Allocator_t
Definition: memory_resource.h:92
const Deallocator_t m_deallocator
The deallocator object.
Definition: memory_resource.h:128
std::size_t Size_t
Definition: memory_resource.h:90
const char * p
Definition: ctype-mb.cc:1225
#define malloc(A)
Definition: lexyy.cc:914
#define free(A)
Definition: lexyy.cc:915
Definition: allocator.h:40
Definition: gcs_xcom_synode.h:64
int n
Definition: xcom_base.cc:509