01:
02:
03: #ifndef _MMap_HH
04: #define _MMap_HH 1
05:
06: #include <unistd.h>
07: #include <sys/stat.h>
08: #include <sys/mman.h>
09:
10: #include "RZError.hh"
11:
12: namespace RZ {
13:
14:
15: inline char * fd_memory_map(
16: int const mapfd,
17: int const size,
18: bool const writeable,
19: std::string const &errormsg)
20: throw (MMapError)
21: {
22: char * const map = (char *)mmap(
23: (void *)0, (size_t)size,
24: writeable ? PROT_READ | PROT_WRITE : PROT_READ,
25: MAP_SHARED, mapfd, (off_t)0);
26: MMapError::what_if("Unable to map " + errormsg + "!", !map);
27: return map;
28: }
29:
30: inline char * anonymous_memory_map(
31: int const size,
32: bool const writeable,
33: std::string const &errormsg)
34: throw (MMapError)
35: {
36: char * const map = (char *)mmap(
37: (void *)0, (size_t)size,
38: writeable ? PROT_READ | PROT_WRITE : PROT_READ,
39: MAP_SHARED | MAP_ANONYMOUS, -1, (off_t)0);
40: MMapError::what_if("Unable to map " + errormsg + "!", !map);
41: return map;
42: }
43:
44:
45:
46: inline unsigned int const given_size(int const givenfd)
47: {
48: struct stat stbuf;
49: if (fstat(givenfd, &stbuf))
50: MMapError::what_if("Can't stat file", 1);
51: MMapError::what_if("Can't stat file size", !stbuf.st_size);
52: if (stbuf.st_size > (1 << 26))
53: throw MMapError("File too big for this naive implementation");
54: return stbuf.st_size;
55: }
56: }
57:
58:
59:
60:
61:
62: #endif