1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // |
3 | // (C) Copyright Ion Gaztanaga 2004-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 | ////////////////////////////////////////////////////////////////////////////// |
10 | |
11 | #include <boost/interprocess/allocators/allocator.hpp> |
12 | #include <boost/interprocess/managed_shared_memory.hpp> |
13 | #include <boost/interprocess/containers/vector.hpp> |
14 | #include <boost/interprocess/containers/list.hpp> |
15 | #include <functional> |
16 | #include <string> |
17 | #include "print_container.hpp" |
18 | #include "get_process_id_name.hpp" |
19 | |
20 | using namespace boost::interprocess; |
21 | |
22 | int main () |
23 | { |
24 | const int memsize = 65536; |
25 | std::string process_name; |
26 | test::get_process_id_name(str&: process_name); |
27 | const char *const shMemName = process_name.c_str(); |
28 | |
29 | BOOST_TRY{ |
30 | shared_memory_object::remove(filename: shMemName); |
31 | |
32 | //Create shared memory |
33 | managed_shared_memory segment(create_only, shMemName, memsize); |
34 | |
35 | //STL compatible allocator object, uses allocate(), deallocate() functions |
36 | typedef allocator<int, managed_shared_memory::segment_manager> |
37 | shmem_allocator_int_t; |
38 | |
39 | const shmem_allocator_int_t myallocator (segment.get_segment_manager()); |
40 | |
41 | const int max = 100; |
42 | void *array[max]; |
43 | |
44 | const char *allocName = "testAllocation" ; |
45 | |
46 | typedef boost::interprocess::vector<int, shmem_allocator_int_t > MyVect; |
47 | |
48 | //---- ALLOC, NAMED_ALLOC, NAMED_NEW TEST ----// |
49 | { |
50 | std::size_t i; |
51 | //Let's allocate some memory |
52 | for(i = 0; i < max; ++i){ |
53 | array[std::ptrdiff_t(i)] = segment.allocate(nbytes: i+1u); |
54 | } |
55 | |
56 | //Deallocate allocated memory |
57 | for(i = 0; i < max; ++i){ |
58 | segment.deallocate(addr: array[std::ptrdiff_t(i)]); |
59 | } |
60 | |
61 | bool res; |
62 | |
63 | MyVect *shmem_vect; |
64 | |
65 | //Construct and find |
66 | shmem_vect = segment.construct<MyVect> (name: allocName) (myallocator); |
67 | res = (shmem_vect == segment.find<MyVect>(name: allocName).first); |
68 | if(!res) |
69 | return 1; |
70 | //Destroy and check it is not present |
71 | segment.destroy<MyVect> (name: allocName); |
72 | res = (0 == segment.find<MyVect>(name: allocName).first); |
73 | if(!res) |
74 | return 1; |
75 | |
76 | //Construct, dump to a file |
77 | shmem_vect = segment.construct<MyVect> (name: allocName) (myallocator); |
78 | |
79 | if(shmem_vect != segment.find<MyVect>(name: allocName).first) |
80 | return 1; |
81 | //Destroy and check it is not present |
82 | segment.destroy<MyVect> (name: allocName); |
83 | res = (0 == segment.find<MyVect>(name: allocName).first); |
84 | if(!res) |
85 | return 1; |
86 | } |
87 | } |
88 | BOOST_CATCH(...){ |
89 | shared_memory_object::remove(filename: shMemName); |
90 | BOOST_RETHROW |
91 | } BOOST_CATCH_END |
92 | shared_memory_object::remove(filename: shMemName); |
93 | return 0; |
94 | } |
95 | |