]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_named_alloc.cpp
1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <cstdlib> //std::system
19 #include "../test/get_process_id_name.hpp"
22 int main(int argc
, char *argv
[])
24 using namespace boost::interprocess
;
25 typedef std::pair
<double, int> MyType
;
27 if(argc
== 1){ //Parent process
28 //Remove shared memory on construction and destruction
33 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
34 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
37 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
38 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
47 //Construct managed shared memory
50 managed_shared_memory
segment(create_only
, test::get_process_id_name(), 65536);
53 managed_shared_memory
segment(create_only
, "MySharedMemory", 65536);
58 //Create an object of MyType initialized to {0.0, 0}
59 MyType
*instance
= segment
.construct
<MyType
>
60 ("MyType instance") //name of the object
61 (0.0, 0); //ctor first argument
63 //Create an array of 10 elements of MyType initialized to {0.0, 0}
64 MyType
*array
= segment
.construct
<MyType
>
65 ("MyType array") //name of the object
66 [10] //number of elements
67 (0.0, 0); //Same two ctor arguments for all objects
69 //Create an array of 3 elements of MyType initializing each one
70 //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
71 float float_initializer
[3] = { 0.0, 1.0, 2.0 };
72 int int_initializer
[3] = { 0, 1, 2 };
74 MyType
*array_it
= segment
.construct_it
<MyType
>
75 ("MyType array from it") //name of the object
76 [3] //number of elements
77 ( &float_initializer
[0] //Iterator for the 1st ctor argument
78 , &int_initializer
[0]); //Iterator for the 2nd ctor argument
80 //Launch child process
81 std::string
s(argv
[0]); s
+= " child ";
83 s
+= test::get_process_id_name();
85 if(0 != std::system(s
.c_str()))
94 //Check child has destroyed all objects
95 if(segment
.find
<MyType
>("MyType array").first
||
96 segment
.find
<MyType
>("MyType instance").first
||
97 segment
.find
<MyType
>("MyType array from it").first
)
101 //Open managed shared memory
104 managed_shared_memory
segment(open_only
, argv
[2]);
107 managed_shared_memory
segment(open_only
, "MySharedMemory");
112 std::pair
<MyType
*, managed_shared_memory::size_type
> res
;
115 res
= segment
.find
<MyType
> ("MyType array");
116 //Length should be 10
117 if(res
.second
!= 10) return 1;
120 res
= segment
.find
<MyType
> ("MyType instance");
122 if(res
.second
!= 1) return 1;
124 //Find the array constructed from iterators
125 res
= segment
.find
<MyType
> ("MyType array from it");
127 if(res
.second
!= 3) return 1;
129 //We're done, delete all the objects
130 segment
.destroy
<MyType
>("MyType array");
131 segment
.destroy
<MyType
>("MyType instance");
132 segment
.destroy
<MyType
>("MyType array from it");
137 #include <boost/interprocess/detail/config_end.hpp>