anonymous_shared_memory.hpp 3.3 KB

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