1

I'm using Boost Interprocess to map a vector in shared memory.

This is the code I have from Boost tutorials:

using namespace boost::interprocess;
using ShmemAllocator = allocator<int, managed_shared_memory::segment_manager>;
using MyVector = boost::interprocess::vector<int, ShmemAllocator> MyVector;

//                                                    question refers to this size
//                                                                V
managed_shared_memory segment(open_or_create, "MySharedMemory", 65536); 

const ShmemAllocator alloc_inst (segment.get_segment_manager());

MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
myvector->resize(SIZE);

I resize my vector after creating the managed_shared_memory.

However, what happens if the managed_shared_memory size argument is smaller than the vector memory allocation?

Rewording the question: how do I choose a size for the managed_shared_memory?

user997112
  • 29,025
  • 43
  • 182
  • 361

1 Answers1

1

However, what happens if the managed_shared_memory size argument is smaller than the vector memory allocation?

Have you tried? Live Demo:

terminate called after throwing an instance of 'boost::container::length_error'
  what():  get_next_capacity, allocator's max size reached

Rewording the question: how do I choose a size for the managed_shared_memory?

Carefully. Seriously, see e.g.:

Also, segments can be resized/grown, and I have multiple answers on that ranging from simple to advanced: https://stackoverflow.com/search?tab=newest&q=user%3a85371%20interprocess%20grow

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks for your answer. So when I resize my vector, I have to re-create a new memory-backed segment and copy my vector in to there, use that to continue and delete the old memory backed segment? – user997112 Jan 28 '22 at 17:21
  • I want to use the vector as a vector and not have to worry about reallocations but it seems I do need to worry about them and I can't find an example how to handle this. – user997112 Jan 28 '22 at 17:45
  • I do not think I'm following either of your comments. Surely you can resize a vector as you would normally. What is the reason you think you need to do more? – sehe Jan 28 '22 at 21:55
  • Here's what I am thinking of: http://coliru.stacked-crooked.com/a/4b82078b4a7bd907 - what is your additional concern? – sehe Jan 28 '22 at 22:31
  • Sorry for the late reply. So the problem is the vector memory consumption cannot exceed the managed segment size. but most-likely the vector won't know about this. So I was wondering if there's a way of handling this management segment size, so the vector can just keep adding (within reason). – user997112 Jan 31 '22 at 16:48
  • Yeah. Probably in the linked answers I frequently do the same suggestion: just allocate a huge sparse segment and never run out of space (within reason). E.g. you should be able to create a segment of 42TB and only the space actually used will be claimed on platforms I'm aware of. – sehe Jan 31 '22 at 18:28