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
26 #include <caml/config.h>
27 #include <caml/alloc.h>
28 #include <caml/callback.h>
29 #include <caml/custom.h>
30 #include <caml/fail.h>
31 #include <caml/memory.h>
32 #include <caml/mlvalues.h>
34 #include "guestfs_c.h"
36 /* This macro was added in OCaml 3.10. Backport for earlier versions. */
38 #define CAMLreturnT(type, result) do{ \
39 type caml__temp_result = (result); \
40 caml_local_roots = caml__frame; \
41 return (caml__temp_result); \
45 /* These prototypes are solely to quiet gcc warning. */
46 CAMLprim value ocaml_guestfs_create (void);
47 CAMLprim value ocaml_guestfs_close (value gv);
49 /* Allocate handles and deal with finalization. */
51 guestfs_finalize (value gv)
53 guestfs_h *g = Guestfs_val (gv);
54 if (g) guestfs_close (g);
57 static struct custom_operations guestfs_custom_operations = {
58 (char *) "guestfs_custom_operations",
60 custom_compare_default,
62 custom_serialize_default,
63 custom_deserialize_default
67 Val_guestfs (guestfs_h *g)
72 rv = caml_alloc_custom (&guestfs_custom_operations,
73 sizeof (guestfs_h *), 0, 1);
80 ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
86 msg = guestfs_last_error (g);
89 v = caml_copy_string (msg);
91 v = caml_copy_string (func);
92 caml_raise_with_arg (*caml_named_value ("ocaml_guestfs_error"), v);
97 ocaml_guestfs_raise_closed (const char *func)
102 v = caml_copy_string (func);
103 caml_raise_with_arg (*caml_named_value ("ocaml_guestfs_closed"), v);
109 ocaml_guestfs_create (void)
115 g = guestfs_create ();
117 caml_failwith ("failed to create guestfs handle");
119 guestfs_set_error_handler (g, NULL, NULL);
121 gv = Val_guestfs (g);
127 ocaml_guestfs_close (value gv)
131 guestfs_finalize (gv);
133 /* So we don't double-free in the finalizer. */
134 Guestfs_val (gv) = NULL;
136 CAMLreturn (Val_unit);
139 /* Copy string array value.
140 * The return value is only 'safe' provided we don't allocate anything
141 * further on the OCaml heap (ie. cannot trigger the OCaml GC) because
142 * that could move the strings around.
145 ocaml_guestfs_strings_val (guestfs_h *g, value sv)
151 r = guestfs_safe_malloc (g, sizeof (char *) * (Wosize_val (sv) + 1));
152 for (i = 0; i < Wosize_val (sv); ++i)
153 r[i] = String_val (Field (sv, i));
156 CAMLreturnT (char **, r);
159 /* Free array of strings. */
161 ocaml_guestfs_free_strings (char **argv)
163 /* Don't free the actual strings - they are String_vals on