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

[Xen-devel] [PATCH for-4.11 2/2] ocaml/xenstored: reduce use of unsafe conversions



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@xxxxxxxxxx>
---
 tools/ocaml/xenstored/logging.ml | 16 ++++++----------
 tools/ocaml/xenstored/stdext.ml  |  2 +-
 tools/ocaml/xenstored/utils.ml   |  9 ++++-----
 3 files changed, 11 insertions(+), 16 deletions(-)

diff --git a/tools/ocaml/xenstored/logging.ml b/tools/ocaml/xenstored/logging.ml
index 45a2c222e6..ea6033195d 100644
--- a/tools/ocaml/xenstored/logging.ml
+++ b/tools/ocaml/xenstored/logging.ml
@@ -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)
diff --git a/tools/ocaml/xenstored/stdext.ml b/tools/ocaml/xenstored/stdext.ml
index 869fec36f2..305a330aa5 100644
--- a/tools/ocaml/xenstored/stdext.ml
+++ b/tools/ocaml/xenstored/stdext.ml
@@ -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)
diff --git a/tools/ocaml/xenstored/utils.ml b/tools/ocaml/xenstored/utils.ml
index 73affb7ea4..b252db799b 100644
--- a/tools/ocaml/xenstored/utils.ml
+++ b/tools/ocaml/xenstored/utils.ml
@@ -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)
-- 
2.17.0


_______________________________________________
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®.