annotate DEPENDENCIES/generic/include/boost/interprocess/anonymous_shared_memory.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6 //
Chris@16 7 // See http://www.boost.org/libs/interprocess for documentation.
Chris@16 8 //
Chris@16 9 //////////////////////////////////////////////////////////////////////////////
Chris@16 10
Chris@16 11 #ifndef BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
Chris@16 12 #define BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP
Chris@16 13
Chris@101 14 #ifndef BOOST_CONFIG_HPP
Chris@101 15 # include <boost/config.hpp>
Chris@101 16 #endif
Chris@101 17 #
Chris@101 18 #if defined(BOOST_HAS_PRAGMA_ONCE)
Chris@101 19 # pragma once
Chris@101 20 #endif
Chris@101 21
Chris@16 22 #include <boost/interprocess/detail/config_begin.hpp>
Chris@16 23 #include <boost/interprocess/detail/workaround.hpp>
Chris@16 24 #include <boost/interprocess/creation_tags.hpp>
Chris@16 25 #include <boost/move/move.hpp>
Chris@16 26 #include <boost/interprocess/interprocess_fwd.hpp>
Chris@16 27 #include <boost/interprocess/mapped_region.hpp>
Chris@16 28 #include <cstddef>
Chris@16 29
Chris@16 30 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
Chris@16 31 # include <fcntl.h> //open, O_CREAT, O_*...
Chris@16 32 # include <sys/mman.h> //mmap
Chris@16 33 # include <sys/stat.h> //mode_t, S_IRWXG, S_IRWXO, S_IRWXU,
Chris@16 34 #else
Chris@16 35 #include <boost/interprocess/windows_shared_memory.hpp>
Chris@16 36 #endif
Chris@16 37
Chris@16 38
Chris@16 39 //!\file
Chris@16 40 //!Describes a function that creates anonymous shared memory that can be
Chris@16 41 //!shared between forked processes
Chris@16 42
Chris@16 43 namespace boost {
Chris@16 44 namespace interprocess {
Chris@16 45
Chris@101 46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 47
Chris@16 48 namespace ipcdetail{
Chris@16 49
Chris@16 50 class raw_mapped_region_creator
Chris@16 51 {
Chris@16 52 public:
Chris@16 53 static mapped_region
Chris@16 54 create_posix_mapped_region(void *address, std::size_t size)
Chris@16 55 {
Chris@16 56 mapped_region region;
Chris@16 57 region.m_base = address;
Chris@16 58 region.m_size = size;
Chris@16 59 return region;
Chris@16 60 }
Chris@16 61 };
Chris@16 62 }
Chris@16 63
Chris@101 64 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 65
Chris@16 66 //!A function that creates an anonymous shared memory segment of size "size".
Chris@16 67 //!If "address" is passed the function will try to map the segment in that address.
Chris@16 68 //!Otherwise the operating system will choose the mapping address.
Chris@16 69 //!The function returns a mapped_region holding that segment or throws
Chris@16 70 //!interprocess_exception if the function fails.
Chris@16 71 //static mapped_region
Chris@16 72 static mapped_region
Chris@16 73 anonymous_shared_memory(std::size_t size, void *address = 0)
Chris@16 74 #if (!defined(BOOST_INTERPROCESS_WINDOWS))
Chris@16 75 {
Chris@16 76 int flags;
Chris@16 77 int fd = -1;
Chris@16 78
Chris@16 79 #if defined(MAP_ANONYMOUS) //Use MAP_ANONYMOUS
Chris@16 80 flags = MAP_ANONYMOUS | MAP_SHARED;
Chris@16 81 #elif !defined(MAP_ANONYMOUS) && defined(MAP_ANON) //use MAP_ANON
Chris@16 82 flags = MAP_ANON | MAP_SHARED;
Chris@16 83 #else // Use "/dev/zero"
Chris@16 84 fd = open("/dev/zero", O_RDWR);
Chris@16 85 flags = MAP_SHARED;
Chris@16 86 if(fd == -1){
Chris@16 87 error_info err = system_error_code();
Chris@16 88 throw interprocess_exception(err);
Chris@16 89 }
Chris@16 90 #endif
Chris@16 91
Chris@16 92
Chris@16 93 address = mmap( address
Chris@16 94 , size
Chris@16 95 , PROT_READ|PROT_WRITE
Chris@16 96 , flags
Chris@16 97 , fd
Chris@16 98 , 0);
Chris@16 99
Chris@16 100 if(address == MAP_FAILED){
Chris@16 101 if(fd != -1)
Chris@16 102 close(fd);
Chris@16 103 error_info err = system_error_code();
Chris@16 104 throw interprocess_exception(err);
Chris@16 105 }
Chris@16 106
Chris@16 107 if(fd != -1)
Chris@16 108 close(fd);
Chris@16 109
Chris@16 110 return ipcdetail::raw_mapped_region_creator::create_posix_mapped_region(address, size);
Chris@16 111 }
Chris@16 112 #else
Chris@16 113 {
Chris@16 114 windows_shared_memory anonymous_mapping(create_only, 0, read_write, size);
Chris@16 115 return mapped_region(anonymous_mapping, read_write, 0, size, address);
Chris@16 116 }
Chris@16 117
Chris@16 118 #endif
Chris@16 119
Chris@16 120 } //namespace interprocess {
Chris@16 121 } //namespace boost {
Chris@16 122
Chris@16 123 #include <boost/interprocess/detail/config_end.hpp>
Chris@16 124
Chris@16 125 #endif //BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP