Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_MAPPED_REGION_HPP Chris@16: #define BOOST_INTERPROCESS_MAPPED_REGION_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: Chris@16: //Some Unixes use caddr_t instead of void * in madvise Chris@16: // SunOS Tru64 HP-UX AIX Chris@16: #if defined(sun) || defined(__sun) || defined(__osf__) || defined(__osf) || defined(_hpux) || defined(hpux) || defined(_AIX) Chris@16: #define BOOST_INTERPROCESS_MADVISE_USES_CADDR_T Chris@16: #include Chris@16: #endif Chris@16: Chris@16: //A lot of UNIXes have destructive semantics for MADV_DONTNEED, so Chris@16: //we need to be careful to allow it. Chris@16: #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) Chris@16: #define BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS Chris@16: #endif Chris@16: Chris@16: #if defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: # include Chris@16: # include Chris@16: #else Chris@16: # ifdef BOOST_HAS_UNISTD_H Chris@16: # include Chris@16: # include //mmap Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS) Chris@16: # include //System V shared memory... Chris@16: # endif Chris@16: # include Chris@16: # else Chris@16: # error Unknown platform Chris@16: # endif Chris@16: Chris@101: #endif //#if defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: Chris@16: //!\file Chris@16: //!Describes mapped region class Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: //Solaris declares madvise only in some configurations but defines MADV_XXX, a bit confusing. Chris@16: //Predeclare it here to avoid any compilation error Chris@16: #if (defined(sun) || defined(__sun)) && defined(MADV_NORMAL) Chris@16: extern "C" int madvise(caddr_t, size_t, int); Chris@16: #endif Chris@16: Chris@16: namespace ipcdetail{ class interprocess_tester; } Chris@16: namespace ipcdetail{ class raw_mapped_region_creator; } Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //!The mapped_region class represents a portion or region created from a Chris@16: //!memory_mappable object. Chris@16: //! Chris@16: //!The OS can map a region bigger than the requested one, as region must Chris@16: //!be multiple of the page size, but mapped_region will always refer to Chris@16: //!the region specified by the user. Chris@16: class mapped_region Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(mapped_region) Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: Chris@16: //!Creates a mapping region of the mapped memory "mapping", starting in Chris@16: //!offset "offset", and the mapping's size will be "size". The mapping Chris@16: //!can be opened for read only, read-write or copy-on-write. Chris@16: //! Chris@16: //!If an address is specified, both the offset and the address must be Chris@16: //!multiples of the page size. Chris@16: //! Chris@16: //!The map is created using "default_map_options". This flag is OS Chris@16: //!dependant and it should not be changed unless the user needs to Chris@16: //!specify special options. Chris@16: //! Chris@16: //!In Windows systems "map_options" is a DWORD value passed as Chris@16: //!"dwDesiredAccess" to "MapViewOfFileEx". If "default_map_options" is passed Chris@16: //!it's initialized to zero. "map_options" is XORed with FILE_MAP_[COPY|READ|WRITE]. Chris@16: //! Chris@16: //!In UNIX systems and POSIX mappings "map_options" is an int value passed as "flags" Chris@16: //!to "mmap". If "default_map_options" is specified it's initialized to MAP_NOSYNC Chris@16: //!if that option exists and to zero otherwise. "map_options" XORed with MAP_PRIVATE or MAP_SHARED. Chris@16: //! Chris@16: //!In UNIX systems and XSI mappings "map_options" is an int value passed as "shmflg" Chris@16: //!to "shmat". If "default_map_options" is specified it's initialized to zero. Chris@16: //!"map_options" is XORed with SHM_RDONLY if needed. Chris@16: //! Chris@16: //!The OS could allocate more pages than size/page_size(), but get_address() Chris@16: //!will always return the address passed in this function (if not null) and Chris@16: //!get_size() will return the specified size. Chris@16: template Chris@16: mapped_region(const MemoryMappable& mapping Chris@16: ,mode_t mode Chris@16: ,offset_t offset = 0 Chris@16: ,std::size_t size = 0 Chris@16: ,const void *address = 0 Chris@16: ,map_options_t map_options = default_map_options); Chris@16: Chris@16: //!Default constructor. Address will be 0 (nullptr). Chris@16: //!Size will be 0. Chris@16: //!Does not throw Chris@16: mapped_region(); Chris@16: Chris@16: //!Move constructor. *this will be constructed taking ownership of "other"'s Chris@16: //!region and "other" will be left in default constructor state. Chris@16: mapped_region(BOOST_RV_REF(mapped_region) other) Chris@16: #if defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: : m_base(0), m_size(0) Chris@16: , m_page_offset(0) Chris@16: , m_mode(read_only) Chris@16: , m_file_or_mapping_hnd(ipcdetail::invalid_file()) Chris@16: #else Chris@16: : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only), m_is_xsi(false) Chris@16: #endif Chris@16: { this->swap(other); } Chris@16: Chris@16: //!Destroys the mapped region. Chris@16: //!Does not throw Chris@16: ~mapped_region(); Chris@16: Chris@16: //!Move assignment. If *this owns a memory mapped region, it will be Chris@16: //!destroyed and it will take ownership of "other"'s memory mapped region. Chris@16: mapped_region &operator=(BOOST_RV_REF(mapped_region) other) Chris@16: { Chris@16: mapped_region tmp(boost::move(other)); Chris@16: this->swap(tmp); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Swaps the mapped_region with another Chris@16: //!mapped region Chris@16: void swap(mapped_region &other); Chris@16: Chris@16: //!Returns the size of the mapping. Never throws. Chris@16: std::size_t get_size() const; Chris@16: Chris@16: //!Returns the base address of the mapping. Chris@16: //!Never throws. Chris@16: void* get_address() const; Chris@16: Chris@16: //!Returns the mode of the mapping used to construct the mapped region. Chris@16: //!Never throws. Chris@16: mode_t get_mode() const; Chris@16: Chris@16: //!Flushes to the disk a byte range within the mapped memory. Chris@16: //!If 'async' is true, the function will return before flushing operation is completed Chris@16: //!If 'async' is false, function will return once data has been written into the underlying Chris@16: //!device (i.e., in mapped files OS cached information is written to disk). Chris@16: //!Never throws. Returns false if operation could not be performed. Chris@16: bool flush(std::size_t mapping_offset = 0, std::size_t numbytes = 0, bool async = true); Chris@16: Chris@16: //!Shrinks current mapped region. If after shrinking there is no longer need for a previously Chris@16: //!mapped memory page, accessing that page can trigger a segmentation fault. Chris@16: //!Depending on the OS, this operation might fail (XSI shared memory), it can decommit storage Chris@16: //!and free a portion of the virtual address space (e.g.POSIX) or this Chris@16: //!function can release some physical memory wihout freeing any virtual address space(Windows). Chris@16: //!Returns true on success. Never throws. Chris@16: bool shrink_by(std::size_t bytes, bool from_back = true); Chris@16: Chris@16: //!This enum specifies region usage behaviors that an application can specify Chris@16: //!to the mapped region implementation. Chris@101: enum advice_types{ Chris@16: //!Specifies that the application has no advice to give on its behavior with respect to Chris@16: //!the region. It is the default characteristic if no advice is given for a range of memory. Chris@16: advice_normal, Chris@16: //!Specifies that the application expects to access the region sequentially from Chris@16: //!lower addresses to higher addresses. The implementation can lower the priority of Chris@16: //!preceding pages within the region once a page have been accessed. Chris@16: advice_sequential, Chris@16: //!Specifies that the application expects to access the region in a random order, Chris@16: //!and prefetching is likely not advantageous. Chris@16: advice_random, Chris@16: //!Specifies that the application expects to access the region in the near future. Chris@16: //!The implementation can prefetch pages of the region. Chris@16: advice_willneed, Chris@16: //!Specifies that the application expects that it will not access the region in the near future. Chris@16: //!The implementation can unload pages within the range to save system resources. Chris@16: advice_dontneed Chris@16: }; Chris@16: Chris@16: //!Advises the implementation on the expected behavior of the application with respect to the data Chris@16: //!in the region. The implementation may use this information to optimize handling of the region data. Chris@16: //!This function has no effect on the semantics of access to memory in the region, although it may affect Chris@16: //!the performance of access. Chris@16: //!If the advise type is not known to the implementation, the function returns false. True otherwise. Chris@16: bool advise(advice_types advise); Chris@16: Chris@16: //!Returns the size of the page. This size is the minimum memory that Chris@16: //!will be used by the system when mapping a memory mappable source and Chris@16: //!will restrict the address and the offset to map. Chris@16: static std::size_t get_page_size(); Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: //!Closes a previously opened memory mapping. Never throws Chris@16: void priv_close(); Chris@16: Chris@16: void* priv_map_address() const; Chris@16: std::size_t priv_map_size() const; Chris@16: bool priv_flush_param_check(std::size_t mapping_offset, void *&addr, std::size_t &numbytes) const; Chris@16: bool priv_shrink_param_check(std::size_t bytes, bool from_back, void *&shrink_page_start, std::size_t &shrink_page_bytes); Chris@16: static void priv_size_from_mapping_size Chris@16: (offset_t mapping_size, offset_t offset, offset_t page_offset, std::size_t &size); Chris@16: static offset_t priv_page_offset_addr_fixup(offset_t page_offset, const void *&addr); Chris@16: Chris@16: template Chris@16: struct page_size_holder Chris@16: { Chris@16: static const std::size_t PageSize; Chris@16: static std::size_t get_page_size(); Chris@16: }; Chris@16: Chris@16: void* m_base; Chris@16: std::size_t m_size; Chris@16: std::size_t m_page_offset; Chris@16: mode_t m_mode; Chris@16: #if defined(BOOST_INTERPROCESS_WINDOWS) Chris@16: file_handle_t m_file_or_mapping_hnd; Chris@16: #else Chris@16: bool m_is_xsi; Chris@16: #endif Chris@16: Chris@16: friend class ipcdetail::interprocess_tester; Chris@16: friend class ipcdetail::raw_mapped_region_creator; Chris@16: void dont_close_on_destruction(); Chris@16: #if defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) Chris@16: template Chris@16: static void destroy_syncs_in_range(const void *addr, std::size_t size); Chris@16: #endif Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: inline void swap(mapped_region &x, mapped_region &y) Chris@16: { x.swap(y); } Chris@16: Chris@16: inline mapped_region::~mapped_region() Chris@16: { this->priv_close(); } Chris@16: Chris@16: inline std::size_t mapped_region::get_size() const Chris@16: { return m_size; } Chris@16: Chris@16: inline mode_t mapped_region::get_mode() const Chris@16: { return m_mode; } Chris@16: Chris@16: inline void* mapped_region::get_address() const Chris@16: { return m_base; } Chris@16: Chris@16: inline void* mapped_region::priv_map_address() const Chris@16: { return static_cast(m_base) - m_page_offset; } Chris@16: Chris@16: inline std::size_t mapped_region::priv_map_size() const Chris@16: { return m_size + m_page_offset; } Chris@16: Chris@16: inline bool mapped_region::priv_flush_param_check Chris@16: (std::size_t mapping_offset, void *&addr, std::size_t &numbytes) const Chris@16: { Chris@16: //Check some errors Chris@16: if(m_base == 0) Chris@16: return false; Chris@16: Chris@16: if(mapping_offset >= m_size || (mapping_offset + numbytes) > m_size){ Chris@16: return false; Chris@16: } Chris@16: Chris@16: //Update flush size if the user does not provide it Chris@16: if(numbytes == 0){ Chris@16: numbytes = m_size - mapping_offset; Chris@16: } Chris@16: addr = (char*)this->priv_map_address() + mapping_offset; Chris@16: numbytes += m_page_offset; Chris@16: return true; Chris@16: } Chris@16: Chris@16: inline bool mapped_region::priv_shrink_param_check Chris@16: (std::size_t bytes, bool from_back, void *&shrink_page_start, std::size_t &shrink_page_bytes) Chris@16: { Chris@16: //Check some errors Chris@16: if(m_base == 0 || bytes > m_size){ Chris@16: return false; Chris@16: } Chris@16: else if(bytes == m_size){ Chris@16: this->priv_close(); Chris@16: return true; Chris@16: } Chris@16: else{ Chris@16: const std::size_t page_size = mapped_region::get_page_size(); Chris@16: if(from_back){ Chris@16: const std::size_t new_pages = (m_size + m_page_offset - bytes - 1)/page_size + 1; Chris@16: shrink_page_start = static_cast(this->priv_map_address()) + new_pages*page_size; Chris@16: shrink_page_bytes = m_page_offset + m_size - new_pages*page_size; Chris@16: m_size -= bytes; Chris@16: } Chris@16: else{ Chris@16: shrink_page_start = this->priv_map_address(); Chris@16: m_page_offset += bytes; Chris@16: shrink_page_bytes = (m_page_offset/page_size)*page_size; Chris@16: m_page_offset = m_page_offset % page_size; Chris@16: m_size -= bytes; Chris@16: m_base = static_cast(m_base) + bytes; Chris@16: BOOST_ASSERT(shrink_page_bytes%page_size == 0); Chris@16: } Chris@16: return true; Chris@16: } Chris@16: } Chris@16: Chris@16: inline void mapped_region::priv_size_from_mapping_size Chris@16: (offset_t mapping_size, offset_t offset, offset_t page_offset, std::size_t &size) Chris@16: { Chris@16: //Check if mapping size fits in the user address space Chris@16: //as offset_t is the maximum file size and its signed. Chris@16: if(mapping_size < offset || Chris@16: boost::uintmax_t(mapping_size - (offset - page_offset)) > Chris@16: boost::uintmax_t(std::size_t(-1))){ Chris@16: error_info err(size_error); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: size = static_cast(mapping_size - (offset - page_offset)); Chris@16: } Chris@16: Chris@16: inline offset_t mapped_region::priv_page_offset_addr_fixup(offset_t offset, const void *&address) Chris@16: { Chris@16: //We can't map any offset so we have to obtain system's Chris@16: //memory granularity Chris@16: const std::size_t page_size = mapped_region::get_page_size(); Chris@16: Chris@16: //We calculate the difference between demanded and valid offset Chris@16: //(always less than a page in std::size_t, thus, representable by std::size_t) Chris@16: const std::size_t page_offset = Chris@16: static_cast(offset - (offset / page_size) * page_size); Chris@16: //Update the mapping address Chris@16: if(address){ Chris@16: address = static_cast(address) - page_offset; Chris@16: } Chris@16: return page_offset; Chris@16: } Chris@16: Chris@16: #if defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: Chris@16: inline mapped_region::mapped_region() Chris@16: : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only) Chris@16: , m_file_or_mapping_hnd(ipcdetail::invalid_file()) Chris@16: {} Chris@16: Chris@16: template Chris@16: inline std::size_t mapped_region::page_size_holder::get_page_size() Chris@16: { Chris@16: winapi::system_info info; Chris@101: winapi::get_system_info(&info); Chris@16: return std::size_t(info.dwAllocationGranularity); Chris@16: } Chris@16: Chris@16: template Chris@16: inline mapped_region::mapped_region Chris@16: (const MemoryMappable &mapping Chris@16: ,mode_t mode Chris@16: ,offset_t offset Chris@16: ,std::size_t size Chris@16: ,const void *address Chris@16: ,map_options_t map_options) Chris@16: : m_base(0), m_size(0), m_page_offset(0), m_mode(mode) Chris@16: , m_file_or_mapping_hnd(ipcdetail::invalid_file()) Chris@16: { Chris@16: mapping_handle_t mhandle = mapping.get_mapping_handle(); Chris@16: { Chris@16: file_handle_t native_mapping_handle = 0; Chris@16: Chris@16: //Set accesses Chris@16: //For "create_file_mapping" Chris@16: unsigned long protection = 0; Chris@16: //For "mapviewoffile" Chris@16: unsigned long map_access = map_options == default_map_options ? 0 : map_options; Chris@16: Chris@16: switch(mode) Chris@16: { Chris@16: case read_only: Chris@16: case read_private: Chris@16: protection |= winapi::page_readonly; Chris@16: map_access |= winapi::file_map_read; Chris@16: break; Chris@16: case read_write: Chris@16: protection |= winapi::page_readwrite; Chris@16: map_access |= winapi::file_map_write; Chris@16: break; Chris@16: case copy_on_write: Chris@16: protection |= winapi::page_writecopy; Chris@16: map_access |= winapi::file_map_copy; Chris@16: break; Chris@16: default: Chris@16: { Chris@16: error_info err(mode_error); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: break; Chris@16: } Chris@16: Chris@16: //For file mapping (including emulated shared memory through temporary files), Chris@16: //the device is a file handle so we need to obtain file's size and call create_file_mapping Chris@16: //to obtain the mapping handle. Chris@16: //For files we don't need the file mapping after mapping the memory, as the file is there Chris@16: //so we'll program the handle close Chris@16: void * handle_to_close = winapi::invalid_handle_value; Chris@16: if(!mhandle.is_shm){ Chris@16: //Create mapping handle Chris@16: native_mapping_handle = winapi::create_file_mapping Chris@16: ( ipcdetail::file_handle_from_mapping_handle(mapping.get_mapping_handle()) Chris@16: , protection, 0, 0, 0); Chris@16: Chris@16: //Check if all is correct Chris@16: if(!native_mapping_handle){ Chris@16: error_info err = winapi::get_last_error(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: handle_to_close = native_mapping_handle; Chris@16: } Chris@16: else{ Chris@16: //For windows_shared_memory the device handle is already a mapping handle Chris@16: //and we need to maintain it Chris@16: native_mapping_handle = mhandle.handle; Chris@16: } Chris@16: //RAII handle close on scope exit Chris@16: const winapi::handle_closer close_handle(handle_to_close); Chris@16: (void)close_handle; Chris@16: Chris@16: const offset_t page_offset = priv_page_offset_addr_fixup(offset, address); Chris@16: Chris@16: //Obtain mapping size if user provides 0 size Chris@16: if(size == 0){ Chris@16: offset_t mapping_size; Chris@16: if(!winapi::get_file_mapping_size(native_mapping_handle, mapping_size)){ Chris@16: error_info err = winapi::get_last_error(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: //This can throw Chris@16: priv_size_from_mapping_size(mapping_size, offset, page_offset, size); Chris@16: } Chris@16: Chris@16: //Map with new offsets and size Chris@16: void *base = winapi::map_view_of_file_ex Chris@16: (native_mapping_handle, Chris@16: map_access, Chris@16: offset - page_offset, Chris@16: static_cast(page_offset + size), Chris@16: const_cast(address)); Chris@16: //Check error Chris@16: if(!base){ Chris@16: error_info err = winapi::get_last_error(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: Chris@16: //Calculate new base for the user Chris@16: m_base = static_cast(base) + page_offset; Chris@16: m_page_offset = page_offset; Chris@16: m_size = size; Chris@16: } Chris@16: //Windows shared memory needs the duplication of the handle if we want to Chris@16: //make mapped_region independent from the mappable device Chris@16: // Chris@16: //For mapped files, we duplicate the file handle to be able to FlushFileBuffers Chris@16: if(!winapi::duplicate_current_process_handle(mhandle.handle, &m_file_or_mapping_hnd)){ Chris@16: error_info err = winapi::get_last_error(); Chris@16: this->priv_close(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: inline bool mapped_region::flush(std::size_t mapping_offset, std::size_t numbytes, bool async) Chris@16: { Chris@16: void *addr; Chris@16: if(!this->priv_flush_param_check(mapping_offset, addr, numbytes)){ Chris@16: return false; Chris@16: } Chris@16: //Flush it all Chris@16: if(!winapi::flush_view_of_file(addr, numbytes)){ Chris@16: return false; Chris@16: } Chris@16: //m_file_or_mapping_hnd can be a file handle or a mapping handle. Chris@16: //so flushing file buffers has only sense for files... Chris@16: else if(!async && m_file_or_mapping_hnd != winapi::invalid_handle_value && Chris@16: winapi::get_file_type(m_file_or_mapping_hnd) == winapi::file_type_disk){ Chris@16: return winapi::flush_file_buffers(m_file_or_mapping_hnd); Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: inline bool mapped_region::shrink_by(std::size_t bytes, bool from_back) Chris@16: { Chris@16: void *shrink_page_start; Chris@16: std::size_t shrink_page_bytes; Chris@16: if(!this->priv_shrink_param_check(bytes, from_back, shrink_page_start, shrink_page_bytes)){ Chris@16: return false; Chris@16: } Chris@16: else if(shrink_page_bytes){ Chris@16: //In Windows, we can't decommit the storage or release the virtual address space, Chris@101: //the best we can do is try to remove some memory from the process working set. Chris@16: //With a bit of luck we can free some physical memory. Chris@16: unsigned long old_protect_ignored; Chris@16: bool b_ret = winapi::virtual_unlock(shrink_page_start, shrink_page_bytes) Chris@16: || (winapi::get_last_error() == winapi::error_not_locked); Chris@16: (void)old_protect_ignored; Chris@16: //Change page protection to forbid any further access Chris@16: b_ret = b_ret && winapi::virtual_protect Chris@16: (shrink_page_start, shrink_page_bytes, winapi::page_noaccess, old_protect_ignored); Chris@16: return b_ret; Chris@16: } Chris@16: else{ Chris@16: return true; Chris@16: } Chris@16: } Chris@16: Chris@16: inline bool mapped_region::advise(advice_types) Chris@16: { Chris@16: //Windows has no madvise/posix_madvise equivalent Chris@16: return false; Chris@16: } Chris@16: Chris@16: inline void mapped_region::priv_close() Chris@16: { Chris@16: if(m_base){ Chris@16: void *addr = this->priv_map_address(); Chris@16: #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) Chris@16: mapped_region::destroy_syncs_in_range<0>(addr, m_size); Chris@16: #endif Chris@16: winapi::unmap_view_of_file(addr); Chris@16: m_base = 0; Chris@16: } Chris@16: if(m_file_or_mapping_hnd != ipcdetail::invalid_file()){ Chris@16: winapi::close_handle(m_file_or_mapping_hnd); Chris@16: m_file_or_mapping_hnd = ipcdetail::invalid_file(); Chris@16: } Chris@16: } Chris@16: Chris@16: inline void mapped_region::dont_close_on_destruction() Chris@16: {} Chris@16: Chris@101: #else //#if defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: Chris@16: inline mapped_region::mapped_region() Chris@16: : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only), m_is_xsi(false) Chris@16: {} Chris@16: Chris@16: template Chris@16: inline std::size_t mapped_region::page_size_holder::get_page_size() Chris@16: { return std::size_t(sysconf(_SC_PAGESIZE)); } Chris@16: Chris@16: template Chris@16: inline mapped_region::mapped_region Chris@16: ( const MemoryMappable &mapping Chris@16: , mode_t mode Chris@16: , offset_t offset Chris@16: , std::size_t size Chris@16: , const void *address Chris@16: , map_options_t map_options) Chris@16: : m_base(0), m_size(0), m_page_offset(0), m_mode(mode), m_is_xsi(false) Chris@16: { Chris@16: mapping_handle_t map_hnd = mapping.get_mapping_handle(); Chris@16: Chris@16: //Some systems dont' support XSI shared memory Chris@16: #ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS Chris@16: if(map_hnd.is_xsi){ Chris@16: //Get the size Chris@16: ::shmid_ds xsi_ds; Chris@16: int ret = ::shmctl(map_hnd.handle, IPC_STAT, &xsi_ds); Chris@16: if(ret == -1){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: //Compare sizess Chris@16: if(size == 0){ Chris@16: size = (std::size_t)xsi_ds.shm_segsz; Chris@16: } Chris@16: else if(size != (std::size_t)xsi_ds.shm_segsz){ Chris@16: error_info err(size_error); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: //Calculate flag Chris@16: int flag = map_options == default_map_options ? 0 : map_options; Chris@16: if(m_mode == read_only){ Chris@16: flag |= SHM_RDONLY; Chris@16: } Chris@16: else if(m_mode != read_write){ Chris@16: error_info err(mode_error); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: //Attach memory Chris@16: void *base = ::shmat(map_hnd.handle, (void*)address, flag); Chris@16: if(base == (void*)-1){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: //Update members Chris@16: m_base = base; Chris@16: m_size = size; Chris@16: m_mode = mode; Chris@16: m_page_offset = 0; Chris@16: m_is_xsi = true; Chris@16: return; Chris@16: } Chris@16: #endif //ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS Chris@16: Chris@16: //We calculate the difference between demanded and valid offset Chris@16: const offset_t page_offset = priv_page_offset_addr_fixup(offset, address); Chris@16: Chris@16: if(size == 0){ Chris@16: struct ::stat buf; Chris@16: if(0 != fstat(map_hnd.handle, &buf)){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: //This can throw Chris@16: priv_size_from_mapping_size(buf.st_size, offset, page_offset, size); Chris@16: } Chris@16: Chris@16: #ifdef MAP_NOSYNC Chris@16: #define BOOST_INTERPROCESS_MAP_NOSYNC MAP_NOSYNC Chris@16: #else Chris@16: #define BOOST_INTERPROCESS_MAP_NOSYNC 0 Chris@16: #endif //MAP_NOSYNC Chris@16: Chris@16: //Create new mapping Chris@16: int prot = 0; Chris@16: int flags = map_options == default_map_options ? BOOST_INTERPROCESS_MAP_NOSYNC : map_options; Chris@16: Chris@16: #undef BOOST_INTERPROCESS_MAP_NOSYNC Chris@16: Chris@16: switch(mode) Chris@16: { Chris@16: case read_only: Chris@16: prot |= PROT_READ; Chris@16: flags |= MAP_SHARED; Chris@16: break; Chris@16: Chris@16: case read_private: Chris@16: prot |= (PROT_READ); Chris@16: flags |= MAP_PRIVATE; Chris@16: break; Chris@16: Chris@16: case read_write: Chris@16: prot |= (PROT_WRITE | PROT_READ); Chris@16: flags |= MAP_SHARED; Chris@16: break; Chris@16: Chris@16: case copy_on_write: Chris@16: prot |= (PROT_WRITE | PROT_READ); Chris@16: flags |= MAP_PRIVATE; Chris@16: break; Chris@16: Chris@16: default: Chris@16: { Chris@16: error_info err(mode_error); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: break; Chris@16: } Chris@16: Chris@16: //Map it to the address space Chris@16: void* base = mmap ( const_cast(address) Chris@16: , static_cast(page_offset + size) Chris@16: , prot Chris@16: , flags Chris@16: , mapping.get_mapping_handle().handle Chris@16: , offset - page_offset); Chris@16: Chris@16: //Check if mapping was successful Chris@16: if(base == MAP_FAILED){ Chris@16: error_info err = system_error_code(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: Chris@16: //Calculate new base for the user Chris@16: m_base = static_cast(base) + page_offset; Chris@16: m_page_offset = page_offset; Chris@16: m_size = size; Chris@16: Chris@16: //Check for fixed mapping error Chris@16: if(address && (base != address)){ Chris@16: error_info err(busy_error); Chris@16: this->priv_close(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: inline bool mapped_region::shrink_by(std::size_t bytes, bool from_back) Chris@16: { Chris@16: void *shrink_page_start = 0; Chris@16: std::size_t shrink_page_bytes = 0; Chris@16: if(m_is_xsi || !this->priv_shrink_param_check(bytes, from_back, shrink_page_start, shrink_page_bytes)){ Chris@16: return false; Chris@16: } Chris@16: else if(shrink_page_bytes){ Chris@16: //In UNIX we can decommit and free virtual address space. Chris@16: return 0 == munmap(shrink_page_start, shrink_page_bytes); Chris@16: } Chris@16: else{ Chris@16: return true; Chris@16: } Chris@16: } Chris@16: Chris@16: inline bool mapped_region::flush(std::size_t mapping_offset, std::size_t numbytes, bool async) Chris@16: { Chris@16: void *addr; Chris@16: if(m_is_xsi || !this->priv_flush_param_check(mapping_offset, addr, numbytes)){ Chris@16: return false; Chris@16: } Chris@16: //Flush it all Chris@16: return msync(addr, numbytes, async ? MS_ASYNC : MS_SYNC) == 0; Chris@16: } Chris@16: Chris@16: inline bool mapped_region::advise(advice_types advice) Chris@16: { Chris@16: int unix_advice = 0; Chris@16: //Modes; 0: none, 2: posix, 1: madvise Chris@16: const unsigned int mode_none = 0; Chris@16: const unsigned int mode_padv = 1; Chris@16: const unsigned int mode_madv = 2; Chris@101: // Suppress "unused variable" warnings Chris@101: (void)mode_padv; Chris@101: (void)mode_madv; Chris@16: unsigned int mode = mode_none; Chris@16: //Choose advice either from POSIX (preferred) or native Unix Chris@16: switch(advice){ Chris@16: case advice_normal: Chris@16: #if defined(POSIX_MADV_NORMAL) Chris@16: unix_advice = POSIX_MADV_NORMAL; Chris@16: mode = mode_padv; Chris@16: #elif defined(MADV_NORMAL) Chris@16: unix_advice = MADV_NORMAL; Chris@16: mode = mode_madv; Chris@16: #endif Chris@16: break; Chris@16: case advice_sequential: Chris@16: #if defined(POSIX_MADV_SEQUENTIAL) Chris@16: unix_advice = POSIX_MADV_SEQUENTIAL; Chris@16: mode = mode_padv; Chris@16: #elif defined(MADV_SEQUENTIAL) Chris@16: unix_advice = MADV_SEQUENTIAL; Chris@16: mode = mode_madv; Chris@16: #endif Chris@16: break; Chris@16: case advice_random: Chris@16: #if defined(POSIX_MADV_RANDOM) Chris@16: unix_advice = POSIX_MADV_RANDOM; Chris@16: mode = mode_padv; Chris@16: #elif defined(MADV_RANDOM) Chris@16: unix_advice = MADV_RANDOM; Chris@16: mode = mode_madv; Chris@16: #endif Chris@16: break; Chris@16: case advice_willneed: Chris@16: #if defined(POSIX_MADV_WILLNEED) Chris@16: unix_advice = POSIX_MADV_WILLNEED; Chris@16: mode = mode_padv; Chris@16: #elif defined(MADV_WILLNEED) Chris@16: unix_advice = MADV_WILLNEED; Chris@16: mode = mode_madv; Chris@16: #endif Chris@16: break; Chris@16: case advice_dontneed: Chris@16: #if defined(POSIX_MADV_DONTNEED) Chris@16: unix_advice = POSIX_MADV_DONTNEED; Chris@16: mode = mode_padv; Chris@16: #elif defined(MADV_DONTNEED) && defined(BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS) Chris@16: unix_advice = MADV_DONTNEED; Chris@16: mode = mode_madv; Chris@16: #endif Chris@16: break; Chris@16: default: Chris@16: return false; Chris@16: } Chris@16: switch(mode){ Chris@16: #if defined(POSIX_MADV_NORMAL) Chris@16: case mode_padv: Chris@16: return 0 == posix_madvise(this->priv_map_address(), this->priv_map_size(), unix_advice); Chris@16: #endif Chris@16: #if defined(MADV_NORMAL) Chris@16: case mode_madv: Chris@16: return 0 == madvise( Chris@16: #if defined(BOOST_INTERPROCESS_MADVISE_USES_CADDR_T) Chris@16: (caddr_t) Chris@16: #endif Chris@16: this->priv_map_address(), this->priv_map_size(), unix_advice); Chris@16: #endif Chris@16: default: Chris@16: return false; Chris@16: Chris@16: } Chris@16: } Chris@16: Chris@16: inline void mapped_region::priv_close() Chris@16: { Chris@16: if(m_base != 0){ Chris@16: #ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS Chris@16: if(m_is_xsi){ Chris@16: int ret = ::shmdt(m_base); Chris@16: BOOST_ASSERT(ret == 0); Chris@16: (void)ret; Chris@16: return; Chris@16: } Chris@16: #endif //#ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS Chris@16: munmap(this->priv_map_address(), this->priv_map_size()); Chris@16: m_base = 0; Chris@16: } Chris@16: } Chris@16: Chris@16: inline void mapped_region::dont_close_on_destruction() Chris@16: { m_base = 0; } Chris@16: Chris@101: #endif //#if defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: Chris@16: template Chris@16: const std::size_t mapped_region::page_size_holder::PageSize Chris@16: = mapped_region::page_size_holder::get_page_size(); Chris@16: Chris@16: inline std::size_t mapped_region::get_page_size() Chris@16: { Chris@16: if(!page_size_holder<0>::PageSize) Chris@16: return page_size_holder<0>::get_page_size(); Chris@16: else Chris@16: return page_size_holder<0>::PageSize; Chris@16: } Chris@16: Chris@16: inline void mapped_region::swap(mapped_region &other) Chris@16: { Chris@101: ::boost::adl_move_swap(this->m_base, other.m_base); Chris@101: ::boost::adl_move_swap(this->m_size, other.m_size); Chris@101: ::boost::adl_move_swap(this->m_page_offset, other.m_page_offset); Chris@101: ::boost::adl_move_swap(this->m_mode, other.m_mode); Chris@101: #if defined (BOOST_INTERPROCESS_WINDOWS) Chris@101: ::boost::adl_move_swap(this->m_file_or_mapping_hnd, other.m_file_or_mapping_hnd); Chris@16: #else Chris@101: ::boost::adl_move_swap(this->m_is_xsi, other.m_is_xsi); Chris@16: #endif Chris@16: } Chris@16: Chris@16: //!No-op functor Chris@16: struct null_mapped_region_function Chris@16: { Chris@16: bool operator()(void *, std::size_t , bool) const Chris@16: { return true; } Chris@16: Chris@16: std::size_t get_min_size() const Chris@16: { return 0; } Chris@16: }; Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_MAPPED_REGION_HPP Chris@16: Chris@16: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP Chris@16: #define BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP Chris@16: Chris@16: #if defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) Chris@16: # include Chris@16: # include Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: template Chris@16: inline void mapped_region::destroy_syncs_in_range(const void *addr, std::size_t size) Chris@16: { Chris@16: ipcdetail::sync_handles &handles = Chris@16: ipcdetail::windows_intermodule_singleton::get(); Chris@16: handles.destroy_syncs_in_range(addr, size); Chris@16: } Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #endif //defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) Chris@16: Chris@16: #endif //#ifdef BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP Chris@16: Chris@16: #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: