managed_external_buffer.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. basic_managed_external_buffer
  59. (create_only_t, void *addr, size_type size)
  60. {
  61. //Check if alignment is correct
  62. BOOST_ASSERT((0 == (((std::size_t)addr) & (AllocationAlgorithm::Alignment - size_type(1u)))));
  63. if(!base_t::create_impl(addr, size)){
  64. throw interprocess_exception("Could not initialize buffer in basic_managed_external_buffer constructor");
  65. }
  66. }
  67. //!Creates and places the segment manager. This can throw
  68. basic_managed_external_buffer
  69. (open_only_t, void *addr, size_type size)
  70. {
  71. //Check if alignment is correct
  72. BOOST_ASSERT((0 == (((std::size_t)addr) & (AllocationAlgorithm::Alignment - size_type(1u)))));
  73. if(!base_t::open_impl(addr, size)){
  74. throw interprocess_exception("Could not initialize buffer in basic_managed_external_buffer constructor");
  75. }
  76. }
  77. //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
  78. basic_managed_external_buffer(BOOST_RV_REF(basic_managed_external_buffer) moved) BOOST_NOEXCEPT
  79. {
  80. this->swap(moved);
  81. }
  82. //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
  83. basic_managed_external_buffer &operator=(BOOST_RV_REF(basic_managed_external_buffer) moved) BOOST_NOEXCEPT
  84. {
  85. basic_managed_external_buffer tmp(boost::move(moved));
  86. this->swap(tmp);
  87. return *this;
  88. }
  89. void grow(size_type extra_bytes)
  90. { base_t::grow(extra_bytes); }
  91. //!Swaps the ownership of the managed heap memories managed by *this and other.
  92. //!Never throws.
  93. void swap(basic_managed_external_buffer &other) BOOST_NOEXCEPT
  94. { base_t::swap(other); }
  95. };
  96. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  97. //!Typedef for a default basic_managed_external_buffer
  98. //!of narrow characters
  99. typedef basic_managed_external_buffer
  100. <char
  101. ,rbtree_best_fit<null_mutex_family>
  102. ,iset_index>
  103. managed_external_buffer;
  104. //!Typedef for a default basic_managed_external_buffer
  105. //!of wide characters
  106. typedef basic_managed_external_buffer
  107. <wchar_t
  108. ,rbtree_best_fit<null_mutex_family>
  109. ,iset_index>
  110. wmanaged_external_buffer;
  111. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  112. } //namespace interprocess {
  113. } //namespace boost {
  114. #include <boost/interprocess/detail/config_end.hpp>
  115. #endif //BOOST_INTERPROCESS_MANAGED_EXTERNAL_BUFFER_HPP