Source file src/cmd/go/internal/mmap/mmap.go
1 // Copyright 2011 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 file. 4 5 // This package is a lightly modified version of the mmap code 6 // in github.com/google/codesearch/index. 7 8 // The mmap package provides an abstraction for memory mapping files 9 // on different platforms. 10 package mmap 11 12 import ( 13 "os" 14 ) 15 16 // Data is mmap'ed read-only data from a file. 17 // The backing file is never closed, so Data 18 // remains valid for the lifetime of the process. 19 type Data struct { 20 f *os.File 21 Data []byte 22 } 23 24 // Mmap maps the given file into memory. 25 // The boolean result indicates whether the file was opened. 26 // If it is true, the caller should avoid attempting 27 // to write to the file on Windows, because Windows locks 28 // the open file, and writes to it will fail. 29 func Mmap(file string) (Data, bool, error) { 30 f, err := os.Open(file) 31 if err != nil { 32 return Data{}, false, err 33 } 34 data, err := mmapFile(f) 35 36 // Closing the file causes it not to count against this process's 37 // limit on open files; however, the mapping still counts against 38 // the system-wide limit, which is typically higher. Examples: 39 // 40 // macOS process (sysctl kern.maxfilesperproc): 61440 41 // macOS system (sysctl kern.maxfiles): 122880 42 // linux process (ulimit -n) 1048576 43 // linux system (/proc/sys/fs/file-max) 100000 44 if cerr := f.Close(); cerr != nil && err == nil { 45 return data, true, cerr 46 } 47 48 // The file is still considered to be in use on Windows after 49 // it's closed because of the mapping. 50 return data, true, err 51 } 52