Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::interprocess - allocate_aligned in shared memory?

If I use allocate_aligned to allocate an aligned memory block within a chunk of shared memory, how do I then identify that same block in another process ? E.g.

managed_shared_memory managed_shm(open_or_create, "SharedMemory", 65536);
void *ptr = managed_shm.allocate_aligned(256, 16);

How do I then find ptr from within another process ?

For non-aligned allocations I just use find_or_construct and then obviously there is a name associated with the allocation which makes it possible to find the allocation from another process. However there doesn't seem to be any way to do aligned allocations with find_or_construct and evidently I must be missing some fundamental point as to how to identify anonymous allocations.

like image 480
Paul R Avatar asked Jun 27 '26 18:06

Paul R


1 Answers

Example from the docs included below. This is applicable to pointers to memory returned allocate_aligned method as well as the vanilla allocate method.

//Process A obtains the offset of the address
managed_shared_memory::handle handle = 
   segment.get_handle_from_address(processA_address);

//Process A sends this address using any mechanism to process B

//Process B obtains the handle and transforms it again to an address
managed_shared_memory::handle handle = ...
void * processB_address = segment.get_address_from_handle(handle)
like image 198
Josh Heitzman Avatar answered Jun 30 '26 07:06

Josh Heitzman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!