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) 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 NDB_POOL_HPP
19 #define NDB_POOL_HPP
20 
21 #include <ndb_global.h>
22 #include <kernel_types.h>
23 
34 #define RG_BITS 5
35 #define RG_MASK ((1 << RG_BITS) - 1)
36 #define MAKE_TID(TID,RG) ((TID << RG_BITS) | RG)
37 
41 #define POOL_RECORD_BITS 13
42 #define POOL_RECORD_MASK ((1 << POOL_RECORD_BITS) - 1)
43 
49 {
50  Uint16 m_size;
51  Uint16 m_type_id;
52  Uint16 m_offset_next_pool;
53  Uint16 m_offset_magic;
54 };
55 
60 {
61  Uint32 m_min;
62  Uint32 m_max;
63  Uint32 m_curr;
64  Uint32 m_resource_id;
65 };
66 
68 {
69  Pool_context() {}
70  class SimulatedBlock* m_block;
71 
75  void* get_memroot();
76 
85  void* alloc_page(Uint32 type_id, Uint32 *i);
86 
93  void release_page(Uint32 type_id, Uint32 i);
94 
107  void* alloc_pages(Uint32 type_id, Uint32 *i, Uint32 *cnt, Uint32 min =1);
108 
116  void release_pages(Uint32 type_id, Uint32 i, Uint32 cnt);
117 
121  void handleAbort(int code, const char* msg) ATTRIBUTE_NORETURN;
122 };
123 
124 template <typename T>
125 struct Ptr
126 {
127  T * p;
128  Uint32 i;
129  inline bool isNull() const { return i == RNIL; }
130  inline void setNull() { i = RNIL; }
131 };
132 
133 template <typename T>
134 struct ConstPtr
135 {
136  const T * p;
137  Uint32 i;
138  inline bool isNull() const { return i == RNIL; }
139  inline void setNull() { i = RNIL; }
140 };
141 
142 #ifdef XX_DOCUMENTATION_XX
143 
146 struct PoolImpl
147 {
148  Pool_context m_ctx;
149  Record_info m_record_info;
150 
151  void init(const Record_info& ri, const Pool_context& pc);
152  void init(const Record_info& ri, const Pool_context& pc);
153 
154  bool seize(Ptr<void>&);
155  void release(Ptr<void>);
156  void * getPtr(Uint32 i);
157 };
158 #endif
159 
160 struct ArenaHead; // forward decl.
161 class ArenaAllocator; // forward decl.
162 
163 template <typename T, typename P>
164 class RecordPool {
165 public:
166  RecordPool();
167  ~RecordPool();
168 
169  void init(Uint32 type_id, const Pool_context& pc);
170  void wo_pool_init(Uint32 type_id, const Pool_context& pc);
171  void arena_pool_init(ArenaAllocator*, Uint32 type_id, const Pool_context& pc);
172 
176  void getPtr(Ptr<T> &);
177  void getPtr(ConstPtr<T> &) const;
178 
182  T * getPtr(Uint32 i);
183  const T * getConstPtr(Uint32 i) const;
184 
188  void getPtr(Ptr<T> &, Uint32 i);
189  void getPtr(ConstPtr<T> &, Uint32 i) const;
190 
196  bool seize(Ptr<T> &);
197 
201  bool seize(ArenaHead&, Ptr<T>&);
202 
206  void release(Uint32 i);
207 
211  void release(Ptr<T>);
212 private:
213  P m_pool;
214 };
215 
216 template <typename T, typename P>
217 inline
219 {
220 }
221 
222 template <typename T, typename P>
223 inline
224 void
225 RecordPool<T, P>::init(Uint32 type_id, const Pool_context& pc)
226 {
227  T tmp;
228  const char * off_base = (char*)&tmp;
229  const char * off_next = (char*)&tmp.nextPool;
230  const char * off_magic = (char*)&tmp.m_magic;
231 
232  Record_info ri;
233  ri.m_size = sizeof(T);
234  ri.m_offset_next_pool = Uint32(off_next - off_base);
235  ri.m_offset_magic = Uint32(off_magic - off_base);
236  ri.m_type_id = type_id;
237  m_pool.init(ri, pc);
238 }
239 
240 template <typename T, typename P>
241 inline
242 void
243 RecordPool<T, P>::wo_pool_init(Uint32 type_id, const Pool_context& pc)
244 {
245  T tmp;
246  const char * off_base = (char*)&tmp;
247  const char * off_magic = (char*)&tmp.m_magic;
248 
249  Record_info ri;
250  ri.m_size = sizeof(T);
251  ri.m_offset_next_pool = 0;
252  ri.m_offset_magic = Uint32(off_magic - off_base);
253  ri.m_type_id = type_id;
254  m_pool.init(ri, pc);
255 }
256 
257 template <typename T, typename P>
258 inline
259 void
261  Uint32 type_id, const Pool_context& pc)
262 {
263  T tmp;
264  const char * off_base = (char*)&tmp;
265  const char * off_next = (char*)&tmp.nextPool;
266  const char * off_magic = (char*)&tmp.m_magic;
267 
268  Record_info ri;
269  ri.m_size = sizeof(T);
270  ri.m_offset_next_pool = Uint32(off_next - off_base);
271  ri.m_offset_magic = Uint32(off_magic - off_base);
272  ri.m_type_id = type_id;
273  m_pool.init(alloc, ri, pc);
274 }
275 
276 
277 template <typename T, typename P>
278 inline
280 {
281 }
282 
283 
284 template <typename T, typename P>
285 inline
286 void
288 {
289  ptr.p = static_cast<T*>(m_pool.getPtr(ptr.i));
290 }
291 
292 template <typename T, typename P>
293 inline
294 void
296 {
297  ptr.p = static_cast<const T*>(m_pool.getPtr(ptr.i));
298 }
299 
300 template <typename T, typename P>
301 inline
302 void
304 {
305  ptr.i = i;
306  ptr.p = static_cast<T*>(m_pool.getPtr(ptr.i));
307 }
308 
309 template <typename T, typename P>
310 inline
311 void
312 RecordPool<T, P>::getPtr(ConstPtr<T> & ptr, Uint32 i) const
313 {
314  ptr.i = i;
315  ptr.p = static_cast<const T*>(m_pool.getPtr(ptr.i));
316 }
317 
318 template <typename T, typename P>
319 inline
320 T *
322 {
323  return static_cast<T*>(m_pool.getPtr(i));
324 }
325 
326 template <typename T, typename P>
327 inline
328 const T *
329 RecordPool<T, P>::getConstPtr(Uint32 i) const
330 {
331  return static_cast<const T*>(m_pool.getPtr(i));
332 }
333 
334 template <typename T, typename P>
335 inline
336 bool
338 {
339  Ptr<void> tmp;
340  bool ret = m_pool.seize(tmp);
341  if(likely(ret))
342  {
343  ptr.i = tmp.i;
344  ptr.p = static_cast<T*>(tmp.p);
345  }
346  return ret;
347 }
348 
349 template <typename T, typename P>
350 inline
351 bool
353 {
354  Ptr<void> tmp;
355  bool ret = m_pool.seize(ah, tmp);
356  if(likely(ret))
357  {
358  ptr.i = tmp.i;
359  ptr.p = static_cast<T*>(tmp.p);
360  }
361  return ret;
362 }
363 
364 template <typename T, typename P>
365 inline
366 void
368 {
369  Ptr<void> ptr;
370  ptr.i = i;
371  ptr.p = m_pool.getPtr(i);
372  m_pool.release(ptr);
373 }
374 
375 template <typename T, typename P>
376 inline
377 void
379 {
380  Ptr<void> tmp;
381  tmp.i = ptr.i;
382  tmp.p = ptr.p;
383  m_pool.release(tmp);
384 }
385 
386 #endif