2 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
21 #ifndef AMANDA_AMGLUE_H
22 #define AMANDA_AMGLUE_H
24 #include "../config/config.h"
29 #include <glib-object.h>
31 /* These defines are missing from older glibs, so we add them here */
33 #define G_MAXINT8 (127)
37 #define G_MININT8 (-127-1)
41 #define G_MAXUINT8 (255)
45 #define G_MAXINT16 (32767)
49 #define G_MININT16 (-32767-1)
53 #define G_MAXUINT16 (65535)
57 #define G_MAXINT32 (2147483647)
61 #define G_MININT32 (-2147483647-1)
65 #define G_MAXUINT32 (4294967295U)
69 * prototypes for ghashtable.c
72 /* Turn a GLib hash table (mapping strings to strings) into a reference
73 * to a Perl hash table.
75 * @param hash: GLib hash table
76 * @returns: Perl hashref
78 SV *g_hash_table_to_hashref(GHashTable *hash);
80 /* Turn a GLib hash table (mapping strings to GSList of strings) into a reference
81 * to a Perl hash table.
83 * @param hash: GLib hash table
84 * @returns: Perl hashref
86 SV *g_hash_table_to_hashref_gslist(GHashTable *hash);
88 /* Turn a GLib hash table (mapping strings to property_t) into a reference
89 * to a Perl hash table.
91 * @param hash: GLib hash table
92 * @returns: Perl hashref
94 SV *g_hash_table_to_hashref_property(GHashTable *hash);
97 * prototypes for gerror.c
100 /* Call perl's croak (die) for a GError (if there is one)
102 * @note This is not thread-safe
103 * @note This function does not return if error is non-NULL
105 * @param domain: String to prefix to error message (followed by ": ")
106 * @param error: The GError pointer
108 void croak_gerror(const char *domain, GError **error);
111 * prototypes for bigint.c
115 * These functions handle conversion of integers to and from Perl-compatible
116 * values. Most perls do not natively support 64-bit integers, so these functions
117 * interface with the Math::BigInt module to support those integers. The functions
118 * also handle conversions from floating-point to integer values, with silent fraction
119 * truncation, as perl automatically promotes integers to doubles on overflow.
122 /* Convert an (unsigned) integer to a Perl SV. These will always produce a
123 * Math::BigInt object. Any failure is fatal. *All* C-to-Perl integer conversions
124 * must use these functions.
128 * Due to the way SWIG constructs return values, *any* outgoing typemap (out or
129 * argout) must use the following syntax:
130 * SP += argvi; PUTBACK;
131 * $result = sv_2mortal(amglue_newSVi64(...));
132 * SPAGAIN; SP -= argvi; argvi++;
133 * This has the effect of saving the arguments added to the perl stack so far, by
134 * setting the global perl stack to a point above them.
136 * @param v: value to convert
137 * @returns: pointer to a new SV (refcount=1)
139 SV *amglue_newSVi64(gint64 v);
140 SV *amglue_newSVu64(guint64 v);
142 /* Convert a Perl SV to an integer of the specified size. These functions should
143 * be used for *all* Perl-to-C integer conversions, since the Perl value may be a
144 * Math::BigInt object. All of these functions will call croak() on an overflow
145 * condition, rather than silently truncate.
147 * @param sv: perl value to convert
148 * @returns: value of the given type
150 gint64 amglue_SvI64(SV *sv);
151 guint64 amglue_SvU64(SV *sv);
152 gint32 amglue_SvI32(SV *sv);
153 guint32 amglue_SvU32(SV *sv);
154 gint16 amglue_SvI16(SV *sv);
155 guint16 amglue_SvU16(SV *sv);
156 gint8 amglue_SvI8(SV *sv);
157 guint8 amglue_SvU8(SV *sv);
160 * prototypes for objwrap.c
163 /* Return a new SV with refcount 1 representing the given C object
164 * with the given class.
166 * @param c_obj: the object to represent
167 * @param perl_class: the perl with which to bless and tie the SV
169 SV * new_sv_for_c_obj(gpointer c_obj, const char *perl_class);
171 /* Return the C object buried in an SV, asserting that the perl SV is
172 * derived from derived_from. Returns NULL for undefined perl values.
174 * This function is based on SWIG's SWIG_Perl_ConvertPtr. The INT2PTR
175 * situation certainly looks strange, but is documented in perlxs.
177 * @param sv: the SV to convert
178 * @param derived_from: perl class from which the SV should be derived
179 * @return: underlying pointer
181 gpointer c_obj_from_sv(SV *sv, const char *derived_from);
184 * prototypes for xferwrap.c
187 /* declare structs */
191 /* Return a new SV representing a transfer.
193 * @param xfer: the transfer to represent
195 SV *new_sv_for_xfer(struct Xfer *xfer);
197 /* Return a new SV representing a transfer element.
199 * @param xe: the transfer element to represent
201 SV *new_sv_for_xfer_element(struct XferElement *xe);
203 /* Convert an SV to an Xfer. The Xfer's reference count is not
204 * incremented -- this is a "borrowed" reference.
206 * @param sv: the perl value
207 * @returns: pointer to the corresponding transfer, or NULL
209 struct Xfer *xfer_from_sv(SV *sv);
211 /* Convert an SV to an XferElement. The element's reference count is
212 * not incremented -- this is a "borrowed" reference.
214 * @param sv: the perl value
215 * @returns: pointer to the corresponding transfer element, or NULL.
217 struct XferElement *xfer_element_from_sv(SV *sv);
220 * prototypes for source.c
223 typedef enum amglue_Source_state {
225 AMGLUE_SOURCE_ATTACHED,
226 AMGLUE_SOURCE_DESTROYED
227 } amglue_Source_state;
229 /* There is *one* amglue_Source object for each GSource; this
230 * allows us to attach amglue-related information to the
231 * GSource. See amglue/source.c for more detail. */
233 typedef struct amglue_Source {
235 GSourceFunc callback;
237 amglue_Source_state state;
241 /* Get the amglue_Source object associated with this GSource, creating a
242 * new one if necessary, and increment its refcount.
244 * The 'callback' parameter should be a C function with the
245 * appropriate signature for this GSource. The callback will
246 * be given the amglue_Source as its 'data' argument, and should
247 * invoke its callback_sv as a Perl sub with the appropriate
248 * parameters. Simple GSources can use amglue_source_callback_simple,
251 * This amglue_Source object can be returned directly to perl via a
252 * SWIG binding; it will be bound as an Amanda::MainLoop::Source
253 * object, and its memory management will be handled correctly.
255 * @param gsrc: the GSource object to wrap
256 * @param callback: function to trigger a perl callback
257 * @returns: an amglue_Source with appropriate refcount
259 amglue_Source *amglue_source_get(GSource *gsrc, GSourceFunc callback);
261 /* Create a new amglue_Source object for this GSource. Use this when
262 * the GSource was just created and does not yet have a corresponding
265 * @param gsrc: the GSource object to wrap
266 * @param callback: function to trigger a perl callback
267 * @returns: an amglue_Source with appropriate refcount
269 amglue_Source *amglue_source_new(GSource *gsrc, GSourceFunc callback);
271 /* Increment the refcount on an amglue_Source */
272 #define amglue_source_ref(aS) aS->refcount++
274 /* Unref an amglue_Source object, freeing it if its refcount reaches
276 #define amglue_source_unref(aS) if (!--(aS)->refcount) amglue_source_free((aS))
277 void amglue_source_free(amglue_Source *);
279 #endif /* AMANDA_AMGLUE_H */