如何在Go中使用Mmap()映射一个大于2GB的文件?

huangapple go评论104阅读模式
英文:

How do you Mmap() a file bigger than 2GB in Go?

问题

syscall.Mmap()调用需要一个int类型的长度参数,这个参数只能支持2GB大小的文件。那么如何映射一个更大的文件呢?

注意:我使用的是64位系统,所以地址空间不是问题。

英文:

The syscall.Mmap() call takes a length argument of type int, which is only good for 2GB. How do I mmap a bigger file then?

Note: 64-bit system, so address space is not a problem.

答案1

得分: 4

在http://golang.org/src/pkg/syscall/syscall_unix.go中查看mmapper上的Mmap方法。您应该能够复制该代码并根据需要进行调整。

当然,您将无法将mmap映射到[]byte,因为切片长度被定义为“int”(目前在所有地方都是32位)。您可以将mmap映射到更大的元素类型(例如[]int32),或者只是操作指向内存的指针,但它不会是syscall.Mmap的即插即用替代品。

英文:

Look in http://golang.org/src/pkg/syscall/syscall_unix.go at the Mmap method on mmapper. You should be able to copy that code and adapt it as required.

Of course you won't be able to mmap to a []byte, since slice lengths are defined to be "int" (which is 32-bit everywhere at the moment). You could mmap to a larger element type (e.g. []int32), or just muck with the pointer to the memory, but it won't be a drop-in replacement to syscall.Mmap.

huangapple
  • 本文由 发表于 2012年7月24日 08:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/11622438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定