~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/kernel/vm/Pool.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef NDB_POOL_HPP
 
17
#define NDB_POOL_HPP
 
18
 
 
19
#include <ndb_global.h>
 
20
#include <kernel_types.h>
 
21
 
 
22
/**
 
23
 * Type bits
 
24
 *
 
25
 * Type id is 11 bits record type, and 5 bits resource id
 
26
 *   -> 2048 different kind of records and 32 different resource groups
 
27
 * 
 
28
 * Resource id is used to handle configuration parameters
 
29
 *
 
30
 * see blocks/records_types.hpp
 
31
 */
 
32
#define RG_BITS 5
 
33
#define RG_MASK ((1 << RG_BITS) - 1)
 
34
#define MAKE_TID(TID,RG) ((TID << RG_BITS) | RG)
 
35
 
 
36
/**
 
37
 * Page bits
 
38
 */
 
39
#define POOL_RECORD_BITS 13
 
40
#define POOL_RECORD_MASK ((1 << POOL_RECORD_BITS) - 1)
 
41
 
 
42
/**
 
43
 * Record_info
 
44
 *
 
45
 */
 
46
struct Record_info
 
47
{
 
48
  Uint16 m_size;
 
49
  Uint16 m_type_id;
 
50
  Uint16 m_offset_next_pool;
 
51
  Uint16 m_offset_magic;
 
52
};
 
53
 
 
54
/**
 
55
 * Resource_limit
 
56
 */
 
57
struct Resource_limit
 
58
{
 
59
  Uint32 m_min;
 
60
  Uint32 m_max;
 
61
  Uint32 m_curr;
 
62
  Uint32 m_resource_id;
 
63
};
 
64
 
 
65
struct Pool_context
 
66
{
 
67
  Pool_context() {}
 
68
  class SimulatedBlock* m_block;
 
69
 
 
70
  /**
 
71
   * Get mem root
 
72
   */
 
73
  void* get_memroot();
 
74
  
 
75
  /**
 
76
   * Alloc consekutive pages
 
77
   *
 
78
   *   @param i   : out : i value of first page
 
79
   *   @return    : pointer to first page (NULL if failed)
 
80
   *
 
81
   * Will handle resource limit 
 
82
   */
 
83
  void* alloc_page(Uint32 type_id, Uint32 *i);
 
84
  
 
85
  /**
 
86
   * Release pages
 
87
   * 
 
88
   *   @param i   : in : i value of first page
 
89
   *   @param p   : in : pointer to first page
 
90
   */
 
91
  void release_page(Uint32 type_id, Uint32 i);
 
92
  
 
93
  /**
 
94
   * Alloc consekutive pages
 
95
   *
 
96
   *   @param cnt : in/out : no of requested pages, 
 
97
   *                return no of allocated (undefined return NULL)
 
98
   *                out will never be > in
 
99
   *   @param i   : out : i value of first page
 
100
   *   @param min : in : will never allocate less than min
 
101
   *   @return    : pointer to first page (NULL if failed)
 
102
   *
 
103
   * Will handle resource limit 
 
104
   */
 
105
  void* alloc_pages(Uint32 type_id, Uint32 *i, Uint32 *cnt, Uint32 min =1);
 
106
  
 
107
  /**
 
108
   * Release pages
 
109
   * 
 
110
   *   @param i   : in : i value of first page
 
111
   *   @param p   : in : pointer to first page
 
112
   *   @param cnt : in : no of pages to release
 
113
   */
 
114
  void release_pages(Uint32 type_id, Uint32 i, Uint32 cnt);
 
115
 
 
116
  /**
 
117
   * Abort
 
118
   */
 
119
  void handleAbort(int code, const char* msg);
 
120
};
 
121
 
 
122
template <typename T>
 
123
struct Ptr 
 
124
{
 
125
  T * p;
 
126
  Uint32 i;
 
127
  inline bool isNull() const { return i == RNIL; }
 
128
  inline void setNull() { i = RNIL; }
 
129
};
 
130
 
 
131
template <typename T>
 
132
struct ConstPtr 
 
133
{
 
134
  const T * p;
 
135
  Uint32 i;
 
136
  inline bool isNull() const { return i == RNIL; }
 
137
  inline void setNull() { i = RNIL; }
 
138
};
 
139
 
 
140
#ifdef XX_DOCUMENTATION_XX
 
141
/**
 
142
 * Any pool should implement the following
 
143
 */
 
144
struct PoolImpl
 
145
{
 
146
  Pool_context m_ctx;
 
147
  Record_info m_record_info;
 
148
  
 
149
  void init(const Record_info& ri, const Pool_context& pc);
 
150
  void init(const Record_info& ri, const Pool_context& pc);
 
151
  
 
152
  bool seize(Ptr<void>&);
 
153
  void release(Ptr<void>);
 
154
  void * getPtr(Uint32 i);
 
155
};
 
156
#endif
 
157
 
 
158
template <typename T, typename P>
 
