]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_where_allocate.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/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>
18 #include "../test/get_process_id_name.hpp"
23 using namespace boost::interprocess
;
25 typedef allocator
<char, managed_shared_memory::segment_manager
>
27 typedef basic_string
<char, std::char_traits
<char>, CharAllocator
>
29 typedef allocator
<MyShmString
, managed_shared_memory::segment_manager
>
31 typedef vector
<MyShmString
, StringAllocator
>
35 //Remove shared memory on construction and destruction
40 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
41 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
44 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
45 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
56 managed_shared_memory
shm(create_only
, test::get_process_id_name(), 10000);
59 managed_shared_memory
shm(create_only
, "MySharedMemory", 10000);
65 CharAllocator
charallocator (shm
.get_segment_manager());
66 StringAllocator
stringallocator(shm
.get_segment_manager());
68 //This string is in only in this process (the pointer pointing to the
69 //buffer that will hold the text is not in shared memory).
70 //But the buffer that will hold "this is my text" is allocated from
72 MyShmString
mystring(charallocator
);
73 mystring
= "this is my text";
75 //This vector is only in this process (the pointer pointing to the
76 //buffer that will hold the MyShmString-s is not in shared memory).
77 //But the buffer that will hold 10 MyShmString-s is allocated from
78 //shared memory using StringAllocator. Since strings use a shared
79 //memory allocator (CharAllocator) the 10 buffers that hold
80 //"this is my text" text are also in shared memory.
81 MyShmStringVector
myvector(stringallocator
);
82 myvector
.insert(myvector
.begin(), 10, mystring
);
84 //This vector is fully constructed in shared memory. All pointers
85 //buffers are constructed in the same shared memory segment
86 //This vector can be safely accessed from other processes.
87 MyShmStringVector
*myshmvector
=
88 shm
.construct
<MyShmStringVector
>("myshmvector")(stringallocator
);
89 myshmvector
->insert(myshmvector
->begin(), 10, mystring
);
91 //Destroy vector. This will free all strings that the vector contains
92 shm
.destroy_ptr(myshmvector
);
96 #include <boost/interprocess/detail/config_end.hpp>