Stxxl  1.3.2
typed_block.h
1 /***************************************************************************
2  * include/stxxl/bits/mng/typed_block.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2004 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008-2010 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_TYPED_BLOCK_HEADER
15 #define STXXL_TYPED_BLOCK_HEADER
16 
17 #include <stxxl/bits/io/request.h>
18 #include <stxxl/bits/common/aligned_alloc.h>
19 #include <stxxl/bits/mng/bid.h>
20 
21 #ifndef STXXL_VERBOSE_TYPED_BLOCK
22 #define STXXL_VERBOSE_TYPED_BLOCK STXXL_VERBOSE2
23 #endif
24 
25 
26 __STXXL_BEGIN_NAMESPACE
27 
30 
31 
32 template <unsigned bytes>
33 class filler_struct__
34 {
35  typedef unsigned char byte_type;
36  byte_type filler_array_[bytes];
37 
38 public:
39  filler_struct__() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] filler_struct__ is constructed"); }
40 };
41 
42 template <>
43 class filler_struct__<0>
44 {
45  typedef unsigned char byte_type;
46 
47 public:
48  filler_struct__() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] filler_struct__<> is constructed"); }
49 };
50 
52 template <class T, unsigned Size_>
54 {
55 public:
56  typedef T type;
57  typedef T value_type;
58  typedef T & reference;
59  typedef const T & const_reference;
60  typedef type * pointer;
61  typedef pointer iterator;
62  typedef const type * const_iterator;
63 
64  enum
65  {
66  size = Size_
67  };
68 
70  T elem[size];
71 
72  element_block() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] element_block is constructed"); }
73 
75  reference operator [] (int i)
76  {
77  return elem[i];
78  }
79 
81  iterator begin()
82  {
83  return elem;
84  }
85 
87  const_iterator begin() const
88  {
89  return elem;
90  }
91 
93  const_iterator cbegin() const
94  {
95  return begin();
96  }
97 
99  iterator end()
100  {
101  return elem + size;
102  }
103 
105  const_iterator end() const
106  {
107  return elem + size;
108  }
109 
111  const_iterator cend() const
112  {
113  return end();
114  }
115 };
116 
118 template <class T, unsigned Size_, unsigned RawSize_, unsigned NBids_ = 0>
119 class block_w_bids : public element_block<T, Size_>
120 {
121 public:
122  enum
123  {
124  raw_size = RawSize_,
125  nbids = NBids_
126  };
127 
128  typedef BID<raw_size> bid_type;
129 
131  bid_type ref[nbids];
132 
135  {
136  return ref[i];
137  }
138 
139  block_w_bids() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] block_w_bids is constructed"); }
140 };
141 
142 template <class T, unsigned Size_, unsigned RawSize_>
143 class block_w_bids<T, Size_, RawSize_, 0>: public element_block<T, Size_>
144 {
145 public:
146  enum
147  {
148  raw_size = RawSize_,
149  nbids = 0
150  };
151 
152  typedef BID<raw_size> bid_type;
153 
154  block_w_bids() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] block_w_bids<> is constructed"); }
155 };
156 
158 template <class T_, unsigned RawSize_, unsigned NBids_, class InfoType_ = void>
160  public block_w_bids<T_, ((RawSize_ - sizeof(BID<RawSize_>) * NBids_ - sizeof(InfoType_)) / sizeof(T_)), RawSize_, NBids_>
161 {
162 public:
164  typedef InfoType_ info_type;
165 
168 
169  block_w_info() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] block_w_info is constructed"); }
170 };
171 
172 template <class T_, unsigned RawSize_, unsigned NBids_>
173 class block_w_info<T_, RawSize_, NBids_, void>:
174  public block_w_bids<T_, ((RawSize_ - sizeof(BID<RawSize_>) * NBids_) / sizeof(T_)), RawSize_, NBids_>
175 {
176 public:
177  typedef void info_type;
178 
179  block_w_info() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] block_w_info<> is constructed"); }
180 };
181 
183 template <typename BaseType_, unsigned FillSize_ = 0>
184 class add_filler :
185  public BaseType_
186 {
187 private:
189  filler_struct__<FillSize_> filler;
190 
191 public:
192  add_filler() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] add_filler is constructed"); }
193 };
194 
195 template <typename BaseType_>
196 class add_filler<BaseType_, 0>:
197  public BaseType_
198 {
199 public:
200  add_filler() { STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] add_filler<> is constructed"); }
201 };
202 
204 template <typename Tp_, unsigned RawSize_>
206  public add_filler<Tp_, RawSize_ - sizeof(Tp_)>
207 { };
208 
210 
222 template <unsigned RawSize_, class T_, unsigned NRef_ = 0, class InfoType_ = void>
223 class typed_block :
224  public expand_struct<block_w_info<T_, RawSize_, NRef_, InfoType_>, RawSize_>
225 {
227 
228 public:
229  typedef T_ value_type;
230  typedef value_type & reference;
231  typedef const value_type & const_reference;
232  typedef value_type * pointer;
233  typedef pointer iterator;
234  typedef const value_type * const_pointer;
235  typedef const_pointer const_iterator;
236 
238  {
239  raw_size = RawSize_,
241  has_only_data = (raw_size == (size * sizeof(value_type)))
242  };
243 
244  typedef BID<raw_size> bid_type;
245 
246  typed_block()
247  {
248  STXXL_STATIC_ASSERT(sizeof(typed_block) == raw_size);
249  STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] typed_block is constructed");
250 #if 0
251  assert(((long)this) % BLOCK_ALIGN == 0);
252 #endif
253  }
254 
255 #if 0
256  typed_block(const typed_block & tb)
257  {
258  STXXL_STATIC_ASSERT(sizeof(typed_block) == raw_size);
259  STXXL_MSG("[" << (void *)this << "] typed_block is copy constructed from [" << (void *)&tb << "]");
260  STXXL_UNUSED(tb);
261  }
262 #endif
263 
269  request_ptr write(const bid_type & bid,
271  {
272  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:write " << FMT_BID(bid));
273  return bid.storage->awrite(this, bid.offset, raw_size, on_cmpl);
274  }
275 
281  request_ptr read(const bid_type & bid,
283  {
284  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:read " << FMT_BID(bid));
285  return bid.storage->aread(this, bid.offset, raw_size, on_cmpl);
286  }
287 
288  static void * operator new (size_t bytes)
289  {
290  unsigned_type meta_info_size = bytes % raw_size;
291  STXXL_VERBOSE1("typed::block operator new: Meta info size: " << meta_info_size);
292 
293  void * result = aligned_alloc<BLOCK_ALIGN>(bytes - meta_info_size, meta_info_size);
294  #ifdef STXXL_VALGRIND_TYPED_BLOCK_INITIALIZE_ZERO
295  memset(result, 0, bytes);
296  #endif
297  char * tmp = (char *)result;
298  tmp += RawSize_;
299  while (tmp < ((char *)result) + bytes)
300  {
301  tmp += RawSize_;
302  }
303  return result;
304  }
305 
306  static void * operator new[] (size_t bytes)
307  {
308  unsigned_type meta_info_size = bytes % raw_size;
309  STXXL_VERBOSE1("typed::block operator new[]: Meta info size: " << meta_info_size);
310 
311  void * result = aligned_alloc<BLOCK_ALIGN>(bytes - meta_info_size, meta_info_size);
312  #ifdef STXXL_VALGRIND_TYPED_BLOCK_INITIALIZE_ZERO
313  memset(result, 0, bytes);
314  #endif
315  char * tmp = (char *)result;
316  tmp += RawSize_;
317  while (tmp < ((char *)result) + bytes)
318  {
319  tmp += RawSize_;
320  }
321  return result;
322  }
323 
324  static void * operator new (size_t /*bytes*/, void * ptr) // construct object in existing memory
325  {
326  return ptr;
327  }
328 
329  static void operator delete (void * ptr)
330  {
331  aligned_dealloc<BLOCK_ALIGN>(ptr);
332  }
333 
334  static void operator delete[] (void * ptr)
335  {
336  aligned_dealloc<BLOCK_ALIGN>(ptr);
337  }
338 
339  static void operator delete (void *, void *)
340  { }
341 
342 #if 1
343  // STRANGE: implementing destructor makes g++ allocate
344  // additional 4 bytes in the beginning of every array
345  // of this type !? makes aligning to 4K boundaries difficult
346  //
347  // http://www.cc.gatech.edu/grads/j/Seung.Won.Jun/tips/pl/node4.html :
348  // "One interesting thing is the array allocator requires more memory
349  // than the array size multiplied by the size of an element, by a
350  // difference of delta for metadata a compiler needs. It happens to
351  // be 8 bytes long in g++."
352  ~typed_block()
353  {
354  STXXL_VERBOSE_TYPED_BLOCK("[" << (void *)this << "] typed_block is destructed");
355  }
356 #endif
357 };
358 
360 
361 __STXXL_END_NAMESPACE
362 
363 #endif // !STXXL_TYPED_BLOCK_HEADER
364 // vim: et:ts=4:sw=4
bid_type & operator()(int i)
An operator to access bid references.
Definition: typed_block.h:134
InfoType_ info_type
Type of per block information element.
Definition: typed_block.h:164
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.
Default completion handler class.
Definition: completion_handler.h:94
Block containing elements of fixed length.
Definition: typed_block.h:223
size of block in bytes
Definition: typed_block.h:239
T elem[size]
Array of elements of type T.
Definition: typed_block.h:70
number of elements in block
Definition: typed_block.h:240
no meta info, bids or (non-empty) fillers included in the block, allows value_type array addressing a...
Definition: typed_block.h:241
const_iterator cbegin() const
Returns const_iterator pointing to the first element.
Definition: typed_block.h:93
Completion handler class (Loki-style)
Definition: completion_handler.h:63
file * storage
pointer to the file of the block
Definition: bid.h:48
const_iterator begin() const
Returns const_iterator pointing to the first element.
Definition: typed_block.h:87
const_iterator end() const
Returns const_iterator pointing to the end element.
Definition: typed_block.h:105
Contains data elements for stxxl::typed_block , not intended for direct use.
Definition: typed_block.h:53
iterator begin()
Returns iterator pointing to the first element.
Definition: typed_block.h:81
request_ptr read(const bid_type &bid, completion_handler on_cmpl=default_completion_handler())
Reads block from the disk(s) !
Definition: typed_block.h:281
reference operator[](int i)
An operator to access elements in the block.
Definition: typed_block.h:75
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:49
Implemented as reference counting smart pointer.
Definition: request_ptr.h:34
request_ptr write(const bid_type &bid, completion_handler on_cmpl=default_completion_handler())
Writes block to the disk(s) !
Definition: typed_block.h:269
iterator end()
Returns iterator pointing to the end element.
Definition: typed_block.h:99
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.
bid_type ref[nbids]
Array of BID references.
Definition: typed_block.h:131
number of elements in the block
Definition: typed_block.h:66
info_type info
Per block information element.
Definition: typed_block.h:167
constants
Definition: typed_block.h:237
Helper to compute the size of the filler , not intended for direct use.
Definition: typed_block.h:205
Contains per block filler for stxxl::typed_block , not intended for direct use.
Definition: typed_block.h:184
Contains per block information for stxxl::typed_block , not intended for direct use.
Definition: typed_block.h:159
Contains BID references for stxxl::typed_block , not intended for direct use.
Definition: typed_block.h:119
const_iterator cend() const
Returns const_iterator pointing to the end element.
Definition: typed_block.h:111