Boost logo

Boost Users :

Subject: Re: [Boost-users] [interprocess] Determine size of managed_shared_memory
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2010-08-05 14:54:46

On 05/08/2010 16:39, Kai Benndorf wrote:
> Hi all,
> I want to create a managed_shared_memory in exactly the size I needed.
> Therefore I do something like this:
> size = sizeof( common_managed_shared_memory );
> size += sizeof( MyClassInSharedMem );
> managed_shared_memory segment(create_only, "name", size );
> MyClassInSharedMem allocates itself some memory by segment->allocate().
> I added this also to the size, but get a boost::interprocess::bad_alloc
> exception on allocating this additional memory.
> Are there some magic bytes I forgot to add?

Yes, but you can't calculate it, because there are memory allocation
bookeeping and fragmentation issues that change in runtime depending on
your allocation/deallocation pattern. And shared memory is allocated by
pages by the OS (4K on linux 64k on windows), so any allocation will be
in practice allocated rounded to a page:

managed_shared_memory segment(create_only, "name", 20);

will waste the same memory as:

managed_shared_memory segment(create_only, "name", 4096);


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at