MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ut0list.cc
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2006, 2011, Oracle and/or its affiliates. All Rights Reserved.
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 as published by the Free Software
7 Foundation; version 2 of the License.
8 
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.
12 
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.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /*******************************************************************/
26 #include "ut0list.h"
27 #ifdef UNIV_NONINL
28 #include "ut0list.ic"
29 #endif
30 
31 /****************************************************************/
34 UNIV_INTERN
35 ib_list_t*
37 /*=================*/
38 {
39  ib_list_t* list;
40 
41  list = static_cast<ib_list_t*>(mem_alloc(sizeof(*list)));
42 
43  list->first = NULL;
44  list->last = NULL;
45  list->is_heap_list = FALSE;
46 
47  return(list);
48 }
49 
50 /****************************************************************/
54 UNIV_INTERN
55 ib_list_t*
57 /*================*/
58  mem_heap_t* heap)
59 {
60  ib_list_t* list;
61 
62  list = static_cast<ib_list_t*>(mem_heap_alloc(heap, sizeof(*list)));
63 
64  list->first = NULL;
65  list->last = NULL;
66  list->is_heap_list = TRUE;
67 
68  return(list);
69 }
70 
71 /****************************************************************/
73 UNIV_INTERN
74 void
76 /*=========*/
77  ib_list_t* list)
78 {
79  ut_a(!list->is_heap_list);
80 
81  /* We don't check that the list is empty because it's entirely valid
82  to e.g. have all the nodes allocated from a single heap that is then
83  freed after the list itself is freed. */
84 
85  mem_free(list);
86 }
87 
88 /****************************************************************/
91 UNIV_INTERN
94 /*==============*/
95  ib_list_t* list,
96  void* data,
97  mem_heap_t* heap)
98 {
99  return(ib_list_add_after(list, ib_list_get_first(list), data, heap));
100 }
101 
102 /****************************************************************/
105 UNIV_INTERN
108 /*=============*/
109  ib_list_t* list,
110  void* data,
111  mem_heap_t* heap)
112 {
113  return(ib_list_add_after(list, ib_list_get_last(list), data, heap));
114 }
115 
116 /****************************************************************/
119 UNIV_INTERN
122 /*==============*/
123  ib_list_t* list,
124  ib_list_node_t* prev_node,
126  void* data,
127  mem_heap_t* heap)
128 {
129  ib_list_node_t* node;
130 
131  node = static_cast<ib_list_node_t*>(
132  mem_heap_alloc(heap, sizeof(*node)));
133 
134  node->data = data;
135 
136  if (!list->first) {
137  /* Empty list. */
138 
139  ut_a(!prev_node);
140 
141  node->prev = NULL;
142  node->next = NULL;
143 
144  list->first = node;
145  list->last = node;
146  } else if (!prev_node) {
147  /* Start of list. */
148 
149  node->prev = NULL;
150  node->next = list->first;
151 
152  list->first->prev = node;
153 
154  list->first = node;
155  } else {
156  /* Middle or end of list. */
157 
158  node->prev = prev_node;
159  node->next = prev_node->next;
160 
161  prev_node->next = node;
162 
163  if (node->next) {
164  node->next->prev = node;
165  } else {
166  list->last = node;
167  }
168  }
169 
170  return(node);
171 }
172 
173 /****************************************************************/
175 UNIV_INTERN
176 void
178 /*===========*/
179  ib_list_t* list,
180  ib_list_node_t* node)
181 {
182  if (node->prev) {
183  node->prev->next = node->next;
184  } else {
185  /* First item in list. */
186 
187  ut_ad(list->first == node);
188 
189  list->first = node->next;
190  }
191 
192  if (node->next) {
193  node->next->prev = node->prev;
194  } else {
195  /* Last item in list. */
196 
197  ut_ad(list->last == node);
198 
199  list->last = node->prev;
200  }
201 
202  node->prev = node->next = NULL;
203 }