STXXL  1.4.0
 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 
32 
34 
35 //! \addtogroup mnglayer
36 //! \{
37 
38 //! Block identifier class.
39 //!
40 //! Stores block identity, given by file and offset within the file
41 template <unsigned SIZE>
42 struct BID
43 {
44  enum
45  {
46  size = SIZE, //!< Block size
47  t_size = SIZE //!< Blocks size, given by the parameter
48  };
49 
50  file* storage; //!< pointer to the file of the block
51  stxxl::int64 offset; //!< offset within the file of the block
52 
53  BID() : storage(NULL), offset(0)
54  { }
55 
56  bool valid() const
57  {
58  return storage != NULL;
59  }
60 
61  BID(file* s, stxxl::int64 o) : storage(s), offset(o)
62  { }
63 
64  BID(const BID& obj) : storage(obj.storage), offset(obj.offset)
65  { }
66 
67  template <unsigned BlockSize>
68  explicit BID(const BID<BlockSize>& obj) : 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 
86 //! Specialization of block identifier class (BID) for variable size block size.
87 //!
88 //! Stores block identity, given by file, offset within the file, and size of the block
89 template <>
90 struct BID<0>
91 {
92  file* storage; //!< pointer to the file of the block
93  stxxl::int64 offset; //!< offset within the file of the block
94  unsigned size; //!< size of the block in bytes
95 
96  enum
97  {
98  t_size = 0 //!< Blocks size, given by the parameter
99  };
100 
101  BID() : storage(NULL), offset(0), size(0)
102  { }
103 
104  BID(file* f, stxxl::int64 o, unsigned s) : storage(f), offset(o), size(s)
105  { }
106 
107  bool valid() const
108  {
109  return (storage != NULL);
110  }
111 };
112 
113 template <unsigned blk_sz>
114 bool operator == (const BID<blk_sz>& a, const BID<blk_sz>& b)
115 {
116  return (a.storage == b.storage) && (a.offset == b.offset) && (a.size == b.size);
117 }
118 
119 template <unsigned blk_sz>
120 bool operator != (const BID<blk_sz>& a, const BID<blk_sz>& b)
121 {
122  return (a.storage != b.storage) || (a.offset != b.offset) || (a.size != b.size);
123 }
124 
125 template <unsigned blk_sz>
126 std::ostream& operator << (std::ostream& s, const BID<blk_sz>& bid)
127 {
128  // [0x12345678|0]0x00100000/0x00010000
129  // [file ptr|file id]offset/size
130 
131  std::ios state(NULL);
132  state.copyfmt(s);
133 
134  s << "[" << bid.storage << "|";
135  if (bid.storage)
136  s << bid.storage->get_allocator_id();
137  else
138  s << "?";
139  s << "]0x" << std::hex << std::setfill('0') << std::setw(8) << bid.offset << "/0x" << std::setw(8) << bid.size << std::dec;
140 
141  s.copyfmt(state);
142  return s;
143 }
144 
145 template <unsigned BLK_SIZE>
146 class BIDArray : public simple_vector<BID<BLK_SIZE> >
147 {
148 public:
150  : simple_vector<BID<BLK_SIZE> >()
151  { }
152 
154  : simple_vector<BID<BLK_SIZE> >(size)
155  { }
156 };
157 
158 //! \}
159 
161 
162 #endif // !STXXL_MNG_BID_HEADER
163 // vim: et:ts=4:sw=4
BID(const BID &obj)
Definition: bid.h:64
long long int int64
Definition: types.h:40
bool valid() const
Definition: bid.h:56
BID()
Definition: bid.h:53
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:93
Block size.
Definition: bid.h:46
BID(file *s, stxxl::int64 o)
Definition: bid.h:61
bool is_managed() const
Definition: bid.h:79
BIDArray(unsigned_type size)
Definition: bid.h:153
Defines interface of file.
Definition: file.h:52
bool operator!=(const uint_pair &b) const
inequality checking operator
Definition: uint_types.h:201
Block identifier class.
Definition: bid.h:42
file * storage
pointer to the file of the block
Definition: bid.h:50
bool valid() const
Definition: bid.h:107
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
unsigned size
size of the block in bytes
Definition: bid.h:94
file * storage
pointer to the file of the block
Definition: bid.h:92
Simpler non-growing vector without initialization.
Definition: simple_vector.h:37
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:51
BID(const BID< BlockSize > &obj)
Definition: bid.h:68
BID(file *f, stxxl::int64 o, unsigned s)
Definition: bid.h:104
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:195
#define STXXL_END_NAMESPACE
Definition: namespace.h:17