Skip to content

Commit

Permalink
glib2: add rbg_gc_guard() and rbg_gc_unguard()
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed May 22, 2018
1 parent 2e1ce20 commit f113870
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions glib2/ext/glib2/glib2.def
Expand Up @@ -168,6 +168,9 @@ EXPORTS
rbg_variant_from_ruby
rbg_variant_type_from_ruby

rbg_gc_guard
rbg_gc_unguard

g_source_get_type
g_connect_flags_get_type
g_poll_fd_get_type
Expand Down
82 changes: 82 additions & 0 deletions glib2/ext/glib2/rbglib-gc.c
@@ -0,0 +1,82 @@
/* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
/*
* Copyright (C) 2018 Ruby-GNOME2 Project Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/

#include "rbgprivate.h"

static GHashTable *rbg_objects = NULL;

static void
gc_marker_mark_each(gpointer key, gpointer value, gpointer user_data)
{
VALUE rb_object = (VALUE)value;
rb_gc_mark(rb_object);
}

static void
gc_marker_mark(void *data)
{
GHashTable *hash_table = data;
g_hash_table_foreach(hash_table, gc_marker_mark_each, NULL);
}

static void
gc_marker_free(void *data)
{
GHashTable *hash_table = data;
g_hash_table_unref(hash_table);
}

static const rb_data_type_t rbg_gc_marker_type = {
"GLib::GCMarker",
{
gc_marker_mark,
gc_marker_free,
NULL,
},
NULL,
NULL,
RUBY_TYPED_FREE_IMMEDIATELY,
};

void
rbg_gc_guard(gpointer key, VALUE rb_object)
{
g_hash_table_insert(rbg_objects,
key,
(gpointer)rb_object);
}

void
rbg_gc_unguard(gpointer key)
{
g_hash_table_remove(rbg_objects, key);
}

void
Init_glib_gc(void)
{
VALUE gc_marker;

rbg_objects = g_hash_table_new(g_direct_hash, g_direct_equal);
gc_marker = TypedData_Wrap_Struct(rb_cData,
&rbg_gc_marker_type,
rbg_objects);
rb_ivar_set(mGLib, rb_intern("gc_marker"), gc_marker);
}
3 changes: 3 additions & 0 deletions glib2/ext/glib2/rbglib.c
Expand Up @@ -1146,6 +1146,9 @@ union GDoubleIEEE754;
rb_define_const(RG_TARGET_NAMESPACE, "PRIORITY_LOW", INT2FIX(G_PRIORITY_LOW));

/* Init_mem(); */

Init_glib_gc();

Init_gutil();
Init_gutil_callback();

Expand Down
3 changes: 3 additions & 0 deletions glib2/ext/glib2/rbglib.h
Expand Up @@ -235,6 +235,9 @@ extern GVariant *rbg_variant_from_ruby(VALUE rb_variant);
extern GVariantType *rbg_variant_type_from_ruby(VALUE rb_variant_type);


extern void rbg_gc_guard(gpointer key, VALUE rb_object);
extern void rbg_gc_unguard(gpointer key);

#ifdef __cplusplus
}
#endif /* __cplusplus */
Expand Down
2 changes: 2 additions & 0 deletions glib2/ext/glib2/rbgprivate.h
Expand Up @@ -123,6 +123,8 @@ G_GNUC_INTERNAL void rg_enum_add_constants(VALUE mod, GType enum_type, const gch
G_GNUC_INTERNAL void rg_flags_add_constants(VALUE mod, GType flags_type, const gchar *strip_prefix);
G_GNUC_INTERNAL char *rg_obj_constant_lookup(const char *name);

G_GNUC_INTERNAL void Init_glib_gc(void);

G_GNUC_INTERNAL void Init_gutil(void);
G_GNUC_INTERNAL void Init_gutil_callback(void);
G_GNUC_INTERNAL void Init_glib_gettext(void);
Expand Down

0 comments on commit f113870

Please sign in to comment.