diff options
author | Richard Jones <rjones@redhat.com> | 2010-06-10 15:25:43 +0100 |
---|---|---|
committer | Richard Jones <rjones@redhat.com> | 2010-06-10 17:52:54 +0100 |
commit | eb566f7dc7974b42ac65729a2e5e5bcee329a0a9 (patch) | |
tree | d28e272f278a6f3cc0e652ed09a88f2b84566a8d /src/generator.ml | |
parent | dbfd93b72f99ebdded394541a48177c415db8cbf (diff) | |
download | libguestfs-eb566f7dc7974b42ac65729a2e5e5bcee329a0a9.tar.gz libguestfs-eb566f7dc7974b42ac65729a2e5e5bcee329a0a9.tar.xz libguestfs-eb566f7dc7974b42ac65729a2e5e5bcee329a0a9.zip |
perl: Add explicit close() method (RHBZ#602592).
This add an optional explicit $g->close method which may be
used to force the handle to be closed immediately. Note the
provisos about this method in the manual page entry. Callers
should *not* normally use this method.
The implementation of the handle also changes. Before, the
handle was a blessed reference to an integer (the integer
being the pointer to the C guestfs_h handle). Now we change
this to a hashref containing currently the following field:
_g => pointer to C guestfs_h handle (as an integer)
If this field is not present, it means that the handle has been
explicitly closed. This avoids double-freeing the handle.
The user may add their own fields to this hash in order to store
per-handle data. However any fields whose names begin with
an underscore are reserved for use by the Perl bindings.
This commit also adds a regression test.
This commit also changes the existing warning when you call
a method without a Sys::Guestfs handle as the first parameter,
into an error. This is because such cases are always errors.
Diffstat (limited to 'src/generator.ml')
-rwxr-xr-x | src/generator.ml | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/src/generator.ml b/src/generator.ml index cf28978d..571870da 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -8821,10 +8821,30 @@ _create () RETVAL void -DESTROY (g) +DESTROY (sv) + SV *sv; + PPCODE: + /* For the 'g' argument above we do the conversion explicitly and + * don't rely on the typemap, because if the handle has been + * explicitly closed we don't want the typemap conversion to + * display an error. + */ + HV *hv = (HV *) SvRV (sv); + SV **svp = hv_fetch (hv, \"_g\", 2, 0); + if (svp != NULL) { + guestfs_h *g = (guestfs_h *) SvIV (*svp); + assert (g != NULL); + guestfs_close (g); + } + +void +close (g) guestfs_h *g; PPCODE: guestfs_close (g); + /* Avoid double-free in DESTROY method. */ + HV *hv = (HV *) SvRV (ST(0)); + (void) hv_delete (hv, \"_g\", 2, G_DISCARD); "; @@ -9179,11 +9199,28 @@ sub new { my $proto = shift; my $class = ref ($proto) || $proto; - my $self = Sys::Guestfs::_create (); + my $g = Sys::Guestfs::_create (); + my $self = { _g => $g }; bless $self, $class; return $self; } +=item $h->close (); + +Explicitly close the guestfs handle. + +B<Note:> You should not usually call this function. The handle will +be closed implicitly when its reference count goes to zero (eg. +when it goes out of scope or the program ends). This call is +only required in some exceptional cases, such as where the program +may contain cached references to the handle 'somewhere' and you +really have to have the close happen right away. After calling +C<close> the program must not call any method (including C<close>) +on the handle (but the implicit call to C<DESTROY> that happens +when the final reference is cleaned up is OK). + +=cut + " max_proc_nr; (* Actions. We only need to print documentation for these as |