]> xenbits.xensource.com Git - xen.git/commitdiff
tools/ocaml: Drop int_array_of_uuid_string()
authorAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 16 Mar 2018 14:04:53 +0000 (14:04 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Wed, 4 Jul 2018 15:25:01 +0000 (16:25 +0100)
This function is entirely internal to xenctrl stubs, and serves only to
convert the uuid string to an integer array (making 16 memory allocations as
it goes), while the C stubs turns the integer array back into a binary array.

Instead, pass the string all the way down into C, and have sscanf() unpack it
directly into a xen_domain_handle_t object.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Christian Lindig <christian.lindig@citrix.com>
tools/ocaml/libs/xc/xenctrl.ml
tools/ocaml/libs/xc/xenctrl.mli
tools/ocaml/libs/xc/xenctrl_stubs.c

index 1a01faa61ee07eb404b53b542e7535ce6f973179..b3b33bb327d42b5fecd68214d70a1c32d2977ddc 100644 (file)
@@ -135,26 +135,11 @@ let with_intf f =
        interface_close xc;
        r
 
-external _domain_create: handle -> int32 -> domain_create_flag list -> int array -> arch_domainconfig -> domid
+external domain_create: handle -> int32 -> domain_create_flag list -> string -> arch_domainconfig -> domid
        = "stub_xc_domain_create"
 
-let int_array_of_uuid_string s =
-       try
-               Scanf.sscanf s
-                       "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
-                       (fun a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 ->
-                               [| a0; a1; a2; a3; a4; a5; a6; a7;
-                                  a8; a9; a10; a11; a12; a13; a14; a15 |])
-       with _ -> invalid_arg ("Xc.int_array_of_uuid_string: " ^ s)
-
-let domain_create handle n flags uuid =
-       _domain_create handle n flags (int_array_of_uuid_string uuid)
-
-external _domain_sethandle: handle -> domid -> int array -> unit
-                          = "stub_xc_domain_sethandle"
-
-let domain_sethandle handle n uuid =
-       _domain_sethandle handle n (int_array_of_uuid_string uuid)
+external domain_sethandle: handle -> domid -> string -> unit
+       = "stub_xc_domain_sethandle"
 
 external domain_max_vcpus: handle -> domid -> int -> unit
        = "stub_xc_domain_max_vcpus"
index 7d2e6f0fb461765f3b3a4e7104336bc790eb2f29..35303ab61a11f322c2fe92b1866c1d114d77df31 100644 (file)
@@ -98,8 +98,9 @@ type handle
 external interface_open : unit -> handle = "stub_xc_interface_open"
 external interface_close : handle -> unit = "stub_xc_interface_close"
 val with_intf : (handle -> 'a) -> 'a
-val domain_create : handle -> int32 -> domain_create_flag list -> string -> arch_domainconfig -> domid
-val domain_sethandle : handle -> domid -> string -> unit
+external domain_create : handle -> int32 -> domain_create_flag list -> string -> arch_domainconfig -> domid
+  = "stub_xc_domain_create"
+external domain_sethandle : handle -> domid -> string -> unit = "stub_xc_domain_sethandle"
 external domain_max_vcpus : handle -> domid -> int -> unit
   = "stub_xc_domain_max_vcpus"
 external domain_pause : handle -> domid -> unit = "stub_xc_domain_pause"
index 534c7a43200b68f1d87cf6b807cee5ebad3a300d..5274e56bce2f602700fbf8ddf566a484b8e06ed4 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/mman.h>
 #include <stdint.h>
 #include <string.h>
+#include <inttypes.h>
 
 #define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
@@ -97,6 +98,27 @@ CAMLprim value stub_xc_interface_close(value xch)
        CAMLreturn(Val_unit);
 }
 
+static void domain_handle_of_uuid_string(xen_domain_handle_t h,
+                                        const char *uuid)
+{
+#define X "%02"SCNx8
+#define UUID_FMT (X X X X "-" X X "-" X X "-" X X "-" X X X X X X)
+
+       if ( sscanf(uuid, UUID_FMT, &h[0], &h[1], &h[2], &h[3], &h[4],
+                   &h[5], &h[6], &h[7], &h[8], &h[9], &h[10], &h[11],
+                   &h[12], &h[13], &h[14], &h[15]) != 16 )
+       {
+               char buf[128];
+
+               snprintf(buf, sizeof(buf),
+                        "Xc.int_array_of_uuid_string: %s", uuid);
+
+               caml_invalid_argument(buf);
+       }
+
+#undef X
+}
+
 CAMLprim value stub_xc_domain_create(value xch, value ssidref,
                                      value flags, value handle,
                                      value domconfig)
@@ -104,20 +126,14 @@ CAMLprim value stub_xc_domain_create(value xch, value ssidref,
        CAMLparam4(xch, ssidref, flags, handle);
 
        uint32_t domid = 0;
-       xen_domain_handle_t h = { 0 };
+       xen_domain_handle_t h;
        int result;
-       int i;
        uint32_t c_ssidref = Int32_val(ssidref);
        unsigned int c_flags = 0;
        value l;
        xc_domain_configuration_t config = {};
 
-        if (Wosize_val(handle) != 16)
-               caml_invalid_argument("Handle not a 16-integer array");
-
-       for (i = 0; i < sizeof(h); i++) {
-               h[i] = Int_val(Field(handle, i)) & 0xff;
-       }
+       domain_handle_of_uuid_string(h, String_val(handle));
 
        for (l = flags; l != Val_none; l = Field(l, 1))
                c_flags |= 1u << Int_val(Field(l, 0));
@@ -169,15 +185,10 @@ CAMLprim value stub_xc_domain_max_vcpus(value xch, value domid,
 value stub_xc_domain_sethandle(value xch, value domid, value handle)
 {
        CAMLparam3(xch, domid, handle);
-       xen_domain_handle_t h = { 0 };
+       xen_domain_handle_t h;
        int i;
 
-        if (Wosize_val(handle) != 16)
-               caml_invalid_argument("Handle not a 16-integer array");
-
-       for (i = 0; i < sizeof(h); i++) {
-               h[i] = Int_val(Field(handle, i)) & 0xff;
-       }
+       domain_handle_of_uuid_string(h, String_val(handle));
 
        i = xc_domain_sethandle(_H(xch), _D(domid), h);
        if (i)