| 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 memory_resource.h |
| 28 |
/// |
| 29 |
/// @brief Class that wraps resources in a polymorphic manner. |
| 30 |
|
| 31 |
#ifndef MYSQL_RESOURCE_MEMORY_RESOURCE_H_ |
| 32 |
#define MYSQL_RESOURCE_MEMORY_RESOURCE_H_ |
| 33 |
|
| 34 |
#include <cstdlib> |
| 35 |
#include <functional> |
| 36 |
|
| 37 |
namespace mysqlns::resource { |
| 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. |
| 88 |
class Memory_resource { |
| 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. |
| 106 |
Memory_resource() : Memory_resource(std::malloc, 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. |
| 121 |
Deallocator_t get_deallocator() const { return m_deallocator; } |
| 122 |
|
| 123 |
private: |
| 124 |
/// The allocator object. |
| 125 |
const Allocator_t m_allocator; |
| 126 |
|
| 127 |
/// The deallocator object. |
| 128 |
const Deallocator_t m_deallocator; |
| 129 |
}; |
| 130 |
|
| 131 |
} // namespace mysqlns::resource |
| 132 |
|
| 133 |
/// @} (end of group Replication) |
| 134 |
|
| 135 |
#endif // MYSQL_RESOURCE_MEMORY_RESOURCE_H_ |