MySQL 8.4.0
Source Code Documentation
free_list.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 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#ifndef DD_CACHE__FREE_LIST_INCLUDED
25#define DD_CACHE__FREE_LIST_INCLUDED
26
27#include <assert.h>
28#include <vector> // vector
29
30#include "sql/malloc_allocator.h" // Malloc_allocator.
31#include "sql/psi_memory_key.h" // key_memory_DD_cache_infrastructure
32
33namespace dd {
34namespace cache {
35
36/**
37 Template for management of a free list based on a std::vector.
38
39 The free list template defines functions mostly wrapping the std::vector
40 functions, but additionally doing some asserts to ensure correct usage.
41
42 The first element in the free list is the least recently used element.
43 When a new element becomes unused, it is added to the end of the free list.
44 An element may also be removed from within the middle of the free list
45 when the element is being acquired. This use case means iterating through
46 the vector to find the correct element.
47
48 @tparam E Element type (a Cache_element wrapping some dictionary
49 object type).
50*/
51
52template <typename E>
53class Free_list {
54 private:
55 typedef std::vector<E *, Malloc_allocator<E *>> List_type;
56 List_type m_list; // The actual list.
57
58 public:
61
62 // Return the actual free list length.
63 size_t length() const { return m_list.size(); }
64
65 /**
66 Add an element to the end of the free list.
67
68 @param element Element to add to the end of the list.
69 */
70
71 void add_last(E *element) {
72 assert(element != nullptr && element->usage() == 0);
73 m_list.push_back(element);
74 }
75
76 /**
77 Remove an element from the free list.
78
79 @param element Element to be removed from the list.
80 */
81
82 void remove(E *element) {
83 assert(element != nullptr && element->usage() == 0);
84 assert(!m_list.empty());
85
86 for (typename List_type::iterator it = m_list.begin(); it != m_list.end();
87 ++it)
88 if (*it == element) {
89 m_list.erase(it);
90 return;
91 }
92
93 assert(false); /* purecov: deadcode */
94 }
95
96 /**
97 Get the least recently used element in the list, i.e., the first element.
98
99 @return The least recently used element in the list.
100 */
101
102 E *get_lru() const {
103 assert(!m_list.empty());
104 if (!m_list.empty()) return m_list.front();
105 return nullptr;
106 }
107
108 /**
109 Debug dump of the free list to stderr.
110 */
111 /* purecov: begin inspected */
112 void dump() const {
113#ifndef NDEBUG
114 if (m_list.empty()) {
115 fprintf(stderr, " lru-> NULL\n");
116 return;
117 }
118 fprintf(stderr, " lru-> ");
119 for (typename List_type::const_iterator it = m_list.begin();
120 it != m_list.end(); ++it)
121 fprintf(stderr, "%llu ", (*it)->object()->id());
122 fprintf(stderr, "\n");
123#endif
124 }
125 /* purecov: end */
126};
127
128} // namespace cache
129} // namespace dd
130
131#endif // DD_CACHE__FREE_LIST_INCLUDED
Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: malloc_allocator.h:63
Template for management of a free list based on a std::vector.
Definition: free_list.h:53
void dump() const
Debug dump of the free list to stderr.
Definition: free_list.h:112
List_type m_list
Definition: free_list.h:56
void remove(E *element)
Remove an element from the free list.
Definition: free_list.h:82
std::vector< E *, Malloc_allocator< E * > > List_type
Definition: free_list.h:55
Free_list()
Definition: free_list.h:59
size_t length() const
Definition: free_list.h:63
void add_last(E *element)
Add an element to the end of the free list.
Definition: free_list.h:71
E * get_lru() const
Get the least recently used element in the list, i.e., the first element.
Definition: free_list.h:102
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
PSI_memory_key key_memory_DD_cache_infrastructure
Definition: psi_memory_key.cc:37