Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-23 08:50:11

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_MAPPED_REGION_HPP
0012 #define BOOST_INTERPROCESS_MAPPED_REGION_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 0018 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 #  pragma once
0021 #endif
0022 
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025 
0026 #include <boost/interprocess/interprocess_fwd.hpp>
0027 #include <boost/interprocess/exceptions.hpp>
0028 #include <boost/move/utility_core.hpp>
0029 #include <boost/interprocess/detail/utilities.hpp>
0030 #include <boost/interprocess/detail/os_file_functions.hpp>
0031 #include <string>
0032 #include <boost/cstdint.hpp>
0033 #include <boost/assert.hpp>
0034 #include <boost/move/adl_move_swap.hpp>
0035 
0036 //Some Unixes use caddr_t instead of void * in madvise
0037 //              SunOS                                 Tru64                               HP-UX                    AIX
0038 #if defined(sun) || defined(__sun) || defined(__osf__) || defined(__osf) || defined(_hpux) || defined(hpux) || defined(_AIX)
0039 #define BOOST_INTERPROCESS_MADVISE_USES_CADDR_T
0040 #include <sys/types.h>
0041 #endif
0042 
0043 //A lot of UNIXes have destructive semantics for MADV_DONTNEED, so
0044 //we need to be careful to allow it.
0045 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
0046 #define BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS
0047 #endif
0048 
0049 #if defined (BOOST_INTERPROCESS_WINDOWS)
0050 #  include <boost/interprocess/detail/win32_api.hpp>
0051 #else
0052 #  ifdef BOOST_HAS_UNISTD_H
0053 #    include <fcntl.h>
0054 #    include <sys/mman.h>     //mmap
0055 #    include <unistd.h>
0056 #    include <sys/stat.h>
0057 #    include <sys/types.h>
0058 #    if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
0059 #      include <sys/shm.h>      //System V shared memory...
0060 #    endif
0061 #    include <boost/assert.hpp>
0062 #  else
0063 #    error Unknown platform
0064 #  endif
0065 
0066 #endif   //#if defined (BOOST_INTERPROCESS_WINDOWS)
0067 
0068 //!\file
0069 //!Describes mapped region class
0070 
0071 namespace boost {
0072 namespace interprocess {
0073 
0074 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0075 
0076 //Solaris declares madvise only in some configurations but defines MADV_XXX, a bit confusing.
0077 //Predeclare it here to avoid any compilation error
0078 #if (defined(sun) || defined(__sun)) && defined(MADV_NORMAL)
0079 extern "C" int madvise(caddr_t, size_t, int);
0080 #endif
0081 
0082 namespace ipcdetail{ class interprocess_tester; }
0083 namespace ipcdetail{ class raw_mapped_region_creator; }
0084 
0085 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0086 
0087 //!The mapped_region class represents a portion or region created from a
0088 //!memory_mappable object.
0089 //!
0090 //!The OS can map a region bigger than the requested one, as region must
0091 //!be multiple of the page size, but mapped_region will always refer to
0092 //!the region specified by the user.
0093 class mapped_region
0094 {
0095    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0096    //Non-copyable
0097    BOOST_MOVABLE_BUT_NOT_COPYABLE(mapped_region)
0098    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0099 
0100    public:
0101 
0102    //!Creates a mapping region of the mapped memory "mapping", starting in
0103    //!offset "offset", and the mapping's size will be "size". The mapping
0104    //!can be opened for read only, read-write or copy-on-write.
0105    //!
0106    //!If an address is specified, both the offset and the address must be
0107    //!multiples of the page size.
0108    //!
0109    //!The map is created using "default_map_options". This flag is OS
0110    //!dependant and it should not be changed unless the user needs to
0111    //!specify special options.
0112    //!
0113    //!In Windows systems "map_options" is a DWORD value passed as
0114    //!"dwDesiredAccess" to "MapViewOfFileEx". If "default_map_options" is passed
0115    //!it's initialized to zero. "map_options" is XORed with FILE_MAP_[COPY|READ|WRITE].
0116    //!
0117    //!In UNIX systems and POSIX mappings "map_options" is an int value passed as "flags"
0118    //!to "mmap". If "default_map_options" is specified it's initialized to MAP_NOSYNC
0119    //!if that option exists and to zero otherwise. "map_options" XORed with MAP_PRIVATE or MAP_SHARED.
0120    //!
0121    //!In UNIX systems and XSI mappings "map_options" is an int value passed as "shmflg"
0122    //!to "shmat". If "default_map_options" is specified it's initialized to zero.
0123    //!"map_options" is XORed with SHM_RDONLY if needed.
0124    //!
0125    //!The OS could allocate more pages than size/page_size(), but get_address()
0126    //!will always return the address passed in this function (if not null) and
0127    //!get_size() will return the specified size.
0128    template<class MemoryMappable>
0129    mapped_region(const MemoryMappable& mapping
0130                 ,mode_t mode
0131                 ,offset_t offset = 0
0132                 ,std::size_t size = 0
0133                 ,const void *address = 0
0134                 ,map_options_t map_options = default_map_options);
0135 
0136    //!Default constructor. Address will be 0 (nullptr).
0137    //!Size will be 0.
0138    //!Does not throw
0139    mapped_region() BOOST_NOEXCEPT;
0140 
0141    //!Move constructor. *this will be constructed taking ownership of "other"'s
0142    //!region and "other" will be left in default constructor state.
0143    mapped_region(BOOST_RV_REF(mapped_region) other)  BOOST_NOEXCEPT
0144    #if defined (BOOST_INTERPROCESS_WINDOWS)
0145    :  m_base(0), m_size(0)
0146    ,  m_page_offset(0)
0147    ,  m_mode(read_only)
0148    ,  m_file_or_mapping_hnd(ipcdetail::invalid_file())
0149    #else
0150    :  m_base(0), m_size(0), m_page_offset(0), m_mode(read_only), m_is_xsi(false)
0151    #endif
0152    {  this->swap(other);   }
0153 
0154    //!Destroys the mapped region.
0155    //!Does not throw
0156    ~mapped_region();
0157 
0158    //!Move assignment. If *this owns a memory mapped region, it will be
0159    //!destroyed and it will take ownership of "other"'s memory mapped region.
0160    mapped_region &operator=(BOOST_RV_REF(mapped_region) other) BOOST_NOEXCEPT
0161    {
0162       mapped_region tmp(boost::move(other));
0163       this->swap(tmp);
0164       return *this;
0165    }
0166 
0167    //!Swaps the mapped_region with another
0168    //!mapped region
0169    void swap(mapped_region &other) BOOST_NOEXCEPT;
0170 
0171    //!Returns the size of the mapping. Never throws.
0172    std::size_t get_size() const BOOST_NOEXCEPT;
0173 
0174    //!Returns the base address of the mapping.
0175    //!Never throws.
0176    void*       get_address() const BOOST_NOEXCEPT;
0177 
0178    //!Returns the mode of the mapping used to construct the mapped region.
0179    //!Never throws.
0180    mode_t get_mode() const BOOST_NOEXCEPT;
0181 
0182    //!Flushes to the disk a byte range within the mapped memory.
0183    //!If 'async' is true, the function will return before flushing operation is completed
0184    //!If 'async' is false, function will return once data has been written into the underlying
0185    //!device (i.e., in mapped files OS cached information is written to disk).
0186    //!Never throws. Returns false if operation could not be performed.
0187    bool flush(std::size_t mapping_offset = 0, std::size_t numbytes = 0, bool async = true);
0188 
0189    //!Shrinks current mapped region. If after shrinking there is no longer need for a previously
0190    //!mapped memory page, accessing that page can trigger a segmentation fault.
0191    //!Depending on the OS, this operation might fail (XSI shared memory), it can decommit storage
0192    //!and free a portion of the virtual address space (e.g.POSIX) or this
0193    //!function can release some physical memory without freeing any virtual address space(Windows).
0194    //!Returns true on success. Never throws.
0195    bool shrink_by(std::size_t bytes, bool from_back = true);
0196 
0197    //!This enum specifies region usage behaviors that an application can specify
0198    //!to the mapped region implementation.
0199    enum advice_types{
0200       //!Specifies that the application has no advice to give on its behavior with respect to
0201       //!the region. It is the default characteristic if no advice is given for a range of memory.
0202       advice_normal,
0203       //!Specifies that the application expects to access the region sequentially from
0204       //!lower addresses to higher addresses. The implementation can lower the priority of
0205       //!preceding pages within the region once a page have been accessed.
0206       advice_sequential,
0207       //!Specifies that the application expects to access the region in a random order,
0208       //!and prefetching is likely not advantageous.
0209       advice_random,
0210       //!Specifies that the application expects to access the region in the near future.
0211       //!The implementation can prefetch pages of the region.
0212       advice_willneed,
0213       //!Specifies that the application expects that it will not access the region in the near future.
0214       //!The implementation can unload pages within the range to save system resources.
0215       advice_dontneed
0216    };
0217 
0218    //!Advises the implementation on the expected behavior of the application with respect to the data
0219    //!in the region. The implementation may use this information to optimize handling of the region data.
0220    //!This function has no effect on the semantics of access to memory in the region, although it may affect
0221    //!the performance of access.
0222    //!If the advise type is not known to the implementation, the function returns false. True otherwise.
0223    bool advise(advice_types advise);
0224 
0225    //!Returns the size of the page. This size is the minimum memory that
0226    //!will be used by the system when mapping a memory mappable source and
0227    //!will restrict the address and the offset to map.
0228    static std::size_t get_page_size() BOOST_NOEXCEPT;
0229 
0230    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0231    private:
0232    //!Closes a previously opened memory mapping. Never throws
0233    void priv_close();
0234 
0235    void* priv_map_address()  const;
0236    std::size_t priv_map_size()  const;
0237    bool priv_flush_param_check(std::size_t mapping_offset, void *&addr, std::size_t &numbytes) const;
0238    bool priv_shrink_param_check(std::size_t bytes, bool from_back, void *&shrink_page_start, std::size_t &shrink_page_bytes);
0239    static void priv_size_from_mapping_size
0240       (offset_t mapping_size, offset_t offset, offset_t page_offset, std::size_t &size);
0241    static offset_t priv_page_offset_addr_fixup(offset_t page_offset, const void *&addr);
0242 
0243    template<int dummy>
0244    struct page_size_holder
0245    {
0246       static const std::size_t PageSize;
0247       static std::size_t get_page_size();
0248    };
0249 
0250    void*             m_base;
0251    std::size_t       m_size;
0252    std::size_t       m_page_offset;
0253    mode_t            m_mode;
0254    #if defined(BOOST_INTERPROCESS_WINDOWS)
0255    file_handle_t     m_file_or_mapping_hnd;
0256    #else
0257    bool              m_is_xsi;
0258    #endif
0259 
0260    friend class ipcdetail::interprocess_tester;
0261    friend class ipcdetail::raw_mapped_region_creator;
0262    void dont_close_on_destruction();
0263    #if defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
0264    template<int Dummy>
0265    static void destroy_syncs_in_range(const void *addr, std::size_t size);
0266    #endif
0267    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0268 };
0269 
0270 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0271 
0272 inline void swap(mapped_region &x, mapped_region &y) BOOST_NOEXCEPT
0273 {  x.swap(y);  }
0274 
0275 inline mapped_region::~mapped_region()
0276 {  this->priv_close(); }
0277 
0278 inline std::size_t mapped_region::get_size() const BOOST_NOEXCEPT
0279 {  return m_size; }
0280 
0281 inline mode_t mapped_region::get_mode() const BOOST_NOEXCEPT
0282 {  return m_mode;   }
0283 
0284 inline void*    mapped_region::get_address() const BOOST_NOEXCEPT
0285 {  return m_base; }
0286 
0287 inline void*    mapped_region::priv_map_address()  const
0288 {  return static_cast<char*>(m_base) - m_page_offset; }
0289 
0290 inline std::size_t mapped_region::priv_map_size()  const
0291 {  return m_size + m_page_offset; }
0292 
0293 inline bool mapped_region::priv_flush_param_check
0294    (std::size_t mapping_offset, void *&addr, std::size_t &numbytes) const
0295 {
0296    //Check some errors
0297    if(m_base == 0)
0298       return false;
0299 
0300    if(mapping_offset >= m_size || numbytes > (m_size - size_t(mapping_offset))){
0301       return false;
0302    }
0303 
0304    //Update flush size if the user does not provide it
0305    if(numbytes == 0){
0306       numbytes = m_size - mapping_offset;
0307    }
0308    addr = (char*)this->priv_map_address() + mapping_offset;
0309    numbytes += m_page_offset;
0310    return true;
0311 }
0312 
0313 inline bool mapped_region::priv_shrink_param_check
0314    (std::size_t bytes, bool from_back, void *&shrink_page_start, std::size_t &shrink_page_bytes)
0315 {
0316    //Check some errors
0317    if(m_base == 0 || bytes > m_size){
0318       return false;
0319    }
0320    else if(bytes == m_size){
0321       this->priv_close();
0322       return true;
0323    }
0324    else{
0325       const std::size_t page_size = mapped_region::get_page_size();
0326       if(from_back){
0327          const std::size_t new_pages = (m_size + m_page_offset - bytes - 1)/page_size + 1;
0328          shrink_page_start = static_cast<char*>(this->priv_map_address()) + new_pages*page_size;
0329          shrink_page_bytes = m_page_offset + m_size - new_pages*page_size;
0330          m_size -= bytes;
0331       }
0332       else{
0333          shrink_page_start = this->priv_map_address();
0334          m_page_offset += bytes;
0335          shrink_page_bytes = (m_page_offset/page_size)*page_size;
0336          m_page_offset = m_page_offset % page_size;
0337          m_size -= bytes;
0338          m_base  = static_cast<char *>(m_base) + bytes;
0339          BOOST_ASSERT(shrink_page_bytes%page_size == 0);
0340       }
0341       return true;
0342    }
0343 }
0344 
0345 inline void mapped_region::priv_size_from_mapping_size
0346    (offset_t mapping_size, offset_t offset, offset_t page_offset, std::size_t &size)
0347 {
0348    //Check if mapping size fits in the user address space
0349    //as offset_t is the maximum file size and it's signed.
0350    if(mapping_size < offset ||
0351       boost::uintmax_t(mapping_size - (offset - page_offset)) >
0352          boost::uintmax_t(std::size_t(-1))){
0353       error_info err(size_error);
0354       throw interprocess_exception(err);
0355    }
0356    size = static_cast<std::size_t>(mapping_size - offset);
0357 }
0358 
0359 inline offset_t mapped_region::priv_page_offset_addr_fixup(offset_t offset, const void *&address)
0360 {
0361    //We can't map any offset so we have to obtain system's
0362    //memory granularity
0363    const std::size_t page_size  = mapped_region::get_page_size();
0364 
0365    //We calculate the difference between demanded and valid offset
0366    //(always less than a page in std::size_t, thus, representable by std::size_t)
0367    const std::size_t page_offset =
0368       static_cast<std::size_t>(offset - (offset / offset_t(page_size)) * offset_t(page_size));
0369    //Update the mapping address
0370    if(address){
0371       address = static_cast<const char*>(address) - page_offset;
0372    }
0373    return offset_t(page_offset);
0374 }
0375 
0376 #if defined (BOOST_INTERPROCESS_WINDOWS)
0377 
0378 inline mapped_region::mapped_region() BOOST_NOEXCEPT
0379    :  m_base(0), m_size(0), m_page_offset(0), m_mode(read_only)
0380    ,  m_file_or_mapping_hnd(ipcdetail::invalid_file())
0381 {}
0382 
0383 template<int dummy>
0384 inline std::size_t mapped_region::page_size_holder<dummy>::get_page_size()
0385 {
0386    winapi::interprocess_system_info info;
0387    winapi::get_system_info(&info);
0388    return std::size_t(info.dwAllocationGranularity);
0389 }
0390 
0391 template<class MemoryMappable>
0392 inline mapped_region::mapped_region
0393    (const MemoryMappable &mapping
0394    ,mode_t mode
0395    ,offset_t offset
0396    ,std::size_t size
0397    ,const void *address
0398    ,map_options_t map_options)
0399    :  m_base(0), m_size(0), m_page_offset(0), m_mode(mode)
0400    ,  m_file_or_mapping_hnd(ipcdetail::invalid_file())
0401 {
0402    mapping_handle_t mhandle = mapping.get_mapping_handle();
0403    {
0404       file_handle_t native_mapping_handle = 0;
0405 
0406       //Set accesses
0407       //For "create_file_mapping"
0408       unsigned long protection = 0;
0409       //For "mapviewoffile"
0410       unsigned long map_access = map_options == default_map_options ? 0 : map_options;
0411 
0412       switch(mode)
0413       {
0414          case read_only:
0415          case read_private:
0416             protection   |= winapi::page_readonly;
0417             map_access   |= winapi::file_map_read;
0418          break;
0419          case read_write:
0420             protection   |= winapi::page_readwrite;
0421             map_access   |= winapi::file_map_write;
0422          break;
0423          case copy_on_write:
0424             protection   |= winapi::page_writecopy;
0425             map_access   |= winapi::file_map_copy;
0426          break;
0427          default:
0428             {
0429                error_info err(mode_error);
0430                throw interprocess_exception(err);
0431             }
0432          break;
0433       }
0434 
0435       //For file mapping (including emulated shared memory through temporary files),
0436       //the device is a file handle so we need to obtain file's size and call create_file_mapping
0437       //to obtain the mapping handle.
0438       //For files we don't need the file mapping after mapping the memory, as the file is there
0439       //so we'll program the handle close
0440       void * handle_to_close = winapi::invalid_handle_value;
0441       if(!mhandle.is_shm){
0442          //Create mapping handle
0443          native_mapping_handle = winapi::create_file_mapping
0444             ( ipcdetail::file_handle_from_mapping_handle(mapping.get_mapping_handle())
0445             , protection, 0, (char*)0, 0);
0446 
0447          //Check if all is correct
0448          if(!native_mapping_handle){
0449             error_info err ((int)winapi::get_last_error());
0450             throw interprocess_exception(err);
0451          }
0452          handle_to_close = native_mapping_handle;
0453       }
0454       else{
0455          //For windows_shared_memory the device handle is already a mapping handle
0456          //and we need to maintain it
0457          native_mapping_handle = mhandle.handle;
0458       }
0459       //RAII handle close on scope exit
0460       const winapi::handle_closer close_handle(handle_to_close);
0461       (void)close_handle;
0462 
0463       const offset_t page_offset = priv_page_offset_addr_fixup(offset, address);
0464 
0465       //Obtain mapping size if user provides 0 size
0466       if(size == 0){
0467          offset_t mapping_size;
0468          if(!winapi::get_file_mapping_size(native_mapping_handle, mapping_size)){
0469             error_info err((int)winapi::get_last_error());
0470             throw interprocess_exception(err);
0471          }
0472          //This can throw
0473          priv_size_from_mapping_size(mapping_size, offset, page_offset, size);
0474       }
0475 
0476       //Map with new offsets and size
0477       void *base = winapi::map_view_of_file_ex
0478                                  (native_mapping_handle,
0479                                  map_access,
0480                                  ::boost::ulong_long_type(offset - page_offset),
0481                                  static_cast<std::size_t>(page_offset) + size,
0482                                  const_cast<void*>(address));
0483       //Check error
0484       if(!base){
0485          error_info err((int)winapi::get_last_error());
0486          throw interprocess_exception(err);
0487       }
0488 
0489       //Calculate new base for the user
0490       m_base = static_cast<char*>(base) + page_offset;
0491       m_page_offset = static_cast<std::size_t>(page_offset);
0492       m_size = size;
0493    }
0494    //Windows shared memory needs the duplication of the handle if we want to
0495    //make mapped_region independent from the mappable device
0496    //
0497    //For mapped files, we duplicate the file handle to be able to FlushFileBuffers
0498    if(!winapi::duplicate_current_process_handle(mhandle.handle, &m_file_or_mapping_hnd)){
0499       error_info err((int)winapi::get_last_error());
0500       this->priv_close();
0501       throw interprocess_exception(err);
0502    }
0503 }
0504 
0505 inline bool mapped_region::flush(std::size_t mapping_offset, std::size_t numbytes, bool async)
0506 {
0507    void *addr;
0508    if(!this->priv_flush_param_check(mapping_offset, addr, numbytes)){
0509       return false;
0510    }
0511    //Flush it all
0512    if(!winapi::flush_view_of_file(addr, numbytes)){
0513       return false;
0514    }
0515    //m_file_or_mapping_hnd can be a file handle or a mapping handle.
0516    //so flushing file buffers has only sense for files...
0517    else if(!async && m_file_or_mapping_hnd != winapi::invalid_handle_value &&
0518            winapi::get_file_type(m_file_or_mapping_hnd) == winapi::file_type_disk){
0519       return winapi::flush_file_buffers(m_file_or_mapping_hnd);
0520    }
0521    return true;
0522 }
0523 
0524 inline bool mapped_region::shrink_by(std::size_t bytes, bool from_back)
0525 {
0526    void *shrink_page_start = 0;
0527    std::size_t shrink_page_bytes = 0;
0528    if(!this->priv_shrink_param_check(bytes, from_back, shrink_page_start, shrink_page_bytes)){
0529       return false;
0530    }
0531    else if(shrink_page_bytes){
0532       //In Windows, we can't decommit the storage or release the virtual address space,
0533       //the best we can do is try to remove some memory from the process working set.
0534       //With a bit of luck we can free some physical memory.
0535       unsigned long old_protect_ignored;
0536       bool b_ret = winapi::virtual_unlock(shrink_page_start, shrink_page_bytes)
0537                            || (winapi::get_last_error() == winapi::error_not_locked);
0538       (void)old_protect_ignored;
0539       //Change page protection to forbid any further access
0540       b_ret = b_ret && winapi::virtual_protect
0541          (shrink_page_start, shrink_page_bytes, winapi::page_noaccess, old_protect_ignored);
0542       return b_ret;
0543    }
0544    else{
0545       return true;
0546    }
0547 }
0548 
0549 inline bool mapped_region::advise(advice_types)
0550 {
0551    //Windows has no madvise/posix_madvise equivalent
0552    return false;
0553 }
0554 
0555 inline void mapped_region::priv_close()
0556 {
0557    if(m_base){
0558       void *addr = this->priv_map_address();
0559       #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
0560       mapped_region::destroy_syncs_in_range<0>(addr, m_size);
0561       #endif
0562       winapi::unmap_view_of_file(addr);
0563       m_base = 0;
0564    }
0565    if(m_file_or_mapping_hnd != ipcdetail::invalid_file()){
0566       winapi::close_handle(m_file_or_mapping_hnd);
0567       m_file_or_mapping_hnd = ipcdetail::invalid_file();
0568    }
0569 }
0570 
0571 inline void mapped_region::dont_close_on_destruction()
0572 {}
0573 
0574 #else    //#if defined (BOOST_INTERPROCESS_WINDOWS)
0575 
0576 inline mapped_region::mapped_region() BOOST_NOEXCEPT
0577    :  m_base(0), m_size(0), m_page_offset(0), m_mode(read_only), m_is_xsi(false)
0578 {}
0579 
0580 template<int dummy>
0581 inline std::size_t mapped_region::page_size_holder<dummy>::get_page_size()
0582 {  return std::size_t(sysconf(_SC_PAGESIZE)); }
0583 
0584 template<class MemoryMappable>
0585 inline mapped_region::mapped_region
0586    ( const MemoryMappable &mapping
0587    , mode_t mode
0588    , offset_t offset
0589    , std::size_t size
0590    , const void *address
0591    , map_options_t map_options)
0592    : m_base(0), m_size(0), m_page_offset(0), m_mode(mode), m_is_xsi(false)
0593 {
0594    mapping_handle_t map_hnd = mapping.get_mapping_handle();
0595 
0596    //Some systems dont' support XSI shared memory
0597    #ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
0598    if(map_hnd.is_xsi){
0599       //Get the size
0600       ::shmid_ds xsi_ds;
0601       int ret = ::shmctl(map_hnd.handle, IPC_STAT, &xsi_ds);
0602       if(ret == -1){
0603          error_info err(system_error_code());
0604          throw interprocess_exception(err);
0605       }
0606       //Compare sizess
0607       if(size == 0){
0608          size = (std::size_t)xsi_ds.shm_segsz;
0609       }
0610       else if(size != (std::size_t)xsi_ds.shm_segsz){
0611          error_info err(size_error);
0612          throw interprocess_exception(err);
0613       }
0614       //Calculate flag
0615       int flag = map_options == default_map_options ? 0 : map_options;
0616       if(m_mode == read_only){
0617          flag |= SHM_RDONLY;
0618       }
0619       else if(m_mode != read_write){
0620          error_info err(mode_error);
0621          throw interprocess_exception(err);
0622       }
0623       //Attach memory
0624       //Some old shmat implementation take the address as a non-const void pointer
0625       //so uncast it to make code portable.
0626       void *const final_address = const_cast<void *>(address);
0627       void *base = ::shmat(map_hnd.handle, final_address, flag);
0628       if(base == (void*)-1){
0629          error_info err(system_error_code());
0630          throw interprocess_exception(err);
0631       }
0632       //Update members
0633       m_base   = base;
0634       m_size   = size;
0635       m_mode   = mode;
0636       m_page_offset = 0;
0637       m_is_xsi = true;
0638       return;
0639    }
0640    #endif   //ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
0641 
0642    //We calculate the difference between demanded and valid offset
0643    const offset_t page_offset = priv_page_offset_addr_fixup(offset, address);
0644 
0645    if(size == 0){
0646       struct ::stat buf;
0647       if(0 != fstat(map_hnd.handle, &buf)){
0648          error_info err(system_error_code());
0649          throw interprocess_exception(err);
0650       }
0651       //This can throw
0652       priv_size_from_mapping_size(buf.st_size, offset, page_offset, size);
0653    }
0654 
0655    #ifdef MAP_NOSYNC
0656       #define BOOST_INTERPROCESS_MAP_NOSYNC MAP_NOSYNC
0657    #else
0658       #define BOOST_INTERPROCESS_MAP_NOSYNC 0
0659    #endif   //MAP_NOSYNC
0660 
0661    //Create new mapping
0662    int prot    = 0;
0663    int flags   = map_options == default_map_options ? BOOST_INTERPROCESS_MAP_NOSYNC : map_options;
0664 
0665    #undef BOOST_INTERPROCESS_MAP_NOSYNC
0666 
0667    switch(mode)
0668    {
0669       case read_only:
0670          prot  |= PROT_READ;
0671          flags |= MAP_SHARED;
0672       break;
0673 
0674       case read_private:
0675          prot  |= (PROT_READ);
0676          flags |= MAP_PRIVATE;
0677       break;
0678 
0679       case read_write:
0680          prot  |= (PROT_WRITE | PROT_READ);
0681          flags |= MAP_SHARED;
0682       break;
0683 
0684       case copy_on_write:
0685          prot  |= (PROT_WRITE | PROT_READ);
0686          flags |= MAP_PRIVATE;
0687       break;
0688 
0689       default:
0690          {
0691             error_info err(mode_error);
0692             throw interprocess_exception(err);
0693          }
0694       break;
0695    }
0696 
0697    //Map it to the address space
0698    void* base = mmap ( const_cast<void*>(address)
0699                      , static_cast<std::size_t>(page_offset) + size
0700                      , prot
0701                      , flags
0702                      , mapping.get_mapping_handle().handle
0703                      , offset - page_offset);
0704 
0705    //Check if mapping was successful
0706    if(base == MAP_FAILED){
0707       error_info err = system_error_code();
0708       throw interprocess_exception(err);
0709    }
0710 
0711    //Calculate new base for the user
0712    m_base = static_cast<char*>(base) + page_offset;
0713    m_page_offset = static_cast<std::size_t>(page_offset);
0714    m_size   = size;
0715 
0716    //Check for fixed mapping error
0717    if(address && (base != address)){
0718       error_info err(busy_error);
0719       this->priv_close();
0720       throw interprocess_exception(err);
0721    }
0722 }
0723 
0724 inline bool mapped_region::shrink_by(std::size_t bytes, bool from_back)
0725 {
0726    void *shrink_page_start = 0;
0727    std::size_t shrink_page_bytes = 0;
0728    if(m_is_xsi || !this->priv_shrink_param_check(bytes, from_back, shrink_page_start, shrink_page_bytes)){
0729       return false;
0730    }
0731    else if(shrink_page_bytes){
0732       //In UNIX we can decommit and free virtual address space.
0733       return 0 == munmap(shrink_page_start, shrink_page_bytes);
0734    }
0735    else{
0736       return true;
0737    }
0738 }
0739 
0740 inline bool mapped_region::flush(std::size_t mapping_offset, std::size_t numbytes, bool async)
0741 {
0742    void *addr;
0743    if(m_is_xsi || !this->priv_flush_param_check(mapping_offset, addr, numbytes)){
0744       return false;
0745    }
0746    //Flush it all
0747    return msync(addr, numbytes, async ? MS_ASYNC : MS_SYNC) == 0;
0748 }
0749 
0750 inline bool mapped_region::advise(advice_types advice)
0751 {
0752    int unix_advice = 0;
0753    //Modes; 0: none, 2: posix, 1: madvise
0754    const unsigned int mode_none = 0;
0755    const unsigned int mode_padv = 1;
0756    const unsigned int mode_madv = 2;
0757    // Suppress "unused variable" warnings
0758    (void)mode_padv;
0759    (void)mode_madv;
0760    unsigned int mode = mode_none;
0761    //Choose advice either from POSIX (preferred) or native Unix
0762    switch(advice){
0763       case advice_normal:
0764          #if defined(POSIX_MADV_NORMAL)
0765          unix_advice = POSIX_MADV_NORMAL;
0766          mode = mode_padv;
0767          #elif defined(MADV_NORMAL)
0768          unix_advice = MADV_NORMAL;
0769          mode = mode_madv;
0770          #endif
0771       break;
0772       case advice_sequential:
0773          #if defined(POSIX_MADV_SEQUENTIAL)
0774          unix_advice = POSIX_MADV_SEQUENTIAL;
0775          mode = mode_padv;
0776          #elif defined(MADV_SEQUENTIAL)
0777          unix_advice = MADV_SEQUENTIAL;
0778          mode = mode_madv;
0779          #endif
0780       break;
0781       case advice_random:
0782          #if defined(POSIX_MADV_RANDOM)
0783          unix_advice = POSIX_MADV_RANDOM;
0784          mode = mode_padv;
0785          #elif defined(MADV_RANDOM)
0786          unix_advice = MADV_RANDOM;
0787          mode = mode_madv;
0788          #endif
0789       break;
0790       case advice_willneed:
0791          #if defined(POSIX_MADV_WILLNEED)
0792          unix_advice = POSIX_MADV_WILLNEED;
0793          mode = mode_padv;
0794          #elif defined(MADV_WILLNEED)
0795          unix_advice = MADV_WILLNEED;
0796          mode = mode_madv;
0797          #endif
0798       break;
0799       case advice_dontneed:
0800          #if defined(POSIX_MADV_DONTNEED)
0801          unix_advice = POSIX_MADV_DONTNEED;
0802          mode = mode_padv;
0803          #elif defined(MADV_DONTNEED) && defined(BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS)
0804          unix_advice = MADV_DONTNEED;
0805          mode = mode_madv;
0806          #endif
0807       break;
0808       default:
0809       return false;
0810    }
0811    int ret = -1;
0812    switch(mode){
0813       #if defined(POSIX_MADV_NORMAL)
0814          case mode_padv:
0815          {
0816          ret = posix_madvise(this->priv_map_address(), this->priv_map_size(), unix_advice);
0817          #ifdef __CYGWIN__
0818          //Cygwin returns EINVAL in some valid use cases due to DiscardVirtualMemory limitations
0819          if (ret == EINVAL)
0820             ret = 0;
0821          #endif
0822          return 0 == ret;
0823          }
0824       #endif
0825       #if defined(MADV_NORMAL)
0826          case mode_madv:
0827          return 0 == madvise(
0828             #if defined(BOOST_INTERPROCESS_MADVISE_USES_CADDR_T)
0829             (caddr_t)
0830             #endif
0831             this->priv_map_address(), this->priv_map_size(), unix_advice);
0832       #endif
0833       default:
0834       return false;
0835    }
0836 }
0837 
0838 inline void mapped_region::priv_close()
0839 {
0840    if(m_base != 0){
0841       #ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
0842       if(m_is_xsi){
0843          int ret = ::shmdt(m_base);
0844          BOOST_ASSERT(ret == 0);
0845          (void)ret;
0846          return;
0847       }
0848       #endif //#ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
0849       munmap(this->priv_map_address(), this->priv_map_size());
0850       m_base = 0;
0851    }
0852 }
0853 
0854 inline void mapped_region::dont_close_on_destruction()
0855 {  m_base = 0;   }
0856 
0857 #endif   //#if defined (BOOST_INTERPROCESS_WINDOWS)
0858 
0859 template<int dummy>
0860 const std::size_t mapped_region::page_size_holder<dummy>::PageSize
0861    = mapped_region::page_size_holder<dummy>::get_page_size();
0862 
0863 inline std::size_t mapped_region::get_page_size() BOOST_NOEXCEPT
0864 {
0865    if(!page_size_holder<0>::PageSize)
0866       return page_size_holder<0>::get_page_size();
0867    else
0868       return page_size_holder<0>::PageSize;
0869 }
0870 
0871 inline void mapped_region::swap(mapped_region &other) BOOST_NOEXCEPT
0872 {
0873    ::boost::adl_move_swap(this->m_base, other.m_base);
0874    ::boost::adl_move_swap(this->m_size, other.m_size);
0875    ::boost::adl_move_swap(this->m_page_offset, other.m_page_offset);
0876    ::boost::adl_move_swap(this->m_mode,  other.m_mode);
0877    #if defined (BOOST_INTERPROCESS_WINDOWS)
0878    ::boost::adl_move_swap(this->m_file_or_mapping_hnd, other.m_file_or_mapping_hnd);
0879    #else
0880    ::boost::adl_move_swap(this->m_is_xsi, other.m_is_xsi);
0881    #endif
0882 }
0883 
0884 //!No-op functor
0885 struct null_mapped_region_function
0886 {
0887    bool operator()(void *, std::size_t , bool) const
0888       {   return true;   }
0889 
0890    static std::size_t get_min_size()
0891    {  return 0;  }
0892 };
0893 
0894 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0895 
0896 }  //namespace interprocess {
0897 }  //namespace boost {
0898 
0899 #include <boost/interprocess/detail/config_end.hpp>
0900 
0901 #endif   //BOOST_INTERPROCESS_MAPPED_REGION_HPP
0902 
0903 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0904 
0905 #ifndef BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP
0906 #define BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP
0907 
0908 #if defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
0909 #  include <boost/interprocess/sync/windows/sync_utils.hpp>
0910 #  include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
0911 
0912 namespace boost {
0913 namespace interprocess {
0914 
0915 template<int Dummy>
0916 inline void mapped_region::destroy_syncs_in_range(const void *addr, std::size_t size)
0917 {
0918    ipcdetail::sync_handles &handles =
0919       ipcdetail::windows_intermodule_singleton<ipcdetail::sync_handles>::get();
0920    handles.destroy_syncs_in_range(addr, size);
0921 }
0922 
0923 }  //namespace interprocess {
0924 }  //namespace boost {
0925 
0926 #endif   //defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
0927 
0928 #endif   //#ifdef BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP
0929 
0930 #endif   //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0931