managed_external_buffer.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_MANAGED_EXTERNAL_BUFFER_HPP
  11. #define BOOST_INTERPROCESS_MANAGED_EXTERNAL_BUFFER_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/interprocess/detail/managed_memory_impl.hpp>
  23. #include <boost/move/utility_core.hpp>
  24. #include <boost/assert.hpp>
  25. //These includes needed to fulfill default template parameters of
  26. //predeclarations in interprocess_fwd.hpp
  27. #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
  28. #include <boost/interprocess/sync/mutex_family.hpp>
  29. #include <boost/interprocess/indexes/iset_index.hpp>
  30. //!\file
  31. //!Describes a named user memory allocation user class.
  32. namespace boost {
  33. namespace interprocess {
  34. //!A basic user memory named object creation class. Inherits all
  35. //!basic functionality from
  36. //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
  37. template
  38. <
  39. class CharType,
  40. class AllocationAlgorithm,
  41. template<class IndexConfig> class IndexType
  42. >
  43. class basic_managed_external_buffer
  44. : public ipcdetail::basic_managed_memory_impl <CharType, AllocationAlgorithm, IndexType>
  45. {
  46. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  47. typedef ipcdetail::basic_managed_memory_impl
  48. <CharType, AllocationAlgorithm, IndexType> base_t;
  49. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_external_buffer)
  50. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  51. public:
  52. typedef typename base_t::size_type size_type;
  53. //!Default constructor. Does nothing.
  54. //!Useful in combination with move semantics
  55. basic_managed_external_buffer() BOOST_NOEXCEPT
  56. {}
  57. //!Creates and places the segment manager. This can throw
  58. //!The external memory supplied by the user shall be aligned to the maximum value between
  59. //!`alignof(std::max_align_t)` and the strictest alignment of any over-aligned type to be built
  60. //!inside that memory.
  61. basic_managed_external_buffer
  62. (create_only_t, void *addr, size_type size)
  63. {
  64. //Check if alignment is correct
  65. BOOST_ASSERT((0 == (((std::size_t)addr) & (AllocationAlgorithm::Alignment - size_type(1u)))));
  66. if(!base_t::create_impl(addr, size)){
  67. throw interprocess_exception("Could not initialize buffer in basic_managed_external_buffer constructor");
  68. }
  69. }
  70. //!Creates and places the segment manager. This can throw
  71. //!The external memory supplied by the user shall be aligned to the maximum value between
  72. //!`alignof(std::max_align_t)` and the strictest alignment of any over-aligned type to be built
  73. //!inside that memory.
  74. basic_managed_external_buffer
  75. (open_only_t, void *addr, size_type size)
  76. {
  77. //Check if alignment is correct
  78. BOOST_ASSERT((0 == (((std::size_t)addr) & (AllocationAlgorithm::Alignment - size_type(1u)))));
  79. if(!base_t::open_impl(addr, size)){
  80. throw interprocess_exception("Could not initialize buffer in basic_managed_external_buffer constructor");
  81. }
  82. }
  83. //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
  84. basic_managed_external_buffer(BOOST_RV_REF(basic_managed_external_buffer) moved) BOOST_NOEXCEPT
  85. {
  86. this->swap(moved);
  87. }
  88. //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
  89. basic_managed_external_buffer &operator=(BOOST_RV_REF(basic_managed_external_buffer) moved) BOOST_NOEXCEPT
  90. {
  91. basic_managed_external_buffer tmp(boost::move(moved));
  92. this->swap(tmp);
  93. return *this;
  94. }
  95. void grow(size_type extra_bytes)
  96. { base_t::grow(extra_bytes); }
  97. //!Swaps the ownership of the managed heap memories managed by *this and other.
  98. //!Never throws.
  99. void swap(basic_managed_external_buffer &other) BOOST_NOEXCEPT
  100. { base_t::swap(other); }
  101. };
  102. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  103. //!Typedef for a default basic_managed_external_buffer
  104. //!of narrow characters
  105. typedef basic_managed_external_buffer
  106. <char
  107. ,rbtree_best_fit<null_mutex_family>
  108. ,iset_index>
  109. managed_external_buffer;
  110. //!Typedef for a default basic_managed_external_buffer
  111. //!of wide characters
  112. typedef basic_managed_external_buffer
  113. <wchar_t
  114. ,rbtree_best_fit<null_mutex_family>
  115. ,iset_index>
  116. wmanaged_external_buffer;
  117. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  118. } //namespace interprocess {
  119. } //namespace boost {
  120. #include <boost/interprocess/detail/config_end.hpp>
  121. #endif //BOOST_INTERPROCESS_MANAGED_EXTERNAL_BUFFER_HPP