]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_managed_aligned_allocation.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / interprocess / example / doc_managed_aligned_allocation.cpp
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 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 //[doc_managed_aligned_allocation
12 #include <boost/interprocess/managed_shared_memory.hpp>
13 #include <cassert>
14 //<-
15 #include "../test/get_process_id_name.hpp"
16 //->
17
18 int main()
19 {
20 using namespace boost::interprocess;
21
22 //Remove shared memory on construction and destruction
23 struct shm_remove
24 {
25 //<-
26 #if 1
27 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
28 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
29 #else
30 //->
31 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
32 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
33 //<-
34 #endif
35 //->
36 } remover;
37 //<-
38 (void)remover;
39 //->
40
41 //Managed memory segment that allocates portions of a shared memory
42 //segment with the default management algorithm
43 //<-
44 #if 1
45 managed_shared_memory managed_shm(create_only, test::get_process_id_name(), 65536);
46 #else
47 //->
48 managed_shared_memory managed_shm(create_only, "MySharedMemory", 65536);
49 //<-
50 #endif
51 //->
52
53 const std::size_t Alignment = 128;
54
55 //Allocate 100 bytes aligned to Alignment from segment, throwing version
56 void *ptr = managed_shm.allocate_aligned(100, Alignment);
57
58 //Check alignment
59 assert((static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
60
61 //Deallocate it
62 managed_shm.deallocate(ptr);
63
64 //Non throwing version
65 ptr = managed_shm.allocate_aligned(100, Alignment, std::nothrow);
66
67 //Check alignment
68 assert((static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
69
70 //Deallocate it
71 managed_shm.deallocate(ptr);
72
73 //If we want to efficiently allocate aligned blocks of memory
74 //use managed_shared_memory::PayloadPerAllocation value
75 assert(Alignment > managed_shared_memory::PayloadPerAllocation);
76
77 //This allocation will maximize the size of the aligned memory
78 //and will increase the possibility of finding more aligned memory
79 ptr = managed_shm.allocate_aligned
80 (3*Alignment - managed_shared_memory::PayloadPerAllocation, Alignment);
81
82 //Check alignment
83 assert((static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
84
85 //Deallocate it
86 managed_shm.deallocate(ptr);
87
88 return 0;
89 }
90 //]
91 /*
92
93 #include <vector>
94 #include <boost/interprocess/managed_windows_shared_memory.hpp>
95
96 int main()
97 {
98 using namespace boost::interprocess;
99 typedef boost::interprocess::
100 managed_windows_shared_memory shared_segment;
101
102 std::vector<void *> ptrs;
103 shared_segment m_segment(create_only, "shmem", 4096*16);
104 try{
105 while(1){
106 //Now I have several allocate_aligned operations:
107 ptrs.push_back(m_segment.allocate_aligned(128, 128));
108 }
109 }
110 catch(...){
111 m_segment.deallocate(ptrs.back());
112 ptrs.pop_back();
113 ptrs.push_back(m_segment.allocate_aligned(128, 128));
114 }
115 return 0;
116 }
117 */
118 #include <boost/interprocess/detail/config_end.hpp>