As far as I understand, you need to increase (or reduce) the existing memory mapping. Under Linux memory, implemented as a file, it is located in the / dev / shm file system. All operations in this file are the same as in regular files (and file descriptors).
if you want to expand the existing map, first expand the file size using ftruncate (as you wrote), then use mremap to expand the map with the requested size.
If you store pointers in this region, you may need to update them, but first try calling with flag 0. In this case, the system tries to increase the existing mapping to the requested size (if there is no collision with another stored memory area), and the pointers remain valid .
If the previous option is not available, use the flag MREMAP_MAYMOVE. In this case, the system is reassigned to other locations, but basically it is done efficiently (without the copy used by the system). Then update the pointers.
Abbreviation is the same as in the reverse order.
source share