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

« back to all changes in this revision

Viewing changes to src/pkg/syscall/syscall.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:
13
13
// errno is an operating system error number describing the failure.
14
14
package syscall
15
15
 
16
 
import (
17
 
        "sync"
18
 
        "unsafe"
19
 
)
20
 
 
21
16
// StringByteSlice returns a NUL-terminated slice of bytes
22
17
// containing the text of s.
23
18
func StringByteSlice(s string) []byte {
33
28
// Single-word zero for use when we need a valid pointer to 0 bytes.
34
29
// See mksyscall.sh.
35
30
var _zero uintptr
36
 
 
37
 
// Mmap manager, for use by operating system-specific implementations.
38
 
 
39
 
type mmapper struct {
40
 
        sync.Mutex
41
 
        active map[*byte][]byte // active mappings; key is last byte in mapping
42
 
        mmap   func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, int)
43
 
        munmap func(addr uintptr, length uintptr) int
44
 
}
45
 
 
46
 
func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, errno int) {
47
 
        if length <= 0 {
48
 
                return nil, EINVAL
49
 
        }
50
 
 
51
 
        // Map the requested memory.
52
 
        addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
53
 
        if errno != 0 {
54
 
                return nil, errno
55
 
        }
56
 
 
57
 
        // Slice memory layout
58
 
        var sl = struct {
59
 
                addr uintptr
60
 
                len  int
61
 
                cap  int
62
 
        }{addr, length, length}
63
 
 
64
 
        // Use unsafe to turn sl into a []byte.
65
 
        b := *(*[]byte)(unsafe.Pointer(&sl))
66
 
 
67
 
        // Register mapping in m and return it.
68
 
        p := &b[cap(b)-1]
69
 
        m.Lock()
70
 
        defer m.Unlock()
71
 
        m.active[p] = b
72
 
        return b, 0
73
 
}
74
 
 
75
 
func (m *mmapper) Munmap(data []byte) (errno int) {
76
 
        if len(data) == 0 || len(data) != cap(data) {
77
 
                return EINVAL
78
 
        }
79
 
 
80
 
        // Find the base of the mapping.
81
 
        p := &data[cap(data)-1]
82
 
        m.Lock()
83
 
        defer m.Unlock()
84
 
        b := m.active[p]
85
 
        if b == nil || &b[0] != &data[0] {
86
 
                return EINVAL
87
 
        }
88
 
 
89
 
        // Unmap the memory and update m.
90
 
        if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != 0 {
91
 
                return errno
92
 
        }
93
 
        m.active[p] = nil, false
94
 
        return 0
95
 
}