2 * Copyright (C) 2009 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <caml/config.h>
26 #include <caml/alloc.h>
27 #include <caml/callback.h>
28 #include <caml/custom.h>
29 #include <caml/fail.h>
30 #include <caml/memory.h>
31 #include <caml/mlvalues.h>
33 #include "guestfs_c.h"
35 /* Allocate handles and deal with finalization. */
37 guestfs_finalize (value gv)
39 guestfs_h *g = Guestfs_val (gv);
40 if (g) guestfs_close (g);
43 static struct custom_operations guestfs_custom_operations = {
44 "guestfs_custom_operations",
46 custom_compare_default,
48 custom_serialize_default,
49 custom_deserialize_default
53 Val_guestfs (guestfs_h *g)
58 rv = caml_alloc_custom (&guestfs_custom_operations,
59 sizeof (guestfs_h *), 0, 1);
66 /* XXX Like the current Perl bindings, this is unsafe in a multi-
67 * threaded environment.
69 static char *last_error = NULL;
72 error_handler (guestfs_h *g,
76 if (last_error != NULL) free (last_error);
77 last_error = strdup (msg);
81 ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
86 v = caml_copy_string (last_error);
87 caml_raise_with_arg (*caml_named_value ("ocaml_guestfs_error"), v);
93 ocaml_guestfs_create (void)
99 g = guestfs_create ();
101 caml_failwith ("failed to create guestfs handle");
103 guestfs_set_error_handler (g, error_handler, NULL);
105 gv = Val_guestfs (g);
111 ocaml_guestfs_close (value gv)
115 guestfs_finalize (gv);
117 /* So we don't double-free in the finalizer. */
118 Guestfs_val (gv) = NULL;
120 CAMLreturn (Val_unit);