Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-22 08:30:47

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 ">#
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 #include <boost/interprocess/creation_tags.hpp>
0026 #include <boost/move/move.hpp>
0027 #include <boost/interprocess/interprocess_fwd.hpp>
0028 #include <boost/interprocess/mapped_region.hpp>
0029 #include <cstddef>
0030 
0031 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
0032 #  include <fcntl.h>        //open, O_CREAT, O_*...
0033 #  include <sys/mman.h>     //mmap
0034 #  include <sys/stat.h>     //mode_t, S_IRWXG, S_IRWXO, S_IRWXU,
0035 #else
0036 #include <boost/interprocess/windows_shared_memory.hpp>
0037 #endif
0038 
0039 
0040 //!\file
0041 //!Describes a function that creates anonymous shared memory that can be
0042 //!shared between forked processes
0043 
0044 namespace boost {
0045 namespace interprocess {
0046 
0047 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0048 
0049 namespace ipcdetail{
0050 
0051    class raw_mapped_region_creator
0052    {
0053       public:
0054       static mapped_region
0055          create_posix_mapped_region(void *address, std::size_t size)
0056       {
0057          mapped_region region;
0058          region.m_base = address;
0059          region.m_size = size;
0060          return region;
0061       }
0062    };
0063 }
0064 
0065 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0066 
0067 //!A function that creates an anonymous shared memory segment of size "size".
0068 //!If "address" is passed the function will try to map the segment in that address.
0069 //!Otherwise the operating system will choose the mapping address.
0070 //!The function returns a mapped_region holding that segment or throws
0071 //!interprocess_exception if the function fails.
0072 //static mapped_region
0073 inline mapped_region
0074 anonymous_shared_memory(std::size_t size, void *address = 0)
0075 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
0076 {
0077    int flags;
0078    int fd = -1;
0079 
0080    #if defined(MAP_ANONYMOUS) //Use MAP_ANONYMOUS
0081    flags = MAP_ANONYMOUS | MAP_SHARED;
0082    #elif !defined(MAP_ANONYMOUS) && defined(MAP_ANON) //use MAP_ANON
0083    flags = MAP_ANON | MAP_SHARED;
0084    #else // Use "/dev/zero"
0085    fd = open("/dev/zero", O_RDWR);
0086    flags = MAP_SHARED;
0087    if(fd == -1){
0088       error_info err = system_error_code();
0089       throw interprocess_exception(err);
0090    }
0091    #endif
0092 
0093 
0094    address = mmap( address
0095                   , size
0096                   , PROT_READ|PROT_WRITE
0097                   , flags
0098                   , fd
0099                   , 0);
0100 
0101    if(address == MAP_FAILED){
0102       if(fd != -1)
0103          close(fd);
0104       error_info err = system_error_code();
0105       throw interprocess_exception(err);
0106    }
0107 
0108    if(fd != -1)
0109       close(fd);
0110 
0111    return ipcdetail::raw_mapped_region_creator::create_posix_mapped_region(address, size);
0112 }
0113 #else
0114 {
0115    windows_shared_memory anonymous_mapping(create_only, (char*)0, read_write, size);
0116    return mapped_region(anonymous_mapping, read_write, 0, size, address);
0117 }
0118 
0119 #endif
0120 
0121 }  //namespace interprocess {
0122 }  //namespace boost {
0123 
0124 #include <boost/interprocess/detail/config_end.hpp>
0125 
0126 #endif   //BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP