[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 07/20] tools/ocaml: Drop int_array_of_uuid_string()



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@xxxxxxxxxx>
---
CC: Christian Lindig <christian.lindig@xxxxxxxxxx>
CC: David Scott <dave@xxxxxxxxxx>
CC: Jon Ludlam <jonathan.ludlam@xxxxxxxxxxxxx>
CC: Rob Hoes <Rob.Hoes@xxxxxxxxxx>
---
 tools/ocaml/libs/xc/xenctrl.ml      | 21 +++----------------
 tools/ocaml/libs/xc/xenctrl.mli     |  5 +++--
 tools/ocaml/libs/xc/xenctrl_stubs.c | 41 +++++++++++++++++++++++--------------
 3 files changed, 32 insertions(+), 35 deletions(-)

diff --git a/tools/ocaml/libs/xc/xenctrl.ml b/tools/ocaml/libs/xc/xenctrl.ml
index 1a01faa..b3b33bb 100644
--- a/tools/ocaml/libs/xc/xenctrl.ml
+++ b/tools/ocaml/libs/xc/xenctrl.ml
@@ -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"
diff --git a/tools/ocaml/libs/xc/xenctrl.mli b/tools/ocaml/libs/xc/xenctrl.mli
index 7d2e6f0..35303ab 100644
--- a/tools/ocaml/libs/xc/xenctrl.mli
+++ b/tools/ocaml/libs/xc/xenctrl.mli
@@ -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"
diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c 
b/tools/ocaml/libs/xc/xenctrl_stubs.c
index 45d4e20..af92bfa 100644
--- a/tools/ocaml/libs/xc/xenctrl_stubs.c
+++ b/tools/ocaml/libs/xc/xenctrl_stubs.c
@@ -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)
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.