Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extend the length of a memory-mapped file?

In Delphi 7, I open a file with CreateFileMapping then get a pointer by using MapViewOfFile.

How can I expand the memory and add some characters to the memory and have it saved to that file?

I have already opened the file with appropriate modes (fmOpenReadWrite, PAGE_READWRITE), and if I overwrite the characters, it gets saved to the file, but I need to add extra values in the middle of the file.

like image 336
John Avatar asked Oct 13 '11 10:10

John


People also ask

Is writing to a memory mapped file faster?

Performance: Memory mapped writing is often fast as no stream/file buffers are used. OS does the actual file writing, usually in blocks of several kilo bytes at once. One downside would be, unless you're writing sequentially there could be page faults slowing down your program.

Why is memory mapped IO faster?

Memory-mapped I/O provides several potential advantages over explicit read/write I/O, especially for low latency devices: (1) It does not require a system call, (2) it incurs almost zero overhead for data in memory (I/O cache hits), and (3) it removes copies between kernel and user space.


2 Answers

If the file mapping is backed by an actual file and not a block of memory, then you can resize the file in one of two ways:

  1. call CreateFileMapping() with a size that exceeds the current file size. The file will be resized to match the new mapping.

  2. use SetFilePointer() and SetEndOfFile() to resize the file directly, then call CreateFileMapping() with the new size.

Both conditions are described in the documentation for CreateFileMapping().

like image 157
Remy Lebeau Avatar answered Oct 17 '22 07:10

Remy Lebeau


You cannot resize file mapping created with CreateFileMapping when it's already created. See earlier discussion on the topic: Windows: Resize shared memory .

like image 32
Roman R. Avatar answered Oct 17 '22 08:10

Roman R.