]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_managed_aligned_allocation.cpp
1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 //[doc_managed_aligned_allocation
11 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include "../test/get_process_id_name.hpp"
19 using namespace boost::interprocess
;
21 //Remove shared memory on construction and destruction
26 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
27 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
30 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
31 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
40 //Managed memory segment that allocates portions of a shared memory
41 //segment with the default management algorithm
44 managed_shared_memory
managed_shm(create_only
, test::get_process_id_name(), 65536);
47 managed_shared_memory
managed_shm(create_only
, "MySharedMemory", 65536);
52 const std::size_t Alignment
= 128;
54 //Allocate 100 bytes aligned to Alignment from segment, throwing version
55 void *ptr
= managed_shm
.allocate_aligned(100, Alignment
);
58 assert(std::size_t(static_cast<char*>(ptr
)-static_cast<char*>(0)) % Alignment
== 0);
61 managed_shm
.deallocate(ptr
);
63 //Non throwing version
64 ptr
= managed_shm
.allocate_aligned(100, Alignment
, std::nothrow
);
67 assert(std::size_t(static_cast<char*>(ptr
)-static_cast<char*>(0)) % Alignment
== 0);
70 managed_shm
.deallocate(ptr
);
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
);
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
);
82 assert(std::size_t(static_cast<char*>(ptr
)-static_cast<char*>(0)) % Alignment
== 0);
85 managed_shm
.deallocate(ptr
);