github.com/bir3/gocompiler@v0.9.2202/src/cmd/link/internal/ld/outbuf_darwin.go (about)

     1  // Copyright 2020 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  package ld
     6  
     7  import (
     8  	"syscall"
     9  	"unsafe"
    10  )
    11  
    12  // Implemented in the syscall package.
    13  //
    14  //go:linkname fcntl syscall.fcntl
    15  func fcntl(fd int, cmd int, arg int) (int, error)
    16  
    17  func (out *OutBuf) fallocate(size uint64) error {
    18  	stat, err := out.f.Stat()
    19  	if err != nil {
    20  		return err
    21  	}
    22  	// F_PEOFPOSMODE allocates from the end of the file, so we want the size difference.
    23  	// Apparently, it uses the end of the allocation, instead of the logical end of the
    24  	// file.
    25  	cursize := uint64(stat.Sys().(*syscall.Stat_t).Blocks * 512) // allocated size
    26  	if size <= cursize {
    27  		return nil
    28  	}
    29  
    30  	store := &syscall.Fstore_t{
    31  		Flags:   syscall.F_ALLOCATEALL,
    32  		Posmode: syscall.F_PEOFPOSMODE,
    33  		Offset:  0,
    34  		Length:  int64(size - cursize),
    35  	}
    36  
    37  	_, err = fcntl(int(out.f.Fd()), syscall.F_PREALLOCATE, int(uintptr(unsafe.Pointer(store))))
    38  	return err
    39  }
    40  
    41  func (out *OutBuf) purgeSignatureCache() {
    42  	// Apparently, the Darwin kernel may cache the code signature at mmap.
    43  	// When we mmap the output buffer, it doesn't have a code signature
    44  	// (as we haven't generated one). Invalidate the kernel cache now that
    45  	// we have generated the signature. See issue #42684.
    46  	msync(out.buf, syscall.MS_INVALIDATE)
    47  	// Best effort. Ignore error.
    48  }