Closed niXman closed 3 years ago
can anyone please ping Ion Gaztañaga the author of boost.interprocess?
thanks!
First of all, you can't grow the mapped file wile it's open. "grow" modifies internal data structures and the previous mapping might be invalid, see: https://www.boost.org/doc/libs/1_76_0/doc/html/interprocess/managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_advanced_features.growing_managed_memory
The size to grow can't be know before hand because there is additional metadata to be stored for each allocation. In your case, you need to allocate a map node, and also memory for two strings. "grow" is also a non-trivial operation so calling grow for each insertion is overkill, a bit like reallocating a vector for each insertion. I'd double the size of the mapped file or increase it a 50% for each "grow" call.
@igaztanaga thank you for answer!
that is, do you suggest opening a memory-mapped-file with an initial size, for example 1Mb, adding entries to it until the bad_alloc exception is thrown, close the file and reopen it with a size of 2 Mb? right?
let me ask you one more question. let's say I know the length of the key string, and the length of the value string. how can I find out how many bytes in the file will actually be used to store these lines?
best!
@igaztanaga could you answer please?
It is not easy to determine how many bytes you will need, just like it is not easy to determine it when allocating something in heap (it depends on the alignment, fragmentation, etc.). In Boost.Interprocess there are bookeeping data structures for each allocation. For your map you need to allocate the tree-node and two additional allocations for he key string and the value string. In theory, you'll need aprox. 64 bytes of overhead for each allocation, but it's better to test it yourself observing how get_free_memory() evolves.
hi guys!
I want to use boost::interprocess to store some resources (strings) into
bi::map<bi::string, bi::string>
that usesbi::managed_mapped_file
so I can put the resources in the map once, take the file, and distribute it with the program.the problem is that I cannot calculate the required size in advance, so I want to use
bi::managed_mapped_file::grow ()
before inserting another element into the map. but it doesn't work as i expected, at some point i get exception:what(): boost::interprocess::bad_alloc
I know the length of the key string and the length of the value string, how can I find out the size by which I must
grow()
before inserting another element into the map?now my code looks like this:
(or: https://wandbox.org/permlink/jtTOzlMh16ZS8jG6)
any ideas?
best!