MySQL 8.0.31
Source Code Documentation
ut0lst.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2022, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/ut0lst.h
28 List utilities
29
30 Created 9/10/1995 Heikki Tuuri
31 Rewritten by Sunny Bains Dec 2011.
32 ***********************************************************************/
33
34#ifndef ut0lst_h
35#define ut0lst_h
36
37/* Do not include univ.i because univ.i includes this. */
38
39#include <atomic>
40#include "ut0dbg.h"
41
42/* This module implements the two-way linear list. Note that a single
43list node may belong to two or more lists, but is only on one list
44at a time. */
45
46/** The two way list node.
47 @tparam Type the list node type name */
48template <typename Type>
50 Type *prev; /*!< pointer to the previous
51 node, NULL if start of list */
52 Type *next; /*!< pointer to next node,
53 NULL if end of list */
54
55 void reverse() {
56 Type *tmp = prev;
57 prev = next;
58 next = tmp;
59 }
60};
61
62/** Macro used for legacy reasons */
63#define UT_LIST_NODE_T(t) ut_list_node<t>
64
65#ifdef UNIV_DEBUG
66#define UT_LIST_INITIALISED 0xCAFE
67#endif /* UNIV_DEBUG */
68
69#define UT_LIST_IS_INITIALISED(b) ((b).init == UT_LIST_INITIALISED)
70
71/** The two-way list base node. The base node contains pointers to both ends
72 of the list and a count of nodes in the list (excluding the base node
73 from the count). We also store a pointer to the member field so that it
74 doesn't have to be specified when doing list operations.
75 @tparam Type the type of the list element
76 @tparam NodeGetter a class which has a static member
77 ut_list_node<Type> get_node(const Type & e) which knows how to extract
78 a node from an element */
79template <typename Type, typename NodeGetter>
81 using elem_type = Type;
83 static const node_type &get_node(const elem_type &e) {
84 return NodeGetter::get_node(e);
85 }
87 return const_cast<node_type &>(get_node(const_cast<const elem_type &>(e)));
88 }
89 static const elem_type *next(const elem_type &e) { return get_node(e).next; }
90 static elem_type *next(elem_type &e) {
91 return const_cast<elem_type *>(next(const_cast<const elem_type &>(e)));
92 }
93 static const elem_type *prev(const elem_type &e) { return get_node(e).prev; }
94 static elem_type *prev(elem_type &e) {
95 return const_cast<elem_type *>(prev(const_cast<const elem_type &>(e)));
96 }
97
98 /** Pointer to list start, NULL if empty. */
100 /** Pointer to list end, NULL if empty. */
102#ifdef UNIV_DEBUG
103 /** UT_LIST_INITIALISED if the list was initialised with the constructor. It
104 is used to detect if the ut_list_base object is used directly after
105 allocating memory from malloc-like calls that do not run constructor. */
107#endif /* UNIV_DEBUG */
108
109 /** Returns number of nodes currently present in the list. */
110 size_t get_length() const {
112 return count.load(std::memory_order_acquire);
113 }
114
115 /** Updates the length of the list by the amount specified.
116 @param diff the value by which to increase the length. Can be negative. */
117 void update_length(int diff) {
118 ut_ad(diff > 0 || static_cast<size_t>(-diff) <= get_length());
119 count.store(get_length() + diff, std::memory_order_release);
120 }
121
122 void clear() {
124 first_element = nullptr;
125 last_element = nullptr;
126 count.store(0);
127 }
128
129 void reverse() {
130 Type *tmp = first_element;
132 last_element = tmp;
133 }
134
135 private:
136 /** Number of nodes in list. It is atomic to allow unprotected reads. Writes
137 must be protected by some external latch. */
138 std::atomic<size_t> count{0};
139
140 template <typename E>
142 private:
144
145 public:
146 base_iterator(E *elem) : m_elem(elem) {}
147 bool operator==(const base_iterator &other) const {
148 return m_elem == other.m_elem;
149 }
150 bool operator!=(const base_iterator &other) const {
151 return !(*this == other);
152 }
153 E *operator*() const { return m_elem; }
155 m_elem = next(*m_elem);
156 return *this;
157 }
158 };
159
160 public:
164 iterator end() { return nullptr; }
166 const_iterator end() const { return nullptr; }
167
168 /** A helper wrapper class for the list, which exposes begin(),end() iterators
169 which let you remove the current item or items after it during the loop, while
170 still having O(1) space and time complexity.
171 NOTE: do not attempt to (re)move the previous element! */
172 class Removable {
173 private:
175
176 public:
177 class iterator {
178 private:
182
183 public:
185 : m_list{list},
186 m_elem{elem},
187 m_prev_elem{elem ? prev(*elem) : nullptr} {
188 // We haven't really tested any other case yet:
189 ut_ad(m_prev_elem == nullptr);
190 }
191 bool operator==(const iterator &other) const {
192 return m_elem == other.m_elem;
193 }
194 bool operator!=(const iterator &other) const { return !(*this == other); }
195 elem_type *operator*() const { return m_elem; }
197 /* if m_prev_elem existed before, then it should still belong to the
198 list, which we verify partially here, by checking it's linked to next
199 element or is the last. If this assert fails, it means the m_prev_elem
200 was removed from the list during loop, which is violation of the
201 contract with the user of .removable(). */
204 /* The reason this is so complicated is that we want to support cases in
205 which the body of the loop removed not only the current element, but
206 also some elements even further after it. */
207 auto here =
209 if (here != m_elem) {
210 m_elem = here;
211 } else {
213 m_elem = next(*m_elem);
214 }
215 return *this;
216 }
217 };
220 iterator end() { return iterator{m_list, nullptr}; }
221 };
222 /** Returns a wrapper which lets you remove current item or items after it.
223 It can be used like in this example:
224 for (auto lock : table->locks.removable()) {
225 lock_remove_all_on_table_for_trx(table, lock->trx,..);
226 }
227 Or in general:
228 for (auto item : list.removable()) {
229 remove_items_which_are_similar_to(item);
230 }
231 Basically you can remove any item, except for prev(item).
232
233 You can also insert to the list during iteration, keeping in mind that the
234 position you insert the element at has following impact:
235 - after the current item: the new item WILL be processed eventually,
236 - before the previous item: the new item WILL NOT be processed,
237 - right before the current item: DON'T DO IT, as you risk an endless loop!
238 A safe subcase of this is reinserting the current item, in which case it
239 won't be processed again. This lets you implement "move to front" easily.
240 @see Removable */
241 Removable removable() { return Removable{*this}; }
242};
243template <typename Type, ut_list_node<Type> Type::*node_ptr>
245 static const ut_list_node<Type> &get_node(const Type &element) {
246 return element.*node_ptr;
247 }
248};
249/** A type of a list storing pointers to t, chained by member m of t.
250NOTE: In cases in which definition of t is not yet in scope and thus you can't
251refer to t::m at this point yet, use UT_LIST_BASE_NODE_T_EXTERN macro instead.*/
252#define UT_LIST_BASE_NODE_T(t, m) \
253 ut_list_base<t, ut_list_base_explicit_getter<t, &t::m>>
254
255/** A helper for the UT_LIST_BASE_NODE_T_EXTERN which builds a name of a node
256getter struct from the name of elem type t, and its member name m */
257#define UT_LIST_NODE_GETTER(t, m) t##_##m##_node_getter
258
259/** A helper for the UT_LIST_BASE_NODE_T_EXTERN which declares a node getter
260struct which extracts member m from element of type t. Note that the definition
261of the get_node function is inline, so this declaration/definition can appear
262multiple times in our codebase, and the intent is that you simply put it in the
263header which defines member m of t for the first time, so that it is accessible.
264This way all the places in codebase which know how to access m from t, will be
265also able to use this node getter, and thus iterate over a list chained by it.
266This also ensures, that for(auto elem: list) loops can be fully inlined by the
267compiler as it can see through the get_node implementation, because each place
268in code which knows that get_node exists also knows its implementation.*/
269#define UT_LIST_NODE_GETTER_DEFINITION(t, m) \
270 struct UT_LIST_NODE_GETTER(t, m) \
271 : public ut_list_base_explicit_getter<t, &t::m> {};
272
273/** A variant of UT_LIST_BASE_NODE_T to be used in rare cases where the full
274definition of t is not yet in scope, and thus UT_LIST_BASE_NODE_T can't be used
275yet as it needs to know how to access member m of t. The trick used here is to
276forward declare UT_LIST_NODE_GETTER(t,m) struct to be defined later by the
277UT_LIST_NODE_GETTER_DEFINITION(t,m) once t::m is defined. */
278#define UT_LIST_BASE_NODE_T_EXTERN(t, m) \
279 ut_list_base<t, struct UT_LIST_NODE_GETTER(t, m)>
280
281/** Initializes the base node of a two-way list.
282 @param b the list base node
283*/
284#define UT_LIST_INIT(b) \
285 { \
286 auto &list_ref = (b); \
287 new (&list_ref) std::remove_reference_t<decltype(list_ref)>(); \
288 }
289
290/** Adds the node as the first element in a two-way linked list.
291 @param list the base node (not a pointer to it)
292 @param elem the element to add */
293template <typename List>
294void ut_list_prepend(List &list, typename List::elem_type *elem) {
295 auto &elem_node = List::get_node(*elem);
296
298
299 elem_node.prev = nullptr;
300 elem_node.next = list.first_element;
301
302 if (list.first_element != nullptr) {
303 ut_ad(list.first_element != elem);
304
305 List::get_node(*list.first_element).prev = elem;
306 }
307
308 list.first_element = elem;
309
310 if (list.last_element == nullptr) {
311 list.last_element = elem;
312 }
313
314 list.update_length(1);
315}
316
317/** Adds the node as the first element in a two-way linked list.
318 @param LIST the base node (not a pointer to it)
319 @param ELEM the element to add */
320#define UT_LIST_ADD_FIRST(LIST, ELEM) ut_list_prepend(LIST, ELEM)
321
322/** Adds the node as the last element in a two-way linked list.
323 @param list list
324 @param elem the element to add
325 */
326template <typename List>
327void ut_list_append(List &list, typename List::elem_type *elem) {
328 auto &elem_node = List::get_node(*elem);
329
331
332 elem_node.next = nullptr;
333 elem_node.prev = list.last_element;
334
335 if (list.last_element != nullptr) {
336 ut_ad(list.last_element != elem);
337
338 List::get_node(*list.last_element).next = elem;
339 }
340
341 list.last_element = elem;
342
343 if (list.first_element == nullptr) {
344 list.first_element = elem;
345 }
346
347 list.update_length(1);
348}
349
350/** Adds the node as the last element in a two-way linked list.
351 @param LIST list base node (not a pointer to it)
352 @param ELEM the element to add */
353#define UT_LIST_ADD_LAST(LIST, ELEM) ut_list_append(LIST, ELEM)
354
355/** Inserts a ELEM2 after ELEM1 in a list.
356 @param list the base node
357 @param elem1 node after which ELEM2 is inserted
358 @param elem2 node being inserted after ELEM1 */
359template <typename List>
360void ut_list_insert(List &list, typename List::elem_type *elem1,
361 typename List::elem_type *elem2) {
362 ut_ad(elem1 != elem2);
363 ut_ad(elem1 != nullptr);
364 ut_ad(elem2 != nullptr);
366
367 auto &elem1_node = List::get_node(*elem1);
368 auto &elem2_node = List::get_node(*elem2);
369
370 elem2_node.prev = elem1;
371 elem2_node.next = elem1_node.next;
372 ut_ad((elem2_node.next == nullptr) == (list.last_element == elem1));
373 if (elem2_node.next != nullptr) {
374 List::get_node(*elem2_node.next).prev = elem2;
375 } else {
376 list.last_element = elem2;
377 }
378
379 elem1_node.next = elem2;
380
381 list.update_length(1);
382}
383
384/** Inserts a ELEM2 after ELEM1 in a list.
385 @param LIST list base node (not a pointer to it)
386 @param ELEM1 node after which ELEM2 is inserted
387 @param ELEM2 node being inserted after ELEM1 */
388#define UT_LIST_INSERT_AFTER(LIST, ELEM1, ELEM2) \
389 ut_list_insert(LIST, ELEM1, ELEM2)
390
391/** Removes a node from a two-way linked list.
392 @param list the base node (not a pointer to it)
393 @param elem pointer to the element to remove from the list
394*/
395template <typename List>
396void ut_list_remove(List &list, typename List::elem_type *elem) {
397 ut_a(list.get_length() > 0);
399
400 auto &node = List::get_node(*elem);
401 if (node.next != nullptr) {
402 List::get_node(*node.next).prev = node.prev;
403 } else {
404 list.last_element = node.prev;
405 }
406
407 if (node.prev != nullptr) {
408 List::get_node(*node.prev).next = node.next;
409 } else {
410 list.first_element = node.next;
411 }
412
413 node.next = nullptr;
414 node.prev = nullptr;
415
416 list.update_length(-1);
417}
418
419/** Removes a node from a two-way linked list.
420 @param LIST the base node (not a pointer to it)
421 @param ELEM node to be removed from the list */
422#define UT_LIST_REMOVE(LIST, ELEM) ut_list_remove(LIST, ELEM)
423
424/** Gets the next node in a two-way list.
425 @param NAME list name
426 @param N pointer to a node
427 @return the successor of N in NAME, or NULL */
428#define UT_LIST_GET_NEXT(NAME, N) (((N)->NAME).next)
429
430/** Gets the previous node in a two-way list.
431 @param NAME list name
432 @param N pointer to a node
433 @return the predecessor of N in NAME, or NULL */
434#define UT_LIST_GET_PREV(NAME, N) (((N)->NAME).prev)
435
436/** Alternative macro to get the number of nodes in a two-way list, i.e.,
437 its length.
438 @param BASE the base node (not a pointer to it).
439 @return the number of nodes in the list */
440#define UT_LIST_GET_LEN(BASE) (BASE).get_length()
441
442/** Gets the first node in a two-way list.
443 @param BASE the base node (not a pointer to it)
444 @return first node, or NULL if the list is empty */
445#define UT_LIST_GET_FIRST(BASE) (BASE).first_element
446
447/** Gets the last node in a two-way list.
448 @param BASE the base node (not a pointer to it)
449 @return last node, or NULL if the list is empty */
450#define UT_LIST_GET_LAST(BASE) (BASE).last_element
451
453 void operator()(const void *) {}
454};
455
456/** Iterate over all the elements and call the functor for each element.
457 @param[in] list base node (not a pointer to it)
458 @param[in,out] functor Functor that is called for each element in the list */
459template <typename List, class Functor>
460void ut_list_map(const List &list, Functor &functor) {
461 size_t count = 0;
462
464
465 for (auto elem : list) {
466 functor(elem);
467 ++count;
468 }
469
470 ut_a(count == list.get_length());
471}
472
473template <typename List>
476 // NOTE: we use List::prev to iterate forward as .reverse() swaps arrows
477 for (auto elem = list.first_element; elem != nullptr;
478 elem = List::prev(*elem)) {
479 List::get_node(*elem).reverse();
480 }
481
482 list.reverse();
483}
484
485#define UT_LIST_REVERSE(LIST) ut_list_reverse(LIST)
486
487/** Checks the consistency of a two-way list.
488 @param[in] list base node (not a pointer to it)
489 @param[in,out] functor Functor that is called for each element in
490 the list */
491template <typename List, class Functor>
492void ut_list_validate(const List &list, Functor &functor) {
493 ut_list_map(list, functor);
494 /* Validate the list backwards. */
495 size_t count = 0;
496
497 for (auto elem = list.last_element; elem != nullptr;
498 elem = List::prev(*elem)) {
499 ++count;
500 }
501
502 ut_a(count == list.get_length());
503}
504
505/** Check the consistency of a two-way list.
506@param[in] LIST base node reference */
507#define UT_LIST_CHECK(LIST) \
508 do { \
509 NullValidate nullV; \
510 ut_list_validate(LIST, nullV); \
511 } while (0)
512
513/** Move the given element to the beginning of the list.
514@param[in,out] list the list object
515@param[in] elem the element of the list which will be moved
516 to the beginning of the list. */
517template <typename List>
518void ut_list_move_to_front(List &list, typename List::elem_type *elem) {
519 ut_ad(ut_list_exists(list, elem));
520
521 if (list.first_element != elem) {
522 ut_list_remove(list, elem);
523 ut_list_prepend(list, elem);
524 }
525}
526
527#ifdef UNIV_DEBUG
528/** Check if the given element exists in the list.
529@param[in,out] list the list object
530@param[in] elem the element of the list which will be checked */
531template <typename List>
532bool ut_list_exists(List &list, typename List::elem_type *elem) {
534 for (auto e1 : list) {
535 if (elem == e1) {
536 return true;
537 }
538 }
539 return false;
540}
541#endif
542
543#endif /* ut0lst.h */
Definition: sql_list.h:433
Definition: ut0lst.h:177
bool operator!=(const iterator &other) const
Definition: ut0lst.h:194
elem_type * m_elem
Definition: ut0lst.h:180
elem_type * operator*() const
Definition: ut0lst.h:195
bool operator==(const iterator &other) const
Definition: ut0lst.h:191
ut_list_base & m_list
Definition: ut0lst.h:179
iterator(ut_list_base &list, elem_type *elem)
Definition: ut0lst.h:184
iterator & operator++()
Definition: ut0lst.h:196
elem_type * m_prev_elem
Definition: ut0lst.h:181
A helper wrapper class for the list, which exposes begin(),end() iterators which let you remove the c...
Definition: ut0lst.h:172
ut_list_base & m_list
Definition: ut0lst.h:174
iterator begin()
Definition: ut0lst.h:219
Removable(ut_list_base &list)
Definition: ut0lst.h:218
iterator end()
Definition: ut0lst.h:220
Definition: ut0lst.h:141
base_iterator & operator++()
Definition: ut0lst.h:154
bool operator==(const base_iterator &other) const
Definition: ut0lst.h:147
E * operator*() const
Definition: ut0lst.h:153
E * m_elem
Definition: ut0lst.h:143
base_iterator(E *elem)
Definition: ut0lst.h:146
bool operator!=(const base_iterator &other) const
Definition: ut0lst.h:150
static Bigint * diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:1082
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
static evkeyval * get_node(HttpHeaders::Iterator::IteratorHandle handle)
Definition: http_common.cc:113
static int count
Definition: myisam_ftdump.cc:42
Type
Definition: resource_group_basic_types.h:32
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2859
Definition: ut0lst.h:452
void operator()(const void *)
Definition: ut0lst.h:453
Definition: ut0lst.h:244
static const ut_list_node< Type > & get_node(const Type &element)
Definition: ut0lst.h:245
The two-way list base node.
Definition: ut0lst.h:80
static const elem_type * next(const elem_type &e)
Definition: ut0lst.h:89
iterator end()
Definition: ut0lst.h:164
const_iterator begin() const
Definition: ut0lst.h:165
static elem_type * next(elem_type &e)
Definition: ut0lst.h:90
elem_type * first_element
Pointer to list start, NULL if empty.
Definition: ut0lst.h:99
elem_type * last_element
Pointer to list end, NULL if empty.
Definition: ut0lst.h:101
const_iterator end() const
Definition: ut0lst.h:166
void update_length(int diff)
Updates the length of the list by the amount specified.
Definition: ut0lst.h:117
std::atomic< size_t > count
Number of nodes in list.
Definition: ut0lst.h:138
void clear()
Definition: ut0lst.h:122
static elem_type * prev(elem_type &e)
Definition: ut0lst.h:94
static const node_type & get_node(const elem_type &e)
Definition: ut0lst.h:83
static const elem_type * prev(const elem_type &e)
Definition: ut0lst.h:93
iterator begin()
Definition: ut0lst.h:163
size_t get_length() const
Returns number of nodes currently present in the list.
Definition: ut0lst.h:110
Type elem_type
Definition: ut0lst.h:81
void reverse()
Definition: ut0lst.h:129
static node_type & get_node(elem_type &e)
Definition: ut0lst.h:86
Removable removable()
Returns a wrapper which lets you remove current item or items after it.
Definition: ut0lst.h:241
ulint init
UT_LIST_INITIALISED if the list was initialised with the constructor.
Definition: ut0lst.h:106
The two way list node.
Definition: ut0lst.h:49
Type * prev
pointer to the previous node, NULL if start of list
Definition: ut0lst.h:50
void reverse()
Definition: ut0lst.h:55
Type * next
pointer to next node, NULL if end of list
Definition: ut0lst.h:52
unsigned long int ulint
Definition: univ.i:407
Debug utilities for Innobase.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:68
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:56
#define UT_LIST_IS_INITIALISED(b)
Definition: ut0lst.h:69
void ut_list_validate(const List &list, Functor &functor)
Checks the consistency of a two-way list.
Definition: ut0lst.h:492
void ut_list_map(const List &list, Functor &functor)
Iterate over all the elements and call the functor for each element.
Definition: ut0lst.h:460
void ut_list_prepend(List &list, typename List::elem_type *elem)
Adds the node as the first element in a two-way linked list.
Definition: ut0lst.h:294
void ut_list_remove(List &list, typename List::elem_type *elem)
Removes a node from a two-way linked list.
Definition: ut0lst.h:396
#define UT_LIST_INITIALISED
Definition: ut0lst.h:66
void ut_list_append(List &list, typename List::elem_type *elem)
Adds the node as the last element in a two-way linked list.
Definition: ut0lst.h:327
void ut_list_reverse(List &list)
Definition: ut0lst.h:474
void ut_list_insert(List &list, typename List::elem_type *elem1, typename List::elem_type *elem2)
Inserts a ELEM2 after ELEM1 in a list.
Definition: ut0lst.h:360
void ut_list_move_to_front(List &list, typename List::elem_type *elem)
Move the given element to the beginning of the list.
Definition: ut0lst.h:518
bool ut_list_exists(List &list, typename List::elem_type *elem)
Check if the given element exists in the list.
Definition: ut0lst.h:532