2009-06-01 23:14:57 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
// +build darwin freebsd linux openbsd
|
|
|
|
|
2009-06-01 23:14:57 -06:00
|
|
|
package syscall
|
|
|
|
|
2011-06-14 09:29:51 -06:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2010-04-02 02:11:17 -06:00
|
|
|
var (
|
|
|
|
Stdin = 0
|
|
|
|
Stdout = 1
|
|
|
|
Stderr = 2
|
|
|
|
)
|
|
|
|
|
2011-06-20 16:40:20 -06:00
|
|
|
const darwinAMD64 = OS == "darwin" && ARCH == "amd64"
|
|
|
|
|
2011-02-01 09:49:24 -07:00
|
|
|
func Syscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
|
|
|
|
func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr)
|
|
|
|
func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
|
2011-03-16 20:03:01 -06:00
|
|
|
func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr)
|
2011-02-01 09:49:24 -07:00
|
|
|
|
2009-06-01 23:14:57 -06:00
|
|
|
func Errstr(errno int) string {
|
|
|
|
if errno < 0 || errno >= int(len(errors)) {
|
2011-04-02 15:24:03 -06:00
|
|
|
return "error " + itoa(errno)
|
2009-06-01 23:14:57 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return errors[errno]
|
2009-06-01 23:14:57 -06:00
|
|
|
}
|
2011-06-14 09:29:51 -06:00
|
|
|
|
|
|
|
// Mmap manager, for use by operating system-specific implementations.
|
|
|
|
|
|
|
|
type mmapper struct {
|
|
|
|
sync.Mutex
|
|
|
|
active map[*byte][]byte // active mappings; key is last byte in mapping
|
|
|
|
mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, int)
|
|
|
|
munmap func(addr uintptr, length uintptr) int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, errno int) {
|
|
|
|
if length <= 0 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map the requested memory.
|
|
|
|
addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
|
|
|
|
if errno != 0 {
|
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slice memory layout
|
|
|
|
var sl = struct {
|
|
|
|
addr uintptr
|
|
|
|
len int
|
|
|
|
cap int
|
|
|
|
}{addr, length, length}
|
|
|
|
|
|
|
|
// Use unsafe to turn sl into a []byte.
|
|
|
|
b := *(*[]byte)(unsafe.Pointer(&sl))
|
|
|
|
|
|
|
|
// Register mapping in m and return it.
|
|
|
|
p := &b[cap(b)-1]
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
m.active[p] = b
|
|
|
|
return b, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mmapper) Munmap(data []byte) (errno int) {
|
|
|
|
if len(data) == 0 || len(data) != cap(data) {
|
|
|
|
return EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the base of the mapping.
|
|
|
|
p := &data[cap(data)-1]
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
b := m.active[p]
|
|
|
|
if b == nil || &b[0] != &data[0] {
|
|
|
|
return EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmap the memory and update m.
|
|
|
|
if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != 0 {
|
|
|
|
return errno
|
|
|
|
}
|
|
|
|
m.active[p] = nil, false
|
|
|
|
return 0
|
|
|
|
}
|