将磁盘文件的一部分放入特定(预分配)内存地址 Windows/C++ 的最快方法?
怎么样。
我在本机 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接使用内存映射 MapViewOfFileEx?
您可以使用
p
作为lpBaseAddress
参数来将文件映射到进程地址空间中您想要的任何位置。Why not just use memory mapping with MapViewOfFileEx?
You can use
p
as thelpBaseAddress
parameter to map the file wherever you want to in your process's address space.