summaryrefslogtreecommitdiff
path: root/storage/ndb/src/kernel/vm/Array.hpp
blob: 97b0a345cb4e0758a4dc9947a0d04921e0341630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef ARRAY_HPP
#define ARRAY_HPP

#include "ArrayPool.hpp"

#include <pc.hpp>
#include <ErrorReporter.hpp>

/**
 * Template class used for implementing an
 *   array of object retreived from a pool
 */
template <class T>
class Array {
public:
  Array(ArrayPool<T> & thePool);

  /**
   * Allocate an <b>n</b> objects from pool
   *   These can now be addressed with 0 <= ptr.i < n
   */
  bool seize(Uint32 i);

  /**
   * Release all object from array
   */
  void release();

  /**
   * Return current size of array
   */
  Uint32 getSize() const;

  /**
   * empty
   */
  inline bool empty() const { return sz == 0;}

  /**
   *  Update i & p value according to <b>i</b>
   */
  void getPtr(Ptr<T> &, Uint32 i) const;

  /**
   * Update p value for ptr according to i value 
   */
  void getPtr(Ptr<T> &) const ;
  
  /**
   * Get pointer for i value
   */
  T * getPtr(Uint32 i) const;
  
private:
  Uint32 base, sz;
  ArrayPool<T> & thePool;
};  

template<class T>
inline
Array<T>::Array(ArrayPool<T> & _pool)
  :  thePool(_pool)
{
  sz = 0;
  base = RNIL;
}

template<class T>
inline
bool
Array<T>::seize(Uint32 n){
  if(base == RNIL && n > 0){
    base = thePool.seizeN(n);
    if(base != RNIL){
      sz = n;
      return true;
    }
    return false;
  }
  ErrorReporter::handleAssert("Array<T>::seize failed", __FILE__, __LINE__);
  return false;
}

template<class T>
inline
void 
Array<T>::release(){
  if(base != RNIL){
    thePool.releaseN(base, sz);
    sz = 0;
    base = RNIL;
    return;
  }
}

template<class T>
inline
Uint32 
Array<T>::getSize() const {
  return sz;
}

template <class T>
inline
void 
Array<T>::getPtr(Ptr<T> & p, Uint32 i) const {
  p.i = i;
#ifdef ARRAY_GUARD
  if(i < sz && base != RNIL){
    p.p = thePool.getPtr(i + base);
    return;
  } else {
  ErrorReporter::handleAssert("Array::getPtr failed", __FILE__, __LINE__);
  }
#endif
  p.p = thePool.getPtr(i + base);
}

template<class T>
inline
void
Array<T>::getPtr(Ptr<T> & ptr) const {
#ifdef ARRAY_GUARD
  if(ptr.i < sz && base != RNIL){
    ptr.p = thePool.getPtr(ptr.i + base);
    return;
  } else {
    ErrorReporter::handleAssert("Array<T>::getPtr failed", __FILE__, __LINE__);
  }
#endif
  ptr.p = thePool.getPtr(ptr.i + base);
}

template<class T>
inline
T * 
Array<T>::getPtr(Uint32 i) const {
#ifdef ARRAY_GUARD
  if(i < sz && base != RNIL){
    return thePool.getPtr(i + base);
  } else {
    ErrorReporter::handleAssert("Array<T>::getPtr failed", __FILE__, __LINE__);
  }
#endif
  return thePool.getPtr(i + base);
}


#endif