Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Use memcpy to blit between storage types.
Browse files Browse the repository at this point in the history
  • Loading branch information
klakplok committed Dec 13, 2016
1 parent fddcb08 commit cc40fbe
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 34 deletions.
1 change: 1 addition & 0 deletions .merlin
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ PKG bytes
PKG ctypes.stubs
PKG ocamlbuild
PKG oUnit
PKG memcpy
2 changes: 1 addition & 1 deletion lib/META
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version = "0.5.0"
description = "Binding to libsodium, a shared library wrapper for djb's NaCl"
requires = "bigarray bytes ctypes.stubs"
requires = "bigarray bytes ctypes.stubs memcpy"
archive(byte) = "sodium.cma"
archive(native) = "sodium.cmxa"
exists_if = "sodium.cma"
2 changes: 1 addition & 1 deletion lib/_tags
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<*.{ml,mli}>: package(bytes), package(bigarray), package(ctypes.stubs)
<*.{ml,mli}>: package(bytes), package(bigarray), package(ctypes.stubs), package(memcpy)
<*.{cma,cmxa}>: use_sodium_stubs
<libsodium_stubs.*>: use_sodium
<*.c>: use_ctypes, use_sodium
73 changes: 43 additions & 30 deletions lib/sodium_storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,52 @@ module Bigbytes = struct

let ctype = ptr char

open Bigarray
module Array1 = Bigarray.Array1

let create len = (Array1.create char c_layout len)
let create len = Array1.create Bigarray.char Bigarray.c_layout len
let length str = Array1.dim str
let len_size_t str = Unsigned.Size_t.of_int (Array1.dim str)
let len_ullong str = Unsigned.ULLong.of_int (Array1.dim str)
let to_ptr str = bigarray_start array1 str
let zero str pos len = (Array1.fill (Array1.sub str pos len) '\x00')

let to_bytes str =
let str' = Bytes.create (Array1.dim str) in
Bytes.iteri (fun i _ -> Bytes.set str' i (Array1.unsafe_get str i)) str';
str'

let of_bytes str =
let str' = create (Bytes.length str) in
Bytes.iteri (Array1.unsafe_set str') str;
str'
let to_bytes src =
let len = length src in
let dst = Bytes.create len in
Memcpy.memcpy
(Memcpy.bigarray Ctypes.array1 len Bigarray.char) Memcpy.ocaml_bytes
~src ~src_off: 0 ~dst ~dst_off: 0 ~len;
dst

let of_bytes src =
let len = Bytes.length src in
let dst = create len in
Memcpy.memcpy
Memcpy.ocaml_bytes (Memcpy.bigarray Ctypes.array1 len Bigarray.char)
~src ~src_off: 0 ~dst ~dst_off: 0 ~len;
dst

let sub = Array1.sub

let blit src srcoff dst dstoff len =
Array1.blit (Array1.sub src srcoff len)
(Array1.sub dst dstoff len)
let blit src src_off dst dst_off len =
Array1.blit (Array1.sub src src_off len)
(Array1.sub dst dst_off len)

let blit_bigbytes = blit
let blit_to_bigbytes = blit

let blit_bytes src srcoff dst dstoff len =
for i = 0 to len - 1 do
Array1.set dst (dstoff + i) (Bytes.get src (srcoff + i))
done
let blit_to_bytes src srcoff dst dstoff len =
for i = 0 to len - 1 do
Bytes.set dst (dstoff + i) (Array1.get src (srcoff + i))
done
let blit_bytes src src_off dst dst_off len =
Memcpy.memcpy
Memcpy.ocaml_bytes
(Memcpy.bigarray Ctypes.array1 (length dst) Bigarray.char)
~src ~src_off ~dst ~dst_off ~len

let blit_to_bytes src src_off dst dst_off len =
Memcpy.memcpy
(Memcpy.bigarray Ctypes.array1 (length src) Bigarray.char)
Memcpy.ocaml_bytes
~src ~src_off ~dst ~dst_off ~len

end

module Bytes = struct
Expand All @@ -90,12 +100,15 @@ module Bytes = struct
let blit_bytes = blit
let blit_to_bytes = blit

let blit_bigbytes src srcoff dst dstoff len =
for i = 0 to len - 1 do
Bytes.set dst (dstoff + i) (Bigarray.Array1.get src (srcoff + i))
done
let blit_to_bigbytes src srcoff dst dstoff len =
for i = 0 to len - 1 do
Bigarray.Array1.set dst (dstoff + i) (Bytes.get src (srcoff + i))
done
let blit_bigbytes src src_off dst dst_off len =
Memcpy.memcpy
(Memcpy.bigarray Ctypes.array1 (Bigbytes.length src) Bigarray.char)
Memcpy.ocaml_bytes
~src ~src_off ~dst ~dst_off ~len

let blit_to_bigbytes src src_off dst dst_off len =
Memcpy.memcpy
Memcpy.ocaml_bytes
(Memcpy.bigarray Ctypes.array1 (Bigbytes.length dst) Bigarray.char)
~src ~src_off ~dst ~dst_off ~len
end
2 changes: 1 addition & 1 deletion lib_gen/_tags
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<*.{ml,byte,native}>: package(ctypes.stubs)
<*.{ml,byte,native}>: package(ctypes.stubs), package(memcpy)
2 changes: 1 addition & 1 deletion lib_test/_tags
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<*.{ml,byte,native}>: debug, package(bigarray), package(bytes), package(ctypes.stubs), package(oUnit), use_sodium_stubs
<*.{ml,byte,native}>: debug, package(bigarray), package(bytes), package(ctypes.stubs), package(oUnit), use_sodium_stubs, package(memcpy)
1 change: 1 addition & 0 deletions opam
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ depends: [
"ocamlfind" {build}
"ocamlbuild" {build}
"ctypes" {>= "0.4.0"}
"memcpy"
]
depexts: [
[ ["debian"] ["libsodium-dev"] ]
Expand Down

0 comments on commit cc40fbe

Please sign in to comment.