MySQL 9.0.0
Source Code Documentation
ut0object_cache.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/ut0object_cache.h
29Manage a cache of objects. */
30
31#ifndef ut0object_cache_h
32#define ut0object_cache_h
33
34#include "ut0new.h"
35
36namespace ut {
37
38/** A class to manage objects of type T. */
39template <typename T>
41 public:
42 /** Destructor. Frees the cached objects. */
44 for (auto obj : m_objects) {
45 ut::delete_(obj);
46 }
47 }
48
49 /** Initialize the cache.
50 @param[in] size initial number of objects to cache.
51 @param[in] step when extending cache, number of objects to add.
52 @param[in] args arguments to be passed to constructor of T */
53 template <typename... Types>
54 dberr_t init(size_t size, size_t step, Types &&... args) {
55 m_step = step;
56 return extend(size, std::forward<Types>(args)...);
57 }
58
59 template <typename... Types>
60 T *allocate(Types &&... args) {
61 if (m_index == m_objects.size()) {
62 extend(m_step, std::forward<Types>(args)...);
63 }
64 /* Could set m_objects[m_index] to nullptr, but not required. */
65 return m_objects[m_index++];
66 }
67
68 template <typename... Types>
69 dberr_t extend(size_t size, Types &&... args) {
70 m_objects.reserve(m_objects.size() + size);
71 for (size_t i = 0; i < size; ++i) {
72 auto obj = ut::new_withkey<T>(UT_NEW_THIS_FILE_PSI_KEY,
73 std::forward<Types>(args)...);
74 if (obj == nullptr) {
75 /* dtor will free all allocated objects. */
76 return DB_OUT_OF_MEMORY;
77 }
78 m_objects.push_back(obj);
79 }
80 return DB_SUCCESS;
81 }
82
83 void deallocate(T *obj) {
84 ut_ad(m_index > 0);
85 m_objects[--m_index] = obj;
86 }
87
88 private:
89 /** Cached objects. */
90 std::vector<T *> m_objects;
91
92 /** When the cache is extended, how many new objects needs to be created. */
93 size_t m_step{1};
94
95 /** Position of next object to be allocated. */
96 size_t m_index{0};
97};
98
99} // namespace ut
100
101#endif /* ut0object_cache_h */
A class to manage objects of type T.
Definition: ut0object_cache.h:40
void deallocate(T *obj)
Definition: ut0object_cache.h:83
size_t m_step
When the cache is extended, how many new objects needs to be created.
Definition: ut0object_cache.h:93
std::vector< T * > m_objects
Cached objects.
Definition: ut0object_cache.h:90
T * allocate(Types &&... args)
Definition: ut0object_cache.h:60
size_t m_index
Position of next object to be allocated.
Definition: ut0object_cache.h:96
dberr_t init(size_t size, size_t step, Types &&... args)
Initialize the cache.
Definition: ut0object_cache.h:54
~Object_cache()
Destructor.
Definition: ut0object_cache.h:43
dberr_t extend(size_t size, Types &&... args)
Definition: ut0object_cache.h:69
dberr_t
Definition: db0err.h:39
@ DB_OUT_OF_MEMORY
Definition: db0err.h:49
@ DB_SUCCESS
Definition: db0err.h:43
size_t size(const char *const c)
Definition: base64.h:46
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
void delete_(T *ptr) noexcept
Releases storage which has been dynamically allocated through any of the ut::new*() variants.
Definition: ut0new.h:810
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:105
Dynamic memory allocation routines and custom allocators specifically crafted to support memory instr...
#define UT_NEW_THIS_FILE_PSI_KEY
Definition: ut0new.h:565