STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
typed_block.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/mng/typed_block.h
3  *
4  * Constructs a typed_block object containing as many elements elements plus
5  * some metadata as fits into the given block size.
6  *
7  * Part of the STXXL. See http://stxxl.sourceforge.net
8  *
9  * Copyright (C) 2002-2004 Roman Dementiev <[email protected]>
10  * Copyright (C) 2008-2010 Andreas Beckmann <[email protected]>
11  * Copyright (C) 2013 Timo Bingmann <[email protected]>
12  *
13  * Distributed under the Boost Software License, Version 1.0.
14  * (See accompanying file LICENSE_1_0.txt or copy at
15  * http://www.boost.org/LICENSE_1_0.txt)
16  **************************************************************************/
17 
18 #ifndef STXXL_MNG_TYPED_BLOCK_HEADER
19 #define STXXL_MNG_TYPED_BLOCK_HEADER
20 
21 #include <stxxl/bits/config.h>
22 #include <stxxl/bits/io/request.h>
24 #include <stxxl/bits/mng/bid.h>
25 
26 #ifndef STXXL_VERBOSE_TYPED_BLOCK
27 #define STXXL_VERBOSE_TYPED_BLOCK STXXL_VERBOSE2
28 #endif
29 
30 
32 
33 //! \addtogroup mnglayer
34 //! \{
35 
36 //! Block Manager Internals \internal
37 namespace mng_local {
38 
39 //! \defgroup mnglayer_internals Internals
40 //! \ingroup mnglayer
41 //! Internals and support classes
42 //! \{
43 
44 template <unsigned Bytes>
46 {
47  typedef unsigned char byte_type;
48  byte_type filler_array[Bytes];
49 
50 public:
51  filler_struct() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] filler_struct is constructed"); }
52 };
53 
54 template <>
55 class filler_struct<0>
56 {
57  typedef unsigned char byte_type;
58 
59 public:
60  filler_struct() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] filler_struct<> is constructed"); }
61 };
62 
63 //! Contains data elements for \c stxxl::typed_block , not intended for direct use.
64 template <typename Type, unsigned Size>
66 {
67 public:
68  typedef Type type;
69  typedef Type value_type;
70  typedef Type& reference;
71  typedef const Type& const_reference;
72  typedef type* pointer;
73  typedef pointer iterator;
74  typedef const type* const_iterator;
75 
76  enum
77  {
78  size = Size //!< number of elements in the block
79  };
80 
81  //! Array of elements of type Type
82  value_type elem[size];
83 
84  element_block() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] element_block is constructed"); }
85 
86  //! An operator to access elements in the block
87  reference operator [] (size_t i)
88  {
89  return elem[i];
90  }
91 
92  //! Returns \c iterator pointing to the first element.
94  {
95  return elem;
96  }
97 
98  //! Returns \c const_iterator pointing to the first element.
100  {
101  return elem;
102  }
103 
104  //! Returns \c const_iterator pointing to the first element.
106  {
107  return begin();
108  }
109 
110  //! Returns \c iterator pointing to the end element.
112  {
113  return elem + size;
114  }
115 
116  //! Returns \c const_iterator pointing to the end element.
118  {
119  return elem + size;
120  }
121 
122  //! Returns \c const_iterator pointing to the end element.
124  {
125  return end();
126  }
127 };
128 
129 //! Contains BID references for \c stxxl::typed_block , not intended for direct use.
130 template <typename Type, unsigned Size, unsigned RawSize, unsigned NBids = 0>
131 class block_w_bids : public element_block<Type, Size>
132 {
133 public:
134  enum
135  {
136  raw_size = RawSize,
137  nbids = NBids
138  };
139 
141 
142  //! Array of BID references
143  bid_type ref[nbids];
144 
145  //! An operator to access bid references
146  bid_type& operator () (size_t i)
147  {
148  return ref[i];
149  }
150 
151  block_w_bids() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] block_w_bids is constructed"); }
152 };
153 
154 template <typename Type, unsigned Size, unsigned RawSize>
155 class block_w_bids<Type, Size, RawSize, 0>
156  : public element_block<Type, Size>
157 {
158 public:
159  enum
160  {
161  raw_size = RawSize,
162  nbids = 0
163  };
164 
166 
167  block_w_bids() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] block_w_bids<> is constructed"); }
168 };
169 
170 //! Contains per block information for \c stxxl::typed_block , not intended for direct use.
171 template <typename Type, unsigned RawSize, unsigned NBids, typename MetaInfoType = void>
173  public block_w_bids<Type, ((RawSize - sizeof(BID<RawSize>)* NBids - sizeof(MetaInfoType)) / sizeof(Type)), RawSize, NBids>
174 {
175 public:
176  //! Type of per block information element.
177  typedef MetaInfoType info_type;
178 
179  //! Per block information element.
181 
182  block_w_info() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] block_w_info is constructed"); }
183 };
184 
185 template <typename Type, unsigned RawSize, unsigned NBids>
186 class block_w_info<Type, RawSize, NBids, void>:
187  public block_w_bids<Type, ((RawSize - sizeof(BID<RawSize>)* NBids) / sizeof(Type)), RawSize, NBids>
188 {
189 public:
190  typedef void info_type;
191 
192  block_w_info() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] block_w_info<> is constructed"); }
193 };
194 
195 //! Contains per block filler for \c stxxl::typed_block , not intended for direct use.
196 template <typename BaseType, unsigned FillSize = 0>
197 class add_filler : public BaseType
198 {
199 private:
200  //! Per block filler element.
202 
203 public:
204  add_filler() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] add_filler is constructed"); }
205 };
206 
207 template <typename BaseType>
208 class add_filler<BaseType, 0>
209  : public BaseType
210 {
211 public:
212  add_filler() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] add_filler<> is constructed"); }
213 };
214 
215 //! Helper to compute the size of the filler , not intended for direct use.
216 template <typename Type, unsigned RawSize>
217 class expand_struct : public add_filler<Type, RawSize - sizeof(Type)>
218 { };
219 
220 //! \}
221 
222 } // namespace mng_local
223 
224 //! Block containing elements of fixed length.
225 //!
226 //! \tparam RawSize size of block in bytes
227 //! \tparam Type type of block's records
228 //! \tparam NRef number of block references (BIDs) that can be stored in the block (default is 0)
229 //! \tparam MetaInfoType type of per block information (default is no information - void)
230 //!
231 //! The data array of type Type is contained in the parent class \c stxxl::element_block, see related information there.
232 //! The BID array of references is contained in the parent class \c stxxl::block_w_bids, see related information there.
233 //! The "per block information" is contained in the parent class \c stxxl::block_w_info, see related information there.
234 //! \warning If \c RawSize > 2MB object(s) of this type can not be allocated on the stack (as a
235 //! function variable for example), because Linux POSIX library limits the stack size for the
236 //! main thread to (2MB - system page size)
237 template <unsigned RawSize, typename Type, unsigned NRef = 0, typename MetaInfoType = void>
238 class typed_block :
239  public mng_local::expand_struct<mng_local::block_w_info<Type, RawSize, NRef, MetaInfoType>, RawSize>
240 {
242 
243 public:
244  typedef Type value_type;
246  typedef const value_type& const_reference;
247  typedef value_type* pointer;
248  typedef pointer iterator;
249  typedef const value_type* const_pointer;
251 
253  {
254  raw_size = RawSize, //!< size of block in bytes
255  size = Base::size, //!< number of elements in block
256  has_only_data = (raw_size == (size * sizeof(value_type))) //!< no meta info, bids or (non-empty) fillers included in the block, allows value_type array addressing across block boundaries
257  };
258 
260 
262  {
263  STXXL_STATIC_ASSERT(sizeof(typed_block) == raw_size);
264  STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] typed_block is constructed");
265 #if 0
266  assert(((long)this) % BLOCK_ALIGN == 0);
267 #endif
268  }
269 
270 #if 0
271  typed_block(const typed_block& tb)
272  {
273  STXXL_STATIC_ASSERT(sizeof(typed_block) == raw_size);
274  STXXL_MSG("[" << (void*)this << "] typed_block is copy constructed from [" << (void*)&tb << "]");
275  STXXL_UNUSED(tb);
276  }
277 #endif
278 
279  /*! Writes block to the disk(s).
280  *! \param bid block identifier, points the file(disk) and position
281  *! \param on_cmpl completion handler
282  *! \return \c pointer_ptr object to track status I/O operation after the call
283  */
284  request_ptr write(const bid_type& bid,
286  {
287  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:write " << FMT_BID(bid));
288  return bid.storage->awrite(this, bid.offset, raw_size, on_cmpl);
289  }
290 
291  /*! Reads block from the disk(s).
292  *! \param bid block identifier, points the file(disk) and position
293  *! \param on_cmpl completion handler
294  *! \return \c pointer_ptr object to track status I/O operation after the call
295  */
296  request_ptr read(const bid_type& bid,
298  {
299  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:read " << FMT_BID(bid));
300  return bid.storage->aread(this, bid.offset, raw_size, on_cmpl);
301  }
302 
303  static void* operator new (size_t bytes)
304  {
305  unsigned_type meta_info_size = bytes % raw_size;
306  STXXL_VERBOSE1("typed::block operator new[]: bytes=" << bytes << ", meta_info_size=" << meta_info_size);
307 
308  void* result = aligned_alloc<BLOCK_ALIGN>(bytes - meta_info_size, meta_info_size);
309 #if STXXL_WITH_VALGRIND || STXXL_TYPED_BLOCK_INITIALIZE_ZERO
310  memset(result, 0, bytes);
311 #endif
312  return result;
313  }
314 
315  static void* operator new[] (size_t bytes)
316  {
317  unsigned_type meta_info_size = bytes % raw_size;
318  STXXL_VERBOSE1("typed::block operator new[]: bytes=" << bytes << ", meta_info_size=" << meta_info_size);
319 
320  void* result = aligned_alloc<BLOCK_ALIGN>(bytes - meta_info_size, meta_info_size);
321 #if STXXL_WITH_VALGRIND || STXXL_TYPED_BLOCK_INITIALIZE_ZERO
322  memset(result, 0, bytes);
323 #endif
324  return result;
325  }
326 
327  static void* operator new (size_t /*bytes*/, void* ptr) // construct object in existing memory
328  {
329  return ptr;
330  }
331 
332  static void operator delete (void* ptr)
333  {
334  aligned_dealloc<BLOCK_ALIGN>(ptr);
335  }
336 
337  static void operator delete[] (void* ptr)
338  {
339  aligned_dealloc<BLOCK_ALIGN>(ptr);
340  }
341 
342  static void operator delete (void*, void*)
343  { }
344 
345 #if 1
346  // STRANGE: implementing destructor makes g++ allocate
347  // additional 4 bytes in the beginning of every array
348  // of this type !? makes aligning to 4K boundaries difficult
349  //
350  // http://www.cc.gatech.edu/grads/j/Seung.Won.Jun/tips/pl/node4.html :
351  // "One interesting thing is the array allocator requires more memory
352  // than the array size multiplied by the size of an element, by a
353  // difference of delta for metadata a compiler needs. It happens to
354  // be 8 bytes long in g++."
356  {
357  STXXL_VERBOSE_TYPED_BLOCK("[" << (void*)this << "] typed_block is destructed");
358  }
359 #endif
360 };
361 
362 //! \}
363 
365 
366 #endif // !STXXL_MNG_TYPED_BLOCK_HEADER
367 // vim: et:ts=4:sw=4
Contains per block filler for stxxl::typed_block , not intended for direct use.
Definition: typed_block.h:197
Default completion handler class.
const_iterator begin() const
Returns const_iterator pointing to the first element.
Definition: typed_block.h:99
MetaInfoType info_type
Type of per block information element.
Definition: typed_block.h:177
static const int bytes
number of bytes in uint_pair
Definition: uint_types.h:99
Contains per block information for stxxl::typed_block , not intended for direct use.
Definition: typed_block.h:172
Completion handler class (Loki-style).
const value_type & const_reference
Definition: typed_block.h:246
virtual request_ptr awrite(void *buffer, offset_type pos, size_type bytes, const completion_handler &on_cmpl)=0
Schedules an asynchronous write request to the file.
info_type info
Per block information element.
Definition: typed_block.h:180
const_iterator end() const
Returns const_iterator pointing to the end element.
Definition: typed_block.h:117
#define STXXL_VERBOSE_BLOCK_LIFE_CYCLE
Definition: bid.h:28
const_iterator cend() const
Returns const_iterator pointing to the end element.
Definition: typed_block.h:123
virtual request_ptr aread(void *buffer, offset_type pos, size_type bytes, const completion_handler &on_cmpl)=0
Schedules an asynchronous read request to the file.
request_ptr read(const bid_type &bid, completion_handler on_cmpl=default_completion_handler())
Reads block from the disk(s).
Definition: typed_block.h:296
mng_local::expand_struct< mng_local::block_w_info< Type, RawSize, NRef, MetaInfoType >, RawSize > Base
Definition: typed_block.h:241
Block containing elements of fixed length.
Definition: typed_block.h:238
value_type * pointer
Definition: typed_block.h:247
#define BLOCK_ALIGN
Definition: request.h:34
file * storage
pointer to the file of the block
Definition: bid.h:50
const_pointer const_iterator
Definition: typed_block.h:250
value_type & reference
Definition: typed_block.h:245
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:23
#define STXXL_VERBOSE1(x)
Definition: verbose.h:99
iterator begin()
Returns iterator pointing to the first element.
Definition: typed_block.h:93
iterator end()
Returns iterator pointing to the end element.
Definition: typed_block.h:111
#define STXXL_STATIC_ASSERT(x)
Definition: utils.h:49
filler_struct< FillSize > filler
Per block filler element.
Definition: typed_block.h:201
Helper to compute the size of the filler , not intended for direct use.
Definition: typed_block.h:217
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
BID< raw_size > bid_type
Definition: typed_block.h:259
#define STXXL_VERBOSE_TYPED_BLOCK
Definition: typed_block.h:27
#define STXXL_MSG(x)
Definition: verbose.h:72
const value_type * const_pointer
Definition: typed_block.h:249
request_ptr write(const bid_type &bid, completion_handler on_cmpl=default_completion_handler())
Writes block to the disk(s).
Definition: typed_block.h:284
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:51
const_iterator cbegin() const
Returns const_iterator pointing to the first element.
Definition: typed_block.h:105
#define FMT_BID(_bid_)
Definition: bid.h:30
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
Contains BID references for stxxl::typed_block , not intended for direct use.
Definition: typed_block.h:131
Contains data elements for stxxl::typed_block , not intended for direct use.
Definition: typed_block.h:65