/Src/Dependencies/Boost/boost/interprocess/anonymous_shared_memory.hpp

http://hadesmem.googlecode.com/ · C++ Header · 119 lines · 78 code · 21 blank · 20 comment · 9 complexity · 26e62814f718a1409f09a280ac4fbb6a MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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/interprocess/detail/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 detail{
  33. class raw_mapped_region_creator
  34. {
  35. public:
  36. static mapped_region
  37. create_posix_mapped_region(void *address, offset_t offset, std::size_t size)
  38. {
  39. mapped_region region;
  40. region.m_base = address;
  41. region.m_offset = offset;
  42. region.m_extra_offset = 0;
  43. region.m_size = size;
  44. return region;
  45. }
  46. };
  47. }
  48. /// @endcond
  49. //!A function that creates an anonymous shared memory segment of size "size".
  50. //!If "address" is passed the function will try to map the segment in that address.
  51. //!Otherwise the operating system will choose the mapping address.
  52. //!The function returns a mapped_region holding that segment or throws
  53. //!interprocess_exception if the function fails.
  54. //static mapped_region
  55. static mapped_region
  56. anonymous_shared_memory(std::size_t size, void *address = 0)
  57. #if (!defined(BOOST_INTERPROCESS_WINDOWS))
  58. {
  59. int flags;
  60. int fd = -1;
  61. #if defined(MAP_ANONYMOUS) //Use MAP_ANONYMOUS
  62. flags = MAP_ANONYMOUS | MAP_SHARED;
  63. #elif !defined(MAP_ANONYMOUS) && defined(MAP_ANON) //use MAP_ANON
  64. flags = MAP_ANON | MAP_SHARED;
  65. #else // Use "/dev/zero"
  66. fd = open("/dev/zero", O_RDWR);
  67. flags = MAP_SHARED;
  68. if(fd == -1){
  69. error_info err = system_error_code();
  70. throw interprocess_exception(err);
  71. }
  72. #endif
  73. address = mmap( address
  74. , size
  75. , PROT_READ|PROT_WRITE
  76. , flags
  77. , fd
  78. , 0);
  79. if(address == MAP_FAILED){
  80. if(fd != -1)
  81. close(fd);
  82. error_info err = system_error_code();
  83. throw interprocess_exception(err);
  84. }
  85. if(fd != -1)
  86. close(fd);
  87. return detail::raw_mapped_region_creator::create_posix_mapped_region(address, 0, size);
  88. }
  89. #else
  90. {
  91. windows_shared_memory anonymous_mapping(create_only, 0, read_write, size);
  92. return mapped_region(anonymous_mapping, read_write, 0, size, address);
  93. }
  94. #endif
  95. } //namespace interprocess {
  96. } //namespace boost {
  97. #include <boost/interprocess/detail/config_end.hpp>
  98. #endif //BOOST_INTERPROCESS_ANONYMOUS_SHARED_MEMORY_HPP