+/* This macro was added in OCaml 3.10. Backport for earlier versions. */
+#ifndef CAMLreturnT
+#define CAMLreturnT(type, result) do{ \
+ type caml__temp_result = (result); \
+ caml_local_roots = caml__frame; \
+ return (caml__temp_result); \
+}while(0)
+#endif
+
+/* Allocate handles and deal with finalization. */
+static void
+guestfs_finalize (value gv)
+{
+ guestfs_h *g = Guestfs_val (gv);
+ if (g) guestfs_close (g);
+}
+
+static struct custom_operations guestfs_custom_operations = {
+ "guestfs_custom_operations",
+ guestfs_finalize,
+ custom_compare_default,
+ custom_hash_default,
+ custom_serialize_default,
+ custom_deserialize_default
+};
+
+static value
+Val_guestfs (guestfs_h *g)
+{
+ CAMLparam0 ();
+ CAMLlocal1 (rv);
+
+ rv = caml_alloc_custom (&guestfs_custom_operations,
+ sizeof (guestfs_h *), 0, 1);
+ Guestfs_val (rv) = g;
+
+ CAMLreturn (rv);
+}
+
+void
+ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
+{
+ CAMLparam0 ();
+ CAMLlocal1 (v);
+ const char *msg;
+
+ msg = guestfs_last_error (g);
+
+ if (msg)
+ v = caml_copy_string (msg);
+ else
+ v = caml_copy_string (func);
+ caml_raise_with_arg (*caml_named_value ("ocaml_guestfs_error"), v);
+ CAMLnoreturn;
+}
+
+/* Guestfs.create */