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