On Wed, Aug 02, 2023 at 08:50:24PM -0500, Eric Blake wrote:
See the earlier commit "Add Extent64 arg type" for
rationale in
supporting a new generator arg type. This patch adds the Python
bindings for use of Extent64: it is fairly easy to construct a list of
2-item tuples in place with Py_BuildValue's 'K' argument for unsigned
64-bit values, without needing to write any separate helper code.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
v4: split out of larger patch [Laszlo], use K instead of O to avoid
memory issues while building list
---
generator/Python.ml | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/generator/Python.ml b/generator/Python.ml
index 3a77b81f..761f4511 100644
--- a/generator/Python.ml
+++ b/generator/Python.ml
@@ -169,7 +169,7 @@ let
pr " PyObject *py_args, *py_ret;\n";
List.iter (
function
- | CBArrayAndLen (UInt32 n, _)
+ | CBArrayAndLen ((UInt32 n | Extent64 n), _)
| CBBytesIn (n, _)
| CBMutable (Int n) ->
pr " PyObject *py_%s = NULL;\n" n
@@ -187,6 +187,18 @@ let
pr " if (!py_e_%s) { PyErr_PrintEx (0); goto out; }\n" n;
pr " PyList_SET_ITEM (py_%s, i_%s, py_e_%s);\n" n n n;
pr " }\n"
+ | CBArrayAndLen (Extent64 n, len) ->
+ pr " py_%s = PyList_New (%s);\n" n len;
+ pr " if (!py_%s) { PyErr_PrintEx (0); goto out; }\n" n;
+ pr " size_t i_%s;\n" n;
+ pr " for (i_%s = 0; i_%s < %s; ++i_%s) {\n" n n len n;
+ pr " PyObject *py_e_%s = Py_BuildValue (" n;
+ pr_wrap ',' (fun () ->
+ pr "\"KK\", %s[i_%s].length, %s[i_%s].flags" n n n n);
+ pr ");\n";
+ pr " if (!py_e_%s) { PyErr_PrintEx (0); goto out; }\n" n;
+ pr " PyList_SET_ITEM (py_%s, i_%s, py_e_%s);\n" n n n;
+ pr " }\n"
| CBBytesIn (n, len) ->
pr " py_%s = nbd_internal_py_get_subview (data->view, %s, %s);\n" n
n len;
pr " if (!py_%s) { PyErr_PrintEx (0); goto out; }\n" n
@@ -205,7 +217,7 @@ let
let params =
List.map (
function
- | CBArrayAndLen (UInt32 n, _) -> "O", sprintf "py_%s" n
+ | CBArrayAndLen ((UInt32 n | Extent64 n), _) -> "O", sprintf
"py_%s" n
| CBBytesIn (n, _) -> "O", sprintf "py_%s" n
| CBInt n -> "i", n
| CBInt64 n -> "L", n
@@ -254,7 +266,7 @@ let
pr " out:\n";
List.iter (
function
- | CBArrayAndLen (UInt32 n, _) ->
+ | CBArrayAndLen ((UInt32 n | Extent64 n), _) ->
pr " Py_XDECREF (py_%s);\n" n
| CBBytesIn (n, _) ->
pr " Py_XDECREF (py_%s);\n" n
--
Reviewed-by: Richard W.M. Jones <rjones(a)redhat.com>
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
virt-top is 'top' for virtual machines. Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top