mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
json/generator: typed data
* ext/json/generator/generator.c (JSON_Generator_State_type): turn into typed data. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47797 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ab7695fc50
commit
bdfc2e2942
2 changed files with 35 additions and 9 deletions
|
@ -486,8 +486,9 @@ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
|
||||||
return cState_partial_generate(state, string);
|
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->indent) ruby_xfree(state->indent);
|
||||||
if (state->space) ruby_xfree(state->space);
|
if (state->space) ruby_xfree(state->space);
|
||||||
if (state->space_before) ruby_xfree(state->space_before);
|
if (state->space_before) ruby_xfree(state->space_before);
|
||||||
|
@ -499,17 +500,38 @@ static void State_free(JSON_Generator_State *state)
|
||||||
ruby_xfree(state);
|
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)
|
static JSON_Generator_State *State_allocate(void)
|
||||||
{
|
{
|
||||||
JSON_Generator_State *state = ALLOC(JSON_Generator_State);
|
JSON_Generator_State *state = ZALLOC(JSON_Generator_State);
|
||||||
MEMZERO(state, JSON_Generator_State, 1);
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE cState_s_allocate(VALUE klass)
|
static VALUE cState_s_allocate(VALUE klass)
|
||||||
{
|
{
|
||||||
JSON_Generator_State *state = State_allocate();
|
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, VALUE orig)
|
||||||
JSON_Generator_State *objState, *origState;
|
JSON_Generator_State *objState, *origState;
|
||||||
|
|
||||||
if (obj == orig) return obj;
|
if (obj == orig) return obj;
|
||||||
Data_Get_Struct(obj, JSON_Generator_State, objState);
|
GET_STATE_TO(obj, objState);
|
||||||
Data_Get_Struct(orig, JSON_Generator_State, origState);
|
GET_STATE_TO(orig, origState);
|
||||||
if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
|
if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
|
||||||
|
|
||||||
MEMCPY(objState, origState, JSON_Generator_State, 1);
|
MEMCPY(objState, origState, JSON_Generator_State, 1);
|
||||||
|
|
|
@ -78,9 +78,12 @@ typedef struct JSON_Generator_StateStruct {
|
||||||
long buffer_initial_length;
|
long buffer_initial_length;
|
||||||
} JSON_Generator_State;
|
} 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) \
|
#define GET_STATE(self) \
|
||||||
JSON_Generator_State *state; \
|
JSON_Generator_State *state; \
|
||||||
Data_Get_Struct(self, JSON_Generator_State, state)
|
GET_STATE_TO(self, state)
|
||||||
|
|
||||||
#define GENERATE_JSON(type) \
|
#define GENERATE_JSON(type) \
|
||||||
FBuffer *buffer; \
|
FBuffer *buffer; \
|
||||||
|
@ -89,7 +92,7 @@ typedef struct JSON_Generator_StateStruct {
|
||||||
\
|
\
|
||||||
rb_scan_args(argc, argv, "01", &Vstate); \
|
rb_scan_args(argc, argv, "01", &Vstate); \
|
||||||
Vstate = cState_from_state_s(cState, 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); \
|
buffer = cState_prepare_buffer(Vstate); \
|
||||||
generate_json_##type(buffer, Vstate, state, self); \
|
generate_json_##type(buffer, Vstate, state, self); \
|
||||||
return fbuffer_to_s(buffer)
|
return fbuffer_to_s(buffer)
|
||||||
|
@ -108,7 +111,7 @@ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
|
||||||
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
|
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
|
||||||
static VALUE mNilClass_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 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 JSON_Generator_State *State_allocate(void);
|
||||||
static VALUE cState_s_allocate(VALUE klass);
|
static VALUE cState_s_allocate(VALUE klass);
|
||||||
static VALUE cState_configure(VALUE self, VALUE opts);
|
static VALUE cState_configure(VALUE self, VALUE opts);
|
||||||
|
@ -144,5 +147,6 @@ static VALUE cState_ascii_only_p(VALUE self);
|
||||||
static VALUE cState_depth(VALUE self);
|
static VALUE cState_depth(VALUE self);
|
||||||
static VALUE cState_depth_set(VALUE self, VALUE depth);
|
static VALUE cState_depth_set(VALUE self, VALUE depth);
|
||||||
static FBuffer *cState_prepare_buffer(VALUE self);
|
static FBuffer *cState_prepare_buffer(VALUE self);
|
||||||
|
static const rb_data_type_t JSON_Generator_State_type;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue