summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gc.c48
-rw-r--r--src/kernel.c2
-rw-r--r--src/string.c4
3 files changed, 51 insertions, 3 deletions
diff --git a/src/gc.c b/src/gc.c
index 15e1bd423..885368f37 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -385,6 +385,7 @@ gc_protect(mrb_state *mrb, struct RBasic *p)
mrb->arena[mrb->arena_idx++] = p;
}
+/* mrb_gc_protect() leaves the object in the arena */
MRB_API void
mrb_gc_protect(mrb_state *mrb, mrb_value obj)
{
@@ -392,6 +393,53 @@ mrb_gc_protect(mrb_state *mrb, mrb_value obj)
gc_protect(mrb, mrb_basic_ptr(obj));
}
+#define GC_ROOT_NAME "_gc_root_"
+
+/* mrb_gc_register() keeps the object from GC.
+
+ Register your object when it's exported to C world,
+ without reference from Ruby world, e.g. callback
+ arguments. Don't forget to remove the obejct using
+ mrb_gc_unregister, otherwise your object will leak.
+*/
+
+MRB_API void
+mrb_gc_register(mrb_state *mrb, mrb_value obj)
+{
+ mrb_sym root = mrb_intern_lit(mrb, GC_ROOT_NAME);
+ mrb_value table = mrb_gv_get(mrb, root);
+
+ if (mrb_nil_p(table) || mrb_type(table) != MRB_TT_ARRAY) {
+ table = mrb_ary_new(mrb);
+ mrb_gv_set(mrb, root, table);
+ }
+ mrb_ary_push(mrb, table, obj);
+}
+
+/* mrb_gc_unregister() removes the object from GC root. */
+MRB_API void
+mrb_gc_unregister(mrb_state *mrb, mrb_value obj)
+{
+ mrb_sym root = mrb_intern_lit(mrb, GC_ROOT_NAME);
+ mrb_value table = mrb_gv_get(mrb, root);
+ struct RArray *a;
+ mrb_int i, j;
+
+ if (mrb_nil_p(table)) return;
+ if (mrb_type(table) != MRB_TT_ARRAY) {
+ mrb_gv_set(mrb, root, mrb_nil_value());
+ return;
+ }
+ a = mrb_ary_ptr(table);
+ mrb_ary_modify(mrb, a);
+ for (i=j=0; i<a->len; i++) {
+ if (!mrb_obj_eq(mrb, a->ptr[i], obj)) {
+ a->ptr[j++] = a->ptr[i];
+ }
+ }
+ a->len = j;
+}
+
MRB_API struct RBasic*
mrb_obj_alloc(mrb_state *mrb, enum mrb_vtype ttype, struct RClass *cls)
{
diff --git a/src/kernel.c b/src/kernel.c
index 225f7fa54..09d869f9d 100644
--- a/src/kernel.c
+++ b/src/kernel.c
@@ -660,7 +660,7 @@ mrb_class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass* kl
if (!recur && (klass->flags & MRB_FLAG_IS_PREPENDED)) {
MRB_CLASS_ORIGIN(klass);
- prepended = 1;
+ prepended = TRUE;
}
oldklass = 0;
diff --git a/src/string.c b/src/string.c
index e93fd4606..12aefa8a0 100644
--- a/src/string.c
+++ b/src/string.c
@@ -56,7 +56,7 @@ mrb_utf8_from_locale(const char *str, size_t len)
if (len == 0)
return strdup("");
if (len == -1)
- len = strlen(str);
+ len = strlen(str);
wcssize = MultiByteToWideChar(GetACP(), 0, str, len, NULL, 0);
wcsp = (wchar_t*) malloc((wcssize + 1) * sizeof(wchar_t));
if (!wcsp)
@@ -86,7 +86,7 @@ mrb_locale_from_utf8(const char *utf8, size_t len)
if (len == 0)
return strdup("");
if (len == -1)
- len = strlen(utf8);
+ len = strlen(utf8);
wcssize = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
wcsp = (wchar_t*) malloc((wcssize + 1) * sizeof(wchar_t));
if (!wcsp)