mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50661 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
cbf902fc33
commit
4d059bf9f5
3 changed files with 18 additions and 11 deletions
|
@ -526,16 +526,11 @@ static const rb_data_type_t JSON_Generator_State_type = {
|
|||
};
|
||||
#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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -112,7 +112,6 @@ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
|
|||
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)
|
|||
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
|
||||
|
||||
|
|
|
@ -344,4 +344,17 @@ EOT
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue