Stxxl  1.3.2
bid.h
1 /***************************************************************************
2  * include/stxxl/bits/mng/bid.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) 2009, 2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2009 Johannes Singler <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_BID_HEADER
16 #define STXXL_BID_HEADER
17 
18 #include <cstring>
19 #include <ostream>
20 #include <iomanip>
21 
22 #include <stxxl/bits/io/file.h>
23 #include <stxxl/bits/common/utils.h>
24 
25 #ifndef STXXL_VERBOSE_BLOCK_LIFE_CYCLE
26 #define STXXL_VERBOSE_BLOCK_LIFE_CYCLE STXXL_VERBOSE2
27 #endif
28 #define FMT_BID(_bid_) "[" << (_bid_).storage->get_allocator_id() << "]0x" << std::hex << std::setfill('0') << std::setw(8) << (_bid_).offset << "/0x" << std::setw(8) << (_bid_).size
29 
30 
31 __STXXL_BEGIN_NAMESPACE
32 
35 
37 
39 template <unsigned SIZE>
40 struct BID
41 {
42  enum
43  {
44  size = SIZE,
45  t_size = SIZE
46  };
47 
49  stxxl::int64 offset;
50 
51  BID() : storage(NULL), offset(0)
52  { }
53 
54  bool valid() const
55  {
56  return storage != NULL;
57  }
58 
59  BID(file * s, stxxl::int64 o) : storage(s), offset(o)
60  { }
61 
62  BID(const BID & obj) : storage(obj.storage), offset(obj.offset)
63  { }
64 
65  template <unsigned BlockSize>
66  explicit BID(const BID<BlockSize> & obj) : storage(obj.storage), offset(obj.offset)
67  { }
68 
69  template <unsigned BlockSize>
70  BID & operator = (const BID<BlockSize> & obj)
71  {
72  storage = obj.storage;
73  offset = obj.offset;
74  return *this;
75  }
76 
77  bool is_managed() const
78  {
79  return storage->get_allocator_id() != file::NO_ALLOCATOR;
80  }
81 };
82 
83 
85 
87 template <>
88 struct BID<0>
89 {
91  stxxl::int64 offset;
92  unsigned size;
93 
94  enum
95  {
96  t_size = 0
97  };
98 
99  BID() : storage(NULL), offset(0), size(0)
100  { }
101 
102  BID(file * f, stxxl::int64 o, unsigned s) : storage(f), offset(o), size(s)
103  { }
104 
105  bool valid() const
106  {
107  return storage;
108  }
109 };
110 
111 template <unsigned blk_sz>
112 bool operator == (const BID<blk_sz> & a, const BID<blk_sz> & b)
113 {
114  return (a.storage == b.storage) && (a.offset == b.offset) && (a.size == b.size);
115 }
116 
117 template <unsigned blk_sz>
118 bool operator != (const BID<blk_sz> & a, const BID<blk_sz> & b)
119 {
120  return (a.storage != b.storage) || (a.offset != b.offset) || (a.size != b.size);
121 }
122 
123 template <unsigned blk_sz>
124 std::ostream & operator << (std::ostream & s, const BID<blk_sz> & bid)
125 {
126  // [0x12345678|0]0x00100000/0x00010000
127  // [file ptr|file id]offset/size
128 
129  s << "[" << bid.storage << "|";
130  if (bid.storage)
131  s << bid.storage->get_allocator_id();
132  else
133  s << "?";
134  s << "]0x" << std::hex << std::setfill('0') << std::setw(8) << bid.offset << "/0x" << std::setw(8) << bid.size << std::dec;
135  return s;
136 }
137 
138 
139 #if 0
140 template <unsigned BLK_SIZE>
141 class BIDArray : public std::vector<BID<BLK_SIZE> >
142 {
143 public:
144  BIDArray(std::vector<BID<BLK_SIZE> >::size_type size = 0) : std::vector<BID<BLK_SIZE> >(size) { }
145 };
146 #endif
147 
148 template <unsigned BLK_SIZE>
149 class BIDArray : private noncopyable
150 {
151 protected:
152  unsigned_type _size;
153  BID<BLK_SIZE> * array;
154 
155 public:
156  typedef BID<BLK_SIZE> & reference;
157  typedef BID<BLK_SIZE> * iterator;
158  typedef const BID<BLK_SIZE> * const_iterator;
159 
160  BIDArray() : _size(0), array(NULL)
161  { }
162 
163  iterator begin()
164  {
165  return array;
166  }
167 
168  iterator end()
169  {
170  return array + _size;
171  }
172 
173  BIDArray(unsigned_type size) : _size(size)
174  {
175  array = new BID<BLK_SIZE>[size];
176  }
177 
178  unsigned_type size() const
179  {
180  return _size;
181  }
182 
183  reference operator [] (int_type i)
184  {
185  return array[i];
186  }
187 
188  void resize(unsigned_type newsize)
189  {
190  if (array)
191  {
192  STXXL_MSG("Warning: resizing nonempty BIDArray");
193  BID<BLK_SIZE> * tmp = array;
194  array = new BID<BLK_SIZE>[newsize];
195  memcpy((void *)array, (void *)tmp,
196  sizeof(BID<BLK_SIZE>) * (STXXL_MIN(_size, newsize)));
197  delete[] tmp;
198  _size = newsize;
199  }
200  else
201  {
202  array = new BID<BLK_SIZE>[newsize];
203  _size = newsize;
204  }
205  }
206 
207  ~BIDArray()
208  {
209  if (array)
210  delete[] array;
211  }
212 };
213 
215 
216 __STXXL_END_NAMESPACE
217 
218 #endif // !STXXL_BID_HEADER
219 // vim: et:ts=4:sw=4
Defines interface of file.
Definition: file.h:90
Block size.
Definition: bid.h:44
unsigned size
size of the block in bytes
Definition: bid.h:92
Blocks size, given by the parameter.
Definition: bid.h:45
file * storage
pointer to the file of the block
Definition: bid.h:48
virtual int get_allocator_id() const =0
Returns the file&#39;s allocator.
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:91
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:49
External vector container.
Definition: vector.h:259
file * storage
pointer to the file of the block
Definition: bid.h:90
Block identifier class.
Definition: bid.h:40