简体   繁体   English

截断内存映射文件

[英]Truncating a memory mapped file

I am currently using Boost.Iostreams to memory map multiple files for writing out very large data sets. 我目前正在使用Boost.Iostreams内存映射多个文件,以写出非常大的数据集。 The amount of data to be written varies with a guarantee that it will not exceed a predefined limit. 要写入的数据量会有所不同,并保证不会超出预定义的限制。 Usually the full capacity of the mapped file is used but sometimes it is not, with only a very small portion being written. 通常,使用映射文件的全部容量,但有时不使用,仅写入很小的一部分。

My issue is that I do not know beforehand the amount of data to be written, so each memory mapped file is created with an equal size. 我的问题是,我事先不知道要写入的数据量,因此每个内存映射文件都是以相等的大小创建的。 As a result the program is less efficient as the files that have not been used to their full capacity are zero filled upon closing. 结果,该程序效率较低,因为在关闭时尚未完全使用的文件将被零填充。

Is there a way of truncating the file before closing using Boost.Iostreams or another cross platform library (preferably boost)? 有没有一种方法可以使用Boost.Iostreams或另一个跨平台库(最好是boost)在关闭之前截断文件?

I know there are other similar questions but I don't believe they offer a cross platform solution. 我知道还有其他类似的问题,但我不认为它们提供了跨平台的解决方案。

You can use the truncate(2) or the ftruncate(2) system call to truncate the size of an existing file. 您可以使用truncate(2)ftruncate(2)系统调用来截断现有文件的大小。

Note, that use should munmap() the file, before truncating in. If you review the contents of the mmap(2) man page, it documents that truncating an mmap-ed file is undefined behavior. 请注意,该使用应在截断之前先对文件进行munmap() 。如果查看mmap(2)手册页的内容,则该文件说明截断mmap-ed文件是未定义的行为。

But, munmap() ing the file before truncating it should not break any rules. 但是,在将文件截断之前munmap()处理不应破坏任何规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM