1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build aix darwin dragonfly freebsd hurd linux netbsd openbsd solaris
23 darwin64Bit
= runtime
.GOOS
== "darwin" && sizeofPtr
== 8
24 netbsd32Bit
= runtime
.GOOS
== "netbsd" && sizeofPtr
== 4
27 // clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
28 func clen(n
[]byte) int {
29 for i
:= 0; i
< len(n
); i
++ {
37 // Mmap manager, for use by operating system-specific implementations.
38 // Gccgo only has one implementation but we do this to correspond to gc.
42 active
map[*byte][]byte // active mappings; key is last byte in mapping
43 mmap
func(addr
, length
uintptr, prot
, flags
, fd
int, offset
int64) (uintptr, error
)
44 munmap
func(addr
uintptr, length
uintptr) error
47 func (m
*mmapper
) Mmap(fd
int, offset
int64, length
int, prot
int, flags
int) (data
[]byte, err error
) {
52 // Map the requested memory.
53 addr
, errno
:= m
.mmap(0, uintptr(length
), prot
, flags
, fd
, offset
)
58 // Slice memory layout
63 }{addr
, length
, length
}
65 // Use unsafe to turn sl into a []byte.
66 b
:= *(*[]byte)(unsafe
.Pointer(&sl
))
68 // Register mapping in m and return it.
76 func (m
*mmapper
) Munmap(data
[]byte) (err error
) {
77 if len(data
) == 0 ||
len(data
) != cap(data
) {
81 // Find the base of the mapping.
82 p
:= &data
[cap(data
)-1]
86 if b
== nil ||
&b
[0] != &data
[0] {
90 // Unmap the memory and update m.
91 if errno
:= m
.munmap(uintptr(unsafe
.Pointer(&b
[0])), uintptr(len(b
))); errno
!= nil {
98 var mapper
= &mmapper
{
99 active
: make(map[*byte][]byte),
104 func Mmap(fd
int, offset
int64, length
int, prot
int, flags
int) (data
[]byte, err error
) {
105 return mapper
.Mmap(fd
, offset
, length
, prot
, flags
)
108 func Munmap(b
[]byte) (err error
) {
109 return mapper
.Munmap(b
)
112 // Do the interface allocations only once for common
115 errEAGAIN error
= EAGAIN
116 errEINVAL error
= EINVAL
117 errENOENT error
= ENOENT
120 // errnoErr returns common boxed Errno values, to prevent
121 // allocations at runtime.
122 func errnoErr(e Errno
) error
{
136 // A Signal is a number describing a process signal.
137 // It implements the os.Signal interface.
140 func (s Signal
) Signal() {}
142 func Signame(s Signal
) string
144 func (s Signal
) String() string {
148 func Read(fd
int, p
[]byte) (n
int, err error
) {
152 race
.WriteRange(unsafe
.Pointer(&p
[0]), n
)
155 race
.Acquire(unsafe
.Pointer(&ioSync
))
158 if msanenabled
&& n
> 0 {
159 msanWrite(unsafe
.Pointer(&p
[0]), n
)
164 func Write(fd
int, p
[]byte) (n
int, err error
) {
166 race
.ReleaseMerge(unsafe
.Pointer(&ioSync
))
168 if faketime
&& (fd
== 1 || fd
== 2) {
169 n
= faketimeWrite(fd
, p
)
171 n
, err
= 0, errnoErr(Errno(-n
))
174 n
, err
= write(fd
, p
)
176 if race
.Enabled
&& n
> 0 {
177 race
.ReadRange(unsafe
.Pointer(&p
[0]), n
)
179 if msanenabled
&& n
> 0 {
180 msanRead(unsafe
.Pointer(&p
[0]), n
)