syscall: hoist cstring() out of two functions to reduce repetition and allocations
Этот коммит содержится в:
родитель
2a17a3e56c
коммит
aea4f57a2b
1 изменённых файлов: 10 добавлений и 2 удалений
|
@ -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) {
|
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))
|
fd = int(libc_open(&data[0], int32(flag), mode))
|
||||||
if fd < 0 {
|
if fd < 0 {
|
||||||
err = getErrno()
|
err = getErrno()
|
||||||
|
@ -63,7 +63,7 @@ func Kill(pid int, sig Signal) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Getenv(key string) (value string, found bool) {
|
func Getenv(key string) (value string, found bool) {
|
||||||
data := append([]byte(key), 0)
|
data := cstring(key)
|
||||||
raw := libc_getenv(&data[0])
|
raw := libc_getenv(&data[0])
|
||||||
if raw == nil {
|
if raw == nil {
|
||||||
return "", false
|
return "", false
|
||||||
|
@ -96,6 +96,14 @@ func Mprotect(b []byte, prot int) (err error) {
|
||||||
return
|
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) {
|
func splitSlice(p []byte) (buf *byte, len uintptr) {
|
||||||
slice := (*sliceHeader)(unsafe.Pointer(&p))
|
slice := (*sliceHeader)(unsafe.Pointer(&p))
|
||||||
return slice.buf, slice.len
|
return slice.buf, slice.len
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче