ext/cairo/rb_cairo_context.c in cairo-1.17.6 vs ext/cairo/rb_cairo_context.c in cairo-1.17.7

- old
+ new

@@ -3,11 +3,11 @@ * Ruby Cairo Binding * * $Author: kou $ * $Date: 2008-09-26 13:52:08 $ * - * Copyright 2005-2019 Kouhei Sutou <kou@cozmixng.org> + * Copyright 2005-2022 Sutou Kouhei <kou@cozmixng.org> * Copyright 2005 Øyvind Kolås <pippin@freedesktop.org> * Copyright 2004-2005 MenTaLguY <mental@rydia.com> * * This file is made available under the same terms as Ruby * @@ -38,20 +38,37 @@ cr_check_status (cairo_t *context) { rb_cairo_check_status (cairo_status (context)); } +static void +cr_context_free (void *ptr) +{ + cairo_destroy ((cairo_t *) ptr); +} + +static const rb_data_type_t cr_context_type = { + "Cairo::Context", + { + NULL, + cr_context_free, + }, + NULL, + NULL, + RUBY_TYPED_FREE_IMMEDIATELY, +}; + /* Functions for manipulating state objects */ cairo_t * rb_cairo_context_from_ruby_object (VALUE obj) { cairo_t *context; if (!rb_cairo__is_kind_of (obj, rb_cCairo_Context)) { rb_raise (rb_eTypeError, "not a cairo graphics context"); } - Data_Get_Struct (obj, cairo_t, context); + TypedData_Get_Struct (obj, cairo_t, &cr_context_type, context); if (!context) rb_cairo_check_status (CAIRO_STATUS_NULL_POINTER); return context; } @@ -65,37 +82,28 @@ cr_object_holder_free (void *ptr) { rb_cairo__object_holder_free (rb_cCairo_Context, ptr); } -static void -cr_context_free (void *ptr) -{ - if (ptr) - { - cairo_destroy ((cairo_t *) ptr); - } -} - VALUE rb_cairo_context_to_ruby_object (cairo_t *cr) { if (cr) { cairo_reference (cr); - return Data_Wrap_Struct (rb_cCairo_Context, NULL, cr_context_free, cr); + return TypedData_Wrap_Struct (rb_cCairo_Context, &cr_context_type, cr); } else { return Qnil; } } static VALUE cr_allocate (VALUE klass) { - return Data_Wrap_Struct (klass, NULL, cr_context_free, NULL); + return TypedData_Wrap_Struct (klass, &cr_context_type, NULL); } static void cr_set_user_data (cairo_t *cr, const cairo_user_data_key_t *key, void *user_data, cairo_destroy_func_t destroy) @@ -1593,23 +1601,29 @@ /* Paths */ static VALUE cr_copy_path (VALUE self) { cairo_path_t *path; + VALUE rb_path; path = cairo_copy_path (_SELF); rb_cairo_check_status (path->status); - return CRPATH2RVAL (path); + rb_path = CRPATH2RVAL (path); + cairo_path_destroy (path); + return rb_path; } static VALUE cr_copy_path_flat (VALUE self) { cairo_path_t *path; + VALUE rb_path; - path = cairo_copy_path_flat (_SELF); + path = cairo_copy_path_flat (_SELF); rb_cairo_check_status (path->status); - return CRPATH2RVAL (path); + rb_path = CRPATH2RVAL (path); + cairo_path_destroy (path); + return rb_path; } static VALUE cr_copy_append_path (VALUE self, VALUE path) {