159
class RecordPool {
 
160
public:
 
161
  RecordPool();
 
162
  ~RecordPool();
 
163
  
 
164
  void init(Uint32 type_id, const Pool_context& pc);
 
165
  void wo_pool_init(Uint32 type_id, const Pool_context& pc);
 
166
  
 
167
  /**
 
168
   * Update p value for ptr according to i value 
 
169
   */
 
170
  void getPtr(Ptr<T> &);
 
171
  void getPtr(ConstPtr<T> &) const;
 
172
  
 
173
  /**
 
174
   * Get pointer for i value
 
175
   */
 
176
  T * getPtr(Uint32 i);
 
177
  const T * getConstPtr(Uint32 i) const;
 
178
 
 
179
  /**
 
180
   * Update p & i value for ptr according to <b>i</b> value 
 
181
   */
 
182
  void getPtr(Ptr<T> &, Uint32 i);
 
183
  void getPtr(ConstPtr<T> &, Uint32 i) const;
 
184
 
 
185
  /**
 
186
   * Allocate an object from pool - update Ptr
 
187
   *
 
188
   * Return i
 
189
   */
 
190
  bool seize(Ptr<T> &);
 
191
 
 
192
  /**
 
193
   * Return an object to pool
 
194
   */
 
195
  void release(Uint32 i);
 
196
 
 
197
  /**
 
198
   * Return an object to pool
 
199
   */
 
200
  void release(Ptr<T>);
 
201
private:
 
202
  P m_pool;
 
203
};
 
204
 
 
205
template <typename T, typename P>
 
206
inline
 
207
RecordPool<T, P>::RecordPool()
 
208
{
 
209
}
 
210
 
 
211
template <typename T, typename P>
 
212
inline
 
213
void
 
214
RecordPool<T, P>::init(Uint32 type_id, const Pool_context& pc)
 
215
{
 
216
  T tmp;
 
217
  const char * off_base = (char*)&tmp;
 
218
  const char * off_next = (char*)&tmp.nextPool;
 
219
  const char * off_magic = (char*)&tmp.m_magic;
 
220
 
 
221
  Record_info ri;
 
222
  ri.m_size = sizeof(T);
 
223
  ri.m_offset_next_pool = Uint32(off_next - off_base);
 
224
  ri.m_offset_magic = Uint32(off_magic - off_base);
 
225
  ri.m_type_id = type_id;
 
226
  m_pool.init(ri, pc);
 
227
}
 
228
 
 
229
template <typename T, typename P>
 
230
inline
 
231
void
 
232
RecordPool<T, P>::wo_pool_init(Uint32 type_id, const Pool_context& pc)
 
233
{
 
234
  T tmp;
 
235
  const char * off_base = (char*)&tmp;
 
236
  const char * off_magic = (char*)&tmp.m_magic;
 
237
  
 
238
  Record_info ri;
 
239
  ri.m_size = sizeof(T);
 
240
  ri.m_offset_next_pool = 0;
 
241
  ri.m_offset_magic = Uint32(off_magic - off_base);
 
242
  ri.m_type_id = type_id;
 
243
  m_pool.init(ri, pc);
 
244
}
 
245
 
 
246
template <typename T, typename P>
 
247
inline
 
248
RecordPool<T, P>::~RecordPool()
 
249
{
 
250
}
 
251
 
 
252
  
 
253
template <typename T, typename P>
 
254
inline
 
255
void
 
256
RecordPool<T, P>::getPtr(Ptr<T> & ptr)
 
257
{
 
258
  ptr.p = static_cast<T*>(m_pool.getPtr(ptr.i));
 
259
}
 
260
 
 
261
template <typename T, typename P>
 
262
inline
 
263
void
 
264
RecordPool<T, P>::getPtr(ConstPtr<T> & ptr) const 
 
265
{
 
266
  ptr.p = static_cast<const T*>(m_pool.getPtr(ptr.i));
 
267
}
 
268
 
 
269
template <typename T, typename P>
 
270
inline
 
271
void
 
272
RecordPool<T, P>::getPtr(Ptr<T> & ptr, Uint32 i)
 
273
{
 
274
  ptr.i = i;
 
275
  ptr.p = static_cast<T*>(m_pool.getPtr(ptr.i));  
 
276
}
 
277
 
 
278
template <typename T, typename P>
 
279
inline
 
280
void
 
281
RecordPool<T, P>::getPtr(ConstPtr<T> & ptr, Uint32 i) const 
 
282
{
 
283
  ptr.i = i;
 
284
  ptr.p = static_cast<const T*>(m_pool.getPtr(ptr.i));  
 
285
}
 
286
  
 
287
template <typename T, typename P>
 
288
inline
 
289
T * 
 
290
RecordPool<T, P>::getPtr(Uint32 i)
 
291
{
 
292
  return static_cast<T*>(m_pool.getPtr(i));  
 
293
}
 
294
 
 
295
template <typename T, typename P>
 
296
inline
 
297
const T * 
 
298
RecordPool<T, P>::getConstPtr(Uint32 i) const 
 
299
{
 
300
  return static_cast<const T*>(m_pool.getPtr(i)); 
 
301
}
 
302
  
 
303
template <typename T, typename P>
 
304
inline
 
305
bool
 
306
RecordPool<T, P>::seize(Ptr<T> & ptr)
 
307
{
 
308
  Ptr<void> tmp;
 
309
  bool ret = m_pool.seize(tmp);
 
310
  if(likely(ret))
 
311
  {
 
312
    ptr.i = tmp.i;
 
313
    ptr.p = static_cast<T*>(tmp.p);
 
314
  }
 
315
  return ret;
 
316
}
 
317
 
 
318
template <typename T, typename P>
 
319
inline
 
320
void
 
321
RecordPool<T, P>::release(Uint32 i)
 
322
{
 
323
  Ptr<void> ptr;
 
324
  ptr.i = i;
 
325
  ptr.p = m_pool.getPtr(i);
 
326
  m_pool.release(ptr);
 
327
}
 
328
 
 
329
template <typename T, typename P>
 
330
inline
 
331
void
 
332
RecordPool<T, P>::release(Ptr<T> ptr)
 
333
{
 
334
  Ptr<void> tmp;
 
335
  tmp.i = ptr.i;
 
336
  tmp.p = ptr.p;
 
337
  m_pool.release(tmp);
 
338
}
 
339
 
 
340
#endif