将磁盘文件的一部分放入特定(预分配)内存地址 Windows/C++ 的最快方法?

发布于 2024-10-06 02:04:50 字数 500 浏览 0 评论 0原文

怎么样。

我在本机 Windows 中工作。也就是说,我希望编写一个函数,其原型类似于:

void getData(uint8_t* p, std::string const& fn, size_t off, 
    size_t s, boost::function<void()> const& F);

所以我有一块内存,预先分配在堆上(使用 new )。我在磁盘上有一个文件。我想指定一个指向我的内存块的指针(“p”)、文件名(“fn”)和其中的偏移量(“off”)、大小(“s”)和回调(“F”) 。当我的内存“p”已被文件“fn”中偏移量“off”处的“s”字节填充时,我希望子例程最终调用“F”。我将保证“p”、“s”和“off”与某个预定义的2的幂对齐,但我不希望发生可能避免的复制。理想情况下,它会将数据从磁盘直接通过 DMA 传输到我的位置。

我已经研究了内存映射文件,但这需要将数据从映射区域复制到我的“p”中。难道就没有更快的方法吗?

Howdo.

I'm working in native Windows. That said I'm looking to write a function whose prototype is something like:

void getData(uint8_t* p, std::string const& fn, size_t off, 
    size_t s, boost::function<void()> const& F);

So I've a chunk of memory, preallocated on the heap (with new). I have a file on disk. I'd like to specify a pointer into my chunk of memory ("p"), a filename ("fn") and offset therein ("off"), a size ("s") and a callback ("F"). I'd like the subroutine to eventually call "F" when my memory "p" has been populated with "s" bytes from the file "fn" at offset "off". I will guarantee "p", "s" and "off" are aligned to some predefined power of 2, but I'd like no copying that can possibly be avoided to occur. Ideally, it would DMA the data from the disk to my location directly.

I've looked into memory-mapping the file, but that would require copying the data from the mapped area into my "p". Is there no faster way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风启觞 2024-10-13 02:04:50

为什么不直接使用内存映射 MapViewOfFileEx

您可以使用 p 作为 lpBaseAddress 参数来将文件映射到进程地址空间中您想要的任何位置。

Why not just use memory mapping with MapViewOfFileEx?

You can use p as the lpBaseAddress parameter to map the file wherever you want to in your process's address space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文