MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Pool.hpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef FOR_LIB_POOL_H
20 #define FOR_LIB_POOL_H
21 
22 
23 //===========================================================================
24 //
25 // .PUBLIC
26 //
27 //===========================================================================
28 
30 //
31 // enum { defInitSize = 256, defIncSize = 64 };
32 // Description: type to store initial and incremental size in.
33 //
35 //
36 // Pool(int anInitSize = defInitSize, int anIncSize = defIncSize);
37 // Description:
38 // Constructor. Allocates anInitSize of objects <template argument>.
39 // When the pool runs out of elements, anIncSize elements are added to the
40 // pool. (When the pool is not optimized to allocate multiple elements
41 // more efficient, the anIncSize MUST be set to 1 to get the best
42 // performance...
43 //
44 // Parameters:
45 // defInitSize: Initial size of the pool (# of elements in the pool)
46 // defIncSize: # of elements added to the pool when a request to an empty
47 // pool is made.
48 // Return value:
49 // _
50 // Errors:
51 // -
52 // Asserts:
53 // _
54 //
56 //
57 // virtual ~Pool();
58 // Description:
59 // Elements in the pool are all deallocated.
60 // Parameters:
61 // _
62 // Return value:
63 // _
64 // Errors:
65 // -
66 // Asserts:
67 // theEmptyNodeList==0. No elements are in still in use.
68 //
70 //
71 // T& get();
72 // Description:
73 // get's an element from the Pool.
74 // Parameters:
75 // _
76 // Return value:
77 // T& the element extracted from the Pool. (element must be cleared to
78 // mimick newly created element)
79 // Errors:
80 // -
81 // Asserts:
82 // _
83 //
85 //
86 // void put(T& aT);
87 // Description:
88 // Returns an element to the pool.
89 // Parameters:
90 // aT The element to put back in the pool
91 // Return value:
92 // void
93 // Errors:
94 // -
95 // Asserts:
96 // The pool has "empty" elements, to put element back in...
97 //
98 //===========================================================================
99 //
100 // .PRIVATE
101 //
102 //===========================================================================
103 
105 //
106 // void allocate(int aSize);
107 // Description:
108 // add aSize elements to the pool
109 // Parameters:
110 // aSize: # of elements to add to the pool
111 // Return value:
112 // void
113 // Errors:
114 // -
115 // Asserts:
116 // _
117 //
119 //
120 // void deallocate();
121 // Description:
122 // frees all elements kept in the pool.
123 // Parameters:
124 // _
125 // Return value:
126 // void
127 // Errors:
128 // -
129 // Asserts:
130 // No elements are "empty" i.e. in use.
131 //
132 //===========================================================================
133 //
134 // .PRIVATE
135 //
136 //===========================================================================
137 
139 //
140 // Pool<T>& operator=(const Pool<T>& cp);
141 // Description:
142 // Prohibit use of assignement operator.
143 // Parameters:
144 // cp
145 // Return value:
146 // Pool<T>&
147 // Asserts:
148 // _
149 //
151 //
152 // Pool(const Pool<T>& cp);
153 // Description:
154 // Prohibit use of default copy constructor.
155 // Parameters:
156 // cp
157 // Return value:
158 // _
159 // Errors:
160 // -
161 // Asserts:
162 // _
163 //
165 //
166 // int initSize;
167 // Description: size of the initial size of the pool
168 //
170 //
171 // int incSize;
172 // Description: # of elements added to the pool when pool is exhausted.
173 //
175 //
176 // PoolElement<T>* theFullNodeList;
177 // Description: List to contain all "unused" elements in the pool
178 //
180 //
181 // PoolElement<T>* theEmptyNodeList;
182 // Description: List to contain all "in use" elements in the pool
183 //
184 //-------------------------------------------------------------------------
185 
186 template <class T>
187 class Pool
188 {
189 public:
190  enum { defInitSize = 256, defIncSize = 64 };
191 
192  Pool(int anInitSize = defInitSize, int anIncSize = defIncSize) :
193  theIncSize(anIncSize),
194  theTop(0),
195  theCurrentSize(0),
196  theList(0)
197  {
198  allocate(anInitSize);
199  }
200 
201  virtual ~Pool(void)
202  {
203  for (int i=0; i <theTop ; ++i)
204  delete theList[i];
205 
206  delete []theList;
207  }
208 
209  T* get();
210  void put(T* aT);
211 
212  unsigned size(){ return theTop; };
213 
214 protected:
215  void allocate(int aSize)
216  {
217  T** tList = theList;
218  int i;
219  theList = new T*[aSize+theCurrentSize];
220  // allocate full list
221  for (i = 0; i < theTop; i++) {
222  theList[i] = tList[i];
223  }
224  delete []tList;
225  for (; (theTop < aSize); theTop++){
226  theList[theTop] = (T*)new T;
227  }
228  theCurrentSize += aSize;
229  }
230 
231 private:
232  Pool<T>& operator=(const Pool<T>& cp);
233  Pool(const Pool<T>& cp);
234 
235  int theIncSize;
236  int theTop;
237  int theCurrentSize;
238 
239  T** theList;
240 };
241 
242 //******************************************************************************
243 template <class T> inline T* Pool<T>::get()
244 {
245  T* tmp;
246  if( theTop == 0 )
247  {
248  allocate(theIncSize);
249  }
250  --theTop;
251  tmp = theList[theTop];
252  tmp->atGet();
253  return tmp;
254 }
255 
256 //
257 //******************************************************************************
258 template <class T> inline void Pool<T>::put(T* aT)
259 {
260  theList[theTop]= aT;
261  ++theTop;
262 }
263 
264 #endif