| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* @file std_string_vector.cpp |
| 3 |
|
|
* @author Vincent Berenz |
| 4 |
|
|
* @license License BSD-3-Clause |
| 5 |
|
|
* @copyright Copyright (c) 2019, New York University and Max Planck |
| 6 |
|
|
* Gesellschaft. |
| 7 |
|
|
* @date 2019-05-22 |
| 8 |
|
|
* |
| 9 |
|
|
* @brief This demonstrate how to use the equivalent of std::string in the |
| 10 |
|
|
* shared memory |
| 11 |
|
|
*/ |
| 12 |
|
|
#include <boost/interprocess/allocators/allocator.hpp> |
| 13 |
|
|
#include <boost/interprocess/containers/string.hpp> |
| 14 |
|
|
#include <boost/interprocess/containers/vector.hpp> |
| 15 |
|
|
#include <boost/interprocess/managed_shared_memory.hpp> |
| 16 |
|
|
|
| 17 |
|
✗ |
int main() |
| 18 |
|
|
{ |
| 19 |
|
|
using namespace boost::interprocess; |
| 20 |
|
|
// Typedefs |
| 21 |
|
|
typedef allocator<char, managed_shared_memory::segment_manager> |
| 22 |
|
|
CharAllocator; |
| 23 |
|
|
typedef basic_string<char, std::char_traits<char>, CharAllocator> |
| 24 |
|
|
MyShmString; |
| 25 |
|
|
typedef allocator<MyShmString, managed_shared_memory::segment_manager> |
| 26 |
|
|
StringAllocator; |
| 27 |
|
|
typedef vector<MyShmString, StringAllocator> MyShmStringVector; |
| 28 |
|
|
|
| 29 |
|
|
// Open shared memory |
| 30 |
|
|
// Remove shared memory on construction and destruction |
| 31 |
|
|
struct shm_remove |
| 32 |
|
|
{ |
| 33 |
|
✗ |
shm_remove() |
| 34 |
|
|
{ |
| 35 |
|
✗ |
shared_memory_object::remove("MySharedMemory"); |
| 36 |
|
|
} |
| 37 |
|
✗ |
~shm_remove() |
| 38 |
|
|
{ |
| 39 |
|
✗ |
shared_memory_object::remove("MySharedMemory"); |
| 40 |
|
|
} |
| 41 |
|
✗ |
} remover; |
| 42 |
|
|
|
| 43 |
|
✗ |
managed_shared_memory shm(create_only, "MySharedMemory", 10000); |
| 44 |
|
|
|
| 45 |
|
|
// Create allocators |
| 46 |
|
✗ |
CharAllocator charallocator(shm.get_segment_manager()); |
| 47 |
|
✗ |
StringAllocator stringallocator(shm.get_segment_manager()); |
| 48 |
|
|
|
| 49 |
|
|
// This string is in only in this process (the pointer pointing to the |
| 50 |
|
|
// buffer that will hold the text is not in shared memory). |
| 51 |
|
|
// But the buffer that will hold "this is my text" is allocated from |
| 52 |
|
|
// shared memory |
| 53 |
|
✗ |
MyShmString mystring(charallocator); |
| 54 |
|
✗ |
mystring = "this is my text"; |
| 55 |
|
|
|
| 56 |
|
|
// This vector is only in this process (the pointer pointing to the |
| 57 |
|
|
// buffer that will hold the MyShmString-s is not in shared memory). |
| 58 |
|
|
// But the buffer that will hold 10 MyShmString-s is allocated from |
| 59 |
|
|
// shared memory using StringAllocator. Since strings use a shared |
| 60 |
|
|
// memory allocator (CharAllocator) the 10 buffers that hold |
| 61 |
|
|
//"this is my text" text are also in shared memory. |
| 62 |
|
✗ |
MyShmStringVector myvector(stringallocator); |
| 63 |
|
✗ |
myvector.insert(myvector.begin(), 10, mystring); |
| 64 |
|
|
|
| 65 |
|
|
// This vector is fully constructed in shared memory. All pointers |
| 66 |
|
|
// buffers are constructed in the same shared memory segment |
| 67 |
|
|
// This vector can be safely accessed from other processes. |
| 68 |
|
|
MyShmStringVector *myshmvector = |
| 69 |
|
✗ |
shm.construct<MyShmStringVector>("myshmvector")(stringallocator); |
| 70 |
|
✗ |
myshmvector->insert(myshmvector->begin(), 10, mystring); |
| 71 |
|
|
|
| 72 |
|
|
// Destroy vector. This will free all strings that the vector contains |
| 73 |
|
✗ |
shm.destroy_ptr(myshmvector); |
| 74 |
|
✗ |
return 0; |
| 75 |
|
|
} |
| 76 |
|
|
|