123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- #include <util/memory_segment_mapped.h>
- #include <util/unittests/check_valgrind.h>
- #include <exceptions/exceptions.h>
- #include <boost/scoped_ptr.hpp>
- #include <boost/interprocess/exceptions.hpp>
- #include <boost/interprocess/managed_mapped_file.hpp>
- #include <boost/interprocess/offset_ptr.hpp>
- #include <boost/interprocess/mapped_region.hpp>
- #include <boost/interprocess/sync/file_lock.hpp>
- #include <cassert>
- #include <string>
- #include <new>
- using boost::interprocess::basic_managed_mapped_file;
- using boost::interprocess::rbtree_best_fit;
- using boost::interprocess::null_mutex_family;
- using boost::interprocess::iset_index;
- using boost::interprocess::create_only_t;
- using boost::interprocess::create_only;
- using boost::interprocess::open_or_create_t;
- using boost::interprocess::open_or_create;
- using boost::interprocess::open_read_only;
- using boost::interprocess::open_only;
- using boost::interprocess::offset_ptr;
- namespace isc {
- namespace util {
- const size_t MemorySegmentMapped::INITIAL_SIZE;
- typedef basic_managed_mapped_file<char,
- rbtree_best_fit<null_mutex_family>,
- iset_index> BaseSegment;
- struct MemorySegmentMapped::Impl {
-
-
-
-
- Impl(const std::string& filename, create_only_t, size_t initial_size) :
- read_only_(false), filename_(filename)
- {
- try {
-
-
- base_sgmt_.reset(new BaseSegment(create_only, filename.c_str(),
- initial_size));
- } catch (const boost::interprocess::interprocess_exception& ex) {
-
-
-
-
- lock_.reset(new boost::interprocess::file_lock(filename.c_str()));
-
-
-
-
- checkWriter();
- lock_.reset();
-
-
-
-
- boost::interprocess::file_mapping::remove(filename.c_str());
- base_sgmt_.reset(new BaseSegment(create_only, filename.c_str(),
- initial_size));
- }
-
- lock_.reset(new boost::interprocess::file_lock(filename.c_str()));
- checkWriter();
- }
-
- Impl(const std::string& filename, open_or_create_t, size_t initial_size) :
- read_only_(false), filename_(filename),
- base_sgmt_(new BaseSegment(open_or_create, filename.c_str(),
- initial_size)),
- lock_(new boost::interprocess::file_lock(filename.c_str()))
- {
- checkWriter();
- }
-
- Impl(const std::string& filename, bool read_only) :
- read_only_(read_only), filename_(filename),
- base_sgmt_(read_only_ ?
- new BaseSegment(open_read_only, filename.c_str()) :
- new BaseSegment(open_only, filename.c_str())),
- lock_(new boost::interprocess::file_lock(filename.c_str()))
- {
- if (read_only_) {
- checkReader();
- } else {
- checkWriter();
- }
- }
-
- void growSegment() {
-
- const size_t prev_size = base_sgmt_->get_size();
- base_sgmt_.reset();
-
-
-
-
-
- const size_t new_size = prev_size * 2;
- assert(new_size > prev_size);
- if (!BaseSegment::grow(filename_.c_str(), new_size - prev_size)) {
- throw std::bad_alloc();
- }
- try {
-
-
-
- base_sgmt_.reset(new BaseSegment(open_only, filename_.c_str()));
- } catch (const boost::interprocess::interprocess_exception& ex) {
- throw std::bad_alloc();
- }
- }
-
- const bool read_only_;
-
- const std::string filename_;
-
- boost::scoped_ptr<BaseSegment> base_sgmt_;
- private:
-
-
-
-
- void checkReader() {
- if (!lock_->try_lock_sharable()) {
- isc_throw(MemorySegmentOpenError,
- "mapped memory segment can't be opened as read-only "
- "with a writer process");
- }
- }
- void checkWriter() {
- if (!lock_->try_lock()) {
- isc_throw(MemorySegmentOpenError,
- "mapped memory segment can't be opened as read-write "
- "with other reader or writer processes");
- }
- }
- boost::scoped_ptr<boost::interprocess::file_lock> lock_;
- };
- MemorySegmentMapped::MemorySegmentMapped(const std::string& filename) :
- impl_(NULL)
- {
- try {
- impl_ = new Impl(filename, true);
- } catch (const boost::interprocess::interprocess_exception& ex) {
- isc_throw(MemorySegmentOpenError,
- "failed to open mapped memory segment for " << filename
- << ": " << ex.what());
- }
- }
- MemorySegmentMapped::MemorySegmentMapped(const std::string& filename,
- OpenMode mode, size_t initial_size) :
- impl_(NULL)
- {
- try {
- switch (mode) {
- case OPEN_FOR_WRITE:
- impl_ = new Impl(filename, false);
- break;
- case OPEN_OR_CREATE:
- impl_ = new Impl(filename, open_or_create, initial_size);
- break;
- case CREATE_ONLY:
- impl_ = new Impl(filename, create_only, initial_size);
- break;
- default:
- isc_throw(InvalidParameter,
- "invalid open mode for MemorySegmentMapped: " << mode);
- }
- } catch (const boost::interprocess::interprocess_exception& ex) {
- isc_throw(MemorySegmentOpenError,
- "failed to open mapped memory segment for " << filename
- << ": " << ex.what());
- }
- }
- MemorySegmentMapped::~MemorySegmentMapped() {
- if (impl_->base_sgmt_ && !impl_->read_only_) {
- impl_->base_sgmt_->flush();
- }
- delete impl_;
- }
- void*
- MemorySegmentMapped::allocate(size_t size) {
- if (impl_->read_only_) {
- isc_throw(MemorySegmentError, "allocate attempt on read-only segment");
- }
-
-
-
- if (impl_->base_sgmt_->get_free_memory() >= size) {
- void* ptr = impl_->base_sgmt_->allocate(size, std::nothrow);
- if (ptr) {
- return (ptr);
- }
- }
-
-
- do {
- impl_->growSegment();
- } while (impl_->base_sgmt_->get_free_memory() < size);
- isc_throw(MemorySegmentGrown, "mapped memory segment grown, size: "
- << impl_->base_sgmt_->get_size() << ", free size: "
- << impl_->base_sgmt_->get_free_memory());
- }
- void
- MemorySegmentMapped::deallocate(void* ptr, size_t) {
- if (impl_->read_only_) {
- isc_throw(MemorySegmentError,
- "deallocate attempt on read-only segment");
- }
-
-
- if (!ptr) {
- return;
- }
- impl_->base_sgmt_->deallocate(ptr);
- }
- bool
- MemorySegmentMapped::allMemoryDeallocated() const {
- return (impl_->base_sgmt_->all_memory_deallocated());
- }
- MemorySegment::NamedAddressResult
- MemorySegmentMapped::getNamedAddressImpl(const char* name) const {
- offset_ptr<void>* storage =
- impl_->base_sgmt_->find<offset_ptr<void> >(name).first;
- if (storage) {
- return (NamedAddressResult(true, storage->get()));
- }
- return (NamedAddressResult(false, NULL));
- }
- bool
- MemorySegmentMapped::setNamedAddressImpl(const char* name, void* addr) {
- if (impl_->read_only_) {
- isc_throw(MemorySegmentError, "setNamedAddress on read-only segment");
- }
- if (addr && !impl_->base_sgmt_->belongs_to_segment(addr)) {
- isc_throw(MemorySegmentError, "address is out of segment: " << addr);
- }
- bool grown = false;
- while (true) {
- offset_ptr<void>* storage =
- impl_->base_sgmt_->find_or_construct<offset_ptr<void> >(
- name, std::nothrow)();
- if (storage) {
- *storage = addr;
- return (grown);
- }
- impl_->growSegment();
- grown = true;
- }
- }
- bool
- MemorySegmentMapped::clearNamedAddressImpl(const char* name) {
- if (impl_->read_only_) {
- isc_throw(MemorySegmentError,
- "clearNamedAddress on read-only segment");
- }
- return (impl_->base_sgmt_->destroy<offset_ptr<void> >(name));
- }
- void
- MemorySegmentMapped::shrinkToFit() {
- if (impl_->read_only_) {
- isc_throw(MemorySegmentError, "shrinkToFit on read-only segment");
- }
-
-
-
-
-
-
-
- if (getSize() < INITIAL_SIZE) {
- return;
- }
-
- impl_->base_sgmt_.reset();
- BaseSegment::shrink_to_fit(impl_->filename_.c_str());
- try {
-
-
-
- impl_->base_sgmt_.reset(
- new BaseSegment(open_only, impl_->filename_.c_str()));
- } catch (const boost::interprocess::interprocess_exception& ex) {
- isc_throw(MemorySegmentError,
- "remap after shrink failed; segment is now unusable");
- }
- }
- size_t
- MemorySegmentMapped::getSize() const {
- return (impl_->base_sgmt_->get_size());
- }
- size_t
- MemorySegmentMapped::getCheckSum() const {
- const size_t pagesize =
- boost::interprocess::mapped_region::get_page_size();
- const uint8_t* const cp_begin = static_cast<const uint8_t*>(
- impl_->base_sgmt_->get_address());
- const uint8_t* const cp_end = cp_begin + impl_->base_sgmt_->get_size();
- size_t sum = 0;
- for (const uint8_t* cp = cp_begin; cp < cp_end; cp += pagesize) {
- sum += *cp;
- }
- return (sum);
- }
- }
- }
|