简体   繁体   English

升级内存映射文件是否在Linux上归零

[英]Is boost memory mapped file zeroed on Linux

I'm re-learning C++, and I need to use memory mapped files. 我正在重新学习C ++,我需要使用内存映射文件。 I decided to use boost (since it seems to be solid library). 我决定使用boost(因为它似乎是实体库)。

I created a memory mapped file mapping to an array of doubles, and wrote to first double in this array. 我创建了一个映射到双精度数组的内存映射文件,并写入此数组中的第一个double。 On disk file contained some data in first four bytes, and rest of this were zeroed, this was curious for me as generally if I obtain a pointer in C++ to memory location, in most cases I have to assume that it contains garbage. 在磁盘文件中包含前四个字节中的一些数据,其余部分归零,这对我来说很奇怪,如果我在C ++中获取指向内存位置的指针,在大多数情况下我必须假设它包含垃圾。

Do I have any guarantees that newly created memory mapped files will be zeroed (at least on Linux)? 我是否有任何保证新创建的内存映射文件将被清零(至少在Linux上)? I didn't find any reference for that. 我没有找到任何参考。

BOOST_AUTO_TEST_CASE(OpenMMapFile){

boost::iostreams::mapped_file file;

boost::iostreams::mapped_file_params params;

params.path = "/tmp/mmaptest-1";
params.mode = std::ios::in | std::ios::out;
params.new_file_size =  10*sizeof(double);

file.open(params);

double* data = static_cast<double*>((void*)file.data());

data[0] = 12;

file.close();

}

Here is the file contents: 这是文件内容:

cat /tmp/mmaptest-1 | base64
AAAAAAAAKEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA =

EDIT 编辑

As @Zan pointed --- boost actually uses ftruncate to resize mmaped files, so zeroing is guaranteed (at least on Linux). 正如@Zan指出的那样--- boost实际上使用ftruncate来调整mmaped文件的大小,因此保证归零(至少在Linux上)。

A memory mapped file contains whatever was in the file. 内存映射文件包含文件中的内容。

If it is a new file, it has been extended to the right size and the extension will contain zeros. 如果它是一个新文件,它已扩展到正确的大小,扩展名将包含零。 Extending a file is usually done with the ftruncate function. 通常使用ftruncate函数来扩展文件。

The ftruncate manpage says: ftruncate联机帮助页说:

  If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\\0'). 

So yes, zeros are guaranteed. 所以是的,保证零。

I think boost is zeroing the file for you in order to achieve that the mapped address space is really backed up by disk-space and not by a sparse file. 我认为为了实现映射的地址空间实际上是由磁盘空间而不是稀疏文件备份,boost会将文件归零。 This is slow, especially if you want to create a big address space up front, which might never be fully used -- just so that you can allocate many objects in this address space. 这很慢,特别是如果你想要预先创建一个大的地址空间,这可能永远不会被完全使用 - 只是为了你可以在这个地址空间中分配许多对象。 They are doing this, as there is no useable way on UNIXs to handle out-of-disk-space when writing to memory mapped sparse files (ignoring for the moment such sick solutions as setjmp/longjmp). 他们正在这样做,因为在写入内存映射的稀疏文件时,UNIX上没有可用的方法来处理磁盘空间(暂时忽略像setjmp / longjmp这样的病态解决方案)。 But you have still the possibility that some other process truncates the file on disk, in which case the before mentioned problem rears its head again. 但是你仍有可能某些其他进程截断磁盘上的文件,在这种情况下前面提到的问题再次出现。

Unfortunately they are also doing this (allocating disk space matching the size of the address space instead of using a sparse file) on Windows, where structured exception handling exists. 不幸的是,他们也在Windows上执行此操作(分配与地址空间大小匹配的磁盘空间而不是使用稀疏文件),其中存在结构化异常处理。

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

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