anonymous_shared_memory.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/creation_tags.hpp>
  22. #include <boost/move/move.hpp>
  23. #include <boost/interprocess/interprocess_fwd.hpp>
  24. #include <boost/interprocess/mapped_region.hpp>
  25. #include <cstddef>
  26. #if (!defined(BOOST_INTERPROCESS_WINDOWS))
  27. # include <fcntl.h> //open, O_CREAT, O_*...
  28. # include <sys/mman.h> //mmap
  29. # include <sys/stat.h> //mode_t, S_IRWXG, S_IRWXO, S_IRWXU,
  30. #else
  31. #include <boost/interprocess/windows_shared_memory.hpp>
  32. #endif
  33. //!\file
  34. //!Describes a function that creates anonymous shared memory that can be
  35. //!shared between forked processes
  36. namespace boost {
  37. namespace interprocess {
  38. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  39. namespace ipcdetail{
  40. class raw_mapped_region_creator
  41. {
  42. public:
  43. static mapped_region
  44. create_posix_mapped_region(void *address, std::size_t size)
  45. {
  46. mapped_region region;
  47. region.m_base = address;
  48. region.m_size = size;
  49. return region;
  50. }
  51. };
  52. }
  53. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  54. //!A function that creates an anonymous shared memory segment of size "size".
  55. //!If "address" is passed the function will try to map the segment in that address.
  56. //!Otherwise the operating system will choose the mapping address.
  57. //!The function returns a mapped_region holding that segment or throws
  58. //!interprocess_exception if the function fails.
  59. //static mapped_region
  60. static mapped_region
  61. anonymous_shared_memory(std::size_t size, void *address = 0)
  62. #if (!defined(BOOST_INTERPROCESS_WINDOWS))
  63. {
  64. int flags;
  65. int fd = -1;
  66. #if defined(MAP_ANONYMOUS) //Use MAP_ANONYMOUS
  67. flags = MAP_ANONYMOUS | MAP_SHARED;
  68. #elif !defined(MAP_ANONYMOUS) && defined(MAP_ANON) //use MAP_ANON
  69. flags = MAP_ANON | MAP_SHARED;
  70. #else // Use "/dev/zero"
  71. fd = open("/dev/zero", O_RDWR);
  72. flags = MAP_SHARED;
  73. if(fd == -1){
  74. error_info err = system_error_code();
  75. throw interprocess_exception(err);
  76. }
  77. #endif
  78. address = mmap( address
  79. , size
  80. , PROT_READ|PROT_WRITE
  81. , flags
  82. , fd
  83. , 0);
  84. if(address == MAP_FAILED){
  85. if(fd != -1)
  86. close(fd);
  87. error_info err = system_error_code();
  88. throw interprocess_exception(err);
  89. }
  90. if(fd != -1)
  91. close(fd);
  92. return ipcdetail::raw_mapped_region_creator::create_posix_mapped_region(address, size);
  93. }
  94. #else
  95. {
  96. windows_shared_memory anonymous_mapping(create_only, (char*)0, read_write, size);
  97. return mapped_region(anonymous_mapping, read_write, 0, size, address);
  98. }
  99. #endif
  100. } //namespace interprocess {
  101. } //namespace boost {
  102. #include <boost/interprocess/detail/config_end.hpp>
  103. #endif //BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP