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 <dementiev@mpi-sb.mpg.de>
7  *  Copyright (C) 2009, 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
8  *  Copyright (C) 2009 Johannes Singler <singler@ira.uka.de>
9  *  Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
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>
24 #include <stxxl/bits/common/utils.h>
25 #include <stxxl/bits/common/simple_vector.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 STXXL_BEGIN_NAMESPACE
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 
BIDBID52     BID() : storage(NULL), offset(0)
53     { }
54 
validBID55     bool valid() const
56     {
57         return storage != NULL;
58     }
59 
BIDBID60     BID(file* s, stxxl::int64 o) : storage(s), offset(o)
61     { }
62 
BIDBID63     BID(const BID& obj) : storage(obj.storage), offset(obj.offset)
64     { }
65 
66     template <unsigned BlockSize>
BIDBID67     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 
is_managedBID79     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>
113 bool operator == (const BID<BlockSize>& a, const BID<BlockSize>& b)
114 {
115     return (a.storage == b.storage) && (a.offset == b.offset) && (a.size == b.size);
116 }
117 
118 template <unsigned BlockSize>
119 bool operator != (const BID<BlockSize>& a, const BID<BlockSize>& b)
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:
148     BIDArray()
149         : simple_vector<BID<BlockSize> >()
150     { }
151 
152     BIDArray(unsigned_type size)
153         : simple_vector<BID<BlockSize> >(size)
154     { }
155 };
156 
157 //! \}
158 
159 STXXL_END_NAMESPACE
160 
161 #endif // !STXXL_MNG_BID_HEADER
162 // vim: et:ts=4:sw=4
163