From 400d825ea0d0471f4b395eabde6b963835250ec3 Mon Sep 17 00:00:00 2001 From: Yuval Pavel Zholkover Date: Tue, 14 Jun 2011 11:29:51 -0400 Subject: [PATCH] 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 --- src/pkg/syscall/syscall.go | 65 ------------------------------- src/pkg/syscall/syscall_plan9.go | 5 --- src/pkg/syscall/syscall_unix.go | 66 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 70 deletions(-) diff --git a/src/pkg/syscall/syscall.go b/src/pkg/syscall/syscall.go index 157abaa8b4..9f777f59e1 100644 --- a/src/pkg/syscall/syscall.go +++ b/src/pkg/syscall/syscall.go @@ -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 -} diff --git a/src/pkg/syscall/syscall_plan9.go b/src/pkg/syscall/syscall_plan9.go index 730126f231..831cbddb24 100644 --- a/src/pkg/syscall/syscall_plan9.go +++ b/src/pkg/syscall/syscall_plan9.go @@ -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) diff --git a/src/pkg/syscall/syscall_unix.go b/src/pkg/syscall/syscall_unix.go index a77e40bc6b..d17ef52d5c 100644 --- a/src/pkg/syscall/syscall_unix.go +++ b/src/pkg/syscall/syscall_unix.go @@ -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 +}