mirror of
https://github.com/golang/go
synced 2024-11-21 20:14:52 -07:00
syscall: fix Plan 9 build.
Move mmapper from syscall.go to syscall_unix.go. Remove Sendfile from syscall_plan9.go. R=rsc, alex.brainman CC=golang-dev https://golang.org/cl/4368060
This commit is contained in:
parent
b100a29bc4
commit
400d825ea0
@ -13,11 +13,6 @@
|
||||
// errno is an operating system error number describing the failure.
|
||||
package syscall
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// StringByteSlice returns a NUL-terminated slice of bytes
|
||||
// containing the text of s.
|
||||
func StringByteSlice(s string) []byte {
|
||||
@ -33,63 +28,3 @@ func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] }
|
||||
// Single-word zero for use when we need a valid pointer to 0 bytes.
|
||||
// See mksyscall.sh.
|
||||
var _zero uintptr
|
||||
|
||||
// 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
|
||||
}
|
||||
|
@ -327,11 +327,6 @@ func Getgroups() (gids []int, err Error) {
|
||||
return make([]int, 0), nil
|
||||
}
|
||||
|
||||
// TODO
|
||||
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, errno int) {
|
||||
return -1, ENOSYS
|
||||
}
|
||||
|
||||
//sys Dup(oldfd int, newfd int) (fd int, err Error)
|
||||
//sys Open(path string, mode int) (fd int, err Error)
|
||||
//sys Create(path string, mode int, perm uint32) (fd int, err Error)
|
||||
|
@ -4,6 +4,12 @@
|
||||
|
||||
package syscall
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
|
||||
var (
|
||||
Stdin = 0
|
||||
Stdout = 1
|
||||
@ -21,3 +27,63 @@ func Errstr(errno int) string {
|
||||
}
|
||||
return errors[errno]
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user