github.com/Rookout/GoSDK@v0.1.48/pkg/services/assembler/internal/bio/buf_mmap.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE.assembler file. 4 5 //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || (solaris && go1.20) 6 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris,go1.20 7 8 package bio 9 10 import ( 11 "runtime" 12 "sync/atomic" 13 "syscall" 14 ) 15 16 17 18 19 20 21 22 23 24 25 26 27 var mmapLimit int32 = 1<<31 - 1 28 29 func init() { 30 31 if runtime.GOOS == "linux" { 32 mmapLimit = 30000 33 } 34 } 35 36 func (r *Reader) sliceOS(length uint64) ([]byte, bool) { 37 38 39 const threshold = 16 << 10 40 if length < threshold { 41 return nil, false 42 } 43 44 45 if atomic.AddInt32(&mmapLimit, -1) < 0 { 46 atomic.AddInt32(&mmapLimit, 1) 47 return nil, false 48 } 49 50 51 off := r.Offset() 52 align := syscall.Getpagesize() 53 aoff := off &^ int64(align-1) 54 55 data, err := syscall.Mmap(int(r.f.Fd()), aoff, int(length+uint64(off-aoff)), syscall.PROT_READ, syscall.MAP_SHARED|syscall.MAP_FILE) 56 if err != nil { 57 return nil, false 58 } 59 60 data = data[off-aoff:] 61 r.MustSeek(int64(length), 1) 62 return data, true 63 }