[PATCH] Use Glib's infrastructure for attaching magic



Instead of doing it all manually and getting things wrong by allowing only one
PERL_MAGIC_ext at a time or de-referencing the attached structures as something
they aren't, just use the tools Glib provides.
---
 xs/GdkPixbuf.xs       |    6 +++---
 xs/GtkCellLayout.xs   |    6 +++---
 xs/GtkTreeSortable.xs |    6 +++---
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/xs/GdkPixbuf.xs b/xs/GdkPixbuf.xs
index 43116d4..3cf1f95 100644
--- a/xs/GdkPixbuf.xs
+++ b/xs/GdkPixbuf.xs
@@ -82,7 +82,7 @@ newSVGdkPixbufFormat (GdkPixbufFormat * format)
 
        /* Store the original format pointer in the hash so that
           SvGdkPixbufFormat can retrieve and return it. */
-       sv_magic ((SV*) hv, 0, PERL_MAGIC_ext, (const char *)format, 0);
+       gperl_attach_mg ((SV*) hv, format);
 
        stash = gv_stashpv ("Gtk2::Gdk::PixbufFormat", TRUE);
        return sv_bless ((SV*) newRV_noinc ((SV*) hv), stash);
@@ -93,7 +93,7 @@ SvGdkPixbufFormat (SV * sv)
 {
        MAGIC *mg;
 
-       if (!gperl_sv_is_defined (sv) || !SvROK (sv) || !(mg = mg_find (SvRV (sv), PERL_MAGIC_ext)))
+       if (!(mg = gperl_find_mg (SvRV (sv))))
                return NULL;
 
        return (GdkPixbufFormat *) mg->mg_ptr;
@@ -1025,7 +1025,7 @@ void
 DESTROY (sv)
        SV *sv
     CODE:
-       sv_unmagic (sv, PERL_MAGIC_ext);
+       gperl_remove_mg (SvRV (sv));
 
 #endif /* 2.2.0 */
 
diff --git a/xs/GtkCellLayout.xs b/xs/GtkCellLayout.xs
index 8dbae40..e15de1f 100644
--- a/xs/GtkCellLayout.xs
+++ b/xs/GtkCellLayout.xs
@@ -160,7 +160,7 @@ create_callback (GtkCellLayoutDataFunc func,
        wrapper->destroy = destroy;
 
        data_sv = newSViv (PTR2IV (wrapper));
-       sv_magic ((SV *) dummy_cv, 0, PERL_MAGIC_ext, (const char *) data_sv, 0);
+       gperl_attach_mg ((SV *) dummy_cv, data_sv);
 
        *code_return = code_sv;
        *data_return = data_sv;
@@ -506,13 +506,13 @@ DESTROY (SV *code)
        MAGIC *mg;
        Gtk2PerlCellLayoutDataFunc *wrapper;
     CODE:
-       if (!gperl_sv_is_defined (code) || !SvROK (code) || !(mg = mg_find (SvRV (code), PERL_MAGIC_ext)))
+       if (!(mg = gperl_find_mg (SvRV (code))))
                return;
 
        wrapper = INT2PTR (Gtk2PerlCellLayoutDataFunc*, SvIV ((SV *) mg->mg_ptr));
        if (wrapper && wrapper->destroy)
                wrapper->destroy (wrapper->data);
 
-       sv_unmagic (SvRV (code), PERL_MAGIC_ext);
+       gperl_remove_mg (SvRV (code));
        if (wrapper)
                g_free (wrapper);
diff --git a/xs/GtkTreeSortable.xs b/xs/GtkTreeSortable.xs
index 1fd9520..082569c 100644
--- a/xs/GtkTreeSortable.xs
+++ b/xs/GtkTreeSortable.xs
@@ -144,7 +144,7 @@ create_callback (GtkTreeIterCompareFunc func,
        stuff->destroy = destroy;
 
        my_data = newSViv (PTR2IV (stuff));
-       sv_magic ((SV *) dummy, 0, PERL_MAGIC_ext, (const char *) my_data, 0);
+       gperl_attach_mg ((SV *) dummy, my_data);
 
        *code_return = code;
        *data_return = my_data;
@@ -449,13 +449,13 @@ DESTROY (code)
        MAGIC *mg;
        Gtk2PerlTreeIterCompareFunc *stuff;
     CODE:
-       if (!gperl_sv_is_defined (code) || !SvROK (code) || !(mg = mg_find (SvRV (code), PERL_MAGIC_ext)))
+       if (!(mg = gperl_find_mg (SvRV (code))))
                return;
 
        stuff = INT2PTR (Gtk2PerlTreeIterCompareFunc*, SvIV ((SV *) mg->mg_ptr));
        if (stuff && stuff->destroy)
                stuff->destroy (stuff->data);
 
-       sv_unmagic (SvRV (code), PERL_MAGIC_ext);
+       gperl_remove_mg (SvRV (code));
        if (stuff)
                g_free (stuff);
-- 
1.7.2.3




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]