]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/interprocess/example/doc_where_allocate.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / interprocess / example / doc_where_allocate.cpp
CommitLineData
7c673cae
FG
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//////////////////////////////////////////////////////////////////////////////
1e59de90 10
7c673cae
FG
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
21int 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 (void)remover;
52 //->
53
54 //<-
55 #if 1
56 managed_shared_memory shm(create_only, test::get_process_id_name(), 10000);
57 #else
58 //->
59 managed_shared_memory shm(create_only, "MySharedMemory", 10000);
60 //<-
61 #endif
62 //->
63
64 //Create allocators
65 CharAllocator charallocator (shm.get_segment_manager());
66 StringAllocator stringallocator(shm.get_segment_manager());
67
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
71 //shared memory
72 MyShmString mystring(charallocator);
73 mystring = "this is my text";
74
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);
83
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);
90
91 //Destroy vector. This will free all strings that the vector contains
92 shm.destroy_ptr(myshmvector);
93 return 0;
94}
95//]
1e59de90 96