exception Error of string
(** This exception is raised when there is an error. *)
+exception Handle_closed of string
+(** This exception is raised if you use a {!Guestfs.t} handle
+ after calling {!close} on it. The string is the name of
+ the function. *)
+
val create : unit -> t
+(** Create a {!Guestfs.t} handle. *)
val close : t -> unit
-(** Handles are closed by the garbage collector when they become
- unreferenced, but callers can also call this in order to
- provide predictable cleanup. *)
+(** Close the {!Guestfs.t} handle and free up all resources used
+ by it immediately.
+
+ Handles are closed by the garbage collector when they become
+ unreferenced, but callers can call this in order to provide
+ predictable cleanup. *)
";
generate_ocaml_structure_decls ();
generate_ocaml_prototype name style;
pr "(** %s *)\n" shortdesc;
pr "\n"
- ) all_functions
+ ) all_functions_sorted
(* Generate the OCaml bindings implementation. *)
and generate_ocaml_ml () =
pr "\
type t
+
exception Error of string
+exception Handle_closed of string
+
external create : unit -> t = \"ocaml_guestfs_create\"
external close : t -> unit = \"ocaml_guestfs_close\"
+(* Give the exceptions names, so they can be raised from the C code. *)
let () =
- Callback.register_exception \"ocaml_guestfs_error\" (Error \"\")
+ Callback.register_exception \"ocaml_guestfs_error\" (Error \"\");
+ Callback.register_exception \"ocaml_guestfs_closed\" (Handle_closed \"\")
";
List.iter (
fun (name, style, _, _, _, shortdesc, _) ->
generate_ocaml_prototype ~is_external:true name style;
- ) all_functions
+ ) all_functions_sorted
(* Generate the OCaml bindings C implementation. *)
and generate_ocaml_c () =
(* The wrappers. *)
List.iter (
fun (name, style, _, _, _, _, _) ->
+ pr "/* Automatically generated wrapper for function\n";
+ pr " * ";
+ generate_ocaml_prototype name style;
+ pr " */\n";
+ pr "\n";
+
let params =
"gv" :: List.map (fun arg -> name_of_argt arg ^ "v") (snd style) in
pr "/* Emit prototype to appease gcc's -Wmissing-prototypes. */\n";
pr "CAMLprim value ocaml_guestfs_%s (value %s" name (List.hd params);
List.iter (pr ", value %s") (List.tl params); pr ");\n";
+ pr "\n";
pr "CAMLprim value\n";
pr "ocaml_guestfs_%s (value %s" name (List.hd params);
pr " guestfs_h *g = Guestfs_val (gv);\n";
pr " if (g == NULL)\n";
- pr " caml_failwith (\"%s: used handle after closing it\");\n" name;
+ pr " ocaml_guestfs_raise_closed (\"%s\");\n" name;
pr "\n";
List.iter (
pr "}\n";
pr "\n"
)
- ) all_functions
+ ) all_functions_sorted
and generate_ocaml_structure_decls () =
List.iter (