ruby-changes:35718
From: nobu <ko1@a...>
Date: Sun, 5 Oct 2014 08:33:43 +0900 (JST)
Subject: [ruby-changes:35718] nobu:r47797 (trunk): json/generator: typed data
nobu 2014-10-05 08:33:09 +0900 (Sun, 05 Oct 2014) New Revision: 47797 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47797 Log: json/generator: typed data * ext/json/generator/generator.c (JSON_Generator_State_type): turn into typed data. Modified files: trunk/ext/json/generator/generator.c trunk/ext/json/generator/generator.h Index: ext/json/generator/generator.c =================================================================== --- ext/json/generator/generator.c (revision 47796) +++ ext/json/generator/generator.c (revision 47797) @@ -486,8 +486,9 @@ static VALUE mObject_to_json(int argc, V https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L486 return cState_partial_generate(state, string); } -static void State_free(JSON_Generator_State *state) +static void State_free(void *ptr) { + JSON_Generator_State *state = ptr; if (state->indent) ruby_xfree(state->indent); if (state->space) ruby_xfree(state->space); if (state->space_before) ruby_xfree(state->space_before); @@ -499,17 +500,38 @@ static void State_free(JSON_Generator_St https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L500 ruby_xfree(state); } +static size_t State_memsize(const void *ptr) +{ + const JSON_Generator_State *state = ptr; + size_t size = sizeof(*state); + if (state->indent) size += state->indent_len + 1; + if (state->space) size += state->space_len + 1; + if (state->space_before) size += state->space_before_len + 1; + if (state->object_nl) size += state->object_nl_len + 1; + if (state->array_nl) size += state->array_nl_len + 1; + if (state->array_delim) size += FBUFFER_CAPA(state->array_delim); + if (state->object_delim) size += FBUFFER_CAPA(state->object_delim); + if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2); + return size; +} + +static const rb_data_type_t JSON_Generator_State_type = { + "JSON/Generator/State", + {NULL, State_free, State_memsize,}, + NULL, NULL, + RUBY_TYPED_FREE_IMMEDIATELY, +}; + static JSON_Generator_State *State_allocate(void) { - JSON_Generator_State *state = ALLOC(JSON_Generator_State); - MEMZERO(state, JSON_Generator_State, 1); + JSON_Generator_State *state = ZALLOC(JSON_Generator_State); return state; } static VALUE cState_s_allocate(VALUE klass) { JSON_Generator_State *state = State_allocate(); - return Data_Wrap_Struct(klass, NULL, State_free, state); + return TypedData_Wrap_Struct(klass, &JSON_Generator_State_type, state); } /* @@ -966,8 +988,8 @@ static VALUE cState_init_copy(VALUE obj, https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L988 JSON_Generator_State *objState, *origState; if (obj == orig) return obj; - Data_Get_Struct(obj, JSON_Generator_State, objState); - Data_Get_Struct(orig, JSON_Generator_State, origState); + GET_STATE_TO(obj, objState); + GET_STATE_TO(orig, origState); if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State"); MEMCPY(objState, origState, JSON_Generator_State, 1); Index: ext/json/generator/generator.h =================================================================== --- ext/json/generator/generator.h (revision 47796) +++ ext/json/generator/generator.h (revision 47797) @@ -78,9 +78,12 @@ typedef struct JSON_Generator_StateStruc https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.h#L78 long buffer_initial_length; } JSON_Generator_State; +#define GET_STATE_TO(self, state) \ + TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state) + #define GET_STATE(self) \ JSON_Generator_State *state; \ - Data_Get_Struct(self, JSON_Generator_State, state) + GET_STATE_TO(self, state) #define GENERATE_JSON(type) \ FBuffer *buffer; \ @@ -89,7 +92,7 @@ typedef struct JSON_Generator_StateStruc https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.h#L92 \ rb_scan_args(argc, argv, "01", &Vstate); \ Vstate = cState_from_state_s(cState, Vstate); \ - Data_Get_Struct(Vstate, JSON_Generator_State, state); \ + TypedData_Get_Struct(Vstate, JSON_Generator_State, &JSON_Generator_State_type, state); \ buffer = cState_prepare_buffer(Vstate); \ generate_json_##type(buffer, Vstate, state, self); \ return fbuffer_to_s(buffer) @@ -108,7 +111,7 @@ static VALUE mTrueClass_to_json(int argc https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.h#L111 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(JSON_Generator_State *state); +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); @@ -144,5 +147,6 @@ static VALUE cState_ascii_only_p(VALUE s https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.h#L147 static VALUE cState_depth(VALUE self); static VALUE cState_depth_set(VALUE self, VALUE depth); static FBuffer *cState_prepare_buffer(VALUE self); +static const rb_data_type_t JSON_Generator_State_type; #endif -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/