diff --git a/cachet.opam b/cachet.opam new file mode 100644 index 0000000..ddbef78 --- /dev/null +++ b/cachet.opam @@ -0,0 +1,23 @@ +opam-version: "2.0" +name: "cachet" +maintainer: [ "Romain Calascibetta " + "Reynir Björnsson " ] +authors: [ "Romain Calascibetta " + "Reynir Björnsson " ] +homepage: "https://git.robur.coop/robur/cachet" +bug-reports: "https://git.robur.coop/robur/cachet" +dev-repo: "git+https://git.robur.coop/robur/cachet" +doc: "https://robur-coop.github.io/cachet/" +license: "MIT" +synopsis: "A simple cache system for mmap" +description: """A small library that provides a simple cache system for page-by-page read access on a block device.""" + +build: [ "dune" "build" "-p" name "-j" jobs ] +run-test: [ "dune" "runtest" "-p" name "-j" jobs ] + +depends: [ + "ocaml" {>= "4.14.0"} + "dune" {>= "3.5.0"} + "bigstringaf" {with-test & >= "0.9.0"} + "alcotest" {with-test & >= "1.7.0"} +] diff --git a/lib/cachet.ml b/lib/cachet.ml index cc34e8a..5538db4 100644 --- a/lib/cachet.ml +++ b/lib/cachet.ml @@ -9,6 +9,7 @@ module Bstr = struct type t = bigstring let of_bigstring x = x + let empty = Bigarray.Array1.create Bigarray.char Bigarray.c_layout 0 let length = Bigarray.Array1.dim external get : t -> int -> char = "%caml_ba_ref_1" @@ -230,6 +231,54 @@ let get_string t ~len logical_address = blit_to_bytes t ~src_off:logical_address buf ~dst_off:0 ~len; Bytes.unsafe_to_string buf +let get_uint16_ne t logical_address = + let str = get_string t ~len:2 logical_address in + String.get_uint16_ne str 0 + +let get_uint16_le t logical_address = + let str = get_string t ~len:2 logical_address in + String.get_uint16_le str 0 + +let get_uint16_be t logical_address = + let str = get_string t ~len:2 logical_address in + String.get_uint16_be str 0 + +let get_int16_ne t logical_address = + let str = get_string t ~len:2 logical_address in + String.get_int16_ne str 0 + +let get_int16_le t logical_address = + let str = get_string t ~len:2 logical_address in + String.get_int16_le str 0 + +let get_int16_be t logical_address = + let str = get_string t ~len:2 logical_address in + String.get_int16_be str 0 + +let get_int32_ne t logical_address = + let str = get_string t ~len:4 logical_address in + String.get_int32_ne str 0 + +let get_int32_le t logical_address = + let str = get_string t ~len:4 logical_address in + String.get_int32_le str 0 + +let get_int32_be t logical_address = + let str = get_string t ~len:4 logical_address in + String.get_int32_be str 0 + +let get_int64_ne t logical_address = + let str = get_string t ~len:8 logical_address in + String.get_int64_ne str 0 + +let get_int64_le t logical_address = + let str = get_string t ~len:8 logical_address in + String.get_int64_le str 0 + +let get_int64_be t logical_address = + let str = get_string t ~len:8 logical_address in + String.get_int64_be str 0 + let rec get_seq t logical_address () = match load t logical_address with | Some { offset; payload; length; _ } -> diff --git a/lib/cachet.mli b/lib/cachet.mli index 411bc0d..b31596c 100644 --- a/lib/cachet.mli +++ b/lib/cachet.mli @@ -6,9 +6,20 @@ module Bstr : sig type t = private bigstring + val empty : t + (** [empty] is an empty bigstring. *) + val of_bigstring : bigstring -> t + val length : t -> int + (** [length bstr] is the number of bytes in [bstr]. *) + val get : t -> int -> char + (** [get bstr i] is the byte of [bstr]' at index [i]. This is + equivalent to the [bstr.{i}] notation. + + @raise Invalid_argument if [i] is not an index of [bstr]. *) + val get_int8 : t -> int -> int val get_uint8 : t -> int -> int val get_int16_ne : t -> int -> int @@ -129,8 +140,13 @@ val invalidate : 'fd t -> off:int -> len:int -> unit zero-extend) their result. *) val get_int8 : 'fd t -> int -> int +(** [get_int8 t logical_address] is [t]'s signed 8-bit integer starting at byte + index [logical_address]. *) + val get_uint8 : 'fd t -> int -> int -(* +(** [get_uint8 t logical_address] is [t]'s unsigned 8-bit integer starting at byte + index [logical_address]. *) + val get_uint16_ne : 'fd t -> int -> int val get_uint16_le : 'fd t -> int -> int val get_uint16_be : 'fd t -> int -> int @@ -143,8 +159,6 @@ val get_int32_be : 'fd t -> int -> int32 val get_int64_ne : 'fd t -> int -> int64 val get_int64_le : 'fd t -> int -> int64 val get_int64_be : 'fd t -> int -> int64 -*) - val get_string : 'fd t -> len:int -> int -> string val get_seq : 'fd t -> int -> string Seq.t val next : 'fd t -> slice -> slice option diff --git a/lib/dune b/lib/dune index 8e62221..e70a1aa 100644 --- a/lib/dune +++ b/lib/dune @@ -1,8 +1,12 @@ (library (name cachet) - (modes native) - ; (foreign_stubs - ; (language c) - ; (mode byte) - ; (names hash)) + (modes native byte) + (foreign_stubs + (language c) + (mode byte) + (names hash)) + (foreign_stubs + (language c) + (mode native) + (names stub)) (modules cachet)) diff --git a/lib/stub.c b/lib/stub.c new file mode 100644 index 0000000..e69de29