managed_global_memory.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2009-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_BASIC_GLOBAL_MEMORY_HPP
  11. #define BOOST_INTERPROCESS_BASIC_GLOBAL_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/offset_ptr.hpp>
  22. #include <boost/interprocess/sync/spin/mutex.hpp>
  23. #include <boost/interprocess/sync/spin/recursive_mutex.hpp>
  24. #include <boost/interprocess/detail/managed_memory_impl.hpp>
  25. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  26. #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
  27. #include <boost/interprocess/indexes/iset_index.hpp>
  28. #include <boost/interprocess/creation_tags.hpp>
  29. #include <boost/interprocess/permissions.hpp>
  30. namespace boost{
  31. namespace interprocess{
  32. namespace ipcdetail{
  33. struct intermodule_singleton_mutex_family
  34. {
  35. typedef boost::interprocess::ipcdetail::spin_mutex mutex_type;
  36. typedef boost::interprocess::ipcdetail::spin_recursive_mutex recursive_mutex_type;
  37. };
  38. struct intermodule_types
  39. {
  40. //We must use offset_ptr since a loaded DLL can map the singleton holder shared memory
  41. //at a different address than other DLLs or the main executable
  42. typedef rbtree_best_fit<intermodule_singleton_mutex_family, offset_ptr<void> > mem_algo;
  43. template<class Device, bool FileBased>
  44. struct open_or_create
  45. {
  46. typedef managed_open_or_create_impl
  47. <Device, mem_algo::Alignment, FileBased, false> type;
  48. };
  49. };
  50. //we must implement our own managed shared memory to avoid circular dependencies
  51. template<class Device, bool FileBased>
  52. class basic_managed_global_memory
  53. : public basic_managed_memory_impl
  54. < char
  55. , intermodule_types::mem_algo
  56. , iset_index
  57. , intermodule_types::open_or_create<Device, FileBased>::type::ManagedOpenOrCreateUserOffset
  58. >
  59. , private intermodule_types::open_or_create<Device, FileBased>::type
  60. {
  61. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  62. typedef typename intermodule_types::template open_or_create<Device, FileBased>::type base2_t;
  63. typedef basic_managed_memory_impl
  64. < char
  65. , intermodule_types::mem_algo
  66. , iset_index
  67. , base2_t::ManagedOpenOrCreateUserOffset
  68. > base_t;
  69. typedef create_open_func<base_t> create_open_func_t;
  70. basic_managed_global_memory *get_this_pointer()
  71. { return this; }
  72. public:
  73. typedef typename base_t::size_type size_type;
  74. private:
  75. typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
  76. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_global_memory)
  77. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  78. public: //functions
  79. basic_managed_global_memory (open_or_create_t open_or_create,
  80. const char *name, size_type size,
  81. const void *addr = 0, const permissions& perm = permissions())
  82. : base_t()
  83. , base2_t(open_or_create, name, size, read_write, addr,
  84. create_open_func_t(get_this_pointer(),
  85. DoOpenOrCreate), perm)
  86. {}
  87. basic_managed_global_memory (open_only_t open_only, const char* name,
  88. const void *addr = 0)
  89. : base_t()
  90. , base2_t(open_only, name, read_write, addr,
  91. create_open_func_t(get_this_pointer(),
  92. DoOpen))
  93. {}
  94. };
  95. } //namespace ipcdetail{
  96. } //namespace interprocess{
  97. } //namespace boost{
  98. #include <boost/interprocess/detail/config_end.hpp>
  99. #endif //#ifndef BOOST_INTERPROCESS_BASIC_GLOBAL_MEMORY_HPP