1 | #include <boost/interprocess/managed_mapped_file.hpp>
|
---|
2 | #include <boost/interprocess/containers/vector.hpp>
|
---|
3 | #include <boost/interprocess/containers/list.hpp>
|
---|
4 | #include <boost/interprocess/offset_ptr.hpp>
|
---|
5 | #include <boost/interprocess/allocators/allocator.hpp>
|
---|
6 |
|
---|
7 | //#include <boost/interprocess/containers/vector.hpp>
|
---|
8 | //#include <boost/interprocess/allocators/node_allocator.hpp>
|
---|
9 |
|
---|
10 |
|
---|
11 | using namespace boost::intrusive;
|
---|
12 | namespace ip= boost::interprocess;
|
---|
13 |
|
---|
14 |
|
---|
15 | class channel_t : public list_base_hook< void_pointer< ip::offset_ptr<void> > >
|
---|
16 | {
|
---|
17 |
|
---|
18 | public:
|
---|
19 | char xxx[4];
|
---|
20 | int ch_order;
|
---|
21 | int ch_id;
|
---|
22 |
|
---|
23 | channel_t(int _ch_order, int _ch_id) : ch_order(_ch_order), ch_id(_ch_id) {}
|
---|
24 |
|
---|
25 | channel_t(const channel_t& other) : ch_order(other.ch_order), ch_id(other.ch_id) {
|
---|
26 | printf("copy constructor 0x%x -> 0x%x \n", (long long)&other, (long long) this);
|
---|
27 | }
|
---|
28 |
|
---|
29 | channel_t() {}
|
---|
30 |
|
---|
31 | void set (unsigned int a, unsigned int b) {
|
---|
32 | ch_order=a;
|
---|
33 | ch_id=b;
|
---|
34 | xxx[0]='A';
|
---|
35 | xxx[1]='B';
|
---|
36 | xxx[2]='C';
|
---|
37 | xxx[3]='C';
|
---|
38 | }
|
---|
39 | };
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | typedef ip::allocator<channel_t, ip::managed_mapped_file::segment_manager> shm_allocator_t;
|
---|
45 |
|
---|
46 | typedef ip::list<channel_t, shm_allocator_t> chlist_t;
|
---|
47 |
|
---|
48 |
|
---|
49 | int main_create (int argc, char **argv)
|
---|
50 | {
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | try{
|
---|
56 | //Shared memory front-end that is able to construct objects
|
---|
57 | //associated with a c-string. Erase previous shared memory with the name
|
---|
58 | //to be used and create the memory segment at the specified address and initialize resources
|
---|
59 | ip::managed_mapped_file segment(ip::create_only,"/tmp/MyMappedFile", 4096*4);
|
---|
60 | //Alias an STL compatible allocator of ints that allocates ints from the managed
|
---|
61 | //shared memory segment. This allocator will allow to place containers
|
---|
62 | //in managed shared memory segments
|
---|
63 |
|
---|
64 | //Initialize shared memory STL-compatible allocator
|
---|
65 | shm_allocator_t shm_alloc (segment.get_segment_manager());
|
---|
66 |
|
---|
67 |
|
---|
68 | typedef ip::vector<channel_t, shm_allocator_t> shm_vector_t;
|
---|
69 | unsigned int i;
|
---|
70 |
|
---|
71 | //Construct a list in shared memory
|
---|
72 | chlist_t *list =
|
---|
73 | segment.construct<chlist_t>("ChannelInfo")(shm_alloc);
|
---|
74 |
|
---|
75 | for(int i = 0; i < 100; ++i) {
|
---|
76 | channel_t c(i, i+100);
|
---|
77 | list->push_front(c);
|
---|
78 | }
|
---|
79 |
|
---|
80 | chlist_t::iterator it=list->begin();
|
---|
81 | for(;it!=list->end();++it) {
|
---|
82 | channel_t d= *it;
|
---|
83 | printf("ch_order=%d ch_id=%d\n", d.ch_order, d.ch_id);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | catch(...){
|
---|
87 | throw;
|
---|
88 | }
|
---|
89 | return 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | int main_test (int argc, char **argv)
|
---|
95 | {
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 |
|
---|
100 | try{
|
---|
101 | //Shared memory front-end that is able to construct objects
|
---|
102 | //associated with a c-string. Erase previous shared memory with the name
|
---|
103 | //to be used and create the memory segment at the specified address and initialize resources
|
---|
104 | ip::managed_mapped_file segment(ip::open_only,"/tmp/MyMappedFile");
|
---|
105 | //Alias an STL compatible allocator of ints that allocates ints from the managed
|
---|
106 | //shared memory segment. This allocator will allow to place containers
|
---|
107 | //in managed shared memory segments
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 | //Initialize shared memory STL-compatible allocator
|
---|
113 | shm_allocator_t shm_alloc (segment.get_segment_manager());
|
---|
114 |
|
---|
115 |
|
---|
116 | //Find the vector using the c-string name
|
---|
117 | chlist_t *list = segment.find<chlist_t>("ChannelInfo").first;
|
---|
118 | printf("list=0x%x\n", (long long)list);
|
---|
119 | unsigned int i;
|
---|
120 | chlist_t::iterator it=list->begin();
|
---|
121 | for(;it!=list->end();++it) {
|
---|
122 | channel_t d= *it;
|
---|
123 | printf("ch_order=%d ch_id=%d\n", d.ch_order, d.ch_id);
|
---|
124 | }
|
---|
125 | channel_t& d= *list->begin();
|
---|
126 | }
|
---|
127 | catch(...){
|
---|
128 | throw;
|
---|
129 | }
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | int main(int argc, char**argv) {
|
---|
135 |
|
---|
136 | switch(atoi(argv[1])) {
|
---|
137 | case 1:
|
---|
138 | return main_create(argc,argv);
|
---|
139 | break;
|
---|
140 | case 2:
|
---|
141 | return main_test(argc,argv);
|
---|
142 | break;
|
---|
143 |
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|