[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libnbd PATCH 01/13] golang: Simplify nbd_block_status callback array co
From: |
Eric Blake |
Subject: |
[libnbd PATCH 01/13] golang: Simplify nbd_block_status callback array copy |
Date: |
Fri, 3 Dec 2021 17:17:29 -0600 |
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
- Re: [PATCH 04/14] nbd/client: Add safety check on chunk payload length, (continued)
- [PATCH 08/14] nbd/server: Initial support for extended headers, Eric Blake, 2021/12/03
- [PATCH 07/14] nbd: Add types for extended headers, Eric Blake, 2021/12/03
- [PATCH 09/14] nbd/server: Support 64-bit block status, Eric Blake, 2021/12/03
- [PATCH 11/14] nbd/client: Accept 64-bit hole chunks, Eric Blake, 2021/12/03
- [PATCH 10/14] nbd/client: Initial support for extended headers, Eric Blake, 2021/12/03
- [PATCH 12/14] nbd/client: Accept 64-bit block status chunks, Eric Blake, 2021/12/03
- [PATCH 13/14] nbd/client: Request extended headers during negotiation, Eric Blake, 2021/12/03
- [PATCH 14/14] do not apply: nbd/server: Send 64-bit hole chunk, Eric Blake, 2021/12/03
- [libnbd PATCH 00/13] libnbd patches for NBD_OPT_EXTENDED_HEADERS, Eric Blake, 2021/12/03
- [libnbd PATCH 01/13] golang: Simplify nbd_block_status callback array copy,
Eric Blake <=
- [libnbd PATCH 02/13] block_status: Refactor array storage, Eric Blake, 2021/12/03
- [libnbd PATCH 03/13] protocol: Add definitions for extended headers, Eric Blake, 2021/12/03
- [libnbd PATCH 05/13] protocol: Prepare to receive 64-bit replies, Eric Blake, 2021/12/03
- [libnbd PATCH 04/13] protocol: Prepare to send 64-bit requests, Eric Blake, 2021/12/03
- [libnbd PATCH 06/13] protocol: Accept 64-bit holes during pread, Eric Blake, 2021/12/03
- [libnbd PATCH 08/13] block_status: Track 64-bit extents internally, Eric Blake, 2021/12/03
- [libnbd PATCH 10/13] api: Add [aio_]nbd_block_status_64, Eric Blake, 2021/12/03
- [libnbd PATCH 07/13] generator: Add struct nbd_extent in prep for 64-bit extents, Eric Blake, 2021/12/03
- [libnbd PATCH 09/13] block_status: Accept 64-bit extents during block status, Eric Blake, 2021/12/03
- [libnbd PATCH 11/13] api: Add three functions for controlling extended headers, Eric Blake, 2021/12/03