Windows:调整共享内存的大小 [英] Windows: Resize shared memory

查看:91
本文介绍了Windows:调整共享内存的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Windows 上创建共享内存段时(如 CreateFileMapping(INVALID_HANDLE_VALUE, ...)),除了创建更大的段并复制数据之外,还有什么方法可以调整它的大小?

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?

我在 MSDN 中读到文件映射具有固定大小,但是否有可能在同一内存上创建新映射?就像在 Linux 中一样,您可以在其中使用 shm_open(),然后再次使用 ftruncate()mmap().

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.

推荐答案

简短的回答是否定的 - 一旦创建文件映射,您就无法调整其大小.您描述的创建/复制序列是我所知道的通过系统分页文件支持的文件映射来完成此操作的唯一方法.

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.

也就是说,您可以自己管理支持映射的文件并完成此操作.从您自己的零初始化数据文件开始,并为您对 CreateFileMapping() 的调用指定有效句柄.

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.

这篇关于Windows:调整共享内存的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