Skip to content

Commit c1aa152

Browse files
aykevldeadprogram
authored andcommitted
unix: avoid possible heap allocation with -opt=0
This heap allocation would normally be optimized away, but with -opt=0 perhaps not. This is a problem if the conservative GC is used, because the conservative GC needs to be initialized before use.
1 parent dd3d8a3 commit c1aa152

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/runtime/runtime_unix.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,16 @@ func main(argc int32, argv *unsafe.Pointer) int {
5555
cap uintptr
5656
})(unsafe.Pointer(&args))
5757
argsSlice.ptr = malloc(uintptr(argc) * (unsafe.Sizeof(uintptr(0))) * 3)
58-
argsSlice.len = 0
58+
argsSlice.len = uintptr(argc)
5959
argsSlice.cap = uintptr(argc)
6060

6161
// Initialize command line parameters.
62-
for *argv != nil {
62+
for i := 0; i < int(argc); i++ {
6363
// Convert the C string to a Go string.
6464
length := strlen(*argv)
65-
argString := _string{
66-
length: length,
67-
ptr: (*byte)(*argv),
68-
}
69-
args = append(args, *(*string)(unsafe.Pointer(&argString)))
65+
arg := (*_string)(unsafe.Pointer(&args[i]))
66+
arg.length = length
67+
arg.ptr = (*byte)(*argv)
7068
// This is the Go equivalent of "argc++" in C.
7169
argv = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + unsafe.Sizeof(argv)))
7270
}

0 commit comments

Comments
 (0)