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_ANONYMOUS_SHARED_MEMORY_HPP Chris@16: #define BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_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: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if (!defined(BOOST_INTERPROCESS_WINDOWS)) Chris@16: # include //open, O_CREAT, O_*... Chris@16: # include //mmap Chris@16: # include //mode_t, S_IRWXG, S_IRWXO, S_IRWXU, Chris@16: #else Chris@16: #include Chris@16: #endif Chris@16: Chris@16: Chris@16: //!\file Chris@16: //!Describes a function that creates anonymous shared memory that can be Chris@16: //!shared between forked processes Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: namespace ipcdetail{ Chris@16: Chris@16: class raw_mapped_region_creator Chris@16: { Chris@16: public: Chris@16: static mapped_region Chris@16: create_posix_mapped_region(void *address, std::size_t size) Chris@16: { Chris@16: mapped_region region; Chris@16: region.m_base = address; Chris@16: region.m_size = size; Chris@16: return region; Chris@16: } Chris@16: }; Chris@16: } Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //!A function that creates an anonymous shared memory segment of size "size". Chris@16: //!If "address" is passed the function will try to map the segment in that address. Chris@16: //!Otherwise the operating system will choose the mapping address. Chris@16: //!The function returns a mapped_region holding that segment or throws Chris@16: //!interprocess_exception if the function fails. Chris@16: //static mapped_region Chris@16: static mapped_region Chris@16: anonymous_shared_memory(std::size_t size, void *address = 0) Chris@16: #if (!defined(BOOST_INTERPROCESS_WINDOWS)) Chris@16: { Chris@16: int flags; Chris@16: int fd = -1; Chris@16: Chris@16: #if defined(MAP_ANONYMOUS) //Use MAP_ANONYMOUS Chris@16: flags = MAP_ANONYMOUS | MAP_SHARED; Chris@16: #elif !defined(MAP_ANONYMOUS) && defined(MAP_ANON) //use MAP_ANON Chris@16: flags = MAP_ANON | MAP_SHARED; Chris@16: #else // Use "/dev/zero" Chris@16: fd = open("/dev/zero", O_RDWR); Chris@16: flags = MAP_SHARED; Chris@16: if(fd == -1){ Chris@16: error_info err = system_error_code(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: #endif Chris@16: Chris@16: Chris@16: address = mmap( address Chris@16: , size Chris@16: , PROT_READ|PROT_WRITE Chris@16: , flags Chris@16: , fd Chris@16: , 0); Chris@16: Chris@16: if(address == MAP_FAILED){ Chris@16: if(fd != -1) Chris@16: close(fd); Chris@16: error_info err = system_error_code(); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: Chris@16: if(fd != -1) Chris@16: close(fd); Chris@16: Chris@16: return ipcdetail::raw_mapped_region_creator::create_posix_mapped_region(address, size); Chris@16: } Chris@16: #else Chris@16: { Chris@16: windows_shared_memory anonymous_mapping(create_only, 0, read_write, size); Chris@16: return mapped_region(anonymous_mapping, read_write, 0, size, address); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP