mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / src / kernel / vm / Array.hpp
blob3c4995c1f9c324abb3904143264b87ddc067b22c
1 /* Copyright (c) 2003, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #ifndef ARRAY_HPP
17 #define ARRAY_HPP
19 #include "ArrayPool.hpp"
21 #include <pc.hpp>
22 #include <ErrorReporter.hpp>
24 /**
25 * Template class used for implementing an
26 * array of object retreived from a pool
28 template <class T>
29 class Array {
30 public:
31 Array(ArrayPool<T> & thePool);
33 /**
34 * Allocate an <b>n</b> objects from pool
35 * These can now be addressed with 0 <= ptr.i < n
37 bool seize(Uint32 i);
39 /**
40 * Release all object from array
42 void release();
44 /**
45 * Return current size of array
47 Uint32 getSize() const;
49 /**
50 * empty
52 inline bool empty() const { return sz == 0;}
54 /**
55 * Update i & p value according to <b>i</b>
57 void getPtr(Ptr<T> &, Uint32 i) const;
59 /**
60 * Update p value for ptr according to i value
62 void getPtr(Ptr<T> &) const ;
64 /**
65 * Get pointer for i value
67 T * getPtr(Uint32 i) const;
69 private:
70 Uint32 base, sz;
71 ArrayPool<T> & thePool;
72 };
74 template<class T>
75 inline
76 Array<T>::Array(ArrayPool<T> & _pool)
77 : thePool(_pool)
79 sz = 0;
80 base = RNIL;
83 template<class T>
84 inline
85 bool
86 Array<T>::seize(Uint32 n){
87 if(base == RNIL && n > 0){
88 base = thePool.seizeN(n);
89 if(base != RNIL){
90 sz = n;
91 return true;
93 return false;
95 ErrorReporter::handleAssert("Array<T>::seize failed", __FILE__, __LINE__);
96 return false;
99 template<class T>
100 inline
101 void
102 Array<T>::release(){
103 if(base != RNIL){
104 thePool.releaseN(base, sz);
105 sz = 0;
106 base = RNIL;
107 return;
111 template<class T>
112 inline
113 Uint32
114 Array<T>::getSize() const {
115 return sz;
118 template <class T>
119 inline
120 void
121 Array<T>::getPtr(Ptr<T> & p, Uint32 i) const {
122 p.i = i;
123 #ifdef ARRAY_GUARD
124 if(i < sz && base != RNIL){
125 p.p = thePool.getPtr(i + base);
126 return;
127 } else {
128 ErrorReporter::handleAssert("Array::getPtr failed", __FILE__, __LINE__);
130 #endif
131 p.p = thePool.getPtr(i + base);
134 template<class T>
135 inline
136 void
137 Array<T>::getPtr(Ptr<T> & ptr) const {
138 #ifdef ARRAY_GUARD
139 if(ptr.i < sz && base != RNIL){
140 ptr.p = thePool.getPtr(ptr.i + base);
141 return;
142 } else {
143 ErrorReporter::handleAssert("Array<T>::getPtr failed", __FILE__, __LINE__);
145 #endif
146 ptr.p = thePool.getPtr(ptr.i + base);
149 template<class T>
150 inline
151 T *
152 Array<T>::getPtr(Uint32 i) const {
153 #ifdef ARRAY_GUARD
154 if(i < sz && base != RNIL){
155 return thePool.getPtr(i + base);
156 } else {
157 ErrorReporter::handleAssert("Array<T>::getPtr failed", __FILE__, __LINE__);
159 #endif
160 return thePool.getPtr(i + base);
164 #endif