1
/* Copyright (C) 2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#ifndef DLFIFOLIST_HPP
17
#define DLFIFOLIST_HPP
19
#include <ndb_global.h>
20
#include <kernel_types.h>
24
* Template class used for implementing an
25
* list of object retreived from a pool
27
template <typename P, typename T, typename U = T>
44
inline bool isEmpty() const { return firstItem == RNIL;}
47
DLFifoListImpl(P & thePool);
49
bool seizeFirst(Ptr<T> &);
50
bool seizeLast(Ptr<T> &);
51
bool seize(Ptr<T> & ptr) { return seizeLast(ptr);}
53
void release(Ptr<T> &);
54
void release(); // release all
56
void addFirst(Ptr<T> &);
57
void addLast(Ptr<T> &);
58
void add(Ptr<T> & ptr) { addLast(ptr);}
61
* Insert object <em>ptr</ptr> _before_ <em>loc</em>
63
void insert(Ptr<T> & ptr, Ptr<T>& loc);
66
void remove(Ptr<T> &);
69
* Update i & p value according to <b>i</b>
71
void getPtr(Ptr<T> &, Uint32 i) const;
74
* Update p value for ptr according to i value
76
void getPtr(Ptr<T> &) const ;
79
* Get pointer for i value
81
T * getPtr(Uint32 i) const ;
84
* Update ptr to first element in list
88
bool first(Ptr<T> &) const ;
91
* Update ptr to first element in list
95
bool last(Ptr<T> &) const ;
100
* NOTE ptr must be both p & i
102
bool next(Ptr<T> &) const ;
107
* NOTE ptr must be both p & i
109
bool prev(Ptr<T> &) const ;
112
* Check if next exists i.e. this is not last
114
* NOTE ptr must be both p & i
116
bool hasNext(const Ptr<T> &) const;
119
* Check if next exists i.e. this is not last
121
* NOTE ptr must be both p & i
123
bool hasPrev(const Ptr<T> &) const;
125
inline bool isEmpty() const { return head.firstItem == RNIL;}
129
* Will construct to identical lists
131
DLFifoListImpl<P,T,U>& operator=(const DLFifoListImpl<P,T,U>& src){
132
assert(&thePool == &src.thePool);
133
this->head = src.head;
142
template <typename P, typename T, typename U = T>
143
class LocalDLFifoListImpl : public DLFifoListImpl<P,T,U>
146
LocalDLFifoListImpl(P & thePool, typename DLFifoListImpl<P,T,U>::Head &_src)
147
: DLFifoListImpl<P,T,U>(thePool), src(_src)
151
assert(src.in_use == false);
156
~LocalDLFifoListImpl(){
158
assert(src.in_use == true);
163
typename DLFifoListImpl<P,T,U>::Head & src;
166
template <typename P, typename T, typename U>
168
DLFifoListImpl<P,T,U>::DLFifoListImpl(P & _pool):
173
template <typename P, typename T, typename U>
175
DLFifoListImpl<P,T,U>::Head::Head()
184
template <typename P, typename T, typename U>
187
DLFifoListImpl<P,T,U>::seizeFirst(Ptr<T> & p)
189
if (likely(thePool.seize(p)))
198
template <typename P, typename T, typename U>
201
DLFifoListImpl<P,T,U>::seizeLast(Ptr<T> & p)
203
if (likely(thePool.seize(p)))
212
template <typename P, typename T, typename U>
215
DLFifoListImpl<P,T,U>::addFirst(Ptr<T> & p)
217
Uint32 ff = head.firstItem;
219
p.p->U::prevList = RNIL;
220
p.p->U::nextList = ff;
221
head.firstItem = p.i;
228
T * t2 = thePool.getPtr(ff);
229
t2->U::prevList = p.i;
233
template <typename P, typename T, typename U>
236
DLFifoListImpl<P,T,U>::addLast(Ptr<T> & p)
239
Uint32 last = head.lastItem;
242
t->U::nextList = RNIL;
243
t->U::prevList = last;
247
T * t2 = thePool.getPtr(last);
248
t2->U::nextList = p.i;
252
head.firstItem = p.i;
256
template <typename P, typename T, typename U>
259
DLFifoListImpl<P,T,U>::insert(Ptr<T> & ptr, Ptr<T> & loc)
261
Uint32 prev= loc.p->U::prevList;
262
if(loc.i == head.firstItem)
264
head.firstItem = ptr.i;
265
assert(prev == RNIL);
269
T* t2 = thePool.getPtr(prev);
270
t2->U::nextList = ptr.i;
273
loc.p->U::prevList = ptr.i;
274
ptr.p->U::prevList = prev;
275
ptr.p->U::nextList = loc.i;
278
template <typename P, typename T, typename U>
281
DLFifoListImpl<P,T,U>::remove()
283
head.firstItem = RNIL;
284
head.lastItem = RNIL;
287
template <typename P, typename T, typename U>
290
DLFifoListImpl<P,T,U>::remove(Ptr<T> & p)
295
template <typename P, typename T, typename U>
298
DLFifoListImpl<P,T,U>::remove(T * t)
300
Uint32 ni = t->U::nextList;
301
Uint32 pi = t->U::prevList;
305
T * t = thePool.getPtr(ni);
310
// We are releasing last
316
T * t = thePool.getPtr(pi);
321
// We are releasing first
326
template <typename P, typename T, typename U>
329
DLFifoListImpl<P,T,U>::release()
332
Uint32 curr = head.firstItem;
335
thePool.getPtr(ptr, curr);
336
curr = ptr.p->U::nextList;
337
thePool.release(ptr);
339
head.firstItem = RNIL;
340
head.lastItem = RNIL;
343
template <typename P, typename T, typename U>
346
DLFifoListImpl<P,T,U>::release(Ptr<T> & p)
352
template <typename P, typename T, typename U>
355
DLFifoListImpl<P,T,U>::getPtr(Ptr<T> & p, Uint32 i) const
358
p.p = thePool.getPtr(i);
361
template <typename P, typename T, typename U>
364
DLFifoListImpl<P,T,U>::getPtr(Ptr<T> & p) const
369
template <typename P, typename T, typename U>
372
DLFifoListImpl<P,T,U>::getPtr(Uint32 i) const
374
return thePool.getPtr(i);
378
* Update ptr to first element in list
382
template <typename P, typename T, typename U>
385
DLFifoListImpl<P,T,U>::first(Ptr<T> & p) const
387
p.i = head.firstItem;
390
p.p = thePool.getPtr(p.i);
397
template <typename P, typename T, typename U>
400
DLFifoListImpl<P,T,U>::last(Ptr<T> & p) const
405
p.p = thePool.getPtr(p.i);
412
template <typename P, typename T, typename U>
415
DLFifoListImpl<P,T,U>::next(Ptr<T> & p) const
417
p.i = p.p->U::nextList;
420
p.p = thePool.getPtr(p.i);
427
template <typename P, typename T, typename U>
430
DLFifoListImpl<P,T,U>::prev(Ptr<T> & p) const
432
p.i = p.p->U::prevList;
435
p.p = thePool.getPtr(p.i);
442
template <typename P, typename T, typename U>
445
DLFifoListImpl<P,T,U>::hasNext(const Ptr<T> & p) const
447
return p.p->U::nextList != RNIL;
450
template <typename P, typename T, typename U>
453
DLFifoListImpl<P,T,U>::hasPrev(const Ptr<T> & p) const
455
return p.p->U::prevList != RNIL;
460
template <typename T, typename U = T>
461
class DLFifoList : public DLFifoListImpl<ArrayPool<T>, T, U>
464
DLFifoList(ArrayPool<T> & p) : DLFifoListImpl<ArrayPool<T>, T, U>(p) {}
467
template <typename T, typename U = T>
468
class LocalDLFifoList : public LocalDLFifoListImpl<ArrayPool<T>,T,U> {
470
LocalDLFifoList(ArrayPool<T> & p, typename DLFifoList<T,U>::Head & _src)
471
: LocalDLFifoListImpl<ArrayPool<T>,T,U>(p, _src) {}