23#ifndef MAPPED_REGION_H
24#define MAPPED_REGION_H
37#ifndef PROVSQL_INPROCESS_STORE
56std::size_t
openFile(
const char *filename,
bool read_only) {
58 fd_ = open(filename, O_CREAT | (read_only ? O_RDONLY : O_RDWR), 0600);
60 throw std::runtime_error(strerror(errno));
61 auto size = lseek(
fd_, 0, SEEK_END);
62 lseek(
fd_, 0, SEEK_SET);
63 return static_cast<std::size_t
>(size);
77#ifdef PROVSQL_INPROCESS_STORE
81 throw std::runtime_error(strerror(errno));
87#ifdef PROVSQL_INPROCESS_STORE
90 throw std::runtime_error(
"ProvSQL: out of memory mapping region");
93 throw std::runtime_error(strerror(errno));
94 if(
static_cast<std::size_t
>(r) <
length)
95 memset(
static_cast<char *
>(
base_) + r, 0,
length -
static_cast<std::size_t
>(r));
99 if(
base_ == MAP_FAILED)
100 throw std::runtime_error(strerror(errno));
107#ifdef PROVSQL_INPROCESS_STORE
109 void *p = realloc(
base_, new_length);
111 throw std::runtime_error(
"ProvSQL: out of memory growing region");
117 throw std::runtime_error(strerror(errno));
119 base_ = ::mmap(
nullptr, new_length, PROT_READ | (
read_only_ ? 0 : PROT_WRITE),
121 if(
base_ == MAP_FAILED)
122 throw std::runtime_error(strerror(errno));
131#ifdef PROVSQL_INPROCESS_STORE
133 throw std::runtime_error(strerror(errno));
142#ifdef PROVSQL_INPROCESS_STORE
void close()
Write back (if writable) and release the region and file.
std::size_t openFile(const char *filename, bool read_only)
Open (creating if absent) the backing file.
void map(std::size_t length)
Establish the initial region of length bytes over the file.
void remap(std::size_t new_length)
Grow the region to new_length, preserving existing content.
bool read_only_
Opened read-only (no write-back).
MappedRegion(const MappedRegion &)=delete
std::size_t length() const
std::size_t length_
Current region length in bytes.
int fd_
Backing file descriptor.
void * base_
Base of the mapped region / heap buffer.
void sync()
Flush the region to the backing file (no-op when read-only).
MappedRegion & operator=(const MappedRegion &)=delete
void resizeFile(std::size_t length)
Set the backing file's size.
Build-configuration switches shared across the C and C++ sources.