]> xenbits.xensource.com Git - people/iwj/xen.git/commitdiff
ocaml/xenstored: reduce use of unsafe conversions
authorMarcello Seri <marcello.seri@citrix.com>
Thu, 31 May 2018 13:05:37 +0000 (14:05 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Mon, 4 Jun 2018 10:17:22 +0000 (11:17 +0100)
The rationalisation of the Xs_ring interface in the xb library
allows to further reduce the unsafe calls withouth introducing
copies. This patch also contains some further code cleanups.

Signed-off-by: Marcello Seri <marcello.seri@citrix.com>
Acked-by: Christian Lindig <christian.lindig@citrix.com>
Tested-by: Andrew Cooper <andrew.cooper3@citrix.com>
Release-acked-by: Juergen Gross <jgross@suse.com>
tools/ocaml/xenstored/logging.ml
tools/ocaml/xenstored/stdext.ml
tools/ocaml/xenstored/utils.ml

index 45a2c222e678084de6c5f9f9a5d917bcec553ee5..ea6033195daccd226ad309eabc07e9c722386a83 100644 (file)
@@ -252,13 +252,11 @@ let string_of_access_type = function
        *)
 
 let sanitize_data data =
-       let data = Bytes.copy data in
-       for i = 0 to Bytes.length data - 1
-       do
-               if Bytes.get data i = '\000' then
-                       Bytes.set data i ' '
-       done;
-       String.escaped (Bytes.unsafe_to_string data)
+       let data = String.init
+               (String.length data)
+               (fun i -> let c = data.[i] in if c = '\000' then ' ' else c)
+       in
+       String.escaped data
 
 let activate_access_log = ref true
 let access_log_destination = ref (File (Paths.xen_log_dir ^ "/xenstored-access.log"))
@@ -291,9 +289,7 @@ let access_logging ~con ~tid ?(data="") ~level access_type =
                                let date = string_of_date() in
                                let tid = string_of_tid ~con tid in
                                let access_type = string_of_access_type access_type in
-                               (* we can use unsafe_of_string here as the sanitize_data function
-                                  immediately makes a copy of the data and operates on that. *)
-                               let data = sanitize_data (Bytes.unsafe_of_string data) in
+                               let data = sanitize_data data in
                                let prefix = prefix !access_log_destination date in
                                let msg = Printf.sprintf "%s %s %s %s" prefix tid access_type data in
                                logger.write ~level msg)
index 869fec36f2b45c760e1914853c6f9b83df14dead..305a330aa59d1c71b50bb965eafedca2a5074b56 100644 (file)
@@ -122,7 +122,7 @@ let pidfile_write filename =
                let pid = Unix.getpid () in
                let buf = string_of_int pid ^ "\n" in
                let len = String.length buf in
-               if Unix.write fd (Bytes.unsafe_of_string buf) 0 len <> len
+               if Unix.write_substring fd buf 0 len <> len
                then failwith "pidfile_write failed";
        )
        (fun () -> Unix.close fd)
index 73affb7ea46ffdc29b1284a3f49167b8d6f497c4..b252db799b8f0e94199e1408e26d3345877dea3f 100644 (file)
@@ -46,12 +46,11 @@ let get_hierarchy path =
 let hexify s =
        let hexseq_of_char c = sprintf "%02x" (Char.code c) in
        let hs = Bytes.create (String.length s * 2) in
-       for i = 0 to String.length s - 1
-       do
-               let seq = hexseq_of_char s.[i] in
+       String.iteri (fun i c ->
+               let seq = hexseq_of_char c in
                Bytes.set hs (i * 2) seq.[0];
                Bytes.set hs (i * 2 + 1) seq.[1];
-       done;
+       ) s;
        Bytes.unsafe_to_string hs
 
 let unhexify hs =
@@ -84,7 +83,7 @@ let create_unix_socket name =
 
 let read_file_single_integer filename =
        let fd = Unix.openfile filename [ Unix.O_RDONLY ] 0o640 in
-       let buf = Bytes.make 20 (char_of_int 0) in
+       let buf = Bytes.make 20 '\000' in
        let sz = Unix.read fd buf 0 20 in
        Unix.close fd;
        int_of_string (Bytes.sub_string buf 0 sz)