~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/syscall/syscall_unix.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
package syscall
6
6
 
 
7
import (
 
8
        "sync"
 
9
        "unsafe"
 
10
)
 
11
 
 
12
 
7
13
var (
8
14
        Stdin  = 0
9
15
        Stdout = 1
10
16
        Stderr = 2
11
17
)
12
18
 
 
19
const darwinAMD64 = OS == "darwin" && ARCH == "amd64"
 
20
 
13
21
func Syscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
14
22
func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr)
15
23
func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
21
29
        }
22
30
        return errors[errno]
23
31
}
 
32
 
 
33
// Mmap manager, for use by operating system-specific implementations.
 
34
 
 
35
type mmapper struct {
 
36
        sync.Mutex
 
37
        active map[*byte][]byte // active mappings; key is last byte in mapping
 
38
        mmap   func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, int)
 
39
        munmap func(addr uintptr, length uintptr) int
 
40
}
 
41
 
 
42
func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, errno int) {
 
43
        if length <= 0 {
 
44
                return nil, EINVAL
 
45
        }
 
46
 
 
47
        // Map the requested memory.
 
48
        addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
 
49
        if errno != 0 {
 
50
                return nil, errno
 
51
        }
 
52
 
 
53
        // Slice memory layout
 
54
        var sl = struct {
 
55
                addr uintptr
 
56
                len  int
 
57
                cap  int
 
58
        }{addr, length, length}
 
59
 
 
60
        // Use unsafe to turn sl into a []byte.
 
61
        b := *(*[]byte)(unsafe.Pointer(&sl))
 
62
 
 
63
        // Register mapping in m and return it.
 
64
        p := &b[cap(b)-1]
 
65
        m.Lock()
 
66
        defer m.Unlock()
 
67
        m.active[p] = b
 
68
        return b, 0
 
69
}
 
70
 
 
71
func (m *mmapper) Munmap(data []byte) (errno int) {
 
72
        if len(data) == 0 || len(data) != cap(data) {
 
73
                return EINVAL
 
74
        }
 
75
 
 
76
        // Find the base of the mapping.
 
77
        p := &data[cap(data)-1]
 
78
        m.Lock()
 
79
        defer m.Unlock()
 
80
        b := m.active[p]
 
81
        if b == nil || &b[0] != &data[0] {
 
82
                return EINVAL
 
83
        }
 
84
 
 
85
        // Unmap the memory and update m.
 
86
        if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != 0 {
 
87
                return errno
 
88
        }
 
89
        m.active[p] = nil, false
 
90
        return 0
 
91
}