managed_heap_memory.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_HEAP_MEMORY_HPP
  11. #define BOOST_INTERPROCESS_MANAGED_HEAP_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/creation_tags.hpp>
  22. #include <boost/move/utility_core.hpp>
  23. #include <vector>
  24. #include <boost/interprocess/detail/managed_memory_impl.hpp>
  25. #include <boost/core/no_exceptions_support.hpp>
  26. //These includes needed to fulfill default template parameters of
  27. //predeclarations in interprocess_fwd.hpp
  28. #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
  29. #include <boost/interprocess/sync/mutex_family.hpp>
  30. #include <boost/interprocess/indexes/iset_index.hpp>
  31. //!\file
  32. //!Describes a named heap memory allocation user class.
  33. namespace boost {
  34. namespace interprocess {
  35. //!A basic heap memory named object creation class. Initializes the
  36. //!heap memory segment. Inherits all basic functionality from
  37. //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
  38. template
  39. <
  40. class CharType,
  41. class AllocationAlgorithm,
  42. template<class IndexConfig> class IndexType
  43. >
  44. class basic_managed_heap_memory
  45. : public ipcdetail::basic_managed_memory_impl <CharType, AllocationAlgorithm, IndexType>
  46. {
  47. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  48. private:
  49. typedef ipcdetail::basic_managed_memory_impl
  50. <CharType, AllocationAlgorithm, IndexType> base_t;
  51. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_heap_memory)
  52. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  53. public: //functions
  54. typedef typename base_t::size_type size_type;
  55. //!Default constructor. Does nothing.
  56. //!Useful in combination with move semantics
  57. basic_managed_heap_memory() BOOST_NOEXCEPT
  58. {}
  59. //!Destructor. Liberates the heap memory holding the managed data.
  60. //!Never throws.
  61. ~basic_managed_heap_memory()
  62. { this->priv_close(); }
  63. //!Creates heap memory and initializes the segment manager.
  64. //!This can throw.
  65. basic_managed_heap_memory(size_type size)
  66. : m_heapmem(size, char(0))
  67. {
  68. if(!base_t::create_impl(&m_heapmem[0], size)){
  69. this->priv_close();
  70. throw interprocess_exception("Could not initialize heap in basic_managed_heap_memory constructor");
  71. }
  72. }
  73. //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
  74. basic_managed_heap_memory(BOOST_RV_REF(basic_managed_heap_memory) moved) BOOST_NOEXCEPT
  75. { this->swap(moved); }
  76. //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
  77. basic_managed_heap_memory &operator=(BOOST_RV_REF(basic_managed_heap_memory) moved) BOOST_NOEXCEPT
  78. {
  79. basic_managed_heap_memory tmp(boost::move(moved));
  80. this->swap(tmp);
  81. return *this;
  82. }
  83. //!Tries to resize internal heap memory so that
  84. //!we have room for more objects.
  85. //!WARNING: If memory is reallocated, all the objects will
  86. //!be binary-copied to the new buffer. To be able to use
  87. //!this function, all pointers constructed in this buffer
  88. //!must be offset pointers. Otherwise, the result is undefined.
  89. //!Returns true if the growth has been successful, so you will
  90. //!have some extra bytes to allocate new objects. If returns
  91. //!false, the heap allocation has failed.
  92. bool grow(size_type extra_bytes)
  93. {
  94. //If memory is reallocated, data will
  95. //be automatically copied
  96. BOOST_TRY{
  97. m_heapmem.resize(m_heapmem.size()+extra_bytes);
  98. }
  99. BOOST_CATCH(...){
  100. return false;
  101. }
  102. BOOST_CATCH_END
  103. //Grow always works
  104. base_t::close_impl();
  105. base_t::open_impl(&m_heapmem[0], m_heapmem.size());
  106. base_t::grow(extra_bytes);
  107. return true;
  108. }
  109. //!Swaps the ownership of the managed heap memories managed by *this and other.
  110. //!Never throws.
  111. void swap(basic_managed_heap_memory &other) BOOST_NOEXCEPT
  112. {
  113. base_t::swap(other);
  114. m_heapmem.swap(other.m_heapmem);
  115. }
  116. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  117. private:
  118. //!Frees resources. Never throws.
  119. void priv_close()
  120. {
  121. base_t::destroy_impl();
  122. std::vector<char>().swap(m_heapmem);
  123. }
  124. std::vector<char> m_heapmem;
  125. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  126. };
  127. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  128. //!Typedef for a default basic_managed_heap_memory
  129. //!of narrow characters
  130. typedef basic_managed_heap_memory
  131. <char
  132. ,rbtree_best_fit<null_mutex_family>
  133. ,iset_index>
  134. managed_heap_memory;
  135. //!Typedef for a default basic_managed_heap_memory
  136. //!of wide characters
  137. typedef basic_managed_heap_memory
  138. <wchar_t
  139. ,rbtree_best_fit<null_mutex_family>
  140. ,iset_index>
  141. wmanaged_heap_memory;
  142. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  143. } //namespace interprocess {
  144. } //namespace boost {
  145. #include <boost/interprocess/detail/config_end.hpp>
  146. #endif //BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP