1
/**********************************************************************
6
Created 9/10/1995 Heikki Tuuri
7
***********************************************************************/
14
/* This module implements the two-way linear list which should be used
15
if a list is used in the database. Note that a single struct may belong
16
to two or more lists, provided that the list are given different names.
17
An example of the usage of the lists can be found in fil0fil.c. */
19
/***********************************************************************
20
This macro expands to the unnamed type definition of a struct which acts
21
as the two-way list base node. The base node contains pointers
22
to both ends of the list and a count of nodes in the list (excluding
23
the base node from the count). TYPE should be the list node type name. */
25
#define UT_LIST_BASE_NODE_T(TYPE)\
27
ulint count; /* count of nodes in list */\
28
TYPE * start; /* pointer to list start, NULL if empty */\
29
TYPE * end; /* pointer to list end, NULL if empty */\
32
/***********************************************************************
33
This macro expands to the unnamed type definition of a struct which
34
should be embedded in the nodes of the list, the node type must be a struct.
35
This struct contains the pointers to next and previous nodes in the list.
36
The name of the field in the node struct should be the name given
37
to the list. TYPE should be the list node type name. Example of usage:
39
typedef struct LRU_node_struct LRU_node_t;
40
struct LRU_node_struct {
41
UT_LIST_NODE_T(LRU_node_t) LRU_list;
44
The example implements an LRU list of name LRU_list. Its nodes are of type
48
#define UT_LIST_NODE_T(TYPE)\
50
TYPE * prev; /* pointer to the previous node,\
51
NULL if start of list */\
52
TYPE * next; /* pointer to next node, NULL if end of list */\
55
/***********************************************************************
56
Initializes the base node of a two-way list. */
58
#define UT_LIST_INIT(BASE)\
65
/***********************************************************************
66
Adds the node as the first element in a two-way linked list.
67
BASE has to be the base node (not a pointer to it). N has to be
68
the pointer to the node to be added to the list. NAME is the list name. */
70
#define UT_LIST_ADD_FIRST(NAME, BASE, N)\
74
((N)->NAME).next = (BASE).start;\
75
((N)->NAME).prev = NULL;\
76
if ((BASE).start != NULL) {\
77
(((BASE).start)->NAME).prev = (N);\
80
if ((BASE).end == NULL) {\
85
/***********************************************************************
86
Adds the node as the last element in a two-way linked list.
87
BASE has to be the base node (not a pointer to it). N has to be
88
the pointer to the node to be added to the list. NAME is the list name. */
90
#define UT_LIST_ADD_LAST(NAME, BASE, N)\
94
((N)->NAME).prev = (BASE).end;\
95
((N)->NAME).next = NULL;\
96
if ((BASE).end != NULL) {\
97
(((BASE).end)->NAME).next = (N);\
100
if ((BASE).start == NULL) {\
105
/***********************************************************************
106
Inserts a NODE2 after NODE1 in a list.
107
BASE has to be the base node (not a pointer to it). NAME is the list
108
name, NODE1 and NODE2 are pointers to nodes. */
110
#define UT_LIST_INSERT_AFTER(NAME, BASE, NODE1, NODE2)\
115
((NODE2)->NAME).prev = (NODE1);\
116
((NODE2)->NAME).next = ((NODE1)->NAME).next;\
117
if (((NODE1)->NAME).next != NULL) {\
118
((((NODE1)->NAME).next)->NAME).prev = (NODE2);\
120
((NODE1)->NAME).next = (NODE2);\
121
if ((BASE).end == (NODE1)) {\
122
(BASE).end = (NODE2);\
126
/***********************************************************************
127
Removes a node from a two-way linked list. BASE has to be the base node
128
(not a pointer to it). N has to be the pointer to the node to be removed
129
from the list. NAME is the list name. */
131
#define UT_LIST_REMOVE(NAME, BASE, N)\
134
ut_a((BASE).count > 0);\
136
if (((N)->NAME).next != NULL) {\
137
((((N)->NAME).next)->NAME).prev = ((N)->NAME).prev;\
139
(BASE).end = ((N)->NAME).prev;\
141
if (((N)->NAME).prev != NULL) {\
142
((((N)->NAME).prev)->NAME).next = ((N)->NAME).next;\
144
(BASE).start = ((N)->NAME).next;\
148
/************************************************************************
149
Gets the next node in a two-way list. NAME is the name of the list
150
and N is pointer to a node. */
152
#define UT_LIST_GET_NEXT(NAME, N)\
155
/************************************************************************
156
Gets the previous node in a two-way list. NAME is the name of the list
157
and N is pointer to a node. */
159
#define UT_LIST_GET_PREV(NAME, N)\
162
/************************************************************************
163
Alternative macro to get the number of nodes in a two-way list, i.e.,
164
its length. BASE is the base node (not a pointer to it). */
166
#define UT_LIST_GET_LEN(BASE)\
169
/************************************************************************
170
Gets the first node in a two-way list, or returns NULL,
171
if the list is empty. BASE is the base node (not a pointer to it). */
173
#define UT_LIST_GET_FIRST(BASE)\
176
/************************************************************************
177
Gets the last node in a two-way list, or returns NULL,
178
if the list is empty. BASE is the base node (not a pointer to it). */
180
#define UT_LIST_GET_LAST(BASE)\
183
/************************************************************************
184
Checks the consistency of a two-way list. NAME is the name of the list,
185
TYPE is the node type, and BASE is the base node (not a pointer to it). */
187
#define UT_LIST_VALIDATE(NAME, TYPE, BASE)\
189
ulint ut_list_i_313;\
190
TYPE * ut_list_node_313;\
192
ut_list_node_313 = (BASE).start;\
194
for (ut_list_i_313 = 0; ut_list_i_313 < (BASE).count;\
196
ut_a(ut_list_node_313);\
197
ut_list_node_313 = (ut_list_node_313->NAME).next;\
200
ut_a(ut_list_node_313 == NULL);\
202
ut_list_node_313 = (BASE).end;\
204
for (ut_list_i_313 = 0; ut_list_i_313 < (BASE).count;\
206
ut_a(ut_list_node_313);\
207
ut_list_node_313 = (ut_list_node_313->NAME).prev;\
210
ut_a(ut_list_node_313 == NULL);\