~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to storage/ndb/src/kernel/vm/SLFifoList.hpp

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
25
/**
 
26
 * Template class used for implementing an
 
27
 *   list of object retreived from a pool
 
28
 */
 
29
template <typename P, typename T, typename U = T>
 
30
class SLFifoListImpl 
 
31
{
 
32
public:
 
33
  /**
 
34
   * List head
 
35
   */
 
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
 
 
75
  /**
 
76
   *  Update i & p value according to <b>i</b>
 
77
   */
 
78
  void getPtr(Ptr<T> &, Uint32 i) const;
 
79
  
 
80
  /**
 
81
   * Update p value for ptr according to i value 
 
82
   */
 
83
  void getPtr(Ptr<T> &) const ;
 
84
  
 
85
  /**
 
86
   * Get pointer for i value
 
87
   */
 
88
  T * getPtr(Uint32 i) const ;
 
89
 
 
90
  /**
 
91
   * Update ptr to first element in list
 
92
   *
 
93
   * Return i
 
94
   */
 
95
  bool first(Ptr<T> &) const ;
 
96
 
 
97
  /**
 
98
   * Update ptr to first element in list
 
99
   *
 
100
   * Return i
 
101
   */
 
102
  bool last(Ptr<T> &) const ;
 
103
 
 
104
  /**
 
105
   * Get next element
 
106
   *
 
107
   * NOTE ptr must be both p & i
 
108
   */
 
109
  bool next(Ptr<T> &) const ;
 
110
  
 
111
  /**
 
112
   * Check if next exists i.e. this is not last
 
113
   *
 
114
   * NOTE ptr must be both p & i
 
115
   */
 
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
  
 
139
  ~LocalSLFifoListImpl(){
 
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
 
151
SLFifoListImpl<P,T,U>::SLFifoListImpl(P & _pool):
 
152
  thePool(_pool)
 
153
{
 
154
}
 
155
 
 
156
template <typename P, typename T, typename U>
 
157
inline
 
158
void
 
159
SLFifoListImpl<P,T,U>::HeadPOD::init()
 
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
 
171
SLFifoListImpl<P,T,U>::seizeFirst(Ptr<T> & p)
 
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
 
185
SLFifoListImpl<P,T,U>::seizeLast(Ptr<T> & p)
 
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
 
199
SLFifoListImpl<P,T,U>::addFirst(Ptr<T> & p)
 
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
 
213
SLFifoListImpl<P,T,U>::addLast(Ptr<T> & p)
 
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
 
235
SLFifoListImpl<P,T,U>::removeFirst(Ptr<T> & p)
 
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 
 
253
SLFifoListImpl<P,T,U>::releaseFirst(Ptr<T> & p)
 
254
{
 
255
  removeFirst(p);
 
256
  thePool.release(p);
 
257
}
 
258
 
 
259
template <typename P, typename T, typename U>
 
260
inline
 
261
void 
 
262
SLFifoListImpl<P,T,U>::getPtr(Ptr<T> & p, Uint32 i) const 
 
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 
 
271
SLFifoListImpl<P,T,U>::getPtr(Ptr<T> & p) const 
 
272
{
 
273
  thePool.getPtr(p);
 
274
}
 
275
  
 
276
template <typename P, typename T, typename U>
 
277
inline
 
278
T * 
 
279
SLFifoListImpl<P,T,U>::getPtr(Uint32 i) const 
 
280
{
 
281
  return thePool.getPtr(i);
 
282
}
 
283
 
 
284
/**
 
285
 * Update ptr to first element in list
 
286
 *
 
287
 * Return i
 
288
 */
 
289
template <typename P, typename T, typename U>
 
290
inline
 
291
bool
 
292
SLFifoListImpl<P,T,U>::first(Ptr<T> & p) const 
 
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
 
307
SLFifoListImpl<P,T,U>::last(Ptr<T> & p) const 
 
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
 
322
SLFifoListImpl<P,T,U>::next(Ptr<T> & p) const 
 
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
 
337
SLFifoListImpl<P,T,U>::hasNext(const Ptr<T> & p) const 
 
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:
 
348
  SLFifoList(ArrayPool<T> & p) : SLFifoListImpl<ArrayPool<T>, T, U>(p) {}
 
349
};
 
350
 
 
351
template <typename T, typename U = T>
 
352
class LocalSLFifoList : public LocalSLFifoListImpl<ArrayPool<T>,T,U> {
 
353
public:
 
354
  LocalSLFifoList(ArrayPool<T> & p, typename SLFifoList<T,U>::Head & _src)
 
355
    : LocalSLFifoListImpl<ArrayPool<T>,T,U>(p, _src) {}
 
356
};
 
357
 
 
358
#endif