mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-20 04:19:04 +00:00
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.
This commit is contained in:
committed by
Ron Evans
parent
dd3d8a363a
commit
c1aa152a63
@@ -55,18 +55,16 @@ func main(argc int32, argv *unsafe.Pointer) int {
|
|||||||
cap uintptr
|
cap uintptr
|
||||||
})(unsafe.Pointer(&args))
|
})(unsafe.Pointer(&args))
|
||||||
argsSlice.ptr = malloc(uintptr(argc) * (unsafe.Sizeof(uintptr(0))) * 3)
|
argsSlice.ptr = malloc(uintptr(argc) * (unsafe.Sizeof(uintptr(0))) * 3)
|
||||||
argsSlice.len = 0
|
argsSlice.len = uintptr(argc)
|
||||||
argsSlice.cap = uintptr(argc)
|
argsSlice.cap = uintptr(argc)
|
||||||
|
|
||||||
// Initialize command line parameters.
|
// Initialize command line parameters.
|
||||||
for *argv != nil {
|
for i := 0; i < int(argc); i++ {
|
||||||
// Convert the C string to a Go string.
|
// Convert the C string to a Go string.
|
||||||
length := strlen(*argv)
|
length := strlen(*argv)
|
||||||
argString := _string{
|
arg := (*_string)(unsafe.Pointer(&args[i]))
|
||||||
length: length,
|
arg.length = length
|
||||||
ptr: (*byte)(*argv),
|
arg.ptr = (*byte)(*argv)
|
||||||
}
|
|
||||||
args = append(args, *(*string)(unsafe.Pointer(&argString)))
|
|
||||||
// This is the Go equivalent of "argc++" in C.
|
// This is the Go equivalent of "argc++" in C.
|
||||||
argv = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + unsafe.Sizeof(argv)))
|
argv = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + unsafe.Sizeof(argv)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user