1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/creation_tags.hpp>
25 #include <boost/move/move.hpp>
26 #include <boost/interprocess/interprocess_fwd.hpp>
27 #include <boost/interprocess/mapped_region.hpp>
30 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
31 # include <fcntl.h> //open, O_CREAT, O_*...
32 # include <sys/mman.h> //mmap
33 # include <sys/stat.h> //mode_t, S_IRWXG, S_IRWXO, S_IRWXU,
35 #include <boost/interprocess/windows_shared_memory.hpp>
40 //!Describes a function that creates anonymous shared memory that can be
41 //!shared between forked processes
44 namespace interprocess {
46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
50 class raw_mapped_region_creator
54 create_posix_mapped_region(void *address, std::size_t size)
57 region.m_base = address;
64 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
66 //!A function that creates an anonymous shared memory segment of size "size".
67 //!If "address" is passed the function will try to map the segment in that address.
68 //!Otherwise the operating system will choose the mapping address.
69 //!The function returns a mapped_region holding that segment or throws
70 //!interprocess_exception if the function fails.
71 //static mapped_region
73 anonymous_shared_memory(std::size_t size, void *address = 0)
74 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
79 #if defined(MAP_ANONYMOUS) //Use MAP_ANONYMOUS
80 flags = MAP_ANONYMOUS | MAP_SHARED;
81 #elif !defined(MAP_ANONYMOUS) && defined(MAP_ANON) //use MAP_ANON
82 flags = MAP_ANON | MAP_SHARED;
83 #else // Use "/dev/zero"
84 fd = open("/dev/zero", O_RDWR);
87 error_info err = system_error_code();
88 throw interprocess_exception(err);
93 address = mmap( address
95 , PROT_READ|PROT_WRITE
100 if(address == MAP_FAILED){
103 error_info err = system_error_code();
104 throw interprocess_exception(err);
110 return ipcdetail::raw_mapped_region_creator::create_posix_mapped_region(address, size);
114 windows_shared_memory anonymous_mapping(create_only, 0, read_write, size);
115 return mapped_region(anonymous_mapping, read_write, 0, size, address);
120 } //namespace interprocess {
121 } //namespace boost {
123 #include <boost/interprocess/detail/config_end.hpp>
125 #endif //BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP