diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-04-07 17:09:10 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-04-07 17:09:10 +0000 |
commit | 405ca10418216fc078ecb29ff13a39c911e8d806 (patch) | |
tree | 39c2c0da7b6c220dd344f134fae07fba0d0266b2 /libgo/syscalls | |
parent | a751005d50fa7347131660e562c5fd9ce3dff75d (diff) | |
download | gcc-405ca10418216fc078ecb29ff13a39c911e8d806.zip gcc-405ca10418216fc078ecb29ff13a39c911e8d806.tar.gz gcc-405ca10418216fc078ecb29ff13a39c911e8d806.tar.bz2 |
libgo: Update to current Go library.
From-SVN: r172106
Diffstat (limited to 'libgo/syscalls')
-rw-r--r-- | libgo/syscalls/syscall.go | 65 | ||||
-rw-r--r-- | libgo/syscalls/syscall_unix.go | 34 | ||||
-rw-r--r-- | libgo/syscalls/sysfile_largefile.go | 1 | ||||
-rw-r--r-- | libgo/syscalls/sysfile_regfile.go | 1 |
4 files changed, 100 insertions, 1 deletions
diff --git a/libgo/syscalls/syscall.go b/libgo/syscalls/syscall.go index fa7d9d0..8c7a7ba 100644 --- a/libgo/syscalls/syscall.go +++ b/libgo/syscalls/syscall.go @@ -13,7 +13,10 @@ // the manuals for the appropriate operating system. package syscall -import "unsafe" +import ( + "sync" + "unsafe" +) func libc_syscall32(trap int32, a1, a2, a3, a4, a5, a6 int32) int32 __asm__ ("syscall"); func libc_syscall64(trap int64, a1, a2, a3, a4, a5, a6 int64) int64 __asm__ ("syscall"); @@ -46,3 +49,63 @@ func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) { } return r, 0, uintptr(GetErrno()); } + +// 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/libgo/syscalls/syscall_unix.go b/libgo/syscalls/syscall_unix.go index a29b6b5..b5a660e 100644 --- a/libgo/syscalls/syscall_unix.go +++ b/libgo/syscalls/syscall_unix.go @@ -4,6 +4,8 @@ package syscall +import "unsafe" + var ( Stdin = 0 Stdout = 1 @@ -22,3 +24,35 @@ func Uname(buf *Utsname) (errno int) { } return } + +var mapper = &mmapper{ + active: make(map[*byte][]byte), + mmap: mmap, + munmap: munmap, +} + +func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, errno int) { + return mapper.Mmap(fd, offset, length, prot, flags) +} + +func Munmap(b []byte) (errno int) { + return mapper.Munmap(b) +} + +func libc_munmap(*byte, Size_t) int __asm__ ("munmap") + +func mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, errno int) { + r0 := libc_mmap((*byte)(unsafe.Pointer(addr)), Size_t(length), prot, flag, fd, Offset_t(pos)) + ret = uintptr(unsafe.Pointer(r0)) + if ret + 1 == 0 { + errno = GetErrno() + } + return +} + +func munmap(addr uintptr, length uintptr) (errno int) { + if libc_munmap((*byte)(unsafe.Pointer(addr)), Size_t(length)) < 0 { + errno = GetErrno() + } + return +} diff --git a/libgo/syscalls/sysfile_largefile.go b/libgo/syscalls/sysfile_largefile.go index 963a624..c0c4e55 100644 --- a/libgo/syscalls/sysfile_largefile.go +++ b/libgo/syscalls/sysfile_largefile.go @@ -11,3 +11,4 @@ func libc_pwrite(fd int, buf *byte, count Size_t, offset Offset_t) Ssize_t __asm func libc_lseek(int, Offset_t, int) Offset_t __asm__ ("lseek64") func libc_truncate(path *byte, length Offset_t) int __asm__ ("truncate64") func libc_ftruncate(fd int, length Offset_t) int __asm__ ("ftruncate64") +func libc_mmap(*byte, Size_t, int, int, int, Offset_t) *byte __asm__ ("mmap64") diff --git a/libgo/syscalls/sysfile_regfile.go b/libgo/syscalls/sysfile_regfile.go index 731c59c..fcbf254 100644 --- a/libgo/syscalls/sysfile_regfile.go +++ b/libgo/syscalls/sysfile_regfile.go @@ -11,3 +11,4 @@ func libc_pwrite(fd int, buf *byte, count Size_t, offset Offset_t) Ssize_t __asm func libc_lseek(int, Offset_t, int) Offset_t __asm__ ("lseek") func libc_truncate(path *byte, length Offset_t) int __asm__ ("truncate") func libc_ftruncate(fd int, length Offset_t) int __asm__ ("ftruncate") +func libc_mmap(*byte, Size_t, int, int, int, Offset_t) *byte __asm__ ("mmap") |