Skip to content

Commit 4638b6d

Browse files
committed
Merge pull request #5 from vbmithr/master
Doc for module Cstruct
2 parents 32ecad6 + a27a69c commit 4638b6d

File tree

8 files changed

+251
-423
lines changed

8 files changed

+251
-423
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ setup.log
33
*.swp
44
_build/
55
dist/
6+
*.docdir

_oasis

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Library unix_cstruct
4949
Findlibname: unix
5050
Findlibparent: cstruct
5151
Modules: Unix_cstruct
52-
52+
5353
Library "cstruct-syntax"
5454
FindlibName: syntax
5555
FindlibParent: cstruct
@@ -61,3 +61,11 @@ Library "cstruct-syntax"
6161
XMETAType: syntax
6262
XMETADescription: Syntax extension for Cstruct
6363
XMETARequires: camlp4
64+
65+
Document api
66+
Title: Documentation and API reference
67+
Type: ocamlbuild (0.3)
68+
BuildTools+: ocamldoc
69+
XOcamlbuildPath: doc
70+
XOcamlbuildModules: Cstruct, Lwt_cstruct, Unix_cstruct
71+
Install: false

_tags

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,8 @@ true: debug
5757
# OASIS_STOP
5858
<syntax/*.ml>: syntax_camlp4o, pkg_camlp4
5959
<lib_test/*.ml{i}>: syntax_camlp4o, pkg_camlp4, pkg_cstruct.syntax
60+
<lib>: include
61+
<lwt>: include
62+
<async>: include
63+
<unix>: include
6064
true: annot

doc/api.odocl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# OASIS_START
2+
# DO NOT EDIT (digest: 0ef43544ccdcb315230e1f71d392a21d)
3+
Cstruct
4+
Lwt_cstruct
5+
Unix_cstruct
6+
# OASIS_STOP

lib/cstruct.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,31 +117,39 @@ let set_len t len =
117117
let add_len t len =
118118
{ t with len = t.len + len }
119119

120+
let invalid_arg fmt =
121+
let b = Buffer.create 20 in (* for thread safety. *)
122+
let ppf = Format.formatter_of_buffer b in
123+
let k ppf = Format.pp_print_flush ppf (); invalid_arg (Buffer.contents b) in
124+
Format.kfprintf k ppf fmt
125+
126+
let invalid_bounds j l = invalid_arg "invalid bounds (index %d, length %d)" j l
127+
120128
external unsafe_blit_bigstring_to_bigstring : buffer -> int -> buffer -> int -> int -> unit = "caml_blit_bigstring_to_bigstring" "noalloc"
121129

122130
external unsafe_blit_string_to_bigstring : string -> int -> buffer -> int -> int -> unit = "caml_blit_string_to_bigstring" "noalloc"
123131

124132
external unsafe_blit_bigstring_to_string : buffer -> int -> string -> int -> int -> unit = "caml_blit_bigstring_to_string" "noalloc"
125133

126134
let copy src srcoff len =
127-
if src.len - srcoff < len then raise (Failure "copy");
135+
if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len));
128136
let s = String.create len in
129137
unsafe_blit_bigstring_to_string src.buffer (src.off+srcoff) s 0 len;
130138
s
131139

132140
let blit src srcoff dst dstoff len =
133-
if src.len - srcoff < len then raise (Failure "blit");
134-
if dst.len - dstoff < len then raise (Failure "blitdst");
141+
if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len));
142+
if dst.len - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len));
135143
unsafe_blit_bigstring_to_bigstring src.buffer (src.off+srcoff) dst.buffer (dst.off+dstoff) len
136144

137145
let blit_from_string src srcoff dst dstoff len =
138-
if String.length src - srcoff < len then raise (Failure "blit_from_string");
139-
if dst.len - dstoff < len then raise (Failure "blit_from_string dst");
146+
if String.length src - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len));
147+
if dst.len - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len));
140148
unsafe_blit_string_to_bigstring src srcoff dst.buffer (dst.off+dstoff) len
141149

142150
let blit_to_string src srcoff dst dstoff len =
143-
if src.len - srcoff < len then raise (Failure "blit_to_string");
144-
if String.length dst - dstoff < len then raise (Failure "blit_to_string dst");
151+
if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len));
152+
if String.length dst - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len));
145153
unsafe_blit_bigstring_to_string src.buffer (src.off+srcoff) dst dstoff len
146154

147155
let set_uint8 t i c =

lib/cstruct.mli

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,41 @@
1414
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*)
1616

17+
(** Manipulate external buffers as C-like structs *)
18+
1719
type buffer = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
20+
(** Type of a buffer. A cstruct is composed of an underlying buffer
21+
and position/length within this buffer. *)
1822

1923
type t = private {
2024
buffer: buffer;
2125
off : int;
2226
len : int;
2327
}
28+
(** Type of a cstruct. *)
29+
30+
(** Functions that create a new cstruct. *)
2431

2532
val of_bigarray: ?off:int -> ?len:int -> buffer -> t
33+
(** [of_bigarray ~off ~len b] is the cstruct contained in [b] starting
34+
at [off], of length [len]. *)
35+
2636
val create : int -> t
37+
(** [create len] is a cstruct of size [len] with an offset of 0. *)
38+
39+
val of_string: ?allocator:(int -> t) -> string -> t
40+
(** [of_string ~alloc str] is the cstruct representation of [str],
41+
with the underlying buffer allocated by [alloc]. If [alloc] is not
42+
provided, [create] is used. *)
43+
44+
(** Functions that operate over cstructs. *)
45+
2746
val check_bounds : t -> int -> bool
47+
(** [check_bounds cstr len] is [true] if [cstr.buffer]'s size is
48+
greater or equal than [len], [false] otherwise. *)
2849

2950
type byte = char
51+
3052
val byte : int -> byte
3153
val byte_to_int : byte -> int
3254

@@ -54,16 +76,47 @@ val set_char: t -> int -> char -> unit
5476
val set_uint8: t -> int -> uint8 -> unit
5577

5678
val sub: t -> int -> int -> t
79+
(** [sub cstr off len] is [{ t with off = t.off + off; len }] *)
5780

5881
val shift: t -> int -> t
82+
(** [shift cstr len] is [{ t with off = t.off + off; len = t.len - off
83+
}] *)
5984

6085
val copy: t -> int -> int -> string
86+
(** [copy cstr off len] is the string representation of the segment of
87+
[t] starting at [off] of size [len].
88+
89+
Raise [Invalid_argument] if [off] and [len] do not designate a
90+
valid segment of [t]. *)
6191

6292
val blit: t -> int -> t -> int -> int -> unit
93+
(** [blit src srcoff dst dstoff len] copies [len] characters from
94+
cstruct [src], starting at index [srcoff], to cstruct [dst],
95+
starting at index [dstoff]. It works correctly even if [src] and
96+
[dst] are the same string, and the source and destination
97+
intervals overlap.
98+
99+
Raise [Invalid_argument] if [srcoff] and [len] do not designate a
100+
valid segment of [src], or if [dstoff] and [len] do not designate
101+
a valid segment of [dst]. *)
63102

64103
val blit_from_string: string -> int -> t -> int -> int -> unit
104+
(** [blit_from_string src srcoff dst dstoff len] copies [len]
105+
characters from string [src], starting at index [srcoff], to
106+
string [dst], starting at index [dstoff].
107+
108+
Raise [Invalid_argument] if [srcoff] and [len] do not designate a
109+
valid substring of [src], or if [dstoff] and [len] do not
110+
designate a valid segment of [dst]. *)
65111

66112
val blit_to_string: t -> int -> string -> int -> int -> unit
113+
(** [blit_to_string src srcoff dst dstoff len] copies [len] characters
114+
from cstruct [src], starting at index [srcoff], to string [dst],
115+
starting at index [dstoff].
116+
117+
Raise [Invalid_argument] if [srcoff] and [len] do not designate a
118+
valid segment of [src], or if [dstoff] and [len] do not designate
119+
a valid substring of [dst]. *)
67120

68121
val len: t -> int
69122

@@ -72,11 +125,13 @@ val set_len : t -> int -> t
72125
val add_len : t -> int -> t
73126

74127
val split: ?start:int -> t -> int -> t * t
128+
(** [split ~start cstr len] is a tuple containing the cstruct
129+
extracted from [cstr] at offset [start] (default: 0) of length
130+
[len] as first element, and the rest of [cstr] as second
131+
element. *)
75132

76133
val to_string: t -> string
77134

78-
val of_string: ?allocator:(int -> t) -> string -> t
79-
80135
val hexdump: t -> unit
81136
val debug: t -> string
82137

@@ -103,13 +158,20 @@ end
103158
(** {2 List of buffers} *)
104159

105160
val lenv: t list -> int
161+
(** [lenv cstrs] is the combined length of all cstructs in [cstrs]. *)
106162

107163
val copyv: t list -> string
164+
(** [copyv cstrs] is the string representation of the concatenation of
165+
all cstructs in [cstrs]. *)
108166

109167
(** {2 Iterations} *)
110168

111169
type 'a iter = unit -> 'a option
170+
(** Type of an iterator. *)
112171

113172
val iter: (t -> int option) -> (t -> 'a) -> t -> 'a iter
173+
(** [iter lenf of_cstr cstr] is an iterator over [cstr] that returns
174+
elements of size [lenf cstr] and type [of_cstr cstr]. *)
114175

115176
val fold: ('b -> 'a -> 'b) -> 'a iter -> 'b -> 'b
177+
(** [fold f iter acc] is [(f iterN accN ... (f iter acc)...)]. *)

myocamlbuild.ml

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(* OASIS_START *)
2-
(* DO NOT EDIT (digest: b1d7287a47d5441fed143addea5c84ab) *)
2+
(* DO NOT EDIT (digest: 5c43404f9413ffbf5bbd77be7addc313) *)
33
module OASISGettext = struct
4-
(* # 21 "/Users/avsm/.opam/system/build/oasis-mirage.0.3.0a/src/oasis/OASISGettext.ml" *)
4+
(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISGettext.ml" *)
55

66
let ns_ str =
77
str
@@ -24,7 +24,7 @@ module OASISGettext = struct
2424
end
2525

2626
module OASISExpr = struct
27-
(* # 21 "/Users/avsm/.opam/system/build/oasis-mirage.0.3.0a/src/oasis/OASISExpr.ml" *)
27+
(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISExpr.ml" *)
2828

2929

3030

@@ -116,7 +116,7 @@ end
116116

117117
# 117 "myocamlbuild.ml"
118118
module BaseEnvLight = struct
119-
(* # 21 "/Users/avsm/.opam/system/build/oasis-mirage.0.3.0a/src/base/BaseEnvLight.ml" *)
119+
(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseEnvLight.ml" *)
120120

121121
module MapString = Map.Make(String)
122122

@@ -214,7 +214,7 @@ end
214214

215215
# 215 "myocamlbuild.ml"
216216
module MyOCamlbuildFindlib = struct
217-
(* # 21 "/Users/avsm/.opam/system/build/oasis-mirage.0.3.0a/src/plugins/ocamlbuild/MyOCamlbuildFindlib.ml" *)
217+
(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildFindlib.ml" *)
218218

219219
(** OCamlbuild extension, copied from
220220
* http://brion.inria.fr/gallium/index.php/Using_ocamlfind_with_ocamlbuild
@@ -234,21 +234,19 @@ module MyOCamlbuildFindlib = struct
234234
Ocamlbuild_pack.Lexers.blank_sep_strings
235235

236236
let split s ch =
237-
let buf = Buffer.create 13 in
238-
let x = ref [] in
239-
let flush () =
240-
x := (Buffer.contents buf) :: !x;
241-
Buffer.clear buf
237+
let x =
238+
ref []
242239
in
243-
String.iter
244-
(fun c ->
245-
if c = ch then
246-
flush ()
247-
else
248-
Buffer.add_char buf c)
249-
s;
250-
flush ();
251-
List.rev !x
240+
let rec go s =
241+
let pos =
242+
String.index s ch
243+
in
244+
x := (String.before s pos)::!x;
245+
go (String.after s (pos + 1))
246+
in
247+
try
248+
go s
249+
with Not_found -> !x
252250

253251
let split_nl s = split s '\n'
254252

@@ -283,27 +281,17 @@ module MyOCamlbuildFindlib = struct
283281

284282
(* When one link an OCaml library/binary/package, one should use -linkpkg *)
285283
flag ["ocaml"; "link"; "program"] & A"-linkpkg";
286-
flag ["ocaml"; "link"; "output_obj"] & A"-linkpkg";
287284

288285
(* For each ocamlfind package one inject the -package option when
289286
* compiling, computing dependencies, generating documentation and
290287
* linking. *)
291288
List.iter
292289
begin fun pkg ->
293-
let base_args = [A"-package"; A pkg] in
294-
let syn_args = [A"-syntax"; A "camlp4o"] in
295-
let args =
296-
(* heuristic to identify syntax extensions:
297-
whether they end in ".syntax"; some might not *)
298-
if Filename.check_suffix pkg "syntax"
299-
then syn_args @ base_args
300-
else base_args
301-
in
302-
flag ["ocaml"; "compile"; "pkg_"^pkg] & S args;
303-
flag ["ocaml"; "ocamldep"; "pkg_"^pkg] & S args;
304-
flag ["ocaml"; "doc"; "pkg_"^pkg] & S args;
305-
flag ["ocaml"; "link"; "pkg_"^pkg] & S base_args;
306-
flag ["ocaml"; "infer_interface"; "pkg_"^pkg] & S args;
290+
flag ["ocaml"; "compile"; "pkg_"^pkg] & S[A"-package"; A pkg];
291+
flag ["ocaml"; "ocamldep"; "pkg_"^pkg] & S[A"-package"; A pkg];
292+
flag ["ocaml"; "doc"; "pkg_"^pkg] & S[A"-package"; A pkg];
293+
flag ["ocaml"; "link"; "pkg_"^pkg] & S[A"-package"; A pkg];
294+
flag ["ocaml"; "infer_interface"; "pkg_"^pkg] & S[A"-package"; A pkg];
307295
end
308296
(find_packages ());
309297

@@ -335,7 +323,7 @@ module MyOCamlbuildFindlib = struct
335323
end
336324

337325
module MyOCamlbuildBase = struct
338-
(* # 21 "/Users/avsm/.opam/system/build/oasis-mirage.0.3.0a/src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *)
326+
(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *)
339327

340328
(** Base functions for writing myocamlbuild.ml
341329
@author Sylvain Le Gall
@@ -351,7 +339,7 @@ module MyOCamlbuildBase = struct
351339
type name = string
352340
type tag = string
353341

354-
(* # 56 "/Users/avsm/.opam/system/build/oasis-mirage.0.3.0a/src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *)
342+
(* # 56 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *)
355343

356344
type t =
357345
{
@@ -464,24 +452,6 @@ module MyOCamlbuildBase = struct
464452
)
465453
t.lib_c;
466454

467-
(* Add output_obj rules mapped to .nobj.o *)
468-
let native_output_obj x =
469-
OC.link_gen "cmx" "cmxa" !Options.ext_lib [!Options.ext_obj; "cmi"]
470-
OC.ocamlopt_link_prog
471-
(fun tags -> tags++"ocaml"++"link"++"native"++"output_obj") x
472-
in
473-
rule "ocaml: cmx* and o* -> .nobj.o" ~prod:"%.nobj.o" ~deps:["%.cmx"; "%.o"]
474-
(native_output_obj "%.cmx" "%.nobj.o");
475-
476-
(* Add output_obj rules mapped to .bobj.o *)
477-
let bytecode_output_obj x =
478-
OC.link_gen "cmo" "cma" !Options.ext_lib [!Options.ext_obj; "cmi"]
479-
OC.ocamlc_link_prog
480-
(fun tags -> tags++"ocaml"++"link"++"byte"++"output_obj") x
481-
in
482-
rule "ocaml: cmo* -> .nobj.o" ~prod:"%.bobj.o" ~deps:["%.cmo"]
483-
(bytecode_output_obj "%.cmo" "%.bobj.o");
484-
485455
(* Add flags *)
486456
List.iter
487457
(fun (tags, cond_specs) ->
@@ -503,7 +473,7 @@ module MyOCamlbuildBase = struct
503473
end
504474

505475

506-
# 506 "myocamlbuild.ml"
476+
# 476 "myocamlbuild.ml"
507477
open Ocamlbuild_plugin;;
508478
let package_default =
509479
{
@@ -527,6 +497,6 @@ let package_default =
527497

528498
let dispatch_default = MyOCamlbuildBase.dispatch_default package_default;;
529499

530-
# 531 "myocamlbuild.ml"
500+
# 501 "myocamlbuild.ml"
531501
(* OASIS_STOP *)
532502
Ocamlbuild_plugin.dispatch dispatch_default;;

0 commit comments

Comments
 (0)