File indexing completed on 2025-01-30 09:44:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
0012 #define BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 # pragma once
0020 #endif
0021
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 #include <boost/interprocess/creation_tags.hpp>
0025 #include <boost/move/move.hpp>
0026 #include <boost/interprocess/interprocess_fwd.hpp>
0027 #include <boost/interprocess/mapped_region.hpp>
0028 #include <cstddef>
0029
0030 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
0031 # include <fcntl.h> //open, O_CREAT, O_*...
0032 # include <sys/mman.h> //mmap
0033 # include <sys/stat.h> //mode_t, S_IRWXG, S_IRWXO, S_IRWXU,
0034 #else
0035 #include <boost/interprocess/windows_shared_memory.hpp>
0036 #endif
0037
0038
0039
0040
0041
0042
0043 namespace boost {
0044 namespace interprocess {
0045
0046 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0047
0048 namespace ipcdetail{
0049
0050 class raw_mapped_region_creator
0051 {
0052 public:
0053 static mapped_region
0054 create_posix_mapped_region(void *address, std::size_t size)
0055 {
0056 mapped_region region;
0057 region.m_base = address;
0058 region.m_size = size;
0059 return region;
0060 }
0061 };
0062 }
0063
0064 #endif
0065
0066
0067
0068
0069
0070
0071
0072 static mapped_region
0073 anonymous_shared_memory(std::size_t size, void *address = 0)
0074 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
0075 {
0076 int flags;
0077 int fd = -1;
0078
0079 #if defined(MAP_ANONYMOUS)
0080 flags = MAP_ANONYMOUS | MAP_SHARED;
0081 #elif !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
0082 flags = MAP_ANON | MAP_SHARED;
0083 #else
0084 fd = open("/dev/zero", O_RDWR);
0085 flags = MAP_SHARED;
0086 if(fd == -1){
0087 error_info err = system_error_code();
0088 throw interprocess_exception(err);
0089 }
0090 #endif
0091
0092
0093 address = mmap( address
0094 , size
0095 , PROT_READ|PROT_WRITE
0096 , flags
0097 , fd
0098 , 0);
0099
0100 if(address == MAP_FAILED){
0101 if(fd != -1)
0102 close(fd);
0103 error_info err = system_error_code();
0104 throw interprocess_exception(err);
0105 }
0106
0107 if(fd != -1)
0108 close(fd);
0109
0110 return ipcdetail::raw_mapped_region_creator::create_posix_mapped_region(address, size);
0111 }
0112 #else
0113 {
0114 windows_shared_memory anonymous_mapping(create_only, (char*)0, read_write, size);
0115 return mapped_region(anonymous_mapping, read_write, 0, size, address);
0116 }
0117
0118 #endif
0119
0120 }
0121 }
0122
0123 #include <boost/interprocess/detail/config_end.hpp>
0124
0125 #endif