In the block status callback glue code, we need to copy a C uint32_t[] into a golang []uint32. The copy is necessary since the lifetime of the C array is not guaranteed to outlive whatever the Go callback may have done with what it was handed; copying ensures that the user's Go code doesn't have to worry about lifetime issues. But we don't have to have quite so many casts and pointer additions: since we can assume C.uint32_t and uint32 occupy the same amount of memory (even though they are different types), we can exploit Go's ability to treat an unsafe pointer as if it were an oversized array, take a slice of that array, and then use idiomatic Go to copy from the slice.
https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices --- generator/GoLang.ml | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/generator/GoLang.ml b/generator/GoLang.ml index eb3aa263..d3b7dc79 100644 --- a/generator/GoLang.ml +++ b/generator/GoLang.ml @@ -1,6 +1,6 @@ (* hey emacs, this is OCaml code: -*- tuareg -*- *) (* nbd client library in userspace: generator - * Copyright (C) 2013-2020 Red Hat Inc. + * Copyright (C) 2013-2021 Red Hat Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -514,11 +514,14 @@ let /* Closures. */ func copy_uint32_array (entries *C.uint32_t, count C.size_t) []uint32 { - ret := make([]uint32, int (count)) - for i := 0; i < int (count); i++ { - entry := (*C.uint32_t) (unsafe.Pointer(uintptr(unsafe.Pointer(entries)) + (unsafe.Sizeof(*entries) * uintptr(i)))) - ret[i] = uint32 (*entry) - } + /* https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices */ + unsafePtr := unsafe.Pointer(entries) + /* Max structured reply payload is 64M, so this array size is more than + * sufficient for the underlying slice we want to access. + */ + arrayPtr := (*[1 << 20]uint32)(unsafePtr) + ret := make([]uint32, count) + copy(ret, arrayPtr[:count:count]) return ret } "; -- 2.33.1