Windows: Resize shared memory

谁都会走 提交于 2019-12-09 11:18:33

问题


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

I've read in MSDN that file mappings have a fixed size, but is there possibly some way to make a new mapping over the same memory? Like in Linux, where you can use shm_open() and then ftruncate() and mmap() it again.


回答1:


The short answer is no - you cannot resize a file mapping once it has been created. The create/copy sequence you describe is the only way I'm aware of to accomplish this with file mappings backed by the system paging file.

That said, you can manage the file backing your mapping yourself and accomplish this. Start with your own zero-initialized data file, and specify valid handles to it to your calls to CreateFileMapping().

When you need to resize, close your mappings, extend the file, and recreate your mappings. This would require some synchronization between growth/truncating operations - not trivial, but shouldn't be too difficult either.

In the end, it's the same old story. Added complexity vs performance.



来源:https://stackoverflow.com/questions/6254637/windows-resize-shared-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!