@@ -486,8 +486,9 @@ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
486486 return cState_partial_generate (state , string );
487487}
488488
489- static void State_free (JSON_Generator_State * state )
489+ static void State_free (void * ptr )
490490{
491+ JSON_Generator_State * state = ptr ;
491492 if (state -> indent ) ruby_xfree (state -> indent );
492493 if (state -> space ) ruby_xfree (state -> space );
493494 if (state -> space_before ) ruby_xfree (state -> space_before );
@@ -499,7 +500,31 @@ static void State_free(JSON_Generator_State *state)
499500 ruby_xfree (state );
500501}
501502
502- static JSON_Generator_State * State_allocate ()
503+ static size_t State_memsize (const void * ptr )
504+ {
505+ const JSON_Generator_State * state = ptr ;
506+ size_t size = sizeof (* state );
507+ if (state -> indent ) size += state -> indent_len + 1 ;
508+ if (state -> space ) size += state -> space_len + 1 ;
509+ if (state -> space_before ) size += state -> space_before_len + 1 ;
510+ if (state -> object_nl ) size += state -> object_nl_len + 1 ;
511+ if (state -> array_nl ) size += state -> array_nl_len + 1 ;
512+ if (state -> array_delim ) size += FBUFFER_CAPA (state -> array_delim );
513+ if (state -> object_delim ) size += FBUFFER_CAPA (state -> object_delim );
514+ if (state -> object_delim2 ) size += FBUFFER_CAPA (state -> object_delim2 );
515+ return size ;
516+ }
517+
518+ static const rb_data_type_t JSON_Generator_State_type = {
519+ "JSON/Generator/State" ,
520+ {NULL , State_free , State_memsize ,},
521+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
522+ 0 , 0 ,
523+ RUBY_TYPED_FREE_IMMEDIATELY ,
524+ #endif
525+ };
526+
527+ static JSON_Generator_State * State_allocate (void )
503528{
504529 JSON_Generator_State * state = ALLOC (JSON_Generator_State );
505530 MEMZERO (state , JSON_Generator_State , 1 );
@@ -509,7 +534,7 @@ static JSON_Generator_State *State_allocate()
509534static VALUE cState_s_allocate (VALUE klass )
510535{
511536 JSON_Generator_State * state = State_allocate ();
512- return Data_Wrap_Struct (klass , NULL , State_free , state );
537+ return TypedData_Wrap_Struct (klass , & JSON_Generator_State_type , state );
513538}
514539
515540/*
@@ -812,10 +837,10 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
812837 if (!allow_nan ) {
813838 if (isinf (value )) {
814839 fbuffer_free (buffer );
815- rb_raise (eGeneratorError , "%u: %s not allowed in JSON" , __LINE__ , StringValueCStr (tmp ));
840+ rb_raise (eGeneratorError , "%u: %" PRIsVALUE " not allowed in JSON" , __LINE__ , RB_OBJ_STRING (tmp ));
816841 } else if (isnan (value )) {
817842 fbuffer_free (buffer );
818- rb_raise (eGeneratorError , "%u: %s not allowed in JSON" , __LINE__ , StringValueCStr (tmp ));
843+ rb_raise (eGeneratorError , "%u: %" PRIsVALUE " not allowed in JSON" , __LINE__ , RB_OBJ_STRING (tmp ));
819844 }
820845 }
821846 fbuffer_append_str (buffer , tmp );
@@ -965,8 +990,9 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
965990{
966991 JSON_Generator_State * objState , * origState ;
967992
968- Data_Get_Struct (obj , JSON_Generator_State , objState );
969- Data_Get_Struct (orig , JSON_Generator_State , origState );
993+ if (obj == orig ) return obj ;
994+ GET_STATE_TO (obj , objState );
995+ GET_STATE_TO (orig , origState );
970996 if (!objState ) rb_raise (rb_eArgError , "unallocated JSON::State" );
971997
972998 MEMCPY (objState , origState , JSON_Generator_State , 1 );
@@ -1326,7 +1352,7 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
13261352/*
13271353 *
13281354 */
1329- void Init_generator ()
1355+ void Init_generator (void )
13301356{
13311357 rb_require ("json/common" );
13321358
0 commit comments