Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-21 08:49:06

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2009-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
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 // move
0041 #include <boost/move/utility_core.hpp>
0042 // other boost
0043 #include <boost/cstdint.hpp>
0044 // std
0045 #include <cstddef>
0046 // OS
0047 #include <sys/shm.h>
0048 
0049 
0050 //!\file
0051 //!Describes a class representing a native xsi shared memory.
0052 
0053 namespace boost {
0054 namespace interprocess {
0055 
0056 //!A class that wraps XSI (System V) shared memory.
0057 //!Unlike shared_memory_object, xsi_shared_memory needs a valid
0058 //!xsi_key to identify a shared memory object.
0059 //!
0060 //!Warning: XSI shared memory and interprocess portable
0061 //!shared memory (boost::interprocess::shared_memory_object)
0062 //!can't communicate between them.
0063 class xsi_shared_memory
0064 {
0065    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0066    //Non-copyable and non-assignable
0067    BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_shared_memory)
0068    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0069 
0070    public:
0071    //!Default constructor.
0072    //!Represents an empty xsi_shared_memory.
0073    xsi_shared_memory() BOOST_NOEXCEPT;
0074 
0075    //!Initializes *this with a shmid previously obtained (possibly from another process)
0076    //!This lower-level initializer allows shared memory mapping without having a key.
0077    xsi_shared_memory(open_only_t, int shmid)
0078       : m_shmid (shmid)
0079    {}
0080 
0081    //!Creates a new XSI shared memory from 'key', with size "size" and permissions "perm".
0082    //!If the shared memory previously exists, throws an error.
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    //!Opens an existing shared memory with identifier 'key' or creates a new XSI shared memory from
0087    //!identifier 'key', with size "size" and permissions "perm".
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    //!Tries to open a XSI shared memory with identifier 'key'
0092    //!If the shared memory does not previously exist, it throws an error.
0093    xsi_shared_memory(open_only_t, const xsi_key &key)
0094    {  this->priv_open_or_create(ipcdetail::DoOpen, key, permissions(), 0);  }
0095 
0096    //!Moves the ownership of "moved"'s shared memory object to *this.
0097    //!After the call, "moved" does not represent any shared memory object.
0098    //!Does not throw
0099    xsi_shared_memory(BOOST_RV_REF(xsi_shared_memory) moved) BOOST_NOEXCEPT
0100       : m_shmid(-1)
0101    {  this->swap(moved);   }
0102 
0103    //!Moves the ownership of "moved"'s shared memory to *this.
0104    //!After the call, "moved" does not represent any shared memory.
0105    //!Does not throw
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    //!Swaps two xsi_shared_memorys. Does not throw
0114    void swap(xsi_shared_memory &other) BOOST_NOEXCEPT;
0115 
0116    //!Destroys *this. The shared memory won't be destroyed, just
0117    //!this connection to it. Use remove() to destroy the shared memory.
0118    ~xsi_shared_memory();
0119 
0120    //!Returns the shared memory ID that
0121    //!identifies the shared memory
0122    int get_shmid() const BOOST_NOEXCEPT;
0123 
0124    //!Returns the mapping handle.
0125    //!Never throws
0126    mapping_handle_t get_mapping_handle() const BOOST_NOEXCEPT;
0127 
0128    //!Erases the XSI shared memory object identified by shmid
0129    //!from the system.
0130    //!Returns false on error. Never throws
0131    static bool remove(int shmid);
0132 
0133    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0134    private:
0135 
0136    //!Closes a previously opened file mapping. Never throws.
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   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
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       //Now get the size
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   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0210 
0211 }  //namespace interprocess {
0212 }  //namespace boost {
0213 
0214 #include <boost/interprocess/detail/config_end.hpp>
0215 
0216 #endif   //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_HPP