Golang Windows Memory File mapping

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Windows implements the memory file mapping, Golang is actually called the Windows API, and Win32 programming no GOD horse difference.

FD, Err: = Syscall. Open ("./111.csv", Syscall. Generic_all, 0)
Defer syscall. Close (FD)

Fsize, err: = Syscall. Seek (FD, 0, 2)
Syscall. Seek (FD, 0, 0)
Number of pages per block
var pagesperblock int64 = 8192
Number of data blocks
var blocksize int64 = Int64 (Syscall. GetPageSize ()) * Pagesperblock
var blocknum int64 = Fsize/blocksize
If fsize%blocksize > 0 {
Blocknum = blocknum + 1
}
Hmap, err: = Syscall. CreateFileMapping (FD, Nil, syscall. Page_readonly, UInt32 (fsize>>32), UInt32 (fsize), nil)
Defer syscall. CloseHandle (HMAP)

var tmbgn = time. Now ()

Fmt. Println ("blocknum=", Blocknum)
var i int64
For i = 0; i < Blocknum; i++ {
var datalen int32 = Int32 (BlockSize)
if i = = (blocknum-1) {
Datalen = Int32 (fsize% blocksize)
}
Fmt. Println ("datalen=", Datalen)
Start reading memory block
var Currpos int64 = i * blocksize

var Tmplen int = int (datalen)

Addr, err: = Syscall. MapViewOfFile (Hmap, Syscall. File_map_read, 0, UInt32 (currpos), UIntPtr (Tmplen))

Defer syscall. UnmapViewOfFile (addr)
Turn addr into slice.
D: = (*[1 << 28]byte) (unsafe. Pointer (addr))
T: = D[:tmplen]
Processing Data T
}

Related Article

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.