]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_managed_aligned_allocation.cpp
bump version to 18.2.4-pve3
[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 //[doc_managed_aligned_allocation
11 #include <boost/interprocess/managed_shared_memory.hpp>
12 #include <cassert>
13 //<-
14 #include "../test/get_process_id_name.hpp"
15 //->
16
17 int main()
18 {
19 using namespace boost::interprocess;
20
21 //Remove shared memory on construction and destruction
22 struct shm_remove
23 {
24 //<-
25 #if 1
26 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
27 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
28 #else
29 //->
30 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
31 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
32 //<-
33 #endif
34 //->
35 } remover;
36 //<-
37 (void)remover;
38 //->
39
40 //Managed memory segment that allocates portions of a shared memory
41 //segment with the default management algorithm
42 //<-
43 #if 1
44 managed_shared_memory managed_shm(create_only, test::get_process_id_name(), 65536);
45 #else
46 //->
47 managed_shared_memory managed_shm(create_only, "MySharedMemory", 65536);
48 //<-
49 #endif
50 //->
51
52 const std::size_t Alignment = 128;
53
54 //Allocate 100 bytes aligned to Alignment from segment, throwing version
55 void *ptr = managed_shm.allocate_aligned(100, Alignment);
56
57 //Check alignment
58 assert(std::size_t(static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
59
60 //Deallocate it
61 managed_shm.deallocate(ptr);
62
63 //Non throwing version
64 ptr = managed_shm.allocate_aligned(100, Alignment, std::nothrow);
65
66 //Check alignment
67 assert(std::size_t(static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
68
69 //Deallocate it
70 managed_shm.deallocate(ptr);
71
72 //If we want to efficiently allocate aligned blocks of memory
73 //use managed_shared_memory::PayloadPerAllocation value
74 assert(Alignment > managed_shared_memory::PayloadPerAllocation);
75
76 //This allocation will maximize the size of the aligned memory
77 //and will increase the possibility of finding more aligned memory
78 ptr = managed_shm.allocate_aligned
79 (3u*Alignment - managed_shared_memory::PayloadPerAllocation, Alignment);
80
81 //Check alignment
82 assert(std::size_t(static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
83
84 //Deallocate it
85 managed_shm.deallocate(ptr);
86
87 return 0;
88 }
89 //]
90