Debian Codegraph @

storage/innobase/ut/ut0list.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2006, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file ut/ut0list.cc
29 A double-linked list
30
31 Created 4/26/2006 Osku Salerma
32 ************************************************************************/
33
34 #include "ut0list.h"
35
36 #include <stddef.h>
37
38 /** Create a new list.
39 @return list */
40 ib_list_t *ib_list_create(void) {
41 return (static_cast<ib_list_t *>(
42 ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(ib_list_t))));
43 }
44
45 /** Free a list. */
46 void ib_list_free(ib_list_t *list) /*!< in: list */
47 {
48 /* We don't check that the list is empty because it's entirely valid
49 to e.g. have all the nodes allocated from a single heap that is then
50 freed after the list itself is freed. */
51
52 ut::free(list);
53 }
54
55 /** Add the data after the indicated node.
56 @return new list node */
57 static ib_list_node_t *ib_list_add_after(
58 ib_list_t *list, /*!< in: list */
59 ib_list_node_t *prev_node, /*!< in: node preceding new node (can
60 be NULL) */
61 void *data, /*!< in: data */
62 mem_heap_t *heap) /*!< in: memory heap to use */
63 {
64 ib_list_node_t *node;
65
66 node = static_cast<ib_list_node_t *>(mem_heap_alloc(heap, sizeof(*node)));
67
68 node->data = data;
69
70 if (!list->first) {
71 /* Empty list. */
72
73 ut_a(!prev_node);
74
75 node->prev = nullptr;
76 node->next = nullptr;
77
78 list->first = node;
79 list->last = node;
80 } else if (!prev_node) {
81 /* Start of list. */
82
83 node->prev = nullptr;
84 node->next = list->first;
85
86 list->first->prev = node;
87
88 list->first = node;
89 } else {
90 /* Middle or end of list. */
91
92 node->prev = prev_node;
93 node->next = prev_node->next;
94
95 prev_node->next = node;
96
97 if (node->next) {
98 node->next->prev = node;
99 } else {
100 list->last = node;
101 }
102 }
103
104 return (node);
105 }
106
107 /** Add the data to the end of the list.
108 @return new list node */
109 ib_list_node_t *ib_list_add_last(
110 ib_list_t *list, /*!< in: list */
111 void *data, /*!< in: data */
112 mem_heap_t *heap) /*!< in: memory heap to use */
113 {
114 return (ib_list_add_after(list, ib_list_get_last(list), data, heap));
115 }
116
117 /** Remove the node from the list.
118 @param[in] list List
119 @param[in] node Node to remove */
120 void ib_list_remove(ib_list_t *list, ib_list_node_t *node) {
121 if (node->prev) {
122 node->prev->next = node->next;
123 } else {
124 /* First item in list. */
125
126 ut_ad(list->first == node);
127
128 list->first = node->next;
129 }
130
131 if (node->next) {
132 node->next->prev = node->prev;
133 } else {
134 /* Last item in list. */
135
136 ut_ad(list->last == node);
137
138 list->last = node->prev;
139 }
140
141 node->prev = node->next = nullptr;
142 }