1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 //[doc_managed_external_buffer
12 #include <boost/interprocess/managed_external_buffer.hpp>
13 #include <boost/interprocess/allocators/allocator.hpp>
14 #include <boost/interprocess/containers/list.hpp>
15 #include <cstring>
16 #include <boost/aligned_storage.hpp>
17 
main()18 int main()
19 {
20    using namespace boost::interprocess;
21 
22    //Create the static memory who will store all objects
23    const int memsize = 65536;
24 
25    static boost::aligned_storage<memsize>::type static_buffer;
26 
27    //This managed memory will construct objects associated with
28    //a wide string in the static buffer
29    wmanaged_external_buffer objects_in_static_memory
30       (create_only, &static_buffer, memsize);
31 
32    //We optimize resources to create 100 named objects in the static buffer
33    objects_in_static_memory.reserve_named_objects(100);
34 
35    //Alias an integer node allocator type
36    //This allocator will allocate memory inside the static buffer
37    typedef allocator<int, wmanaged_external_buffer::segment_manager>
38       allocator_t;
39 
40    //Alias a STL compatible list to be constructed in the static buffer
41    typedef list<int, allocator_t>    MyBufferList;
42 
43    //The list must be initialized with the allocator
44    //All objects created with objects_in_static_memory will
45    //be stored in the static_buffer!
46    MyBufferList *list = objects_in_static_memory.construct<MyBufferList>(L"MyList")
47                            (objects_in_static_memory.get_segment_manager());
48    //<-
49    (void)list;
50    //->
51    //Since the allocation algorithm from wmanaged_external_buffer uses relative
52    //pointers and all the pointers constructed int the static memory point
53    //to objects in the same segment,  we can create another static buffer
54    //from the first one and duplicate all the data.
55    static boost::aligned_storage<memsize>::type static_buffer2;
56    std::memcpy(&static_buffer2, &static_buffer, memsize);
57 
58    //Now open the duplicated managed memory passing the memory as argument
59    wmanaged_external_buffer objects_in_static_memory2
60       (open_only, &static_buffer2, memsize);
61 
62    //Check that "MyList" has been duplicated in the second buffer
63    if(!objects_in_static_memory2.find<MyBufferList>(L"MyList").first)
64       return 1;
65 
66    //Destroy the lists from the static buffers
67    objects_in_static_memory.destroy<MyBufferList>(L"MyList");
68    objects_in_static_memory2.destroy<MyBufferList>(L"MyList");
69    return 0;
70 }
71 //]
72 #include <boost/interprocess/detail/config_end.hpp>
73