Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_where_allocate.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_where_allocate
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/containers/vector.hpp>
15 #include <boost/interprocess/containers/string.hpp>
16 #include <boost/interprocess/allocators/allocator.hpp>
17 //<-
18 #include "../test/get_process_id_name.hpp"
19 //->
20
21 int main ()
22 {
23    using namespace boost::interprocess;
24    //Typedefs
25    typedef allocator<char, managed_shared_memory::segment_manager> 
26       CharAllocator;
27    typedef basic_string<char, std::char_traits<char>, CharAllocator>
28       MyShmString;
29    typedef allocator<MyShmString, managed_shared_memory::segment_manager>
30       StringAllocator;      
31    typedef vector<MyShmString, StringAllocator>
32       MyShmStringVector;
33
34    //Open shared memory
35    //Remove shared memory on construction and destruction
36    struct shm_remove
37    {
38    //<-
39    #if 1
40       shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
41       ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
42    #else
43    //->
44       shm_remove() { shared_memory_object::remove("MySharedMemory"); }
45       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
46    //<-
47    #endif
48    //->
49    } remover;
50
51    //<-
52    #if 1
53    managed_shared_memory shm(create_only, test::get_process_id_name(), 10000);
54    #else
55    //->
56    managed_shared_memory shm(create_only, "MySharedMemory", 10000);
57    //<-
58    #endif
59    //->
60
61    //Create allocators
62    CharAllocator     charallocator  (shm.get_segment_manager());
63    StringAllocator   stringallocator(shm.get_segment_manager());
64
65    //This string is in only in this process (the pointer pointing to the
66    //buffer that will hold the text is not in shared memory). 
67    //But the buffer that will hold "this is my text" is allocated from 
68    //shared memory
69    MyShmString mystring(charallocator);
70    mystring = "this is my text";
71
72    //This vector is only in this process (the pointer pointing to the
73    //buffer that will hold the MyShmString-s is not in shared memory). 
74    //But the buffer that will hold 10 MyShmString-s is allocated from 
75    //shared memory using StringAllocator. Since strings use a shared 
76    //memory allocator (CharAllocator) the 10 buffers that hold 
77    //"this is my text" text are also in shared memory.
78    MyShmStringVector myvector(stringallocator);
79    myvector.insert(myvector.begin(), 10, mystring);
80
81    //This vector is fully constructed in shared memory. All pointers
82    //buffers are constructed in the same shared memory segment
83    //This vector can be safely accessed from other processes.
84    MyShmStringVector *myshmvector = 
85       shm.construct<MyShmStringVector>("myshmvector")(stringallocator);
86    myshmvector->insert(myshmvector->begin(), 10, mystring);
87
88    //Destroy vector. This will free all strings that the vector contains
89    shm.destroy_ptr(myshmvector);
90    return 0;
91 }
92 //]
93 #include <boost/interprocess/detail/config_end.hpp>