Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_spawn_vector.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-2009. 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 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
12 //[doc_spawn_vector
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/containers/vector.hpp>
15 #include <boost/interprocess/allocators/allocator.hpp>
16 #include <string>
17 #include <cstdlib> //std::system
18 //<-
19 #include "../test/get_process_id_name.hpp"
20 //->
21
22 using namespace boost::interprocess;
23
24 //Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
25 //This allocator will allow placing containers in the segment
26 typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;
27
28 //Alias a vector that uses the previous STL-like allocator so that allocates
29 //its values from the segment
30 typedef vector<int, ShmemAllocator> MyVector;
31
32 //Main function. For parent process argc == 1, for child process argc == 2
33 int main(int argc, char *argv[])
34 {
35    if(argc == 1){ //Parent process
36       //Remove shared memory on construction and destruction
37       struct shm_remove 
38       {
39       //<-
40       #if 1
41          shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
42          ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
43       #else
44       //->
45          shm_remove() { shared_memory_object::remove("MySharedMemory"); }
46          ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
47       //<-
48       #endif
49       //->
50       } remover;
51
52       //Create a new segment with given name and size
53       //<-
54       #if 1
55       managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
56       #else
57       //->
58       managed_shared_memory segment(create_only, "MySharedMemory", 65536);
59       //<-
60       #endif
61       //->
62
63       //Initialize shared memory STL-compatible allocator
64       const ShmemAllocator alloc_inst (segment.get_segment_manager());
65
66       //Construct a vector named "MyVector" in shared memory with argument alloc_inst
67       MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
68
69       for(int i = 0; i < 100; ++i)  //Insert data in the vector
70          myvector->push_back(i);
71
72       //Launch child process
73       std::string s(argv[0]); s += " child ";
74       //<-
75       s += test::get_process_id_name();
76       //->
77       if(0 != std::system(s.c_str()))
78          return 1;
79
80       //Check child has destroyed the vector
81       if(segment.find<MyVector>("MyVector").first)
82          return 1;
83    }
84    else{ //Child process
85       //Open the managed segment
86       //<-
87       #if 1
88       managed_shared_memory segment(open_only, argv[2]);
89       #else
90       //->
91       managed_shared_memory segment(open_only, "MySharedMemory");  
92       //<-
93       #endif
94       //->
95
96       //Find the vector using the c-string name
97       MyVector *myvector = segment.find<MyVector>("MyVector").first;
98
99       //Use vector in reverse order
100       std::sort(myvector->rbegin(), myvector->rend());
101
102       //When done, destroy the vector from the segment
103       segment.destroy<MyVector>("MyVector");
104    }
105
106    return 0;
107 };
108
109 //]
110 #include <boost/interprocess/detail/config_end.hpp>