Permalink
Browse files

Merge pull request #35 from djs55/fix-safe-string

Use `bytes` rather than `string` for mutation
  • Loading branch information...
djs55 committed Nov 14, 2017
2 parents 55322c3 + 78ffb1e commit 51c2cda48edc833a44aef237f5f59b9093fef1c7
Showing with 59 additions and 33 deletions.
  1. +12 −4 .travis.yml
  2. +5 −0 CHANGES.md
  3. +9 −4 appveyor.yml
  4. +6 −6 lib/ring.ml
  5. +4 −4 lib/ring.mli
  6. +2 −0 lib_test/jbuild
  7. +21 −15 lib_test/ring_test.ml
View
@@ -1,6 +1,14 @@
language: c
-install: wget https://raw.githubusercontent.com/ocaml/ocaml-travisci-skeleton/master/.travis-opam.sh
-script: bash -ex .travis-opam.sh
+sudo: false
+services:
+ - docker
+install: wget https://raw.githubusercontent.com/ocaml/ocaml-travisci-skeleton/master/.travis-docker.sh
+script: bash ./.travis-docker.sh
env:
- - PACKAGE="shared-memory-ring-lwt" OCAML_VERSION=4.03 REVDEPS=true
- - PACKAGE="shared-memory-ring-lwt" OCAML_VERSION=4.02 REVDEPS=true
+ global:
+ - PINS="shared-memory-ring:. shared-memory-ring-lwt:."
+ matrix:
+ - DISTRO=alpine OCAML_VERSION=4.04.0 PACKAGE="shared-memory-ring"
+ - DISTRO=alpine OCAML_VERSION=4.04.0 PACKAGE="shared-memory-ring-lwt"
+ - DISTRO=alpine OCAML_VERSION=4.06.0 PACKAGE="shared-memory-ring"
+ - DISTRO=alpine OCAML_VERSION=4.06.0 PACKAGE="shared-memory-ring-lwt"
View
@@ -1,3 +1,8 @@
+## 3.0.0 (2017-11-05)
+
+* Update to use `bytes` in the `read` and `write`, to work with OCaml 4.06 and
+ `-safe-string`
+
## 2.0.1 (2017-06-07)
* Don't force clients to link against `cstruct.ppx`: this is the support code
for the PPX rewriter itself, and brings in the `compiler-libs` package etc.
View
@@ -1,15 +1,20 @@
platform:
- - x86
+ - x64
environment:
- CYG_ROOT: "C:\\cygwin"
+ CYG_ROOT: "C:\\cygwin64"
+ CYG_CACHE: C:/cygwin64/var/cache/setup
+ CYG_MIRROR: http://mirrors.kernel.org/sourceware/cygwin/
+ CYG_ARCH: x86_64
CYG_BASH: "%CYG_ROOT%\\bin\\bash -lc"
+ CYGWIN: "winsymlinks:native"
PACKAGE: "shared-memory-ring-lwt"
install:
+ - 'appveyor DownloadFile http://cygwin.com/setup-%CYG_ARCH%.exe -FileName setup.exe'
+ - 'setup.exe -gqnNdO -R "%CYG_ROOT%" -s "%CYG_MIRROR%" -l "%CYG_CACHE%" -P make,git,rsync,perl,gcc-core,gcc-g++,libncurses-devel,unzip,libmpfr-devel,patch,flexdll,libglpk-devel'
- appveyor DownloadFile https://raw.githubusercontent.com/ocaml/ocaml-ci-scripts/master/appveyor-opam.sh
- - "%CYG_ROOT%\\setup-x86.exe -qnNdO -R %CYG_ROOT% -s http://cygwin.mirror.constant.com -l C:/cygwin/var/cache/setup -P rsync -P patch -P make -P git -P perl -P mingw64-x86_64-gcc-core"
- - curl -L -o C:/cygwin/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-win32.exe
+ - curl -L -o C:/cygwin64/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-win32.exe
build_script:
- "%CYG_BASH% '${APPVEYOR_BUILD_FOLDER}/appveyor-opam.sh'"
View
@@ -332,11 +332,11 @@ module type S = sig
module Reader: READABLE
module Writer: WRITABLE
- val write: Cstruct.t -> string -> int -> int -> int
- val read: Cstruct.t -> string -> int -> int -> int
+ val write: Cstruct.t -> bytes -> int -> int -> int
+ val read: Cstruct.t -> bytes -> int -> int -> int
- val unsafe_write: Cstruct.t -> string -> int -> int -> int
- val unsafe_read: Cstruct.t -> string -> int -> int -> int
+ val unsafe_write: Cstruct.t -> bytes -> int -> int -> int
+ val unsafe_read: Cstruct.t -> bytes -> int -> int -> int
end
@@ -409,15 +409,15 @@ module Pipe(RW: RW) = struct
let seq, frag = Reader.read t in
let data_available = Cstruct.len frag in
let can_read = min len data_available in
- Cstruct.blit_to_string frag 0 buf ofs can_read;
+ Cstruct.blit_to_bytes frag 0 buf ofs can_read;
Reader.advance t Int32.(add seq (of_int can_read));
can_read
let write t buf ofs len =
let seq, frag = Writer.write t in
let free_space = Cstruct.len frag in
let can_write = min len free_space in
- Cstruct.blit_from_string buf ofs frag 0 can_write;
+ Cstruct.blit_from_bytes buf ofs frag 0 can_write;
Writer.advance t Int32.(add seq (of_int can_write));
can_write
View
@@ -203,17 +203,17 @@ module type S = sig
If you do need to reconnect or need to avoid copying, use the READER and
WRITABLE signatures above *)
- val write: Cstruct.t -> string -> int -> int -> int
+ val write: Cstruct.t -> bytes -> int -> int -> int
(** [write stream buf ofs len] writes up to [len] bytes from [buf] at [ofs]
to [stream]. If this returns short it means EOF *)
- val read: Cstruct.t -> string -> int -> int -> int
+ val read: Cstruct.t -> bytes -> int -> int -> int
(** [read stream buf ofs len] reads up to [len] bytes to [buf] at [ofs] from
[stream]. If this returns short it means EOF *)
(* These functions are deprecated (and nolonger unsafe, see #10) *)
- val unsafe_write: Cstruct.t -> string -> int -> int -> int
- val unsafe_read: Cstruct.t -> string -> int -> int -> int
+ val unsafe_write: Cstruct.t -> bytes -> int -> int -> int
+ val unsafe_read: Cstruct.t -> bytes -> int -> int -> int
end
module Pipe: functor(RW: RW) -> S
View
@@ -1,11 +1,13 @@
(library
((name test_library)
(c_names (old_ring_stubs))
+ (modules ())
))
(executables
((names (ring_test))
(libraries (lwt lwt.unix shared-memory-ring oUnit test_library))
+ (preprocess (pps (cstruct.ppx)))
))
(alias
View
@@ -56,16 +56,18 @@ let xenstore_init () =
let xenstore_hello () =
let msg = "hello" in
+ let msg' = Bytes.of_string msg in
let buf = String.make 16 '\000' in
+ let buf' = Bytes.make 16 '\000' in
with_xenstores
(fun b1 b2 a b ->
- let x = Xenstore_ring.Ring.Front.unsafe_write a msg 0 (String.length msg) in
+ let x = Xenstore_ring.Ring.Front.unsafe_write a msg' 0 (Bytes.length msg') in
let y = Old_ring.C_Xenstore.unsafe_write b msg (String.length msg) in
assert_equal ~printer:string_of_int x y;
compare_bufs b1 b2;
- let x = Xenstore_ring.Ring.Back.unsafe_read a buf 0 (String.length buf) in
+ let x = Xenstore_ring.Ring.Back.unsafe_read a buf' 0 (Bytes.length buf') in
assert_equal ~printer:string_of_int x (String.length msg);
- assert_equal msg (String.sub buf 0 x);
+ assert_equal msg' (Bytes.sub buf' 0 x);
let x = Old_ring.C_Xenstore.Back.unsafe_read b buf (String.length buf) in
assert_equal ~printer:string_of_int x (String.length msg);
assert_equal msg (String.sub buf 0 x);
@@ -86,14 +88,15 @@ type ring = {
let check_signed_unsigned_write () =
(* Check for errors performing comparison across int32 max_int *)
let msg = "this is a test" in
+ let msg' = Bytes.of_string msg in
let ofs = Int32.(succ (succ max_int)) in
with_xenstores
(fun b1 b2 a b ->
set_ring_output_cons a ofs;
set_ring_output_prod a ofs;
set_ring_output_cons (Cstruct.of_bigarray b2) ofs;
set_ring_output_prod (Cstruct.of_bigarray b2) ofs;
- let x = Xenstore_ring.Ring.Front.unsafe_write a msg 0 (String.length msg) in
+ let x = Xenstore_ring.Ring.Front.unsafe_write a msg' 0 (Bytes.length msg') in
let y = Old_ring.C_Xenstore.unsafe_write b msg (String.length msg) in
assert_equal ~printer:string_of_int x y;
compare_bufs b1 b2;
@@ -102,13 +105,14 @@ let check_signed_unsigned_write () =
let check_signed_unsigned_read () =
let msg = "this is a test" in
let buf = String.make (String.length msg) '\000' in
+ let buf' = Bytes.make (String.length msg) '\000' in
with_xenstores
(fun b1 b2 a b ->
set_ring_output_cons a (Int32.(pred (pred max_int)));
set_ring_output_prod a (Int32.(succ (succ max_int)));
set_ring_output_cons (Cstruct.of_bigarray b2) (Int32.(pred (pred max_int)));
set_ring_output_prod (Cstruct.of_bigarray b2) (Int32.(succ (succ max_int)));
- let x' = Xenstore_ring.Ring.Back.unsafe_read a buf 0 (String.length buf) in
+ let x' = Xenstore_ring.Ring.Back.unsafe_read a buf' 0 (Bytes.length buf') in
let y' = Old_ring.C_Xenstore.Back.unsafe_read b buf (String.length buf) in
assert_equal ~printer:string_of_int x' y';
compare_bufs b1 b2;
@@ -131,16 +135,18 @@ let console_init () =
let console_hello () =
let msg = "hello" in
+ let msg' = Bytes.of_string msg in
let buf = String.make 16 '\000' in
+ let buf' = Bytes.make 16 '\000' in
with_consoles
(fun b1 b2 a b ->
- let x = Console_ring.Ring.Front.unsafe_write a msg 0 (String.length msg) in
+ let x = Console_ring.Ring.Front.unsafe_write a msg' 0 (Bytes.length msg') in
let y = Old_ring.C_Console.unsafe_write b msg (String.length msg) in
assert_equal ~printer:string_of_int x y;
compare_bufs b1 b2;
- let x = Console_ring.Ring.Back.unsafe_read a buf 0 (String.length buf) in
- assert_equal ~printer:string_of_int x (String.length msg);
- assert_equal msg (String.sub buf 0 x);
+ let x = Console_ring.Ring.Back.unsafe_read a buf' 0 (Bytes.length buf') in
+ assert_equal ~printer:string_of_int x (Bytes.length msg');
+ assert_equal msg (Bytes.to_string @@ Bytes.sub buf' 0 x);
let x = Old_ring.C_Console.Back.unsafe_read b buf (String.length buf) in
assert_equal ~printer:string_of_int x (String.length msg);
assert_equal msg (String.sub buf 0 x);
@@ -162,8 +168,8 @@ let throughput_test ~use_ocaml ~write_chunk_size ~read_chunk_size ~verify () =
(fun b1 b2 a b ->
let read_chunk = String.make read_chunk_size '\000' in
let input = bigarray_to_string block' in
- let length = String.length input in
- let output = String.make length 'X' in
+ let length = Bytes.length input in
+ let output = Bytes.make length 'X' in
let producer = ref 0 in
let consumer = ref 0 in
let start = Unix.gettimeofday () in
@@ -174,7 +180,7 @@ let throughput_test ~use_ocaml ~write_chunk_size ~read_chunk_size ~verify () =
let written =
if use_ocaml
then Console_ring.Ring.Front.unsafe_write a input !producer can_write
- else Old_ring.C_Console.unsafe_write b (String.sub input !producer can_write) can_write in
+ else Old_ring.C_Console.unsafe_write b (Bytes.to_string @@ Bytes.sub input !producer can_write) can_write in
producer := !producer + written;
let remaining = length - !consumer in
let can_read = min read_chunk_size remaining in
@@ -194,11 +200,11 @@ let throughput_test ~use_ocaml ~write_chunk_size ~read_chunk_size ~verify () =
end in
(* verify *)
if verify then begin
- let originally_written = String.sub input !consumer read in
- let block_read = String.sub output !consumer read in
+ let originally_written = Bytes.sub input !consumer read in
+ let block_read = Bytes.sub output !consumer read in
if originally_written <> block_read then begin
Printf.fprintf stderr "producer = %d\nconsumer = %d\nwritten = %d\nread = %d\n%!" !producer !consumer written read;
- assert_equal ~msg:"transfer" ~printer:(fun x -> x) originally_written block_read
+ assert_equal ~msg:"transfer" ~printer:Bytes.to_string originally_written block_read
end;
end;

0 comments on commit 51c2cda

Please sign in to comment.