go outbuf_darwin 源码

  • 2022-07-15
  • 浏览 (840)

golang outbuf_darwin 代码

文件路径:/src/cmd/link/internal/ld/outbuf_darwin.go

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin && go1.12
// +build darwin,go1.12

package ld

import (
	"syscall"
	"unsafe"
)

// Implemented in the syscall package.
//
//go:linkname fcntl syscall.fcntl
func fcntl(fd int, cmd int, arg int) (int, error)

func (out *OutBuf) fallocate(size uint64) error {
	stat, err := out.f.Stat()
	if err != nil {
		return err
	}
	// F_PEOFPOSMODE allocates from the end of the file, so we want the size difference.
	// Apparently, it uses the end of the allocation, instead of the logical end of the
	// the file.
	cursize := uint64(stat.Sys().(*syscall.Stat_t).Blocks * 512) // allocated size
	if size <= cursize {
		return nil
	}

	store := &syscall.Fstore_t{
		Flags:   syscall.F_ALLOCATEALL,
		Posmode: syscall.F_PEOFPOSMODE,
		Offset:  0,
		Length:  int64(size - cursize),
	}

	_, err = fcntl(int(out.f.Fd()), syscall.F_PREALLOCATE, int(uintptr(unsafe.Pointer(store))))
	return err
}

func (out *OutBuf) purgeSignatureCache() {
	// Apparently, the Darwin kernel may cache the code signature at mmap.
	// When we mmap the output buffer, it doesn't have a code signature
	// (as we haven't generated one). Invalidate the kernel cache now that
	// we have generated the signature. See issue #42684.
	syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(&out.buf[0])), uintptr(len(out.buf)), syscall.MS_INVALIDATE)
	// Best effort. Ignore error.
}

相关信息

go 源码目录

相关文章

go ar 源码

go asmb 源码

go config 源码

go data 源码

go data_test 源码

go deadcode 源码

go deadcode_test 源码

go decodesym 源码

go dwarf 源码

go dwarf_test 源码

0  赞