MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SLFifoList.hpp
1 /*
2  Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #ifndef SLFIFOLIST_HPP
19 #define SLFIFOLIST_HPP
20 
21 #include <ndb_global.h>
22 #include <kernel_types.h>
23 #include "Pool.hpp"
24 
29 template <typename P, typename T, typename U = T>
31 {
32 public:
36  struct HeadPOD
37  {
38  Uint32 firstItem;
39  Uint32 lastItem;
40 
41 #ifdef VM_TRACE
42  bool in_use;
43 #endif
44  void init();
45  inline bool isEmpty() const { return firstItem == RNIL;}
46  };
47 
48  struct Head : public HeadPOD
49  {
50  Head() { this->init();}
51 
52  Head& operator=(const HeadPOD& src) {
53  this->firstItem = src.firstItem;
54  this->lastItem = src.lastItem;
55 #ifdef VM_TRACE
56  this->in_use = src.in_use;
57 #endif
58  return *this;
59  }
60  };
61  SLFifoListImpl(P & thePool);
62 
63  bool seizeFirst(Ptr<T> &);
64  bool seizeLast(Ptr<T> &);
65  bool seize(Ptr<T> & ptr) { return seizeLast(ptr);}
66 
67  void releaseFirst(Ptr<T> &);
68 
69  void addFirst(Ptr<T> &);
70  void addLast(Ptr<T> &);
71 
72  void removeFirst(Ptr<T> &);
73  void remove() { head.init(); }
74 
78  void getPtr(Ptr<T> &, Uint32 i) const;
79 
83  void getPtr(Ptr<T> &) const ;
84 
88  T * getPtr(Uint32 i) const ;
89 
95  bool first(Ptr<T> &) const ;
96 
102  bool last(Ptr<T> &) const ;
103 
109  bool next(Ptr<T> &) const ;
110 
116  bool hasNext(const Ptr<T> &) const;
117 
118  inline bool isEmpty() const { return head.firstItem == RNIL;}
119 
120 protected:
121  Head head;
122  P & thePool;
123 };
124 
125 template <typename P, typename T, typename U = T>
126 class LocalSLFifoListImpl : public SLFifoListImpl<P,T,U>
127 {
128 public:
129  LocalSLFifoListImpl(P & thePool, typename SLFifoListImpl<P,T,U>::HeadPOD&_src)
130  : SLFifoListImpl<P,T,U>(thePool), src(_src)
131  {
132  this->head = src;
133 #ifdef VM_TRACE
134  assert(src.in_use == false);
135  src.in_use = true;
136 #endif
137  }
138 
140 #ifdef VM_TRACE
141  assert(src.in_use == true);
142 #endif
143  src = this->head;
144  }
145 private:
146  typename SLFifoListImpl<P,T,U>::HeadPOD & src;
147 };
148 
149 template <typename P, typename T, typename U>
150 inline
152  thePool(_pool)
153 {
154 }
155 
156 template <typename P, typename T, typename U>
157 inline
158 void
160 {
161  this->firstItem = RNIL;
162  this->lastItem = RNIL;
163 #ifdef VM_TRACE
164  this->in_use = false;
165 #endif
166 }
167 
168 template <typename P, typename T, typename U>
169 inline
170 bool
172 {
173  if (likely(thePool.seize(p)))
174  {
175  addFirst(p);
176  return true;
177  }
178  p.p = NULL;
179  return false;
180 }
181 
182 template <typename P, typename T, typename U>
183 inline
184 bool
186 {
187  if (likely(thePool.seize(p)))
188  {
189  addLast(p);
190  return true;
191  }
192  p.p = NULL;
193  return false;
194 }
195 
196 template <typename P, typename T, typename U>
197 inline
198 void
200 {
201  Uint32 first = head.firstItem;
202  head.firstItem = p.i;
203  if (first == RNIL)
204  {
205  head.lastItem = p.i;
206  }
207  p.p->U::nextList = first;
208 }
209 
210 template <typename P, typename T, typename U>
211 inline
212 void
214 {
215  T * t = p.p;
216  Uint32 last = head.lastItem;
217 
218  t->U::nextList = RNIL;
219  head.lastItem = p.i;
220 
221  if(last != RNIL)
222  {
223  T * t2 = thePool.getPtr(last);
224  t2->U::nextList = p.i;
225  }
226  else
227  {
228  head.firstItem = p.i;
229  }
230 }
231 
232 template <typename P, typename T, typename U>
233 inline
234 void
236 {
237  Uint32 first = head.firstItem;
238  Uint32 last = head.lastItem;
239  assert(p.i == first);
240  if (first != last)
241  {
242  head.firstItem = p.p->U::nextList;
243  }
244  else
245  {
246  head.firstItem = head.lastItem = RNIL;
247  }
248 }
249 
250 template <typename P, typename T, typename U>
251 inline
252 void
254 {
255  removeFirst(p);
256  thePool.release(p);
257 }
258 
259 template <typename P, typename T, typename U>
260 inline
261 void
263 {
264  p.i = i;
265  p.p = thePool.getPtr(i);
266 }
267 
268 template <typename P, typename T, typename U>
269 inline
270 void
272 {
273  thePool.getPtr(p);
274 }
275 
276 template <typename P, typename T, typename U>
277 inline
278 T *
280 {
281  return thePool.getPtr(i);
282 }
283 
289 template <typename P, typename T, typename U>
290 inline
291 bool
293 {
294  p.i = head.firstItem;
295  if(p.i != RNIL)
296  {
297  p.p = thePool.getPtr(p.i);
298  return true;
299  }
300  p.p = NULL;
301  return false;
302 }
303 
304 template <typename P, typename T, typename U>
305 inline
306 bool
308 {
309  p.i = head.lastItem;
310  if(p.i != RNIL)
311  {
312  p.p = thePool.getPtr(p.i);
313  return true;
314  }
315  p.p = NULL;
316  return false;
317 }
318 
319 template <typename P, typename T, typename U>
320 inline
321 bool
323 {
324  p.i = p.p->U::nextList;
325  if(p.i != RNIL)
326  {
327  p.p = thePool.getPtr(p.i);
328  return true;
329  }
330  p.p = NULL;
331  return false;
332 }
333 
334 template <typename P, typename T, typename U>
335 inline
336 bool
338 {
339  return p.p->U::nextList != RNIL;
340 }
341 
342 // Specializations
343 
344 template <typename T, typename U = T>
345 class SLFifoList : public SLFifoListImpl<ArrayPool<T>, T, U>
346 {
347 public:
349 };
350 
351 template <typename T, typename U = T>
352 class LocalSLFifoList : public LocalSLFifoListImpl<ArrayPool<T>,T,U> {
353 public:
355  : LocalSLFifoListImpl<ArrayPool<T>,T,U>(p, _src) {}
356 };
357 
358 #endif