managed_global_memory.hpp 3.8 KB

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