managed_heap_memory.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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(){}
  58. //!Destructor. Liberates the heap memory holding the managed data.
  59. //!Never throws.
  60. ~basic_managed_heap_memory()
  61. { this->priv_close(); }
  62. //!Creates heap memory and initializes the segment manager.
  63. //!This can throw.
  64. basic_managed_heap_memory(size_type size)
  65. : m_heapmem(size, char(0))
  66. {
  67. if(!base_t::create_impl(&m_heapmem[0], size)){
  68. this->priv_close();
  69. throw interprocess_exception("Could not initialize heap in basic_managed_heap_memory constructor");
  70. }
  71. }
  72. //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
  73. basic_managed_heap_memory(BOOST_RV_REF(basic_managed_heap_memory) moved)
  74. { this->swap(moved); }
  75. //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
  76. basic_managed_heap_memory &operator=(BOOST_RV_REF(basic_managed_heap_memory) moved)
  77. {
  78. basic_managed_heap_memory tmp(boost::move(moved));
  79. this->swap(tmp);
  80. return *this;
  81. }
  82. //!Tries to resize internal heap memory so that
  83. //!we have room for more objects.
  84. //!WARNING: If memory is reallocated, all the objects will
  85. //!be binary-copied to the new buffer. To be able to use
  86. //!this function, all pointers constructed in this buffer
  87. //!must be offset pointers. Otherwise, the result is undefined.
  88. //!Returns true if the growth has been successful, so you will
  89. //!have some extra bytes to allocate new objects. If returns
  90. //!false, the heap allocation has failed.
  91. bool grow(size_type extra_bytes)
  92. {
  93. //If memory is reallocated, data will
  94. //be automatically copied
  95. BOOST_TRY{
  96. m_heapmem.resize(m_heapmem.size()+extra_bytes);
  97. }
  98. BOOST_CATCH(...){
  99. return false;
  100. }
  101. BOOST_CATCH_END
  102. //Grow always works
  103. base_t::close_impl();
  104. base_t::open_impl(&m_heapmem[0], m_heapmem.size());
  105. base_t::grow(extra_bytes);
  106. return true;
  107. }
  108. //!Swaps the ownership of the managed heap memories managed by *this and other.
  109. //!Never throws.
  110. void swap(basic_managed_heap_memory &other)
  111. {
  112. base_t::swap(other);
  113. m_heapmem.swap(other.m_heapmem);
  114. }
  115. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  116. private:
  117. //!Frees resources. Never throws.
  118. void priv_close()
  119. {
  120. base_t::destroy_impl();
  121. std::vector<char>().swap(m_heapmem);
  122. }
  123. std::vector<char> m_heapmem;
  124. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  125. };
  126. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  127. //!Typedef for a default basic_managed_heap_memory
  128. //!of narrow characters
  129. typedef basic_managed_heap_memory
  130. <char
  131. ,rbtree_best_fit<null_mutex_family>
  132. ,iset_index>
  133. managed_heap_memory;
  134. //!Typedef for a default basic_managed_heap_memory
  135. //!of wide characters
  136. typedef basic_managed_heap_memory
  137. <wchar_t
  138. ,rbtree_best_fit<null_mutex_family>
  139. ,iset_index>
  140. wmanaged_heap_memory;
  141. #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  142. } //namespace interprocess {
  143. } //namespace boost {
  144. #include <boost/interprocess/detail/config_end.hpp>
  145. #endif //BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP