26 #ifndef __astro_system_memorymap_h
27 #define __astro_system_memorymap_h
30 #pragma warning "Using fake memory mapping"
33 #define munmap fakemunmap
34 #define msync fakemsync
69 rw = PROT_READ | PROT_WRITE,
80 static const int pagesize;
86 void open(
int fd,
size_t length_,
size_t offset,
int mode,
int mapstyle,
bool closefd =
false);
90 MemoryMap(
const std::string &filename,
size_t length = 0,
size_t offset = 0,
int mode = ro,
int map = shared);
92 void open(
const std::string &filename,
size_t length = 0,
size_t offset = 0,
int mode = ro,
int map = shared);
102 operator void *() {
return map; }
105 size_t size()
const {
return length; }
119 void open(
const std::string &filename,
size_t size = -1,
size_t offset = 0,
int mode = ro,
int mapstyle = shared)
122 if(size < 0) { siz = length/
sizeof(T); }
else { siz =
size; }
126 const T &
operator[](
int i)
const {
return ((
const T *)map)[i]; }
127 T &
operator[](
int i) {
return ((T *)map)[i]; }
130 iterator
begin() {
return (T *)map; }
132 iterator
end() {
return ((T *)map) + siz; }
136 T& back() {
return ((T *)map) + (siz-1); }
139 size_t size()
const {
return siz; }
140 size_t allocated() {
return length /
sizeof(T); }
145 MemoryMapError(
const std::string &msg): std::runtime_error(msg) {};
154 template<
typename Header>
165 : fh(NULL), m_data(NULL)
167 if(!filename.empty())
174 void open(
const std::string &filename,
const std::string &type,
int mode = ro,
bool validate =
true)
176 MemoryMap::open(filename.c_str(), 0, 0, mode, shared);
180 Header ref_ver(type);
181 if(
validate && !ref_ver.is_compatible(*fh))
183 std::cerr <<
"Expecting: " << ref_ver.type() <<
"\n";
184 std::cerr <<
"Got : " << fh->type() <<
"\n";
189 m_data = (
char *)&fh[1];
192 m_size = length -
sizeof(Header);
196 size_t size()
const {
return m_size; }
199 char *
data()
const {
return m_data; }
200 Header &hdr()
const {
return *fh; }
205 #define __peyton_system peyton::system