]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_cont.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/containers/vector.hpp>
14 #include <boost/interprocess/allocators/allocator.hpp>
15 #include <boost/interprocess/managed_shared_memory.hpp>
17 #include "../test/get_process_id_name.hpp"
22 using namespace boost::interprocess
;
23 //Remove shared memory on construction and destruction
28 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
29 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
32 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
33 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
42 //A managed shared memory where we can construct objects
43 //associated with a c-string
46 managed_shared_memory
segment(create_only
,test::get_process_id_name(), 65536);
49 managed_shared_memory
segment(create_only
,
50 "MySharedMemory", //segment name
56 //Alias an STL-like allocator of ints that allocates ints from the segment
57 typedef allocator
<int, managed_shared_memory::segment_manager
>
60 //Alias a vector that uses the previous STL-like allocator
61 typedef vector
<int, ShmemAllocator
> MyVector
;
63 int initVal
[] = {0, 1, 2, 3, 4, 5, 6 };
64 const int *begVal
= initVal
;
65 const int *endVal
= initVal
+ sizeof(initVal
)/sizeof(initVal
[0]);
67 //Initialize the STL-like allocator
68 const ShmemAllocator
alloc_inst (segment
.get_segment_manager());
70 //Construct the vector in the shared memory segment with the STL-like allocator
71 //from a range of iterators
73 segment
.construct
<MyVector
>
74 ("MyVector")/*object name*/
75 (begVal
/*first ctor parameter*/,
76 endVal
/*second ctor parameter*/,
77 alloc_inst
/*third ctor parameter*/);
79 //Use vector as your want
80 std::sort(myvector
->rbegin(), myvector
->rend());
82 //When done, destroy and delete vector from the segment
83 segment
.destroy
<MyVector
>("MyVector");
87 #include <boost/interprocess/detail/config_end.hpp>