1 //////////////////////////////////////////////////////////////////////////////
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)
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"); }
53 managed_shared_memory shm(create_only, test::get_process_id_name(), 10000);
56 managed_shared_memory shm(create_only, "MySharedMemory", 10000);
62 CharAllocator charallocator (shm.get_segment_manager());
63 StringAllocator stringallocator(shm.get_segment_manager());
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
69 MyShmString mystring(charallocator);
70 mystring = "this is my text";
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);
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);
88 //Destroy vector. This will free all strings that the vector contains
89 shm.destroy_ptr(myshmvector);
93 #include <boost/interprocess/detail/config_end.hpp>