]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
13 | #include <boost/config.hpp> | |
14 | ||
15 | #ifdef BOOST_NO_EXCEPTIONS | |
16 | ||
17 | //Interprocess does not support BOOST_NO_EXCEPTIONS so nothing to test here | |
18 | int main() | |
19 | { | |
20 | return 0; | |
21 | } | |
22 | ||
23 | #else //!BOOST_NO_EXCEPTIONS | |
24 | ||
25 | //This is needed to allow concurrent test execution in | |
26 | //several platforms. The shared memory must be unique | |
27 | //for each process... | |
28 | #include <boost/interprocess/detail/os_thread_functions.hpp> | |
29 | #include <sstream> | |
30 | ||
31 | const char *get_shared_memory_name() | |
32 | { | |
33 | std::stringstream s; | |
34 | s << "process_" << boost::interprocess::ipcdetail::get_current_process_id(); | |
35 | static std::string str = s.str(); | |
36 | return str.c_str(); | |
37 | } | |
38 | ||
39 | //[doc_offset_ptr_0 | |
40 | #include <boost/intrusive/list.hpp> | |
41 | #include <boost/interprocess/offset_ptr.hpp> | |
42 | ||
43 | using namespace boost::intrusive; | |
44 | namespace ip = boost::interprocess; | |
45 | ||
46 | class shared_memory_data | |
47 | //Declare the hook with an offset_ptr from Boost.Interprocess | |
48 | //to make this class compatible with shared memory | |
49 | : public list_base_hook< void_pointer< ip::offset_ptr<void> > > | |
50 | { | |
51 | int data_id_; | |
52 | public: | |
53 | ||
54 | int get() const { return data_id_; } | |
55 | void set(int id) { data_id_ = id; } | |
56 | }; | |
57 | //] | |
58 | ||
59 | //[doc_offset_ptr_1 | |
60 | #include <boost/interprocess/managed_shared_memory.hpp> | |
61 | #include <boost/interprocess/containers/vector.hpp> | |
62 | #include <boost/interprocess/allocators/allocator.hpp> | |
63 | ||
64 | //Definition of the shared memory friendly intrusive list | |
65 | typedef list<shared_memory_data> intrusive_list_t; | |
66 | ||
67 | int main() | |
68 | { | |
69 | //Now create an intrusive list in shared memory: | |
70 | //nodes and the container itself must be created in shared memory | |
71 | const int MaxElem = 100; | |
72 | const int ShmSize = 50000; | |
73 | const char *ShmName = get_shared_memory_name(); | |
74 | { | |
75 | //Erase all old shared memory | |
76 | ip::shared_memory_object::remove(ShmName); | |
77 | ip::managed_shared_memory shm(ip::create_only, ShmName, ShmSize); | |
78 | ||
79 | //Create all nodes in shared memory using a shared memory vector | |
80 | //See Boost.Interprocess documentation for more information on this | |
81 | typedef ip::allocator | |
82 | < shared_memory_data, ip::managed_shared_memory::segment_manager> | |
83 | shm_allocator_t; | |
84 | typedef ip::vector<shared_memory_data, shm_allocator_t> shm_vector_t; | |
85 | shm_allocator_t shm_alloc(shm.get_segment_manager()); | |
86 | shm_vector_t *pshm_vect = | |
87 | shm.construct<shm_vector_t>(ip::anonymous_instance)(shm_alloc); | |
88 | pshm_vect->resize(MaxElem); | |
89 | ||
90 | //Initialize all the nodes | |
91 | for(int i = 0; i < MaxElem; ++i) (*pshm_vect)[i].set(i); | |
92 | ||
93 | //Now create the shared memory intrusive list | |
94 | intrusive_list_t *plist = shm.construct<intrusive_list_t>(ip::anonymous_instance)(); | |
95 | ||
96 | //Insert objects stored in shared memory vector in the intrusive list | |
97 | plist->insert(plist->end(), pshm_vect->begin(), pshm_vect->end()); | |
98 | ||
99 | //Check all the inserted nodes | |
100 | int checker = 0; | |
101 | for( intrusive_list_t::const_iterator it = plist->begin(), itend(plist->end()) | |
102 | ; it != itend; ++it, ++checker){ | |
92f5a8d4 | 103 | if(it->get() != checker) return 1; |
7c673cae FG |
104 | } |
105 | ||
106 | //Now delete the list and after that, the nodes | |
107 | shm.destroy_ptr(plist); | |
108 | shm.destroy_ptr(pshm_vect); | |
109 | } | |
110 | ip::shared_memory_object::remove(ShmName); | |
111 | return 0; | |
112 | } | |
113 | //] | |
114 | ||
115 | #endif //BOOST_NO_EXCEPTIONS | |
116 |