Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_complex_map.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_complex_map
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/allocators/allocator.hpp>
15 #include <boost/interprocess/containers/map.hpp>
16 #include <boost/interprocess/containers/vector.hpp>
17 #include <boost/interprocess/containers/string.hpp>
18 //<-
19 #include "../test/get_process_id_name.hpp"
20 //->
21
22 using namespace boost::interprocess;
23
24 //Typedefs of allocators and containers
25 typedef managed_shared_memory::segment_manager                       segment_manager_t;
26 typedef allocator<void, segment_manager_t>                           void_allocator;
27 typedef allocator<int, segment_manager_t>                            int_allocator;
28 typedef vector<int, int_allocator>                                   int_vector;
29 typedef allocator<int_vector, segment_manager_t>                     int_vector_allocator;
30 typedef vector<int_vector, int_vector_allocator>                     int_vector_vector;
31 typedef allocator<char, segment_manager_t>                           char_allocator;
32 typedef basic_string<char, std::char_traits<char>, char_allocator>   char_string;
33
34 class complex_data
35 {
36    int               id_;
37    char_string       char_string_;
38    int_vector_vector int_vector_vector_;
39
40    public:
41    //Since void_allocator is convertible to any other allocator<T>, we can simplify
42    //the initialization taking just one allocator for all inner containers.
43    complex_data(int id, const char *name, const void_allocator &void_alloc)
44       : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc)
45    {}
46    //Other members...
47 };
48
49 //Definition of the map holding a string as key and complex_data as mapped type
50 typedef std::pair<const char_string, complex_data>                      map_value_type;
51 typedef std::pair<char_string, complex_data>                            movable_to_map_value_type;
52 typedef allocator<map_value_type, segment_manager_t>                    map_value_type_allocator;
53 typedef map< char_string, complex_data
54            , std::less<char_string>, map_value_type_allocator>          complex_map_type;
55
56 int main ()
57 {
58    //Remove shared memory on construction and destruction
59    struct shm_remove
60    {
61       //<-
62       #if 1
63       shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
64       ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
65       #else
66       //->
67       shm_remove() { shared_memory_object::remove("MySharedMemory"); }
68       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
69       //<-
70       #endif
71       //->
72    } remover;
73
74    //Create shared memory
75    //<-
76    #if 1
77    managed_shared_memory segment(create_only,test::get_process_id_name(), 65536);
78    #else
79    //->
80    managed_shared_memory segment(create_only,"MySharedMemory", 65536);
81    //<-
82    #endif
83    //->
84
85    //An allocator convertible to any allocator<T, segment_manager_t> type
86    void_allocator alloc_inst (segment.get_segment_manager());
87
88    //Construct the shared memory map and fill it
89    complex_map_type *mymap = segment.construct<complex_map_type>
90       //(object name), (first ctor parameter, second ctor parameter)
91          ("MyMap")(std::less<char_string>(), alloc_inst);
92
93    for(int i = 0; i < 100; ++i){
94       //Both key(string) and value(complex_data) need an allocator in their constructors
95       char_string  key_object(alloc_inst);
96       complex_data mapped_object(i, "default_name", alloc_inst);
97       map_value_type value(key_object, mapped_object);
98       //Modify values and insert them in the map
99       mymap->insert(value);
100    }
101    return 0;
102 }
103 //]
104 #include <boost/interprocess/detail/config_end.hpp>