Memory ing to quickly read and write files

Source: Internet
Author: User

Procedure mapmemoryfilecopy (sourcefilename, targetfilename: string );
Const
Buffersize = 1024*1024*64;
VaR
Sourcehandle: thandle; // source file handle
Targethandle: thandle; // target file handle
Fsize: Cardinal; // the file size is 32 characters low.
Highsize: Cardinal; // 32-bit File Size
Totalsize: int64; // file Overview
Smaphandle: thandle; // Source File Memory ing handle
Tmaphandle: thandle; // memory ing handle of the target file
Psource: pointer; // Source File Memory ing view Address
PTARGET: pointer; // address of the memory ing view of the target file
Startpos: int64;
Mapsize: Cardinal;
Function makeint64 (high, low: Cardinal): int64;
Begin
Move (pchar (@ high) ^, pchar (longint (@ result) + sizeof (Cardinal) ^,
Sizeof (Cardinal ));
Move (pchar (@ low) ^, pchar (@ result) ^, sizeof (Cardinal ));
End;

Function high32 (Num: int64): Cardinal;
Begin
Move (pchar (longint (@ num) + sizeof (Cardinal) ^, pchar (@ result) ^,
Sizeof (Cardinal ));
End;

Function low32 (Num: int64): Cardinal;
Begin
Move (pchar (@ num) ^, pchar (@ result) ^, sizeof (Cardinal ));
End;
Begin
// Source file
Sourcehandle: = fileopen (sourcefilename, fmopenread );
Fsize: = getfilesize (sourcehandle, @ highsize );
Totalsize: = highsize * 1024*1024*1024*4 + fsize;
Smaphandle: = createfilemapping (sourcehandle, nil, page_readonly,
Highsize, fsize, nil );
Closehandle (sourcehandle );
// Target file
Targethandle: = filecreate (targetfilename );
Tmaphandle: = createfilemapping (targethandle, nil, page_readwrite,
Highsize, fsize, nil );
Closehandle (targethandle );

// Initialize the variable
Startpos: = 0;
Mapsize: = buffersize;

While startpos <totalsize do
Begin
If startpos + buffersize> totalsize then
Mapsize: = totalsize-startpos;
// Create a ing view. The obtained psource and pTARGET pointers can be used for memory operations like normal pointers.
Psource: = mapviewoffile (smaphandle, file_map_read,
High32 (startpos), low32 (startpos), mapsize );
PTARGET: = mapviewoffile (tmaphandle, file_map_read or
File_map_write,
High32 (startpos), low32 (startpos), mapsize );
// Copy
Move (pbyte (psource) ^, pbyte (pTARGET) ^, mapsize );
Startpos: = startpos + mapsize;
// Cancel the memory ing View
Unmapviewoffile (psource );
Unmapviewoffile (pTARGET );
End;
// Close file ing
Closehandle (smaphandle );
Closehandle (tmaphandle );
End;
VaR
T: longint;

Begin
T: = gettickcount ();
Mapmemoryfilecopy ('C:/newdl. VCT ', 'c:/11. VCT ');
T: = gettickcount ()-T;
Showmessage (inttostr (t ));

End;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.