Skip to content

Commit

Permalink
Make create zero out the new buffer
Browse files Browse the repository at this point in the history
The new `create_unsafe` function can be used if you want to trade safety
for speed.

Fixes mirage#30
  • Loading branch information
talex5 committed May 17, 2016
1 parent acf815d commit a67c616
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/cstruct.ml
Expand Up @@ -88,7 +88,7 @@ let of_bigarray ?(off=0) ?len buffer =
let to_bigarray buffer =
Bigarray.Array1.sub buffer.buffer buffer.off buffer.len

let create len =
let create_unsafe len =
let buffer = Bigarray.(Array1.create char c_layout len) in
{ buffer ; len ; off = 0 }

Expand Down Expand Up @@ -208,6 +208,11 @@ let equal t1 t2 = compare t1 t2 = 0
(* Note that this is only safe as long as all [t]s are coherent. *)
let memset t x = unsafe_fill_bigstring t.buffer t.off t.len x

let create len =
let t = create_unsafe len in
memset t 0;
t

let set_uint8 t i c =
if i >= t.len || i < 0 then err_invalid_bounds "set_uint8" t i 1
else EndianBigstring.BigEndian.set_int8 t.buffer (t.off+i) c
Expand Down
4 changes: 4 additions & 0 deletions lib/cstruct.mli
Expand Up @@ -196,6 +196,10 @@ val to_bigarray: t -> buffer
sharing of the underlying buffer. *)

val create : int -> t
(** [create len] is a fresh cstruct of size [len] with an offset of 0,
filled with zero bytes. *)

val create_unsafe : int -> t
(** [create len] is a cstruct of size [len] with an offset of 0.
Note that the returned cstruct will contain arbitrary data,
Expand Down

0 comments on commit a67c616

Please sign in to comment.