1
/*****************************************************************************
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/**********************************************************************
22
Created 9/10/1995 Heikki Tuuri
23
***********************************************************************/
30
/* This module implements the two-way linear list which should be used
31
if a list is used in the database. Note that a single struct may belong
32
to two or more lists, provided that the list are given different names.
33
An example of the usage of the lists can be found in fil0fil.c. */
35
/***********************************************************************
36
This macro expands to the unnamed type definition of a struct which acts
37
as the two-way list base node. The base node contains pointers
38
to both ends of the list and a count of nodes in the list (excluding
39
the base node from the count). TYPE should be the list node type name. */
41
#define UT_LIST_BASE_NODE_T(TYPE)\
43
ulint count; /* count of nodes in list */\
44
TYPE * start; /* pointer to list start, NULL if empty */\
45
TYPE * end; /* pointer to list end, NULL if empty */\
48
/***********************************************************************
49
This macro expands to the unnamed type definition of a struct which
50
should be embedded in the nodes of the list, the node type must be a struct.
51
This struct contains the pointers to next and previous nodes in the list.
52
The name of the field in the node struct should be the name given
53
to the list. TYPE should be the list node type name. Example of usage:
55
typedef struct LRU_node_struct LRU_node_t;
56
struct LRU_node_struct {
57
UT_LIST_NODE_T(LRU_node_t) LRU_list;
60
The example implements an LRU list of name LRU_list. Its nodes are of type
64
#define UT_LIST_NODE_T(TYPE)\
66
TYPE * prev; /* pointer to the previous node,\
67
NULL if start of list */\
68
TYPE * next; /* pointer to next node, NULL if end of list */\
71
/***********************************************************************
72
Initializes the base node of a two-way list. */
74
#define UT_LIST_INIT(BASE)\
81
/***********************************************************************
82
Adds the node as the first element in a two-way linked list.
83
BASE has to be the base node (not a pointer to it). N has to be
84
the pointer to the node to be added to the list. NAME is the list name. */
86
#define UT_LIST_ADD_FIRST(NAME, BASE, N)\
90
((N)->NAME).next = (BASE).start;\
91
((N)->NAME).prev = NULL;\
92
if (UNIV_LIKELY((BASE).start != NULL)) {\
93
ut_ad((BASE).start != (N));\
94
(((BASE).start)->NAME).prev = (N);\
97
if (UNIV_UNLIKELY((BASE).end == NULL)) {\
102
/***********************************************************************
103
Adds the node as the last element in a two-way linked list.
104
BASE has to be the base node (not a pointer to it). N has to be
105
the pointer to the node to be added to the list. NAME is the list name. */
107
#define UT_LIST_ADD_LAST(NAME, BASE, N)\
111
((N)->NAME).prev = (BASE).end;\
112
((N)->NAME).next = NULL;\
113
if ((BASE).end != NULL) {\
114
ut_ad((BASE).end != (N));\
115
(((BASE).end)->NAME).next = (N);\
118
if ((BASE).start == NULL) {\
123
/***********************************************************************
124
Inserts a NODE2 after NODE1 in a list.
125
BASE has to be the base node (not a pointer to it). NAME is the list
126
name, NODE1 and NODE2 are pointers to nodes. */
128
#define UT_LIST_INSERT_AFTER(NAME, BASE, NODE1, NODE2)\
132
ut_ad((NODE1) != (NODE2));\
134
((NODE2)->NAME).prev = (NODE1);\
135
((NODE2)->NAME).next = ((NODE1)->NAME).next;\
136
if (((NODE1)->NAME).next != NULL) {\
137
((((NODE1)->NAME).next)->NAME).prev = (NODE2);\
139
((NODE1)->NAME).next = (NODE2);\
140
if ((BASE).end == (NODE1)) {\
141
(BASE).end = (NODE2);\
145
/* Invalidate the pointers in a list node. */
146
#ifdef UNIV_LIST_DEBUG
147
# define UT_LIST_REMOVE_CLEAR(NAME, N) \
148
((N)->NAME.prev = (N)->NAME.next = (void*) -1)
150
# define UT_LIST_REMOVE_CLEAR(NAME, N)
153
/***********************************************************************
154
Removes a node from a two-way linked list. BASE has to be the base node
155
(not a pointer to it). N has to be the pointer to the node to be removed
156
from the list. NAME is the list name. */
158
#define UT_LIST_REMOVE(NAME, BASE, N) \
161
ut_a((BASE).count > 0); \
163
if (((N)->NAME).next != NULL) { \
164
((((N)->NAME).next)->NAME).prev = ((N)->NAME).prev; \
166
(BASE).end = ((N)->NAME).prev; \
168
if (((N)->NAME).prev != NULL) { \
169
((((N)->NAME).prev)->NAME).next = ((N)->NAME).next; \
171
(BASE).start = ((N)->NAME).next; \
173
UT_LIST_REMOVE_CLEAR(NAME, N); \
176
/************************************************************************
177
Gets the next node in a two-way list. NAME is the name of the list
178
and N is pointer to a node. */
180
#define UT_LIST_GET_NEXT(NAME, N)\
183
/************************************************************************
184
Gets the previous node in a two-way list. NAME is the name of the list
185
and N is pointer to a node. */
187
#define UT_LIST_GET_PREV(NAME, N)\
190
/************************************************************************
191
Alternative macro to get the number of nodes in a two-way list, i.e.,
192
its length. BASE is the base node (not a pointer to it). */
194
#define UT_LIST_GET_LEN(BASE)\
197
/************************************************************************
198
Gets the first node in a two-way list, or returns NULL,
199
if the list is empty. BASE is the base node (not a pointer to it). */
201
#define UT_LIST_GET_FIRST(BASE)\
204
/************************************************************************
205
Gets the last node in a two-way list, or returns NULL,
206
if the list is empty. BASE is the base node (not a pointer to it). */
208
#define UT_LIST_GET_LAST(BASE)\
211
/************************************************************************
212
Checks the consistency of a two-way list. NAME is the name of the list,
213
TYPE is the node type, and BASE is the base node (not a pointer to it). */
215
#define UT_LIST_VALIDATE(NAME, TYPE, BASE)\
217
ulint ut_list_i_313;\
218
TYPE * ut_list_node_313;\
220
ut_list_node_313 = (BASE).start;\
222
for (ut_list_i_313 = 0; ut_list_i_313 < (BASE).count;\
224
ut_a(ut_list_node_313);\
225
ut_list_node_313 = (ut_list_node_313->NAME).next;\
228
ut_a(ut_list_node_313 == NULL);\
230
ut_list_node_313 = (BASE).end;\
232
for (ut_list_i_313 = 0; ut_list_i_313 < (BASE).count;\
234
ut_a(ut_list_node_313);\
235
ut_list_node_313 = (ut_list_node_313->NAME).prev;\
238
ut_a(ut_list_node_313 == NULL);\