追加到一个内存映射文件 [英] appending to a memory-mapped file

查看:754
本文介绍了追加到一个内存映射文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断追加到股票报价(整数,多头,双打等)的文件。我有这个文件映射到与MMAP内存。

I'm constantly appending to a file of stock quotes (ints, longs, doubles, etc.). I have this file mapped into memory with mmap.

什么是做新追加的数据可作为内存映射的一部分,最有效的方法是什么?

What's the most efficient way to make newly appended data available as part of the memory mapping?

我明白,我可以再次(新文件描述符)打开该文件,然后mmap设置它来获取新的数据,但似乎是低效的。已经向我建议另一种方法是pre-分配在1MB块的文件,写入特定的位置,直到达到那么最终的文件ftruncate至+ 1MB。

I understand that I can open the file again (new file descriptor) and then mmap it to get the new data but that seems to be inefficient. Another approach that has been suggested to me is to pre-allocate the file in 1mb chunks, write to a specific position until reaching the end then ftruncate the file to +1mb.

有没有其他办法?

合乎理提升的帮助呢?

推荐答案

了Boost.Iostreams有固定大小的内存映射文件,所以它不会与您的特定问题的帮助。 Linux有一个接口 mremap 其工作原理如下:

Boost.IOStreams has fixed-size only memory mapped files, so it won't help with your specific problem. Linux has an interface mremap which works as follows:

void *new_mapping = mremap(mapping, size, size + GROWTH, MREMAP_MAYMOVE);
if (new_mapping == MAP_FAILED)
    // handle error
mapping = new_mapping;

这是不可移植的,但是,(不良记录)。 Mac OS X中似乎没有 mremap

This is non-portable, however (and poorly documented). Mac OS X seems not to have mremap.

在任何情况下,你不需要重新打开该文件,只需则munmap 这和 MMAP 再次

In any case, you don't need to reopen the file, just munmap it and mmap it again:

void *append(int fd, char const *data, size_t nbytes, void *map, size_t &len)
{
    // TODO: check for errors here!
    ssize_t written = write(fd, data, nbytes);
    munmap(map, len);
    len += written;
    return mmap(NULL, len, PROT_READ, 0, fd, 0);
}

一个pre-分配方案可能是非常有用的在这里。一定要跟踪文件的实际长度,并在结束前再次截断它。

A pre-allocation scheme may be very useful here. Be sure to keep track of the file's actual length and truncate it once more before closing.

这篇关于追加到一个内存映射文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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