1 /*****************************************************************************
2
3 Copyright (c) 2006, 2021, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is also distributed 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 included with MySQL.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24
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 #ifdef UNIV_NONINL
36 #include "ut0list.ic"
37 #endif
38
39 /****************************************************************//**
40 Create a new list.
41 @return list */
42 ib_list_t*
ib_list_create(void)43 ib_list_create(void)
44 /*=================*/
45 {
46 ib_list_t* list;
47
48 list = static_cast<ib_list_t*>(ut_malloc_nokey(sizeof(*list)));
49
50 list->first = NULL;
51 list->last = NULL;
52 list->is_heap_list = FALSE;
53
54 return(list);
55 }
56
57 /****************************************************************//**
58 Create a new list using the given heap. ib_list_free MUST NOT BE CALLED for
59 lists created with this function.
60 @return list */
61 ib_list_t*
ib_list_create_heap(mem_heap_t * heap)62 ib_list_create_heap(
63 /*================*/
64 mem_heap_t* heap) /*!< in: memory heap to use */
65 {
66 ib_list_t* list;
67
68 list = static_cast<ib_list_t*>(mem_heap_alloc(heap, sizeof(*list)));
69
70 list->first = NULL;
71 list->last = NULL;
72 list->is_heap_list = TRUE;
73
74 return(list);
75 }
76
77 /****************************************************************//**
78 Free a list. */
79 void
ib_list_free(ib_list_t * list)80 ib_list_free(
81 /*=========*/
82 ib_list_t* list) /*!< in: list */
83 {
84 ut_a(!list->is_heap_list);
85
86 /* We don't check that the list is empty because it's entirely valid
87 to e.g. have all the nodes allocated from a single heap that is then
88 freed after the list itself is freed. */
89
90 ut_free(list);
91 }
92
93 /****************************************************************//**
94 Add the data to the start of the list.
95 @return new list node */
96 ib_list_node_t*
ib_list_add_first(ib_list_t * list,void * data,mem_heap_t * heap)97 ib_list_add_first(
98 /*==============*/
99 ib_list_t* list, /*!< in: list */
100 void* data, /*!< in: data */
101 mem_heap_t* heap) /*!< in: memory heap to use */
102 {
103 return(ib_list_add_after(list, ib_list_get_first(list), data, heap));
104 }
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(ib_list_t * list,void * data,mem_heap_t * heap)110 ib_list_add_last(
111 /*=============*/
112 ib_list_t* list, /*!< in: list */
113 void* data, /*!< in: data */
114 mem_heap_t* heap) /*!< in: memory heap to use */
115 {
116 return(ib_list_add_after(list, ib_list_get_last(list), data, heap));
117 }
118
119 /****************************************************************//**
120 Add the data after the indicated node.
121 @return new list node */
122 ib_list_node_t*
ib_list_add_after(ib_list_t * list,ib_list_node_t * prev_node,void * data,mem_heap_t * heap)123 ib_list_add_after(
124 /*==============*/
125 ib_list_t* list, /*!< in: list */
126 ib_list_node_t* prev_node, /*!< in: node preceding new node (can
127 be NULL) */
128 void* data, /*!< in: data */
129 mem_heap_t* heap) /*!< in: memory heap to use */
130 {
131 ib_list_node_t* node;
132
133 node = static_cast<ib_list_node_t*>(
134 mem_heap_alloc(heap, sizeof(*node)));
135
136 node->data = data;
137
138 if (!list->first) {
139 /* Empty list. */
140
141 ut_a(!prev_node);
142
143 node->prev = NULL;
144 node->next = NULL;
145
146 list->first = node;
147 list->last = node;
148 } else if (!prev_node) {
149 /* Start of list. */
150
151 node->prev = NULL;
152 node->next = list->first;
153
154 list->first->prev = node;
155
156 list->first = node;
157 } else {
158 /* Middle or end of list. */
159
160 node->prev = prev_node;
161 node->next = prev_node->next;
162
163 prev_node->next = node;
164
165 if (node->next) {
166 node->next->prev = node;
167 } else {
168 list->last = node;
169 }
170 }
171
172 return(node);
173 }
174
175 /****************************************************************//**
176 Remove the node from the list. */
177 void
ib_list_remove(ib_list_t * list,ib_list_node_t * node)178 ib_list_remove(
179 /*===========*/
180 ib_list_t* list, /*!< in: list */
181 ib_list_node_t* node) /*!< in: node to remove */
182 {
183 if (node->prev) {
184 node->prev->next = node->next;
185 } else {
186 /* First item in list. */
187
188 ut_ad(list->first == node);
189
190 list->first = node->next;
191 }
192
193 if (node->next) {
194 node->next->prev = node->prev;
195 } else {
196 /* Last item in list. */
197
198 ut_ad(list->last == node);
199
200 list->last = node->prev;
201 }
202
203 node->prev = node->next = NULL;
204 }
205