I am trying to use mmap
for mapping and sharing data between processes on my device. My target is an embedded device running Embedded Linux
My processes are implemented using C++, and using containers such as std::list
and std::map
. Obviously the size of the containers are changing as the program runs.
If I use a structure to be shared between processes as below for example :
struct MYSTRUCT
{
int val1;
int val2;
list <int> list1;
};
MYSTRUCT myStruct;
// later as the program runs for example...
myStruct.list1.push_back(100);
I would then like to map this using mmap
. The API for mmap is as follows :
void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);
For the length
parameter, can I use sizeof(myStruct) + myStruct.list1.size()
for example ?
For the length parameter, can I use sizeof(myStruct) + myStruct.list1.size() for example ?
No, because:
list.size()
is the count of elements in the list. You would need list.size() * sizeof(int)
to store the bare elements, but that doesn't account for the storage of list
itself or its node overhead ...
you probably don't have portable access to the list's internal node type anyway, so you can't reserve space for list.size() * sizeof(list<int>::node_type)
or whatever
even if you could, the nodes will typically contain pointers, so they won't be usable in another process unless you can guarantee they're mapped at exactly the same address
and if you can do that, you also need to provide a custom allocator that knows it's supposed to allocate nodes in shared memory in the first place (you can't just memcpy
a linked list as if it were a contiguous block of data)
tl;dr you can't really do this with standard library types. Either use Boost.Interprocess, or serialize your data into shared memory, or store it in a flat POD/trivial layout in shared memory in the first place.