[前][次][番号順一覧][スレッド一覧]

ruby-changes:38580

From: nobu <ko1@a...>
Date: Thu, 28 May 2015 16:18:11 +0900 (JST)
Subject: [ruby-changes:38580] nobu:r50661 (trunk): generator.c: allocate structs with wrapper

nobu	2015-05-28 16:17:55 +0900 (Thu, 28 May 2015)

  New Revision: 50661

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50661

  Log:
    generator.c: allocate structs with wrapper
    
    * ext/json/generator/generator.c (cState_s_allocate): allocate
      structs with making new wrapper objects and get rid of potential
      memory leak.

  Modified files:
    trunk/ext/json/generator/generator.c
    trunk/ext/json/generator/generator.h
    trunk/test/json/test_json_generate.rb
Index: ext/json/generator/generator.c
===================================================================
--- ext/json/generator/generator.c	(revision 50660)
+++ ext/json/generator/generator.c	(revision 50661)
@@ -526,16 +526,11 @@ static const rb_data_type_t JSON_Generat https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L526
 };
 #endif
 
-static JSON_Generator_State *State_allocate(void)
-{
-    JSON_Generator_State *state = ZALLOC(JSON_Generator_State);
-    return state;
-}
-
 static VALUE cState_s_allocate(VALUE klass)
 {
-    JSON_Generator_State *state = State_allocate();
-    return TypedData_Wrap_Struct(klass, &JSON_Generator_State_type, state);
+    JSON_Generator_State *state;
+    return TypedData_Make_Struct(klass, JSON_Generator_State,
+				 &JSON_Generator_State_type, state);
 }
 
 /*
Index: ext/json/generator/generator.h
===================================================================
--- ext/json/generator/generator.h	(revision 50660)
+++ ext/json/generator/generator.h	(revision 50661)
@@ -112,7 +112,6 @@ static VALUE mFalseClass_to_json(int arg https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.h#L112
 static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
 static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
 static void State_free(void *state);
-static JSON_Generator_State *State_allocate(void);
 static VALUE cState_s_allocate(VALUE klass);
 static VALUE cState_configure(VALUE self, VALUE opts);
 static VALUE cState_to_h(VALUE self);
@@ -156,11 +155,11 @@ static inline void *ruby_zalloc(size_t n https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.h#L155
     return p;
 }
 #endif
-#ifdef TypedData_Wrap_Struct
+#ifdef TypedData_Make_Struct
 static const rb_data_type_t JSON_Generator_State_type;
 #define NEW_TYPEDDATA_WRAPPER 1
 #else
-#define TypedData_Wrap_Struct(klass, ignore, json) Data_Wrap_Struct(klass, NULL, State_free, json)
+#define TypedData_Make_Struct(klass, type, ignore, json) Data_Make_Struct(klass, type, NULL, State_free, json)
 #define TypedData_Get_Struct(self, JSON_Generator_State, ignore, json) Data_Get_Struct(self, JSON_Generator_State, json)
 #endif
 
Index: test/json/test_json_generate.rb
===================================================================
--- test/json/test_json_generate.rb	(revision 50660)
+++ test/json/test_json_generate.rb	(revision 50661)
@@ -344,4 +344,17 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/json/test_json_generate.rb#L344
       assert_equal '[""]', JSON.generate([s.new])
     end
   end
+
+  if EnvUtil.gc_stress_to_class?
+    def assert_no_memory_leak(code, *rest, **opt)
+      code = "8.times {20_000.times {begin #{code}; rescue NoMemoryError; end}; GC.start}"
+      super(["-rjson/ext/generator"],
+            "GC.add_stress_to_class(JSON::Ext::Generator::State); "\
+            "#{code}", code, *rest, rss: true, limit: 1.1, **opt)
+    end
+
+    def test_no_memory_leak_allocate
+      assert_no_memory_leak("JSON::Ext::Generator::State.allocate")
+    end
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]