File indexing completed on 2026-08-21 08:49:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP
0012 #define BOOST_INTERPROCESS_XSI_SHARED_MEMORY_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 #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
0027 #error "This header can't be used in operating systems without XSI (System V) shared memory support"
0028 #endif
0029
0030 #include <boost/interprocess/creation_tags.hpp>
0031 #include <boost/interprocess/exceptions.hpp>
0032 #include <boost/interprocess/detail/utilities.hpp>
0033
0034 #include <boost/interprocess/detail/os_file_functions.hpp>
0035 #include <boost/interprocess/interprocess_fwd.hpp>
0036 #include <boost/interprocess/exceptions.hpp>
0037 #include <boost/interprocess/xsi_key.hpp>
0038 #include <boost/interprocess/permissions.hpp>
0039 #include <boost/interprocess/detail/simple_swap.hpp>
0040
0041 #include <boost/move/utility_core.hpp>
0042
0043 #include <boost/cstdint.hpp>
0044
0045 #include <cstddef>
0046
0047 #include <sys/shm.h>
0048
0049
0050
0051
0052
0053 namespace boost {
0054 namespace interprocess {
0055
0056
0057
0058
0059
0060
0061
0062
0063 class xsi_shared_memory
0064 {
0065 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0066
0067 BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_shared_memory)
0068 #endif
0069
0070 public:
0071
0072
0073 xsi_shared_memory() BOOST_NOEXCEPT;
0074
0075
0076
0077 xsi_shared_memory(open_only_t, int shmid)
0078 : m_shmid (shmid)
0079 {}
0080
0081
0082
0083 xsi_shared_memory(create_only_t, const xsi_key &key, std::size_t size, const permissions& perm = permissions())
0084 { this->priv_open_or_create(ipcdetail::DoCreate, key, perm, size); }
0085
0086
0087
0088 xsi_shared_memory(open_or_create_t, const xsi_key &key, std::size_t size, const permissions& perm = permissions())
0089 { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, key, perm, size); }
0090
0091
0092
0093 xsi_shared_memory(open_only_t, const xsi_key &key)
0094 { this->priv_open_or_create(ipcdetail::DoOpen, key, permissions(), 0); }
0095
0096
0097
0098
0099 xsi_shared_memory(BOOST_RV_REF(xsi_shared_memory) moved) BOOST_NOEXCEPT
0100 : m_shmid(-1)
0101 { this->swap(moved); }
0102
0103
0104
0105
0106 xsi_shared_memory &operator=(BOOST_RV_REF(xsi_shared_memory) moved) BOOST_NOEXCEPT
0107 {
0108 xsi_shared_memory tmp(boost::move(moved));
0109 this->swap(tmp);
0110 return *this;
0111 }
0112
0113
0114 void swap(xsi_shared_memory &other) BOOST_NOEXCEPT;
0115
0116
0117
0118 ~xsi_shared_memory();
0119
0120
0121
0122 int get_shmid() const BOOST_NOEXCEPT;
0123
0124
0125
0126 mapping_handle_t get_mapping_handle() const BOOST_NOEXCEPT;
0127
0128
0129
0130
0131 static bool remove(int shmid);
0132
0133 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0134 private:
0135
0136
0137 bool priv_open_or_create( ipcdetail::create_enum_t type
0138 , const xsi_key &key
0139 , const permissions& perm
0140 , std::size_t size);
0141 int m_shmid;
0142 #endif
0143 };
0144
0145 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0146
0147 inline xsi_shared_memory::xsi_shared_memory() BOOST_NOEXCEPT
0148 : m_shmid(-1)
0149 {}
0150
0151 inline xsi_shared_memory::~xsi_shared_memory()
0152 {}
0153
0154 inline int xsi_shared_memory::get_shmid() const BOOST_NOEXCEPT
0155 { return m_shmid; }
0156
0157 inline void xsi_shared_memory::swap(xsi_shared_memory &other) BOOST_NOEXCEPT
0158 {
0159 (simple_swap)(m_shmid, other.m_shmid);
0160 }
0161
0162 inline mapping_handle_t xsi_shared_memory::get_mapping_handle() const BOOST_NOEXCEPT
0163 { mapping_handle_t mhnd = { m_shmid, true}; return mhnd; }
0164
0165 inline bool xsi_shared_memory::priv_open_or_create
0166 (ipcdetail::create_enum_t type, const xsi_key &key, const permissions& permissions, std::size_t size)
0167 {
0168 int perm = (int)permissions.get_permissions();
0169 perm &= 0x01FF;
0170 int shmflg = perm;
0171
0172 switch(type){
0173 case ipcdetail::DoOpen:
0174 shmflg |= 0;
0175 break;
0176 case ipcdetail::DoCreate:
0177 shmflg |= IPC_CREAT | IPC_EXCL;
0178 break;
0179 case ipcdetail::DoOpenOrCreate:
0180 shmflg |= IPC_CREAT;
0181 break;
0182 default:
0183 {
0184 error_info err = other_error;
0185 throw interprocess_exception(err);
0186 }
0187 }
0188
0189 int ret = ::shmget(key.get_key(), size, shmflg);
0190 int shmid = ret;
0191 if((type == ipcdetail::DoOpen) && (-1 != ret)){
0192
0193 ::shmid_ds xsi_ds;
0194 ret = ::shmctl(ret, IPC_STAT, &xsi_ds);
0195 size = xsi_ds.shm_segsz;
0196 }
0197 if(-1 == ret){
0198 error_info err = system_error_code();
0199 throw interprocess_exception(err);
0200 }
0201
0202 m_shmid = shmid;
0203 return true;
0204 }
0205
0206 inline bool xsi_shared_memory::remove(int shmid)
0207 { return -1 != ::shmctl(shmid, IPC_RMID, 0); }
0208
0209 #endif
0210
0211 }
0212 }
0213
0214 #include <boost/interprocess/detail/config_end.hpp>
0215
0216 #endif