Browse Source

syscall: hoist cstring() out of two functions to reduce repetition and allocations

pull/2293/head
Dan Kegel 3 years ago
committed by Ron Evans
parent
commit
aea4f57a2b
  1. 12
      src/syscall/syscall_libc.go

12
src/syscall/syscall_libc.go

@ -42,7 +42,7 @@ func Seek(fd int, offset int64, whence int) (off int64, err error) {
}
func Open(path string, flag int, mode uint32) (fd int, err error) {
data := append([]byte(path), 0)
data := cstring(path)
fd = int(libc_open(&data[0], int32(flag), mode))
if fd < 0 {
err = getErrno()
@ -63,7 +63,7 @@ func Kill(pid int, sig Signal) (err error) {
}
func Getenv(key string) (value string, found bool) {
data := append([]byte(key), 0)
data := cstring(key)
raw := libc_getenv(&data[0])
if raw == nil {
return "", false
@ -96,6 +96,14 @@ func Mprotect(b []byte, prot int) (err error) {
return
}
// cstring converts a Go string to a C string.
func cstring(s string) []byte {
data := make([]byte, len(s)+1)
copy(data, s)
// final byte should be zero from the initial allocation
return data
}
func splitSlice(p []byte) (buf *byte, len uintptr) {
slice := (*sliceHeader)(unsafe.Pointer(&p))
return slice.buf, slice.len

Loading…
Cancel
Save