Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:44:11

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-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_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 //!\file
0040 //!Describes a function that creates anonymous shared memory that can be
0041 //!shared between forked processes
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   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0065 
0066 //!A function that creates an anonymous shared memory segment of size "size".
0067 //!If "address" is passed the function will try to map the segment in that address.
0068 //!Otherwise the operating system will choose the mapping address.
0069 //!The function returns a mapped_region holding that segment or throws
0070 //!interprocess_exception if the function fails.
0071 //static mapped_region
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) //Use MAP_ANONYMOUS
0080    flags = MAP_ANONYMOUS | MAP_SHARED;
0081    #elif !defined(MAP_ANONYMOUS) && defined(MAP_ANON) //use MAP_ANON
0082    flags = MAP_ANON | MAP_SHARED;
0083    #else // Use "/dev/zero"
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 }  //namespace interprocess {
0121 }  //namespace boost {
0122 
0123 #include <boost/interprocess/detail/config_end.hpp>
0124 
0125 #endif   //BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP