• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
13 
14 #ifndef BOOST_CONFIG_HPP
15 #  include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 #  pragma once
20 #endif
21 
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>
28 #include <cstddef>
29 
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,
34 #else
35 #include <boost/interprocess/windows_shared_memory.hpp>
36 #endif
37 
38 
39 //!\file
40 //!Describes a function that creates anonymous shared memory that can be
41 //!shared between forked processes
42 
43 namespace boost {
44 namespace interprocess {
45 
46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
47 
48 namespace ipcdetail{
49 
50    class raw_mapped_region_creator
51    {
52       public:
53       static mapped_region
create_posix_mapped_region(void * address,std::size_t size)54          create_posix_mapped_region(void *address, std::size_t size)
55       {
56          mapped_region region;
57          region.m_base = address;
58          region.m_size = size;
59          return region;
60       }
61    };
62 }
63 
64 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
65 
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
72 static mapped_region
anonymous_shared_memory(std::size_t size,void * address=0)73 anonymous_shared_memory(std::size_t size, void *address = 0)
74 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
75 {
76    int flags;
77    int fd = -1;
78 
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);
85    flags = MAP_SHARED;
86    if(fd == -1){
87       error_info err = system_error_code();
88       throw interprocess_exception(err);
89    }
90    #endif
91 
92 
93    address = mmap( address
94                   , size
95                   , PROT_READ|PROT_WRITE
96                   , flags
97                   , fd
98                   , 0);
99 
100    if(address == MAP_FAILED){
101       if(fd != -1)
102          close(fd);
103       error_info err = system_error_code();
104       throw interprocess_exception(err);
105    }
106 
107    if(fd != -1)
108       close(fd);
109 
110    return ipcdetail::raw_mapped_region_creator::create_posix_mapped_region(address, size);
111 }
112 #else
113 {
114    windows_shared_memory anonymous_mapping(create_only, 0, read_write, size);
115    return mapped_region(anonymous_mapping, read_write, 0, size, address);
116 }
117 
118 #endif
119 
120 }  //namespace interprocess {
121 }  //namespace boost {
122 
123 #include <boost/interprocess/detail/config_end.hpp>
124 
125 #endif   //BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
126