Boost logo

Boost Users :

Subject: [Boost-users] [intprocess] Incorrect alignment in managed memory named allocations
From: Kevin J. Arunski (kevin.arunski_at_[hidden])
Date: 2011-01-25 20:51:33


It appears that the named allocation functions for the managed memory classes do not place constructed objects at the appropriate alignment for the requested type in all situations. Here is a simple example to illustrate the issue.

#include <iostream>
#include <boost/interprocess/managed_mapped_file.hpp>

// alignment of 16 on i386 and x86_64
struct vector_of_double
{
        long double x;
        long double y;
        long double z;
};

int main()
{
        namespace bip = boost::interprocess;

        {
                std::cout << "Alignment of vector_of_double is " << __alignof__(vector_of_double) << std::endl;
                bip::managed_mapped_file simple_file_mapping(bip::create_only, "simple_mapping", 65536);

                vector_of_double * test_vec = simple_file_mapping.construct<vector_of_double>("a named vector allocation")();

                std::cout << "Resulting allocation is at address: " << test_vec << std::endl;
        }

        bip::remove_file_on_destroy("simple_mapping");

        return 0;
}

On gcc targeting i386, this code prints:
Alignment of vector_of_double is 16
Resulting allocation is at address 0x???????c

Similar results on x86_64. It appears the resulting address is only aligned on a 4 byte boundary on 32 bit targets and 8 byte on 64 bit targets.

Are there workarounds? I have tried explicitly setting the alignment parameter in the mapped region's index parameter to 16, but it did not change the result.

I have tested the problem with anonymous allocations, and those do appear to be appropriately aligned. Only named allocations have the issue.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net