Windows: resizing shared memory

When I create a shared memory segment on Windows (e.g. CreateFileMapping(INVALID_HANDLE_VALUE, ...) ), is there a way to resize it other than creating a larger segment and copying the data?

I read on MSDN that file associations are of a fixed size, but is it possible to somehow create a new association on the same memory? Like on Linux, where you can use shm_open() and then ftruncate() and mmap() again.

+7
source share
1 answer

The short answer is no - you cannot resize the file association after it is created. The create / copy sequence that you describe is the only way I know for this, with the file associations supported by the system page file.

However, you can manage the file that supports your mapping and do it yourself. Start with your own zero-initialized file and specify valid handles for your calls to CreateFileMapping() .

When you need to resize, close your mappings, expand the file, and recreate your mappings. This will require some synchronization between growth / truncation operations - not trivial, but also should not be too complicated.

In the end, this is the same old story. Added complexity and performance.

+6
source

All Articles