void SharedFile::createFile() { // create the file first int fd = scidb::File::openFile(getName().c_str(), (O_RDONLY|O_CREAT|O_EXCL)); if (fd < 0) { int err=errno; if (err==EEXIST) { throw AlreadyExistsException(err, REL_FILE, __FUNCTION__, __LINE__); } throw SystemErrorException(err, REL_FILE, __FUNCTION__, __LINE__); } if (scidb::File::closeFd(fd) != 0) { int err=errno; throw SystemErrorException(err, REL_FILE, __FUNCTION__, __LINE__); } }
void SharedFile::truncate(uint64_t size, bool force) { if (!_file) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__); } if (_region && !force) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__); } _region.reset(); if (::truncate(getName().c_str(), size) != 0) { int err = errno; throw SystemErrorException(err, REL_FILE, __FUNCTION__, __LINE__); } }
void SharedFile::create(AccessMode amode) { if (_file || _region) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__); } createFile(); try { _file.reset(new file_mapping(getName().c_str(), static_cast<boost::interprocess::mode_t>(amode))); } catch(boost::interprocess::interprocess_exception &ex) { std::cerr << "shared file::create error:" << ex.what() << std::endl; if (ex.get_error_code() != boost::interprocess::no_error) { throw SystemErrorException(ex.get_native_error(), REL_FILE, __FUNCTION__, __LINE__); } throw; } }
void SharedMemory::open(AccessMode amode) { if (_shm || _region) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__); } try { _shm.reset(new shared_memory_object(open_only, getName().c_str(), static_cast<boost::interprocess::mode_t>(amode))); } catch(boost::interprocess::interprocess_exception &ex) { std::cerr << "shm::open error:" << ex.what() << std::endl; if (ex.get_error_code() == boost::interprocess::not_found_error) { throw NotFoundException(ex.get_native_error(), REL_FILE, __FUNCTION__, __LINE__); } if (ex.get_error_code() != boost::interprocess::no_error) { throw SystemErrorException(ex.get_native_error(), REL_FILE, __FUNCTION__, __LINE__); } throw; } }
void SharedMemory::truncate(uint64_t size, bool force) { if (!_shm) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__); } if (_region && !force) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__); } try { _region.reset(); _shm->truncate(size); } catch(boost::interprocess::interprocess_exception &ex) { std::cerr << "shm::truncate error:" << ex.what() << std::endl; if (ex.get_error_code() != boost::interprocess::no_error) { throw SystemErrorException(ex.get_native_error(), REL_FILE, __FUNCTION__, __LINE__); } throw; } }
void* SharedMemory::get() { if (!_region) { if (!_shm) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__); } try { // map the whole shared memory in this process _region.reset(new mapped_region((*_shm), _shm->get_mode())); } catch(boost::interprocess::interprocess_exception &ex) { std::cerr << "shm::map error:" << ex.what() << std::endl; if (ex.get_error_code() != boost::interprocess::no_error) { throw SystemErrorException(ex.get_native_error(), REL_FILE, __FUNCTION__, __LINE__); } throw; } } assert(_region); return _region->get_address(); }
void* SharedFile::get() { if (!_region) { if (!_file) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__); } try { // map the whole shared memory in this process _region.reset(new mapped_region((*_file), _file->get_mode())); //XXXX TODO tigor: consider close() } catch(boost::interprocess::interprocess_exception &ex) { std::cerr << "shared file::map error:" << ex.what() << std::endl; if (ex.get_error_code() != boost::interprocess::no_error) { throw SystemErrorException(ex.get_native_error(), REL_FILE, __FUNCTION__, __LINE__); } throw; } preallocateShmMemory(); } assert(_region); return _region->get_address(); }