ROSE  0.11.96.11
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 
86  //-------------------------------------------------------------------------------------------------------------------
87  // The following static methods are convenience wrappers around various buffer types.
88  //-------------------------------------------------------------------------------------------------------------------
89 public:
90 
96  static AddressSegment nullInstance(Address size, unsigned accessBits=0, const std::string &name="") {
97  return AddressSegment(NullBuffer<A, T>::instance(size), 0, accessBits, name);
98  }
99 
104  static AddressSegment anonymousInstance(Address size, unsigned accessBits=0, const std::string &name="") {
105  return AddressSegment(AllocatingBuffer<A, T>::instance(size), 0, accessBits, name);
106  }
107 
113  static AddressSegment staticInstance(Value *buffer, Address size, unsigned accessBits=0, const std::string &name="") {
114  return AddressSegment(StaticBuffer<A, T>::instance(buffer, size), 0, accessBits, name);
115  }
116  static AddressSegment staticInstance(const Value *buffer, Address size, unsigned accessBits=0, const std::string &name="") {
117  return AddressSegment(StaticBuffer<A, T>::instance(buffer, size), 0, accessBits|Access::IMMUTABLE, name);
118  }
122  static AddressSegment fileInstance(const std::string &fileName, unsigned accessBits=Access::READABLE,
123  const std::string &name="") {
124  boost::iostreams::mapped_file::mapmode mode = (accessBits & Access::PRIVATE ? boost::iostreams::mapped_file::priv
125  : (accessBits & Access::WRITABLE ? boost::iostreams::mapped_file::readwrite
126  : boost::iostreams::mapped_file::readonly));
127  return AddressSegment(MappedBuffer<A, T>::instance(fileName, mode), 0, accessBits, name);
128  }
129 
130  //-------------------------------------------------------------------------------------------------------------------
131  // Properties
132  //-------------------------------------------------------------------------------------------------------------------
133 public:
141  typename Buffer<A, T>::Ptr buffer() const { return buffer_; }
142  AddressSegment& buffer(const typename Buffer<A, T>::Ptr &b) { buffer_ = b; return *this; }
150  A offset() const { return offset_; }
151  AddressSegment& offset(A n) { offset_ = n; return *this; }
160  unsigned accessibility() const { return accessibility_; }
161  AddressSegment& accessibility(unsigned bits) { accessibility_ = bits; return *this; }
169  const std::string &name() const { return name_; }
170  AddressSegment& name(const std::string &s) { name_ = s; return *this; }
173  //-------------------------------------------------------------------------------------------------------------------
174  // Other methods
175  //-------------------------------------------------------------------------------------------------------------------
176 public:
181  bool isAccessible(unsigned requiredAccess, unsigned prohibitedAccess) const {
182  return requiredAccess==(accessibility_ & requiredAccess) && 0==(accessibility_ & prohibitedAccess);
183  }
184 
185 };
186 
187 } // namespace
188 } // namespace
189 
190 #endif
Sawyer::Container::AddressSegment
A homogeneous interval of an address space.
Definition: AddressSegment.h:37
Sawyer::Container::AllocatingBuffer
Allocates memory as needed.
Definition: AllocatingBuffer.h:30
Sawyer::Container::AddressSegment::Value
T Value
Type of values stored by the underlying buffer.
Definition: AddressSegment.h:45
Sawyer::Container::AddressSegment::Address
A Address
Address types expected to be used by the underlying buffer.
Definition: AddressSegment.h:44
Sawyer::Container::AddressSegment::staticInstance
static AddressSegment staticInstance(Value *buffer, Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to a static buffer.
Definition: AddressSegment.h:113
Sawyer::Container::AddressSegment::AddressSegment
AddressSegment(const typename Buffer< Address, Value >::Ptr &buffer, Address offset=0, unsigned accessBits=0, const std::string &name="")
Construct a segment with buffer.
Definition: AddressSegment.h:82
Sawyer::Container::AddressSegment::offset
A offset() const
Property: buffer offset.
Definition: AddressSegment.h:150
Sawyer::Container::AddressSegment::AddressSegment
AddressSegment(const AddressSegment &other)
Copy constructor.
Definition: AddressSegment.h:76
Sawyer::Container::AddressSegment::accessibility
AddressSegment & accessibility(unsigned bits)
Property: access rights.
Definition: AddressSegment.h:161
Sawyer::Container::AddressSegment::name
AddressSegment & name(const std::string &s)
Property: name.
Definition: AddressSegment.h:170
Sawyer::Container::AddressSegment::fileInstance
static AddressSegment fileInstance(const std::string &fileName, unsigned accessBits=Access::READABLE, const std::string &name="")
Map a file into an address space.
Definition: AddressSegment.h:122
Sawyer::Container::AddressSegment::staticInstance
static AddressSegment staticInstance(const Value *buffer, Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to a static buffer.
Definition: AddressSegment.h:116
Sawyer::Container::AddressSegment::isAccessible
bool isAccessible(unsigned requiredAccess, unsigned prohibitedAccess) const
Determines whether this segment is accessible.
Definition: AddressSegment.h:181
Sawyer::Container::MappedBuffer
Memory mapped file.
Definition: MappedBuffer.h:43
Sawyer::Container::AddressSegment::offset
AddressSegment & offset(A n)
Property: buffer offset.
Definition: AddressSegment.h:151
Sawyer::Container::AddressSegment::AddressSegment
AddressSegment()
Default constructor.
Definition: AddressSegment.h:70
Sawyer::Container::AddressSegment::nullInstance
static AddressSegment nullInstance(Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to no data.
Definition: AddressSegment.h:96
Sawyer::Container::StaticBuffer
Points to static data.
Definition: StaticBuffer.h:30
Sawyer::Container::AddressSegment::buffer
AddressSegment & buffer(const typename Buffer< A, T >::Ptr &b)
Property: buffer.
Definition: AddressSegment.h:142
Sawyer::Container::AddressSegment::anonymousInstance
static AddressSegment anonymousInstance(Address size, unsigned accessBits=0, const std::string &name="")
Create a segment with no backing store.
Definition: AddressSegment.h:104
Sawyer::SharedPointer
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Sawyer
Name space for the entire library.
Definition: Access.h:13
Sawyer::Container::AddressSegment::buffer
Buffer< A, T >::Ptr buffer() const
Property: buffer.
Definition: AddressSegment.h:141
Sawyer::Container::AddressSegment::accessibility
unsigned accessibility() const
Property: access rights.
Definition: AddressSegment.h:160
Sawyer::Container::AddressSegment::name
const std::string & name() const
Property: name.
Definition: AddressSegment.h:169
Sawyer::Container::NullBuffer
Buffer that has no data.
Definition: NullBuffer.h:26