MySQL 9.4.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
grow_constraint.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2025, 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#ifndef MYSQL_CONTAINERS_BUFFERS_GROW_CONSTRAINT_H
25#define MYSQL_CONTAINERS_BUFFERS_GROW_CONSTRAINT_H
26
27#include <algorithm> // std::min
28#include <limits> // std::numeric_limits
29#include <string> // std::string
30#ifndef NDEBUG
31#include <sstream> // std::stringstream
32#endif
33
34/// @addtogroup GroupLibsMysqlContainers
35/// @{
36
38
39/// Description of a heuristic to determine how much memory to allocate.
40///
41/// This may be used in diverse contexts such as growing a memory
42/// buffer, or growing a pool of objects.
43///
44/// This encapsulates several common heuristics for growth:
45///
46/// - The growth rate can be exponential. This is useful for cases
47/// such as contiguous memory buffers, where each size increment may
48/// copy all the existing data, e.g., using 'realloc'. Or, more
49/// generally, any data structure where size growth has a cost that
50/// is linear in the total size. In such cases an exponential
51/// growth rate ensures that execution time is not quadratic in the
52/// number of grow operations.
53///
54/// - The growth rate can be linear. This is useful for cases such as
55/// linked lists, where each size increment is linear in the
56/// increment size.
57///
58/// - There can be an upper bound on the size. This is useful
59/// e.g. when there are configurable memory limits.
60///
61/// - The size can be specified to be a multiple of a given number.
62/// This can potentially be useful if there is a way to align
63/// allocated objects to page sizes, or similar.
65 public:
66 using Size_t = std::size_t;
67 /// Return type for compute_new_size.
68 using Result_t = std::pair<bool, Size_t>;
69 /// Maximum allowed value for the application max size.
71
72 Grow_constraint() = default;
73 Grow_constraint(const Grow_constraint &other) = default;
74 Grow_constraint(Grow_constraint &&other) = default;
75 Grow_constraint &operator=(const Grow_constraint &other) = default;
77 virtual ~Grow_constraint() = default;
78
79 /// Set the maximum size.
80 ///
81 /// Whenever more than this is requested, the response should be to
82 /// fail. This is an inclusive upper bound, so requests for exactly
83 /// this size are allowed.
84 void set_max_size(Size_t max_size);
85
86 /// @return the maximum size.
87 Size_t get_max_size() const;
88
89 /// Set the grow factor.
90 ///
91 /// Whenever the size needs to increase, it should increase it by at
92 /// least this factor.
93 ///
94 /// Using a value > 1 ensures that successive calls to reserve()
95 /// with sizes increasing up to N take amortized linear time in N; a
96 /// value equal to 1 may result in execution time that is quadratic
97 /// in N.
98 void set_grow_factor(double grow_factor);
99
100 /// @return the grow factor.
101 double get_grow_factor() const;
102
103 /// Set the grow increment.
104 ///
105 /// Whenever the size needs to increase, it should increase by
106 /// at least this amount.
107 void set_grow_increment(Size_t grow_increment);
108
109 /// @return the grow increment.
111
112 /// Set the block size.
113 ///
114 /// The size should be kept to a multiple of this number.
115 void set_block_size(Size_t block_size);
116
117 /// @return the block size.
118 Size_t get_block_size() const;
119
120 /// In debug mode, return a string that describes the internal
121 /// structure of this object, to use for debugging.
122 std::string debug_string() const {
123#ifdef NDEBUG
124 return "";
125#else
127 // clang-format off
128 ss << "Grow_constraint(ptr=" << (const void *)this
129 << ", max_size=" << m_max_size
130 << ", grow_factor=" << m_grow_factor
131 << ", grow_increment=" << m_grow_increment
132 << ", block_size=" << m_block_size
133 << ")";
134 // clang-format on
135 return ss.str();
136#endif
137 }
138
139 /// Combine the constraints of this object with another
140 /// Grow_constraint or Grow_calculator object.
141 ///
142 /// This will return a new object of the same type as the argument.
143 /// The returned object will have the smallest max_size among `this`
144 /// and `other`, and the largest `grow_factor`, `grow_increment`, and
145 /// `block_size`.
146 template <class T>
147 [[nodiscard]] T combine_with(const T &other) const {
148 T ret;
149 ret.set_max_size(std::min(get_max_size(), other.get_max_size()));
150 ret.set_grow_factor(std::max(get_grow_factor(), other.get_grow_factor()));
151 ret.set_grow_increment(
152 std::max(get_grow_increment(), other.get_grow_increment()));
153 ret.set_block_size(std::max(get_block_size(), other.get_block_size()));
154 return ret;
155 }
156
157 private:
158 /// Size must not exceed this number.
160
161 /// By default, don't constrain the grow factor.
162 static constexpr double default_grow_factor = 1.0;
163
164 /// By default, don't constrain the grow increment.
165 static constexpr Size_t default_grow_increment = 0;
166
167 /// By default, don't constrain the block size.
168 static constexpr Size_t default_block_size = 1;
169
170 // Size should grow by at least this factor.
172
173 // Size should grow by at least this number of bytes.
175
176 // Size should be rounded up to a multiple of at least this number of bytes.
178};
179
180} // namespace mysql::containers::buffers
181
182/// @}
183
184#endif /* MYSQL_CONTAINERS_BUFFERS_GROW_CONSTRAINT_H */
Description of a heuristic to determine how much memory to allocate.
Definition: grow_constraint.h:64
T combine_with(const T &other) const
Combine the constraints of this object with another Grow_constraint or Grow_calculator object.
Definition: grow_constraint.h:147
std::size_t Size_t
Definition: grow_constraint.h:66
Grow_constraint & operator=(const Grow_constraint &other)=default
Size_t get_grow_increment() const
Definition: grow_constraint.cpp:48
Size_t m_grow_increment
Definition: grow_constraint.h:174
Grow_constraint & operator=(Grow_constraint &&other)=default
static constexpr Size_t default_grow_increment
By default, don't constrain the grow increment.
Definition: grow_constraint.h:165
void set_grow_factor(double grow_factor)
Set the grow factor.
Definition: grow_constraint.cpp:36
double m_grow_factor
Definition: grow_constraint.h:171
std::pair< bool, Size_t > Result_t
Return type for compute_new_size.
Definition: grow_constraint.h:68
std::string debug_string() const
In debug mode, return a string that describes the internal structure of this object,...
Definition: grow_constraint.h:122
Size_t m_max_size
Size must not exceed this number.
Definition: grow_constraint.h:159
Size_t get_block_size() const
Definition: grow_constraint.cpp:56
Grow_constraint(Grow_constraint &&other)=default
static constexpr double default_grow_factor
By default, don't constrain the grow factor.
Definition: grow_constraint.h:162
Grow_constraint(const Grow_constraint &other)=default
double get_grow_factor() const
Definition: grow_constraint.cpp:41
Size_t m_block_size
Definition: grow_constraint.h:177
Size_t get_max_size() const
Definition: grow_constraint.cpp:32
static constexpr Size_t default_block_size
By default, don't constrain the block size.
Definition: grow_constraint.h:168
static constexpr Size_t machine_max_size
Maximum allowed value for the application max size.
Definition: grow_constraint.h:70
void set_max_size(Size_t max_size)
Set the maximum size.
Definition: grow_constraint.cpp:30
void set_grow_increment(Size_t grow_increment)
Set the grow increment.
Definition: grow_constraint.cpp:43
void set_block_size(Size_t block_size)
Set the block size.
Definition: grow_constraint.cpp:52
#define T
Definition: jit_executor_value.cc:373
ValueType max(X &&first)
Definition: gtid.h:103
Definition: buffer_sequence_view.h:50
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2872