Debian Codegraph @

libbinlogevents/src/buffer/grow_calculator.cpp

raw
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 #include <buffer/grow_calculator.h>
25 #include "libbinlogevents/include/math/math.h" // add_bounded
26 #include "libbinlogevents/include/wrapper_functions.h" // BAPI_TRACE
27
28 #include <cassert>
29
30 namespace mysqlns::buffer {
31
32 Grow_calculator::Grow_calculator() {
33 set_max_size(default_max_size);
34 set_grow_factor(default_grow_factor);
35 set_grow_increment(default_grow_increment);
36 set_block_size(default_block_size);
37 }
38
39 Grow_calculator::Result_t Grow_calculator::compute_new_size(
40 Size_t old_size, Size_t requested_size) const {
41 BAPI_TRACE;
42 // See if we exceed the maximum size.
43 if (std::max(old_size, requested_size) > get_max_size())
44 return Result_t(true, 0);
45 // See if we already have enough size.
46 if (requested_size <= old_size) return Result_t(false, old_size);
47 // Compute new size
48 Size_t new_size = requested_size;
49 // Grow by at least the grow factor.
50 new_size =
51 std::max(new_size, mysqlns::math::multiply_bounded(
52 old_size, get_grow_factor(), machine_max_size));
53 // Grow by at least the grow increment.
54 new_size =
55 std::max(new_size, mysqlns::math::add_bounded(
56 old_size, get_grow_increment(), machine_max_size));
57 // Round up to nearest multiple of block size.
58 auto remainder = new_size % get_block_size();
59 if (remainder != 0)
60 new_size = mysqlns::math::add_bounded(
61 new_size, get_block_size() - remainder, machine_max_size);
62 // Limit by max size.
63 new_size = std::min(new_size, get_max_size());
64
65 assert(new_size >= requested_size);
66 BAPI_LOG("info", BAPI_VAR(old_size) << " " << BAPI_VAR(requested_size) << " "
67 << BAPI_VAR(new_size));
68 return Result_t(false, new_size);
69 }
70
71 } // namespace mysqlns::buffer