doc_offset_ptr.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #include <boost/config.hpp>
  13. #ifdef BOOST_NO_EXCEPTIONS
  14. //Interprocess does not support BOOST_NO_EXCEPTIONS so nothing to test here
  15. int main()
  16. {
  17. return 0;
  18. }
  19. #else //!BOOST_NO_EXCEPTIONS
  20. //This is needed to allow concurrent test execution in
  21. //several platforms. The shared memory must be unique
  22. //for each process...
  23. #include <boost/interprocess/detail/os_thread_functions.hpp>
  24. #include <sstream>
  25. const char *get_shared_memory_name()
  26. {
  27. std::stringstream s;
  28. s << "process_" << boost::interprocess::ipcdetail::get_current_process_id();
  29. static std::string str = s.str();
  30. return str.c_str();
  31. }
  32. //[doc_offset_ptr_0
  33. #include <boost/intrusive/list.hpp>
  34. #include <boost/interprocess/offset_ptr.hpp>
  35. using namespace boost::intrusive;
  36. namespace ip = boost::interprocess;
  37. class shared_memory_data
  38. //Declare the hook with an offset_ptr from Boost.Interprocess
  39. //to make this class compatible with shared memory
  40. : public list_base_hook< void_pointer< ip::offset_ptr<void> > >
  41. {
  42. int data_id_;
  43. public:
  44. int get() const { return data_id_; }
  45. void set(int id) { data_id_ = id; }
  46. };
  47. //]
  48. //[doc_offset_ptr_1
  49. #include <boost/interprocess/managed_shared_memory.hpp>
  50. #include <boost/interprocess/containers/vector.hpp>
  51. #include <boost/interprocess/allocators/allocator.hpp>
  52. //Definition of the shared memory friendly intrusive list
  53. typedef list<shared_memory_data> intrusive_list_t;
  54. int main()
  55. {
  56. //Now create an intrusive list in shared memory:
  57. //nodes and the container itself must be created in shared memory
  58. const int MaxElem = 100;
  59. const int ShmSize = 50000;
  60. const char *ShmName = get_shared_memory_name();
  61. {
  62. //Erase all old shared memory
  63. ip::shared_memory_object::remove(ShmName);
  64. ip::managed_shared_memory shm(ip::create_only, ShmName, ShmSize);
  65. //Create all nodes in shared memory using a shared memory vector
  66. //See Boost.Interprocess documentation for more information on this
  67. typedef ip::allocator
  68. < shared_memory_data, ip::managed_shared_memory::segment_manager>
  69. shm_allocator_t;
  70. typedef ip::vector<shared_memory_data, shm_allocator_t> shm_vector_t;
  71. shm_allocator_t shm_alloc(shm.get_segment_manager());
  72. shm_vector_t *pshm_vect =
  73. shm.construct<shm_vector_t>(ip::anonymous_instance)(shm_alloc);
  74. pshm_vect->resize(MaxElem);
  75. //Initialize all the nodes
  76. for(int i = 0; i < MaxElem; ++i) (*pshm_vect)[i].set(i);
  77. //Now create the shared memory intrusive list
  78. intrusive_list_t *plist = shm.construct<intrusive_list_t>(ip::anonymous_instance)();
  79. //Insert objects stored in shared memory vector in the intrusive list
  80. plist->insert(plist->end(), pshm_vect->begin(), pshm_vect->end());
  81. //Check all the inserted nodes
  82. int checker = 0;
  83. for( intrusive_list_t::const_iterator it = plist->begin(), itend(plist->end())
  84. ; it != itend; ++it, ++checker){
  85. if(it->get() != checker) return 1;
  86. }
  87. //Now delete the list and after that, the nodes
  88. shm.destroy_ptr(plist);
  89. shm.destroy_ptr(pshm_vect);
  90. }
  91. ip::shared_memory_object::remove(ShmName);
  92. return 0;
  93. }
  94. //]
  95. #endif //BOOST_NO_EXCEPTIONS