Skip to content

Commit

Permalink
Add missing bigarray functions
Browse files Browse the repository at this point in the history
These were originally in 0006-update-and-bigarray-support.patch but
went missing in the split patchset.

Original description:

Support for update and bigarray
 This patch exposes the context and provides incremental update
 functions for strings and bigarrays. The bigarray functions run
 without holding the global lock and the file function is also changed
 to run without the global lock. This enables other threads to run
 concurrently while the sha checksum is being computed.

Author: Goswin von Brederlow <[email protected]>

Signed-off-by: David Scott <[email protected]>
  • Loading branch information
David Scott committed Oct 3, 2013
1 parent 4f91e78 commit ada51a7
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sha1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
*)

type ctx
type buf = (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
type t

external init: unit -> ctx = "stub_sha1_init"
external unsafe_update_substring: ctx -> string -> int -> int -> unit = "stub_sha1_update"
external update_buffer: ctx -> buf -> unit = "stub_sha1_update_bigarray"
external finalize: ctx -> t = "stub_sha1_finalize"
external copy : ctx -> ctx = "stub_sha1_copy"
external to_bin: t -> string = "stub_sha1_to_bin"
Expand Down Expand Up @@ -49,6 +51,11 @@ let substring s ofs len =
unsafe_update_substring ctx s ofs len;
finalize ctx

let buffer buf =
let ctx = init () in
update_buffer ctx buf;
finalize ctx

let channel chan len =
let ctx = init ()
and buf = String.create blksize in
Expand Down
7 changes: 7 additions & 0 deletions sha1.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
(** context type - opaque *)
type ctx

(** buffer type *)
type buf = (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t

(** digest type - opaque *)
type t

Expand All @@ -39,6 +42,10 @@ val update_substring: ctx -> string -> int -> int -> unit
(** Sha1.update_string ctx s updates the context with s. *)
val update_string: ctx -> string -> unit

(** Sha1.update_buffer ctx a updates the context with a.
Runs parallel to other threads if any exist. *)
external update_buffer: ctx -> buf -> unit = "stub_sha1_update_bigarray"

(** Finalize the context and return digest *)
external finalize: ctx -> t = "stub_sha1_finalize"

Expand Down
15 changes: 15 additions & 0 deletions sha1_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static inline int sha1_file(char *filename, sha1_digest *digest)
#include <caml/alloc.h>
#include <caml/custom.h>
#include <caml/fail.h>
#include <caml/bigarray.h>
#include <caml/threads.h>

#define GET_CTX_STRUCT(a) ((struct sha1_ctx *) a)
Expand All @@ -69,6 +70,20 @@ CAMLprim value stub_sha1_update(value ctx, value data, value ofs, value len)
CAMLreturn(Val_unit);
}

CAMLprim value stub_sha1_update_bigarray(value ctx, value buf)
{
CAMLparam2(ctx, buf);
unsigned char *data = Data_bigarray_val(buf);
size_t len = Bigarray_val(buf)->dim[0];

caml_release_runtime_system();
sha1_update(GET_CTX_STRUCT(ctx), data, len);
caml_acquire_runtime_system();

CAMLreturn(Val_unit);
}


CAMLprim value stub_sha1_finalize(value ctx)
{
CAMLparam1(ctx);
Expand Down
9 changes: 9 additions & 0 deletions sha256.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
*)

type ctx
type buf = (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
type t

external init: unit -> ctx = "stub_sha256_init"
external unsafe_update_substring: ctx -> string -> int -> int -> unit = "stub_sha256_update"
external update_buffer: ctx -> buf -> unit = "stub_sha256_update_bigarray"
external finalize: ctx -> t = "stub_sha256_finalize"
external copy : ctx -> ctx = "stub_sha256_copy"
external to_bin: t -> string = "stub_sha256_to_bin"
Expand All @@ -34,6 +36,8 @@ let update_substring ctx s ofs len =
let update_string ctx s =
unsafe_update_substring ctx s 0 (String.length s)

external update_bigarray: ctx -> (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t -> unit = "stub_sha256_update_bigarray"

let string s =
let ctx = init () in
unsafe_update_substring ctx s 0 (String.length s);
Expand All @@ -48,6 +52,11 @@ let substring s ofs len =
unsafe_update_substring ctx s ofs len;
finalize ctx

let buffer buf =
let ctx = init () in
update_buffer ctx buf;
finalize ctx

let channel chan len =
let ctx = init ()
and buf = String.create blksize in
Expand Down
10 changes: 10 additions & 0 deletions sha256.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
(** context type - opaque *)
type ctx

(** buffer type *)
type buf = (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t

(** digest type - opaque *)
type t

Expand All @@ -39,6 +42,10 @@ val update_substring: ctx -> string -> int -> int -> unit
(** Sha256.update_string ctx s updates the context with s. *)
val update_string: ctx -> string -> unit

(** Sha256.update_buffer ctx a updates the context with a.
Runs parallel to other threads if any exist. *)
external update_buffer: ctx -> buf -> unit = "stub_sha256_update_bigarray"

(** Finalize the context and return digest *)
external finalize: ctx -> t = "stub_sha256_finalize"

Expand All @@ -52,6 +59,9 @@ val string : string -> t
at character number ofs and containing len characters. *)
val substring : string -> int -> int -> t

(** Return the digest of the given buffer. *)
val buffer : buf -> t

(** If len is nonnegative, Sha256.channel ic len reads len characters from
channel ic and returns their digest, or raises End_of_file if end-of-file is
reached before len characters are read. If len is negative, Sha256.channel ic
Expand Down
7 changes: 7 additions & 0 deletions sha512.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
*)

type ctx
type buf = (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
type t

external init: unit -> ctx = "stub_sha512_init"
external unsafe_update_substring: ctx -> string -> int -> int -> unit = "stub_sha512_update"
external update_buffer: ctx -> buf -> unit = "stub_sha512_update_bigarray"
external finalize: ctx -> t = "stub_sha512_finalize"
external copy : ctx -> ctx = "stub_sha512_copy"
external to_bin: t -> string = "stub_sha512_to_bin"
Expand Down Expand Up @@ -48,6 +50,11 @@ let substring s ofs len =
unsafe_update_substring ctx s ofs len;
finalize ctx

let buffer buf =
let ctx = init () in
update_buffer ctx buf;
finalize ctx

let channel chan len =
let ctx = init ()
and buf = String.create blksize in
Expand Down
10 changes: 10 additions & 0 deletions sha512.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
(** context type - opaque *)
type ctx

(** buffer type *)
type buf = (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t

(** digest type - opaque *)
type t

Expand All @@ -39,6 +42,10 @@ val update_substring: ctx -> string -> int -> int -> unit
(** Sha512.update_string ctx s updates the context with s. *)
val update_string: ctx -> string -> unit

(** Sha512.update_buffer ctx a updates the context with a.
Runs parallel to other threads if any exist. *)
external update_buffer: ctx -> buf -> unit = "stub_sha512_update_bigarray"

(** Finalize the context and return digest *)
external finalize: ctx -> t = "stub_sha512_finalize"

Expand All @@ -52,6 +59,9 @@ val string : string -> t
at character number ofs and containing len characters. *)
val substring : string -> int -> int -> t

(** Return the digest of the given buffer. *)
val buffer : buf -> t

(** If len is nonnegative, Sha512.channel ic len reads len characters from
channel ic and returns their digest, or raises End_of_file if end-of-file is
reached before len characters are read. If len is negative, Sha512.channel ic
Expand Down
14 changes: 14 additions & 0 deletions sha512_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static inline int sha512_file(char *filename, sha512_digest *digest)
#include <caml/alloc.h>
#include <caml/custom.h>
#include <caml/fail.h>
#include <caml/bigarray.h>
#include <caml/threads.h>

#define GET_CTX_STRUCT(a) ((struct sha512_ctx *) a)
Expand All @@ -68,6 +69,19 @@ CAMLprim value stub_sha512_update(value ctx, value data, value ofs, value len)
CAMLreturn(Val_unit);
}

CAMLprim value stub_sha512_update_bigarray(value ctx, value buf)
{
CAMLparam2(ctx, buf);
unsigned char *data = Data_bigarray_val(buf);
size_t len = Bigarray_val(buf)->dim[0];

caml_release_runtime_system();
sha512_update(GET_CTX_STRUCT(ctx), data, len);
caml_acquire_runtime_system();

CAMLreturn(Val_unit);
}

CAMLprim value stub_sha512_finalize(value ctx)
{
CAMLparam1(ctx);
Expand Down

0 comments on commit ada51a7

Please sign in to comment.