Search code examples
c++boostshared-memorymemory-alignmentboost-interprocess

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.


Solution

  • 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)