drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
ut0lst.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (C) 1995, 2010, Innobase Oy. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15St, Fifth Floor, Boston, MA 02110-1301 USA
16
17*****************************************************************************/
18
19/******************************************************************/
25
26#pragma once
27#ifndef ut0lst_h
28#define ut0lst_h
29
30#include "univ.i"
31
32/* This module implements the two-way linear list which should be used
33if a list is used in the database. Note that a single struct may belong
34to two or more lists, provided that the list are given different names.
35An example of the usage of the lists can be found in fil0fil.c. */
36
37/*******************************************************************/
43#ifdef __cplusplus
44template<class T>
45class ut_list_base_node
46{
47public:
48 size_t count; \
49 T * start; \
50 T * end; \
51};
52#define UT_LIST_BASE_NODE_T(TYPE) ut_list_base_node<TYPE>
53#else
54#define UT_LIST_BASE_NODE_T(TYPE) int
55#endif
56
57/*******************************************************************/
64/* Example:
65typedef struct LRU_node_struct LRU_node_t;
66struct LRU_node_struct {
67 UT_LIST_NODE_T(LRU_node_t) LRU_list;
68 ...
69}
70The example implements an LRU list of name LRU_list. Its nodes are of type
71LRU_node_t. */
72
73#define UT_LIST_NODE_T(TYPE)\
74struct {\
75 TYPE * prev; \
77 TYPE * next; \
78}\
79
80/*******************************************************************/
83*/
84#define UT_LIST_INIT(BASE)\
85{\
86 (BASE).count = 0;\
87 (BASE).start = NULL;\
88 (BASE).end = NULL;\
89}\
90
91/*******************************************************************/
96*/
97#define UT_LIST_ADD_FIRST(NAME, BASE, N)\
98{\
99 ut_ad(N);\
100 ((BASE).count)++;\
101 ((N)->NAME).next = (BASE).start;\
102 ((N)->NAME).prev = NULL;\
103 if (UNIV_LIKELY((BASE).start != NULL)) {\
104 ut_ad((BASE).start != (N));\
105 (((BASE).start)->NAME).prev = (N);\
106 }\
107 (BASE).start = (N);\
108 if (UNIV_UNLIKELY((BASE).end == NULL)) {\
109 (BASE).end = (N);\
110 }\
111}\
112
113/*******************************************************************/
119#define UT_LIST_ADD_LAST(NAME, BASE, N)\
120{\
121 ut_ad(N != NULL);\
122 ((BASE).count)++;\
123 ((N)->NAME).prev = (BASE).end;\
124 ((N)->NAME).next = NULL;\
125 if ((BASE).end != NULL) {\
126 ut_ad((BASE).end != (N));\
127 (((BASE).end)->NAME).next = (N);\
128 }\
129 (BASE).end = (N);\
130 if ((BASE).start == NULL) {\
131 (BASE).start = (N);\
132 }\
133}\
134
135/*******************************************************************/
142#define UT_LIST_INSERT_AFTER(NAME, BASE, NODE1, NODE2)\
143{\
144 ut_ad(NODE1);\
145 ut_ad(NODE2);\
146 ut_ad((NODE1) != (NODE2));\
147 ((BASE).count)++;\
148 ((NODE2)->NAME).prev = (NODE1);\
149 ((NODE2)->NAME).next = ((NODE1)->NAME).next;\
150 if (((NODE1)->NAME).next != NULL) {\
151 ((((NODE1)->NAME).next)->NAME).prev = (NODE2);\
152 }\
153 ((NODE1)->NAME).next = (NODE2);\
154 if ((BASE).end == (NODE1)) {\
155 (BASE).end = (NODE2);\
156 }\
157}\
158
159#ifdef UNIV_LIST_DEBUG
163# define UT_LIST_REMOVE_CLEAR(NAME, N) \
164((N)->NAME.prev = (N)->NAME.next = (void*) -1)
165#else
169# define UT_LIST_REMOVE_CLEAR(NAME, N)
170#endif
171
172/*******************************************************************/
178#define UT_LIST_REMOVE(NAME, BASE, N) \
179do { \
180 ut_ad(N); \
181 ut_a((BASE).count > 0); \
182 ((BASE).count)--; \
183 if (((N)->NAME).next != NULL) { \
184 ((((N)->NAME).next)->NAME).prev = ((N)->NAME).prev; \
185 } else { \
186 (BASE).end = ((N)->NAME).prev; \
187 } \
188 if (((N)->NAME).prev != NULL) { \
189 ((((N)->NAME).prev)->NAME).next = ((N)->NAME).next; \
190 } else { \
191 (BASE).start = ((N)->NAME).next; \
192 } \
193 UT_LIST_REMOVE_CLEAR(NAME, N); \
194} while (0)
195
196/********************************************************************/
200@return the successor of N in NAME, or NULL */
201#define UT_LIST_GET_NEXT(NAME, N)\
202 (((N)->NAME).next)
203
204/********************************************************************/
208@return the predecessor of N in NAME, or NULL */
209#define UT_LIST_GET_PREV(NAME, N)\
210 (((N)->NAME).prev)
211
212/********************************************************************/
216@return the number of nodes in the list */
217#define UT_LIST_GET_LEN(BASE)\
218 (BASE).count
219
220/********************************************************************/
223@return first node, or NULL if the list is empty */
224#define UT_LIST_GET_FIRST(BASE)\
225 (BASE).start
226
227/********************************************************************/
231#ifdef __cplusplus
232#define UT_LIST_GET_LAST(BASE)\
233 (BASE).end
234#else
235#define UT_LIST_GET_LAST(BASE) (BASE= NULL)
236#endif
237
238/********************************************************************/
243@param ASSERTION a condition on ut_list_node_313 */
244#define UT_LIST_VALIDATE(NAME, TYPE, BASE, ASSERTION) \
245do { \
246 ulint ut_list_i_313; \
247 TYPE* ut_list_node_313; \
248 \
249 ut_list_node_313 = (BASE).start; \
250 \
251 for (ut_list_i_313 = (BASE).count; ut_list_i_313--; ) { \
252 ut_a(ut_list_node_313); \
253 ASSERTION; \
254 ut_ad((ut_list_node_313->NAME).next || !ut_list_i_313); \
255 ut_list_node_313 = (ut_list_node_313->NAME).next; \
256 } \
257 \
258 ut_a(ut_list_node_313 == NULL); \
259 \
260 ut_list_node_313 = (BASE).end; \
261 \
262 for (ut_list_i_313 = (BASE).count; ut_list_i_313--; ) { \
263 ut_a(ut_list_node_313); \
264 ASSERTION; \
265 ut_ad((ut_list_node_313->NAME).prev || !ut_list_i_313); \
266 ut_list_node_313 = (ut_list_node_313->NAME).prev; \
267 } \
268 \
269 ut_a(ut_list_node_313 == NULL); \
270} while (0)
271
272#endif
273