Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ocaml/idl/ocaml_backend/gen_rbac.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ let hash2uuid str =
in
Uuid.string_of_uuid (Uuid.uuid_of_int_array (int_array hex))

let replace_char _str c1 c2 =
let str = String.copy _str in (*defensive copy*)
for i=0 to String.length str -1 do
if str.[i]=c1 then str.[i]<-c2 else ()
done;
str
let replace_char str c1 c2 =
let buf = Bytes.of_string str in (*defensive copy*)
String.iteri (fun i _ ->
if str.[i]=c1 then Bytes.set buf i c2 else ()
) str;
Bytes.unsafe_to_string buf
Copy link
Contributor

@gaborigloi gaborigloi Apr 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could replace this with Astring.String.map or something similar - performance probably doesn't matter in the autogenerator:
http://erratique.ch/software/astring/doc/Astring.String.html


let role_uuid name = hash2uuid name

Expand Down
2 changes: 1 addition & 1 deletion ocaml/rfb/rfb_randomtest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let server (s: Unix.file_descr) =
(* Update the whole thing *)
let buffer = Bytes.create (w * h * !bpp / 8) in
for i = 0 to String.length buffer - 1 do
buffer.[i] <- char_of_int (Random.int 255)
Bytes.set buffer i (char_of_int @@ Random.int 255)
done;
let raw = { FramebufferUpdate.Raw.buffer = buffer } in
let update = { FramebufferUpdate.x = 0; y = 0; w = w; h = h;
Expand Down
12 changes: 6 additions & 6 deletions ocaml/xapi-cli-protocol/cli_protocol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ let marshal_int32 x =
and b = (x >> 8) && 0xffl
and c = (x >> 16) && 0xffl
and d = (x >> 24) && 0xffl in
let result = String.make 4 '\000' in
result.[0] <- char_of_int (Int32.to_int a);
result.[1] <- char_of_int (Int32.to_int b);
result.[2] <- char_of_int (Int32.to_int c);
result.[3] <- char_of_int (Int32.to_int d);
result
let set buf pos v =
Bytes.set buf pos (char_of_int @@ Int32.to_int v)
in
let result = Bytes.make 4 '\000' in
List.iteri (set result) [a;b;c;d];
Bytes.unsafe_to_string result

let marshal_int x = marshal_int32 (Int32.of_int x)

Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi-client/tasks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let with_tasks_destroy ~rpc ~session_id ~timeout ~tasks =
if Client.Task.get_status ~rpc ~session_id ~self:task = `pending then
Client.Task.cancel ~rpc ~session_id ~task) tasks;
(* cancel is not immediate, give it a reasonable chance to take effect *)
wait_for_all_inner ~rpc ~session_id ~all_timeout:60. ~tasks;
wait_for_all_inner ~rpc ~session_id ~all_timeout:60. ~tasks |> ignore;
false
end else true
in
Expand Down
8 changes: 5 additions & 3 deletions ocaml/xapi/cli_progress_bar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Make(T: Floatable) = struct
max_value: T.t;
mutable current_value: T.t;
width: int;
line: string;
mutable line: string;
mutable spin_index: int;
start_time: float;
mutable summarised: bool;
Expand Down Expand Up @@ -62,11 +62,13 @@ module Make(T: Floatable) = struct

let string_of_bar t =
let w = bar_width t t.current_value in
t.line.[1] <- spinner.(t.spin_index);
let line = Bytes.of_string t.line in
Bytes.set line 1 spinner.(t.spin_index);
t.spin_index <- (t.spin_index + 1) mod (Array.length spinner);
for i = 0 to w - 1 do
t.line.[prefix + i] <- (if i = w - 1 then '>' else '#')
Bytes.set line (prefix + i) (if i = w - 1 then '>' else '#')
done;
t.line <- Bytes.unsafe_to_string line;
let percent = Printf.sprintf "%3d" (percent t) in
String.blit percent 0 t.line (t.width - 19) 3;
let eta = eta t in
Expand Down
13 changes: 7 additions & 6 deletions ocaml/xapi/extauth_plugin_ADpbis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,17 @@ struct
let query_subject_information subject_identifier =

let unmap_lw_space_chars lwname =
let defensive_copy = String.copy lwname in
let defensive_copy = Bytes.of_string lwname in
(* CA-29006: map chars in names back to original space chars in windows-names *)
(* we use + as the pbis space-replacement because it's an invalid NT-username char in windows *)
(* the space-replacement char used by pbis is defined at /etc/pbis/lsassd.conf *)
let current_lw_space_replacement = '+' in
for i = 0 to String.length defensive_copy - 1
do
if defensive_copy.[i] = current_lw_space_replacement then defensive_copy.[i] <- ' '
done;
defensive_copy
String.iteri (fun i c ->
if c = current_lw_space_replacement then
Bytes.set defensive_copy i ' '
else ()
) lwname;
Bytes.unsafe_to_string defensive_copy
in
let get_value name ls = if List.mem_assoc name ls then List.assoc name ls else "" in
let infolist = pbis_get_all_byid subject_identifier in
Expand Down
12 changes: 7 additions & 5 deletions ocaml/xapi/rbac_audit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ let action_params_zip =
let zip data = (* todo: remove i/o, make this more efficient *)
try
let tmp_path = Filename.temp_file "zip-" ".dat" in
let zdata = ref "" in
let zdata = ref (Bytes.empty) in
Stdext.Pervasiveext.finally
(fun ()->
Stdext.Unixext.atomic_write_to_file tmp_path 0o600
Expand All @@ -229,16 +229,18 @@ let zip data = (* todo: remove i/o, make this more efficient *)
let fd_in = Unix.openfile tmp_path [ Unix.O_RDONLY] 0o400 in
Stdext.Pervasiveext.finally
(fun ()->
let cin=Unix.in_channel_of_descr fd_in in
let cin = Unix.in_channel_of_descr fd_in in
let cin_len = in_channel_length cin in
zdata := (Bytes.create cin_len);
for i = 1 to cin_len do !zdata.[i-1] <- input_char cin done;
for i = 1 to cin_len do
Bytes.set !zdata (i-1) (input_char cin);
done
)
(fun ()->Unix.close fd_in)
(fun ()-> Unix.close fd_in)
)
(fun ()-> Sys.remove tmp_path)
;
let b64zdata = Stdext.Base64.encode !zdata in
let b64zdata = Xapi_stdext_base64.Base64.encode (Bytes.unsafe_to_string !zdata) in
b64zdata
with e->
D.debug "error %s zipping data: %s" (ExnHelper.string_of_exn e) data;
Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/record_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ let domain_type_to_string = function
| `unspecified -> "unspecified"

let domain_type_of_string x =
match String.lowercase x with
match String.lowercase_ascii x with
| "hvm" -> `hvm
| "pv" -> `pv
| "pv-in-pvh" -> `pv_in_pvh
Expand Down
26 changes: 10 additions & 16 deletions ocaml/xapi/sparse_encoding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,23 @@ module Marshal = struct
and f = (x >>> 40) &&& 0xffL
and g = (x >>> 48) &&& 0xffL
and h = (x >>> 56) &&& 0xffL in
let result = String.make 8 '\000' in
result.[0] <- char_of_int (Int64.to_int a);
result.[1] <- char_of_int (Int64.to_int b);
result.[2] <- char_of_int (Int64.to_int c);
result.[3] <- char_of_int (Int64.to_int d);
result.[4] <- char_of_int (Int64.to_int e);
result.[5] <- char_of_int (Int64.to_int f);
result.[6] <- char_of_int (Int64.to_int g);
result.[7] <- char_of_int (Int64.to_int h);
result
let result = Bytes.make 8 '\000' in
List.iteri (fun i v ->
Bytes.set result i (char_of_int @@ Int64.to_int v)
) [a; b; c; d; e; f; g; h];
Bytes.unsafe_to_string result
let int32 x =
let (>>>) a b = Int32.shift_right_logical a b
and (&&&) a b = Int32.logand a b in
let a = (x >>> 0) &&& 0xffl
and b = (x >>> 8) &&& 0xffl
and c = (x >>> 16) &&& 0xffl
and d = (x >>> 24) &&& 0xffl in
let result = String.make 4 '\000' in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're ANDing them all with the same value, why not include that in the definition of >>>?

Copy link
Contributor

@gaborigloi gaborigloi Apr 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would find that confusing, because >> is usually the right-shift operator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a different operator of course

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I think it's better to keep the old code and stick to operators that look like the usual ones

result.[0] <- char_of_int (Int32.to_int a);
result.[1] <- char_of_int (Int32.to_int b);
result.[2] <- char_of_int (Int32.to_int c);
result.[3] <- char_of_int (Int32.to_int d);
result
let result = Bytes.make 4 '\000' in
List.iteri (fun i v ->
Bytes.set result i (char_of_int @@ Int32.to_int v)
) [a; b; c; d];
Bytes.unsafe_to_string result

end

Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/storage_mux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ let string_of_sm_result f = function
| SMSuccess x -> Printf.sprintf "Success: %s" (f x)
| SMFailure e -> Printf.sprintf "Failure: %s" (Printexc.to_string e)

let partition l = List.partition (success ++ snd) l
let partition l = List.partition (fun (_,x) -> success x) l

let choose x = snd(List.hd x)

Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/xapi_pif.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let refresh_internal ~__context ~self =
(Db.PIF.get_MAC)
(Db.PIF.set_MAC)
(fun () -> Net.Interface.get_mac dbg device)
(id);
(fun x -> x);

maybe_update_database "PCI"
(Db.PIF.get_PCI)
Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/xapi_sm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open Fun
(* We treat versions as '.'-separated integer lists under the usual
lexicographic ordering. *)
type version = int list
let version_of_string = List.map int_of_string ++ (String.split '.')
let version_of_string s = List.map int_of_string (String.split_on_char '.' s)

module D=Debug.Make(struct let name="xapi" end)
open D
Expand Down