Richard W.M. Jones
2020-Sep-29 11:46 UTC
[Libguestfs] [PATCH libnbd] generator: Add SizeT type, maps to C size_t.
--- generator/API.ml | 1 + generator/API.mli | 1 + generator/C.ml | 14 ++++++++++---- generator/GoLang.ml | 5 +++++ generator/OCaml.ml | 5 +++++ generator/Python.ml | 11 +++++++++-- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/generator/API.ml b/generator/API.ml index 0a876c4..b93580d 100644 --- a/generator/API.ml +++ b/generator/API.ml @@ -47,6 +47,7 @@ and arg | Int of string | Int64 of string | Path of string +| SizeT of string | SockAddrAndLen of string * string | String of string | StringList of string diff --git a/generator/API.mli b/generator/API.mli index 9d2fdb7..2e7aa78 100644 --- a/generator/API.mli +++ b/generator/API.mli @@ -57,6 +57,7 @@ and arg | Int of string (** small int *) | Int64 of string (** 64 bit signed int *) | Path of string (** filename or path *) +| SizeT of string (** like size_t, for counting array elements *) | SockAddrAndLen of string * string (** struct sockaddr * + socklen_t *) | String of string (** string, cannot be NULL *) | StringList of string (** argv-style NULL-terminated array of strings *) diff --git a/generator/C.ml b/generator/C.ml index 5f68b14..82d5e3d 100644 --- a/generator/C.ml +++ b/generator/C.ml @@ -93,6 +93,7 @@ let rec name_of_arg = function | Int n -> [n] | Int64 n -> [n] | Path n -> [n] +| SizeT n -> [n] | SockAddrAndLen (n, len) -> [n; len] | String n -> [n] | StringList n -> [n] @@ -157,6 +158,9 @@ and print_arg_list' ?(handle = false) ?(types = true) ?(closure_style = Direct) | Int64 n -> if types then pr "int64_t "; pr "%s" n + | SizeT n -> + if types then pr "size_t "; + pr "%s" n | Path n | String n -> if types then pr "const char *"; @@ -621,7 +625,8 @@ let generate_lib_api_c () pr " nbd_internal_printable_string_list (%s);\n" n | BytesOut _ | BytesPersistOut _ | Bool _ | Closure _ | Enum _ | Flags _ | Fd _ | Int _ - | Int64 _ | SockAddrAndLen _ | UInt _ | UInt32 _ | UInt64 _ -> () + | Int64 _ | SizeT _ + | SockAddrAndLen _ | UInt _ | UInt32 _ | UInt64 _ -> () ) args; pr " debug (h, \"enter:"; List.iter ( @@ -637,6 +642,7 @@ let generate_lib_api_c () | Flags (n, _) -> pr " %s=0x%%x" n | Fd n | Int n -> pr " %s=%%d" n | Int64 n -> pr " %s=%%\" PRIi64 \"" n + | SizeT n -> pr " %s=%%zu" n | SockAddrAndLen (n, len) -> pr " %s=<sockaddr> %s=%%d" n len | Path n | String n -> pr " %s=%%s" n @@ -662,8 +668,7 @@ let generate_lib_api_c () | Closure { cbname } -> () | Enum (n, _) -> pr ", %s" n | Flags (n, _) -> pr ", %s" n - | Fd n | Int n -> pr ", %s" n - | Int64 n -> pr ", %s" n + | Fd n | Int n | Int64 n | SizeT n -> pr ", %s" n | SockAddrAndLen (_, len) -> pr ", (int) %s" len | Path n | String n | StringList n -> pr ", %s_printable ? %s_printable : \"\"" n n @@ -686,7 +691,8 @@ let generate_lib_api_c () pr " free (%s_printable);\n" n | BytesOut _ | BytesPersistOut _ | Bool _ | Closure _ | Enum _ | Flags _ | Fd _ | Int _ - | Int64 _ | SockAddrAndLen _ | UInt _ | UInt32 _ | UInt64 _ -> () + | Int64 _ | SizeT _ + | SockAddrAndLen _ | UInt _ | UInt32 _ | UInt64 _ -> () ) args; pr " }\n" (* Print the trace when we leave a call with debugging enabled. *) diff --git a/generator/GoLang.ml b/generator/GoLang.ml index 81446a6..e169173 100644 --- a/generator/GoLang.ml +++ b/generator/GoLang.ml @@ -54,6 +54,7 @@ let go_name_of_arg = function | Int n -> n | Int64 n -> n | Path n -> n + | SizeT n -> n | SockAddrAndLen (n, len) -> n | String n -> n | StringList n -> n @@ -74,6 +75,7 @@ let go_arg_type = function | Int _ -> "int" | Int64 _ -> "int64" | Path _ -> "string" + | SizeT _ -> "int" | SockAddrAndLen _ -> "string" | String _ -> "string" | StringList _ -> "[]string" @@ -276,6 +278,8 @@ let print_binding (name, { args; optargs; ret; shortdesc }) | Path n -> pr " c_%s := C.CString (%s)\n" n n; pr " defer C.free (unsafe.Pointer (c_%s))\n" n + | SizeT n -> + pr " c_%s := C.size_t (%s)\n" n n | SockAddrAndLen (n, len) -> pr " panic (\"SockAddrAndLen not supported\")\n"; pr " var c_%s *C.struct_sockaddr\n" n; @@ -336,6 +340,7 @@ let print_binding (name, { args; optargs; ret; shortdesc }) | Int n -> pr ", c_%s" n | Int64 n -> pr ", c_%s" n | Path n -> pr ", c_%s" n + | SizeT n -> pr ", c_%s" n | SockAddrAndLen (n, len) -> pr ", c_%s, c_%s" n len | String n -> pr ", c_%s" n | StringList n -> pr ", &c_%s[0]" n diff --git a/generator/OCaml.ml b/generator/OCaml.ml index 28acb50..81e5529 100644 --- a/generator/OCaml.ml +++ b/generator/OCaml.ml @@ -50,6 +50,7 @@ and ocaml_arg_to_string = function | Int64 _ -> "int64" | Path _ -> "string" | SockAddrAndLen _ -> "string" (* XXX not impl *) + | SizeT _ -> "int" (* OCaml int type is always sufficient for counting *) | String _ -> "string" | StringList _ -> "string list" | UInt _ -> "int" @@ -103,6 +104,7 @@ let ocaml_name_of_arg = function | Int n -> n | Int64 n -> n | Path n -> n + | SizeT n -> n | SockAddrAndLen (n, len) -> n | String n -> n | StringList n -> n @@ -666,6 +668,8 @@ let print_ocaml_binding (name, { args; optargs; ret }) pr " int64_t %s = Int64_val (%sv);\n" n n | Path n | String n -> pr " const char *%s = String_val (%sv);\n" n n + | SizeT n -> + pr " size_t %s = Int_val (%sv);\n" n n | SockAddrAndLen (n, len) -> pr " const struct sockaddr *%s;\n" n; pr " socklen_t %s;\n" len; @@ -738,6 +742,7 @@ let print_ocaml_binding (name, { args; optargs; ret }) | Int _ | Int64 _ | Path _ + | SizeT _ | String _ | SockAddrAndLen _ | UInt _ diff --git a/generator/Python.ml b/generator/Python.ml index 1705ad9..46b5828 100644 --- a/generator/Python.ml +++ b/generator/Python.ml @@ -295,6 +295,8 @@ let print_python_binding name { args; optargs; ret; may_set_error } | Path n -> pr " PyObject *py_%s = NULL;\n" n; pr " char *%s = NULL;\n" n + | SizeT n -> + pr " Py_ssize_t %s;\n" n | SockAddrAndLen (n, _) -> pr " /* XXX Complicated - Python uses a tuple of different\n"; pr " * lengths for the different socket types.\n"; @@ -341,6 +343,7 @@ let print_python_binding name { args; optargs; ret; may_set_error } | Fd n | Int n -> pr " \"i\"" | Int64 n -> pr " \"L\"" | Path n -> pr " \"O&\"" + | SizeT n -> pr " \"n\"" | SockAddrAndLen (n, _) -> pr " \"O\"" | String n -> pr " \"s\"" | StringList n -> pr " \"O\"" @@ -365,8 +368,7 @@ let print_python_binding name { args; optargs; ret; may_set_error } | Closure { cbname } -> pr ", &py_%s_fn" cbname | Enum (n, _) -> pr ", &%s" n | Flags (n, _) -> pr ", &%s" n - | Fd n | Int n -> pr ", &%s" n - | Int64 n -> pr ", &%s" n + | Fd n | Int n | SizeT n | Int64 n -> pr ", &%s" n | Path n -> pr ", PyUnicode_FSConverter, &py_%s" n | SockAddrAndLen (n, _) -> pr ", &%s" n | String n -> pr ", &%s" n @@ -435,6 +437,7 @@ let print_python_binding name { args; optargs; ret; may_set_error } | Path n -> pr " %s = PyBytes_AS_STRING (py_%s);\n" n n; pr " assert (%s != NULL);\n" n + | SizeT n -> () | SockAddrAndLen _ -> pr " abort (); /* XXX SockAddrAndLen not implemented */\n"; | String _ -> () @@ -462,6 +465,7 @@ let print_python_binding name { args; optargs; ret; may_set_error } | Fd n | Int n -> pr ", %s" n | Int64 n -> pr ", %s_i64" n | Path n -> pr ", %s" n + | SizeT n -> pr ", (size_t)%s" n | SockAddrAndLen (n, _) -> pr ", /* XXX */ (void *) %s, 0" n | String n -> pr ", %s" n | StringList n -> pr ", %s" n @@ -510,6 +514,7 @@ let print_python_binding name { args; optargs; ret; may_set_error } | Int64 _ | Path _ | SockAddrAndLen _ + | SizeT _ | String _ | StringList _ | UInt _ @@ -551,6 +556,7 @@ let print_python_binding name { args; optargs; ret; may_set_error } | Int64 _ -> () | Path n -> pr " Py_XDECREF (py_%s);\n" n + | SizeT _ -> () | SockAddrAndLen _ -> () | String n -> () | StringList n -> pr " nbd_internal_py_free_string_list (%s);\n" n @@ -791,6 +797,7 @@ class NBD(object): | Fd n | Int n -> n, None, None | Int64 n -> n, None, None | Path n -> n, None, None + | SizeT n -> n, None, None | SockAddrAndLen (n, _) -> n, None, None | String n -> n, None, None | StringList n -> n, None, None -- 2.28.0.rc2
Eric Blake
2020-Oct-01 01:40 UTC
[Libguestfs] [PATCH libnbd] generator: Add SizeT type, maps to C size_t.
On 9/29/20 6:46 AM, Richard W.M. Jones wrote:> --- > generator/API.ml | 1 + > generator/API.mli | 1 + > generator/C.ml | 14 ++++++++++---- > generator/GoLang.ml | 5 +++++ > generator/OCaml.ml | 5 +++++ > generator/Python.ml | 11 +++++++++-- > 6 files changed, 31 insertions(+), 6 deletions(-)Looks good, and I'll push a counterpart patch adding RSizeT for returning ssize_t values. Question: Should nbd_opt_list() return RSizeT instead of RInt, as it is returning a count of times the callback was called? The same question would apply to my upcoming nbd_opt_list_meta_context(). Changing a return type from 'int' to 'ssize_t' can result in an ABI change (32- vs. 64-bit return on some platforms), which may be too risky, even though 1.4 is still fairly new that we are unlikely to break anyone in practice. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: OpenPGP digital signature URL: <http://listman.redhat.com/archives/libguestfs/attachments/20200930/7e23b89c/attachment.sig>
Richard W.M. Jones
2020-Oct-01 07:32 UTC
Re: [Libguestfs] [PATCH libnbd] generator: Add SizeT type, maps to C size_t.
On Wed, Sep 30, 2020 at 08:40:52PM -0500, Eric Blake wrote:> On 9/29/20 6:46 AM, Richard W.M. Jones wrote: > > --- > > generator/API.ml | 1 + > > generator/API.mli | 1 + > > generator/C.ml | 14 ++++++++++---- > > generator/GoLang.ml | 5 +++++ > > generator/OCaml.ml | 5 +++++ > > generator/Python.ml | 11 +++++++++-- > > 6 files changed, 31 insertions(+), 6 deletions(-) > > Looks good, and I'll push a counterpart patch adding RSizeT for > returning ssize_t values. > > Question: Should nbd_opt_list() return RSizeT instead of RInt, as it is > returning a count of times the callback was called? The same question > would apply to my upcoming nbd_opt_list_meta_context(). Changing a > return type from 'int' to 'ssize_t' can result in an ABI change (32- vs. > 64-bit return on some platforms), which may be too risky, even though > 1.4 is still fairly new that we are unlikely to break anyone in practice.No, I don't think we should break 1.4 users. We could modify the generator to do something with ELF symbol versioning and use that to clean up the API in future. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
Apparently Analagous Threads
- [PATCH libnbd 1/2] generator: Handle closure args (cbargs) specially.
- [PATCH libnbd 3/4] api: Add nbd_connect_socket.
- [PATCH libnbd 2/2] generator: Change handling of Flags to be a true optional argument.
- [libnbd PATCH 2/2] RFC: generator: Handle shared callbacks in Python
- [libnbd PATCH v2 1/5] generator: Allow Int in callbacks