ROSE  0.11.145.0
AddressSegment.h
1 // WARNING: Changes to this file must be contributed back to Sawyer or else they will
2 // be clobbered by the next update from Sawyer. The Sawyer repository is at
3 // https://github.com/matzke1/sawyer.
4 
5 
6 
7 
8 #ifndef Sawyer_AddressSegment_H
9 #define Sawyer_AddressSegment_H
10 
11 #include <Sawyer/Access.h>
12 #include <Sawyer/AllocatingBuffer.h>
13 #include <Sawyer/MappedBuffer.h>
14 #include <Sawyer/NullBuffer.h>
15 #include <Sawyer/StaticBuffer.h>
16 #include <Sawyer/Sawyer.h>
17 
18 #include <boost/cstdint.hpp>
19 #include <boost/serialization/access.hpp>
20 #include <boost/serialization/nvp.hpp>
21 #include <boost/serialization/string.hpp>
22 
23 namespace Sawyer {
24 namespace Container {
25 
36 template<class A = size_t, class T = boost::uint8_t>
38  typename Buffer<A, T>::Ptr buffer_; // reference counted buffer
39  A offset_; // initial offset into buffer
40  unsigned accessibility_; // uninterpreted bit mask
41  std::string name_; // for debugging
42 
43 public:
44  typedef A Address;
45  typedef T Value;
47 private:
48  friend class boost::serialization::access;
49 
50  // Users: You'll need to register the subclass once you know its type, such as
51  // BOOST_CLASS_REGISTER(Sawyer::Container::AddressSegment<size_t,uint8_t>);
52  // You'll also need to register the most-derived buffer type.
53  template<class S>
54  void serialize(S &s, const unsigned /*version*/) {
55  s & BOOST_SERIALIZATION_NVP(buffer_);
56  s & BOOST_SERIALIZATION_NVP(offset_);
57  s & BOOST_SERIALIZATION_NVP(accessibility_);
58  s & BOOST_SERIALIZATION_NVP(name_);
59  }
60 
61  //-------------------------------------------------------------------------------------------------------------------
62  // Constructors
63  //-------------------------------------------------------------------------------------------------------------------
64 public:
65 
70  AddressSegment(): offset_(0), accessibility_(0) {}
71 
77  : buffer_(other.buffer_), offset_(other.offset_), accessibility_(other.accessibility_), name_(other.name_) {}
78 
82  explicit AddressSegment(const typename Buffer<Address, Value>::Ptr &buffer, Address offset=0, unsigned accessBits=0,
83  const std::string &name="")
84  : buffer_(buffer), offset_(offset), accessibility_(accessBits), name_(name) {}
85 
88  buffer_ = other.buffer_;
89  offset_ = other.offset_;
90  accessibility_ = other.accessibility_;
91  name_ = other.name_;
92  return *this;
93  }
94 
95  //-------------------------------------------------------------------------------------------------------------------
96  // The following static methods are convenience wrappers around various buffer types.
97  //-------------------------------------------------------------------------------------------------------------------
98 public:
99 
105  static AddressSegment nullInstance(Address size, unsigned accessBits=0, const std::string &name="") {
106  return AddressSegment(NullBuffer<A, T>::instance(size), 0, accessBits, name);
107  }
108 
113  static AddressSegment anonymousInstance(Address size, unsigned accessBits=0, const std::string &name="") {
114  return AddressSegment(AllocatingBuffer<A, T>::instance(size), 0, accessBits, name);
115  }
116 
122  static AddressSegment staticInstance(Value *buffer, Address size, unsigned accessBits=0, const std::string &name="") {
123  return AddressSegment(StaticBuffer<A, T>::instance(buffer, size), 0, accessBits, name);
124  }
125  static AddressSegment staticInstance(const Value *buffer, Address size, unsigned accessBits=0, const std::string &name="") {
126  return AddressSegment(StaticBuffer<A, T>::instance(buffer, size), 0, accessBits|Access::IMMUTABLE, name);
127  }
131  static AddressSegment fileInstance(const std::string &fileName, unsigned accessBits=Access::READABLE,
132  const std::string &name="") {
133  boost::iostreams::mapped_file::mapmode mode = (accessBits & Access::PRIVATE ? boost::iostreams::mapped_file::priv
134  : (accessBits & Access::WRITABLE ? boost::iostreams::mapped_file::readwrite
135  : boost::iostreams::mapped_file::readonly));
136  return AddressSegment(MappedBuffer<A, T>::instance(fileName, mode), 0, accessBits, name);
137  }
138 
139  //-------------------------------------------------------------------------------------------------------------------
140  // Properties
141  //-------------------------------------------------------------------------------------------------------------------
142 public:
150  typename Buffer<A, T>::Ptr buffer() const { return buffer_; }
151  AddressSegment& buffer(const typename Buffer<A, T>::Ptr &b) { buffer_ = b; return *this; }
159  A offset() const { return offset_; }
160  AddressSegment& offset(A n) { offset_ = n; return *this; }
169  unsigned accessibility() const { return accessibility_; }
170  AddressSegment& accessibility(unsigned bits) { accessibility_ = bits; return *this; }
178  const std::string &name() const { return name_; }
179  AddressSegment& name(const std::string &s) { name_ = s; return *this; }
182  //-------------------------------------------------------------------------------------------------------------------
183  // Other methods
184  //-------------------------------------------------------------------------------------------------------------------
185 public:
190  bool isAccessible(unsigned requiredAccess, unsigned prohibitedAccess) const {
191  return requiredAccess==(accessibility_ & requiredAccess) && 0==(accessibility_ & prohibitedAccess);
192  }
193 
194 };
195 
196 } // namespace
197 } // namespace
198 
199 #endif
static AddressSegment staticInstance(const Value *buffer, Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to a static buffer.
AddressSegment(const typename Buffer< Address, Value >::Ptr &buffer, Address offset=0, unsigned accessBits=0, const std::string &name="")
Construct a segment with buffer.
T Value
Type of values stored by the underlying buffer.
Points to static data.
Definition: StaticBuffer.h:30
AddressSegment(const AddressSegment &other)
Copy constructor.
Buffer< A, T >::Ptr buffer() const
Property: buffer.
A homogeneous interval of an address space.
AddressSegment & accessibility(unsigned bits)
Property: access rights.
AddressSegment & buffer(const typename Buffer< A, T >::Ptr &b)
Property: buffer.
static AddressSegment staticInstance(Value *buffer, Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to a static buffer.
Memory mapped file.
Definition: MappedBuffer.h:43
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Name space for the entire library.
Definition: FeasiblePath.h:767
AddressSegment & name(const std::string &s)
Property: name.
Allocates memory as needed.
AddressSegment & offset(A n)
Property: buffer offset.
const std::string & name() const
Property: name.
unsigned accessibility() const
Property: access rights.
Buffer that has no data.
Definition: NullBuffer.h:26
A Address
Address types expected to be used by the underlying buffer.
static AddressSegment nullInstance(Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to no data.
static AddressSegment anonymousInstance(Address size, unsigned accessBits=0, const std::string &name="")
Create a segment with no backing store.
static AddressSegment fileInstance(const std::string &fileName, unsigned accessBits=Access::READABLE, const std::string &name="")
Map a file into an address space.
AddressSegment & operator=(const AddressSegment &other)
Assignment.
A offset() const
Property: buffer offset.
bool isAccessible(unsigned requiredAccess, unsigned prohibitedAccess) const
Determines whether this segment is accessible.
AddressSegment()
Default constructor.