STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bid.h
Go to the documentation of this file.
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  * Copyright (C) 2013 Timo Bingmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_MNG_BID_HEADER
17 #define STXXL_MNG_BID_HEADER
18 
19 #include <cstring>
20 #include <ostream>
21 #include <iomanip>
22 
23 #include <stxxl/bits/io/file.h>
26 
27 #ifndef STXXL_VERBOSE_BLOCK_LIFE_CYCLE
28 #define STXXL_VERBOSE_BLOCK_LIFE_CYCLE STXXL_VERBOSE2
29 #endif
30 #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
31 
33 
34 //! \addtogroup mnglayer
35 //! \{
36 
37 //! Block identifier class.
38 //!
39 //! Stores block identity, given by file and offset within the file
40 template <unsigned Size>
41 struct BID
42 {
43  enum
44  {
45  size = Size, //!< Block size
46  t_size = Size //!< Blocks size, given by the parameter
47  };
48 
49  file* storage; //!< pointer to the file of the block
50  stxxl::int64 offset; //!< offset within the file of the block
51 
52  BID() : storage(NULL), offset(0)
53  { }
54 
55  bool valid() const
56  {
57  return storage != NULL;
58  }
59 
60  BID(file* s, stxxl::int64 o) : storage(s), offset(o)
61  { }
62 
63  BID(const BID& obj) : storage(obj.storage), offset(obj.offset)
64  { }
65 
66  template <unsigned BlockSize>
67  explicit BID(const BID<BlockSize>& obj)
68  : storage(obj.storage), offset(obj.offset)
69  { }
70 
71  template <unsigned BlockSize>
72  BID& operator = (const BID<BlockSize>& obj)
73  {
74  storage = obj.storage;
75  offset = obj.offset;
76  return *this;
77  }
78 
79  bool is_managed() const
80  {
81  return storage->get_allocator_id() != file::NO_ALLOCATOR;
82  }
83 };
84 
85 //! Specialization of block identifier class (BID) for variable size block size.
86 //!
87 //! Stores block identity, given by file, offset within the file, and size of the block
88 template <>
89 struct BID<0>
90 {
91  file* storage; //!< pointer to the file of the block
92  stxxl::int64 offset; //!< offset within the file of the block
93  unsigned size; //!< size of the block in bytes
94 
95  enum
96  {
97  t_size = 0 //!< Blocks size, given by the parameter
98  };
99 
100  BID() : storage(NULL), offset(0), size(0)
101  { }
102 
103  BID(file* f, stxxl::int64 o, unsigned s) : storage(f), offset(o), size(s)
104  { }
105 
106  bool valid() const
107  {
108  return (storage != NULL);
109  }
110 };
111 
112 template <unsigned BlockSize>
114 {
115  return (a.storage == b.storage) && (a.offset == b.offset) && (a.size == b.size);
116 }
117 
118 template <unsigned BlockSize>
120 {
121  return (a.storage != b.storage) || (a.offset != b.offset) || (a.size != b.size);
122 }
123 
124 template <unsigned BlockSize>
125 std::ostream& operator << (std::ostream& s, const BID<BlockSize>& bid)
126 {
127  // [0x12345678|0]0x00100000/0x00010000
128  // [file ptr|file id]offset/size
129 
130  std::ios state(NULL);
131  state.copyfmt(s);
132 
133  s << "[" << bid.storage << "|";
134  if (bid.storage)
135  s << bid.storage->get_allocator_id();
136  else
137  s << "?";
138  s << "]0x" << std::hex << std::setfill('0') << std::setw(8) << bid.offset << "/0x" << std::setw(8) << bid.size << std::dec;
139 
140  s.copyfmt(state);
141  return s;
142 }
143 
144 template <unsigned BlockSize>
145 class BIDArray : public simple_vector<BID<BlockSize> >
146 {
147 public:
149  : simple_vector<BID<BlockSize> >()
150  { }
151 
153  : simple_vector<BID<BlockSize> >(size)
154  { }
155 };
156 
157 //! \}
158 
160 
161 #endif // !STXXL_MNG_BID_HEADER
162 // vim: et:ts=4:sw=4
long long int int64
Definition: types.h:38
Block size.
Definition: bid.h:45
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:92
Defines interface of file.
Definition: file.h:56
bool operator!=(const uint_pair &b) const
inequality checking operator
Definition: uint_types.h:198
bool is_managed() const
Definition: bid.h:79
Block identifier class.
Definition: bid.h:41
bool valid() const
Definition: bid.h:106
BID(const BID &obj)
Definition: bid.h:63
BIDArray(unsigned_type size)
Definition: bid.h:152
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
BID(file *s, stxxl::int64 o)
Definition: bid.h:60
unsigned size
size of the block in bytes
Definition: bid.h:93
bool valid() const
Definition: bid.h:55
file * storage
pointer to the file of the block
Definition: bid.h:91
Simpler non-growing vector without initialization.
Definition: simple_vector.h:39
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:64
file * storage
pointer to the file of the block
Definition: bid.h:49
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:50
BID(file *f, stxxl::int64 o, unsigned s)
Definition: bid.h:103
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:192
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
BID()
Definition: bid.h:52
BID(const BID< BlockSize > &obj)
Definition: bid.h:67