managed_heap_memory.hpp 4.6 KB

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