简体   繁体   中英

Fastest way to get a part of a disk file into a particular (pre-allocated) memory address Windows/C++?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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