Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

managed_shared_memory can't allocate a chunk of more than half its allocation any more #250

Open
JulesRapanga opened this issue Feb 10, 2025 · 0 comments

Comments

@JulesRapanga
Copy link

#include <unistd.h>
#include <iostream>
#include <boost/interprocess/managed_shared_memory.hpp>

using namespace boost::interprocess;

#define HEADSPACE (1 << 10)
const size_t SIZE = 20'000'000;

struct Header {
    managed_shared_memory::handle_t buf = {};
    unsigned size = 0;
    unsigned counter = 0;
};

int main()
{
    shared_memory_object::remove("Paper name");

    size_t size = SIZE;
    size_t paper_size = (sizeof(Header) + HEADSPACE + size);
    std::cerr << getpid() << " Making paper: total_size: " << paper_size << " req size: " << size << " HEADSPACE: " << (HEADSPACE) << std::endl;
    managed_shared_memory shm(open_or_create, "Paper name", paper_size);
    Header* h = shm.find_or_construct<Header>("Header")();
    void * buf = shm.allocate_aligned(size, 32);
    h->buf = shm.get_handle_from_address(buf);
    h->size = size;
}

Above is a roughly cut down version of some production code I'm running; for a given memory size, allocate create a managed_shared_memory of a slightly larger size, construct a Header in it, and allocate the desired size as an aligned buffer (stored in the header) . In 1.86.0 and before it successfully allocates all the memory, but in 1.87.0 it throws bad_alloc. Some investigation shows that the allocated_aligned step only succeeds if the size is <=1/2 of the desired size, and that allocating smaller chunks does allow access to most of the allocated memory (i.e. you can ask for 1/10 of size 9 times but the 10th fails).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant