summaryrefslogtreecommitdiff
path: root/storage/ndb/src/kernel/blocks/ndbfs/Pool.hpp
blob: 96f0b8008d8bafbf5565ff43ffbf1c93658785b9 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* 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; version 2 of the License.

   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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */

#ifndef FOR_LIB_POOL_H
#define FOR_LIB_POOL_H

 
//===========================================================================
//
// .PUBLIC
//
//===========================================================================
 
////////////////////////////////////////////////////////////////
//
// enum { defInitSize = 256, defIncSize  = 64 }; 
// Description: type to store initial and incremental size in.
//
////////////////////////////////////////////////////////////////
//
// Pool(int anInitSize = defInitSize, int anIncSize = defIncSize);
// Description:
//   Constructor. Allocates anInitSize of objects <template argument>.
//   When the pool runs out of elements, anIncSize elements are added to the
//   pool. (When the pool is not optimized to allocate multiple elements
//   more efficient, the anIncSize MUST be set to 1 to get the best
//   performance...
//
// Parameters:
//   defInitSize: Initial size of the pool (# of elements in the pool)
//   defIncSize:  # of elements added to the pool when a request to an empty
//    pool is made.
// Return value:
//      _ 
// Errors:
//      -
// Asserts:
//   _
//
////////////////////////////////////////////////////////////////
//
// virtual ~Pool();
// Description:
//   Elements in the pool are all deallocated.
// Parameters:
//   _
// Return value:
//   _
// Errors:
//      -
// Asserts:
//   theEmptyNodeList==0. No elements are in still in use.
//
////////////////////////////////////////////////////////////////
//
// T& get();
// Description:
//   get's an element from the Pool.
// Parameters:
//   _
// Return value:
//   T& the element extracted from the Pool. (element must be cleared to
//   mimick newly created element)
// Errors:
//      -
// Asserts:
//   _
//
////////////////////////////////////////////////////////////////
//
// void put(T& aT);
// Description:
//   Returns an element to the pool.
// Parameters:
//   aT The element to put back in the pool
// Return value:
//   void 
// Errors:
//      -
// Asserts:
//   The pool has "empty" elements, to put element back in...
//
//===========================================================================
//
// .PRIVATE
//
//===========================================================================
 
////////////////////////////////////////////////////////////////
//
// void allocate(int aSize);
// Description:
//   add aSize elements to the pool
// Parameters:
//   aSize: # of elements to add to the pool
// Return value:
//   void 
// Errors:
//      -
// Asserts:
//   _
//
////////////////////////////////////////////////////////////////
//
// void deallocate();
// Description:
//   frees all elements kept in the pool.
// Parameters:
//   _
// Return value:
//   void 
// Errors:
//      -
// Asserts:
//   No elements are "empty" i.e. in use.
//
//===========================================================================
//
// .PRIVATE
//
//===========================================================================
 
////////////////////////////////////////////////////////////////
//
// Pool<T>& operator=(const Pool<T>& cp);
// Description:
//   Prohibit use of assignement operator.
// Parameters:
//   cp
// Return value:
//   Pool<T>& 
// Asserts:
//   _
//
////////////////////////////////////////////////////////////////
//
// Pool(const Pool<T>& cp);
// Description:
//   Prohibit use of default copy constructor.
// Parameters:
//   cp
// Return value:
//   _
// Errors:
//      -
// Asserts:
//   _
//
////////////////////////////////////////////////////////////////
//
// int initSize;
// Description: size of the initial size of the pool
//
////////////////////////////////////////////////////////////////
//
// int incSize;
// Description: # of elements added to the pool when pool is exhausted.
//
////////////////////////////////////////////////////////////////
//
// PoolElement<T>* theFullNodeList;
// Description: List to contain all "unused" elements in the pool
//
////////////////////////////////////////////////////////////////
//
// PoolElement<T>* theEmptyNodeList;
// Description: List to contain all "in use" elements in the pool
//
//-------------------------------------------------------------------------

template <class T>
class Pool
{
public:
  enum { defInitSize = 256, defIncSize  = 64 }; 
   
  Pool(int anInitSize = defInitSize, int anIncSize = defIncSize) :
    theIncSize(anIncSize),
    theTop(0),
    theCurrentSize(0),   
    theList(0)
  {
    allocate(anInitSize);
  }
  
  virtual ~Pool(void)
  {
    for (int i=0; i <theTop ; ++i)
      delete theList[i];
    
    delete []theList;
  }
  
  T* get();
  void put(T* aT);

  unsigned size(){ return theTop; };
  
protected:
  void allocate(int aSize)
  {
    T** tList = theList;
    int i;
    theList = new T*[aSize+theCurrentSize];
    // allocate full list
    for (i = 0; i < theTop; i++) {
      theList[i] = tList[i];
    }
    delete []tList;
    for (; (theTop < aSize); theTop++){
      theList[theTop] = (T*)new T;
    }
    theCurrentSize += aSize;
  }
  
private:
  Pool<T>& operator=(const Pool<T>& cp);
  Pool(const Pool<T>& cp);
  
  int theIncSize;
  int theTop;
  int theCurrentSize;
  
  T** theList;
};

//******************************************************************************
template <class T> inline T* Pool<T>::get()
{
   T* tmp;
   if( theTop == 0 )
   {
      allocate(theIncSize);
   }
   --theTop;
   tmp = theList[theTop];
   return tmp;
}

//
//******************************************************************************
template <class T> inline void Pool<T>::put(T* aT)
{
   theList[theTop]= aT;
   ++theTop;
}

#endif