7 #include "common_priv.h"
10 #define MirrorBufferSet_SUPPORTED 1
14 #if defined(_POSIX_SHARED_MEMORY_OBJECTS)
15 #define MBS_USE_POSIX_SHM 1
16 #if !defined(__linux__)
17 #define _POSIX_C_SOURCE 199309
22 #define MBS_USE_POSIX_SHM 0
30 #if !MirrorBufferSet_SUPPORTED
42 #if MirrorBufferSet_SUPPORTED
46 #include <sys/types.h>
60 #define MAP_FAILED ((void*)-1)
65 #define MAP_NORESERVE (0)
70 std::vector<char> errstr(256,
'\0');
72 #if defined(__APPLE__)
74 int err = strerror_r(error, &errstr[0], errstr.size());
78 if (errno == ERANGE) {
79 errstr.resize(2*errstr.size(),
'\0');
85 char *str = strerror_r(error, &errstr[0], errstr.size());
99 if (str == (
char*)-1) {
100 if (errno == ERANGE) {
101 errstr.resize(2*errstr.size(),
'\0');
105 }
else if (str == 0) {
122 #ifndef MirrorBufferSet_VERBOSITY
123 #define MirrorBufferSet_VERBOSITY 0
126 #if MirrorBufferSet_VERBOSITY >= 1
127 #define fprintf1(p) fprintf p
132 #if MirrorBufferSet_VERBOSITY >= 2
133 #define fprintf2(p) fprintf p
143 bufferSize(bufferSz),
144 mirrorSize(mirrorSz),
161 fprintf(stderr,
"### Error: bufferSize = %lu is not a multiple of pageSize = %lu\n",
164 fprintf(stderr,
"### Error: mirrorSize = %lu is not a multiple of pageSize = %lu\n",
172 if ( ftruncate(fd, numBytes) ) {
175 throw std::runtime_error(
StrError(errno));
179 fprintf1((stderr,
"MirrorBufferSet: reserving %lu bytes for mapping ", numBytes));
180 caddr_t baseAddr = (caddr_t) mmap(0, numBytes, PROT_READ|PROT_WRITE,
186 throw std::runtime_error(
StrError(errno));
189 fprintf1((stderr,
"[%p - %p)\n", (
void*)baseAddr, (
void*)(baseAddr+numBytes)));
190 fprintf1((stderr,
"MirrorBufferSet: file \"%s\" is %d bytes\n",
199 fprintf2((stderr,
"mapping 0x%06lX bytes @ offset 0x%06lX ", (
long unsigned)theSize, (
long unsigned)theOffset));
200 caddr_t addr = (caddr_t) mmap(theAddr, theSize, PROT_READ|PROT_WRITE,
206 throw std::runtime_error(
StrError(errno));
208 fprintf2((stderr,
"to [%p - %p)\n", (
void*)theAddr, (
void*)(theAddr+theSize)));
210 fprintf(stderr,
"### Error: mapped to 0x%p instead of 0x%p\n",
213 throw std::runtime_error(
StrError(errno));
219 fprintf2((stderr,
"mapping 0x%06lX bytes @ offset 0x%06lX ", (
long unsigned)theSize, (
long unsigned)theOffset));
220 addr = (caddr_t) mmap(theAddr, theSize, PROT_READ|PROT_WRITE,
226 throw std::runtime_error(
StrError(errno));
228 fprintf2((stderr,
"to [%p - %p)\n", (
void*)theAddr, (
void*)(theAddr+theSize)));
230 fprintf(stderr,
"### Error: mapped to 0x%p instead of 0x%p\n", addr, theAddr );
232 throw std::runtime_error(
StrError(errno));
248 #if MBS_USE_POSIX_SHM
251 sprintf(
fileName,
"/MirrorBufferSet-%u", i);
252 #if defined(OS_IRIX) || defined(OS_HPUX)
258 int fd = shm_open(
fileName,O_RDWR|O_CREAT|O_EXCL,0600);
260 if (errno == EEXIST) {
263 fprintf(stderr,
"shm_open \"%s\": ",
fileName);
270 fprintf(stderr,
"### shm_unlink(\"%s\") failed: ",
fileName);
277 sprintf(
fileName,
"/tmp/MirrorBufferSet-XXXXXX");
281 fprintf(stderr,
"### unlink(\"%s\") failed: ",
fileName);
299 fprintf2((stderr,
"unmapped- \"%s\" [%p - %p), %lu bytes\n",
311 return getpagesize();
313 return sysconf(_SC_PAGESIZE);
std::string StrError(int error)
int GetFileDescriptor(void)
static int Supported(void)
static const char UNKNOWN_ERROR[]
static ulong PageSize(void)
MirrorBufferSet(ulong bufferSz, ulong mirrorSz, int nBuffers=1)