summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/json/ext/fbuffer/fbuffer.h11
-rw-r--r--ext/json/ext/generator/extconf.rb10
-rw-r--r--ext/json/ext/generator/generator.c42
-rw-r--r--ext/json/ext/generator/generator.h12
-rw-r--r--ext/json/ext/parser/extconf.rb10
-rw-r--r--ext/json/ext/parser/parser.c25
-rw-r--r--ext/json/ext/parser/parser.h9
-rw-r--r--ext/json/ext/parser/parser.rl25
-rw-r--r--ext/json/extconf.rb3
9 files changed, 102 insertions, 45 deletions
diff --git a/ext/json/ext/fbuffer/fbuffer.h b/ext/json/ext/fbuffer/fbuffer.h
index af74187..5a0a27c 100644
--- a/ext/json/ext/fbuffer/fbuffer.h
+++ b/ext/json/ext/fbuffer/fbuffer.h
@@ -25,6 +25,15 @@
#define RSTRING_LEN(string) RSTRING(string)->len
#endif
+#ifdef PRIsVALUE
+# define RB_OBJ_CLASSNAME(obj) rb_obj_class(obj)
+# define RB_OBJ_STRING(obj) (obj)
+#else
+# define PRIsVALUE "s"
+# define RB_OBJ_CLASSNAME(obj) rb_obj_classname(obj)
+# define RB_OBJ_STRING(obj) StringValueCStr(obj)
+#endif
+
#ifdef HAVE_RUBY_ENCODING_H
#include "ruby/encoding.h"
#define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
@@ -172,7 +181,7 @@ static FBuffer *fbuffer_dup(FBuffer *fb)
static VALUE fbuffer_to_s(FBuffer *fb)
{
- VALUE result = rb_str_new(FBUFFER_PAIR(fb));
+ VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb);
FORCE_UTF8(result);
return result;
diff --git a/ext/json/ext/generator/extconf.rb b/ext/json/ext/generator/extconf.rb
index c947501..8627c5f 100644
--- a/ext/json/ext/generator/extconf.rb
+++ b/ext/json/ext/generator/extconf.rb
@@ -1,14 +1,4 @@
require 'mkmf'
-unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3')
- $CFLAGS << ' -O3'
-end
-if CONFIG['CC'] =~ /gcc/
- $CFLAGS << ' -Wall'
- unless $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
- $CFLAGS << ' -O0 -ggdb'
- end
-end
-
$defs << "-DJSON_GENERATOR"
create_makefile 'json/ext/generator'
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c
index 976afc5..f789a89 100644
--- a/ext/json/ext/generator/generator.c
+++ b/ext/json/ext/generator/generator.c
@@ -486,8 +486,9 @@ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
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,7 +500,31 @@ static void State_free(JSON_Generator_State *state)
ruby_xfree(state);
}
-static JSON_Generator_State *State_allocate()
+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,},
+#ifdef RUBY_TYPED_FREE_IMMEDIATELY
+ 0, 0,
+ RUBY_TYPED_FREE_IMMEDIATELY,
+#endif
+};
+
+static JSON_Generator_State *State_allocate(void)
{
JSON_Generator_State *state = ALLOC(JSON_Generator_State);
MEMZERO(state, JSON_Generator_State, 1);
@@ -509,7 +534,7 @@ static JSON_Generator_State *State_allocate()
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);
}
/*
@@ -812,10 +837,10 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
if (!allow_nan) {
if (isinf(value)) {
fbuffer_free(buffer);
- rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
+ rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
} else if (isnan(value)) {
fbuffer_free(buffer);
- rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
+ rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
}
}
fbuffer_append_str(buffer, tmp);
@@ -965,8 +990,9 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
{
JSON_Generator_State *objState, *origState;
- Data_Get_Struct(obj, JSON_Generator_State, objState);
- Data_Get_Struct(orig, JSON_Generator_State, origState);
+ if (obj == orig) return obj;
+ 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);
@@ -1326,7 +1352,7 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
/*
*
*/
-void Init_generator()
+void Init_generator(void)
{
rb_require("json/common");
diff --git a/ext/json/ext/generator/generator.h b/ext/json/ext/generator/generator.h
index e2fbf12..7bf8408 100644
--- a/ext/json/ext/generator/generator.h
+++ b/ext/json/ext/generator/generator.h
@@ -78,9 +78,12 @@ typedef struct JSON_Generator_StateStruct {
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_StateStruct {
\
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,8 +111,8 @@ static VALUE mTrueClass_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 mObject_to_json(int argc, VALUE *argv, VALUE self);
-static void State_free(JSON_Generator_State *state);
-static JSON_Generator_State *State_allocate();
+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);
@@ -144,5 +147,6 @@ static VALUE cState_ascii_only_p(VALUE self);
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
diff --git a/ext/json/ext/parser/extconf.rb b/ext/json/ext/parser/extconf.rb
index 4791829..ae4f861 100644
--- a/ext/json/ext/parser/extconf.rb
+++ b/ext/json/ext/parser/extconf.rb
@@ -1,13 +1,3 @@
require 'mkmf'
-unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3')
- $CFLAGS << ' -O3'
-end
-if CONFIG['CC'] =~ /gcc/
- $CFLAGS << ' -Wall'
- if $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
- $CFLAGS << ' -O0 -ggdb'
- end
-end
-
create_makefile 'json/ext/parser'
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index e91e161..efb4dac 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -2092,7 +2092,7 @@ static VALUE cParser_parse(VALUE self)
}
-static JSON_Parser *JSON_allocate()
+static JSON_Parser *JSON_allocate(void)
{
JSON_Parser *json = ALLOC(JSON_Parser);
MEMZERO(json, JSON_Parser, 1);
@@ -2100,8 +2100,9 @@ static JSON_Parser *JSON_allocate()
return json;
}
-static void JSON_mark(JSON_Parser *json)
+static void JSON_mark(void *ptr)
{
+ JSON_Parser *json = ptr;
rb_gc_mark_maybe(json->Vsource);
rb_gc_mark_maybe(json->create_id);
rb_gc_mark_maybe(json->object_class);
@@ -2109,16 +2110,32 @@ static void JSON_mark(JSON_Parser *json)
rb_gc_mark_maybe(json->match_string);
}
-static void JSON_free(JSON_Parser *json)
+static void JSON_free(void *ptr)
{
+ JSON_Parser *json = ptr;
fbuffer_free(json->fbuffer);
ruby_xfree(json);
}
+static size_t JSON_memsize(const void *ptr)
+{
+ const JSON_Parser *json = ptr;
+ return sizeof(*json) + FBUFFER_CAPA(json->fbuffer);
+}
+
+static const rb_data_type_t JSON_Parser_type = {
+ "JSON/Parser",
+ {JSON_mark, JSON_free, JSON_memsize,},
+#ifdef RUBY_TYPED_FREE_IMMEDIATELY
+ 0, 0,
+ RUBY_TYPED_FREE_IMMEDIATELY,
+#endif
+};
+
static VALUE cJSON_parser_s_allocate(VALUE klass)
{
JSON_Parser *json = JSON_allocate();
- return Data_Wrap_Struct(klass, JSON_mark, JSON_free, json);
+ return TypedData_Wrap_Struct(klass, &JSON_Parser_type, json);
}
/*
diff --git a/ext/json/ext/parser/parser.h b/ext/json/ext/parser/parser.h
index b192064..45afbc2 100644
--- a/ext/json/ext/parser/parser.h
+++ b/ext/json/ext/parser/parser.h
@@ -51,7 +51,7 @@ typedef struct JSON_ParserStruct {
if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance")
#define GET_PARSER_INIT \
JSON_Parser *json; \
- Data_Get_Struct(self, JSON_Parser, json)
+ TypedData_Get_Struct(self, JSON_Parser, &JSON_Parser_type, json)
#define MinusInfinity "-Infinity"
#define EVIL 0x666
@@ -68,10 +68,11 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
static VALUE convert_encoding(VALUE source);
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self);
static VALUE cParser_parse(VALUE self);
-static JSON_Parser *JSON_allocate();
-static void JSON_mark(JSON_Parser *json);
-static void JSON_free(JSON_Parser *json);
+static JSON_Parser *JSON_allocate(void);
+static void JSON_mark(void *json);
+static void JSON_free(void *json);
static VALUE cJSON_parser_s_allocate(VALUE klass);
static VALUE cParser_source(VALUE self);
+static const rb_data_type_t JSON_Parser_type;
#endif
diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl
index c60e35c..d3ba554 100644
--- a/ext/json/ext/parser/parser.rl
+++ b/ext/json/ext/parser/parser.rl
@@ -815,7 +815,7 @@ static VALUE cParser_parse(VALUE self)
}
-static JSON_Parser *JSON_allocate()
+static JSON_Parser *JSON_allocate(void)
{
JSON_Parser *json = ALLOC(JSON_Parser);
MEMZERO(json, JSON_Parser, 1);
@@ -823,8 +823,9 @@ static JSON_Parser *JSON_allocate()
return json;
}
-static void JSON_mark(JSON_Parser *json)
+static void JSON_mark(void *ptr)
{
+ JSON_Parser *json = ptr;
rb_gc_mark_maybe(json->Vsource);
rb_gc_mark_maybe(json->create_id);
rb_gc_mark_maybe(json->object_class);
@@ -832,16 +833,32 @@ static void JSON_mark(JSON_Parser *json)
rb_gc_mark_maybe(json->match_string);
}
-static void JSON_free(JSON_Parser *json)
+static void JSON_free(void *ptr)
{
+ JSON_Parser *json = ptr;
fbuffer_free(json->fbuffer);
ruby_xfree(json);
}
+static size_t JSON_memsize(const void *ptr)
+{
+ const JSON_Parser *json = ptr;
+ return sizeof(*json) + FBUFFER_CAPA(json->fbuffer);
+}
+
+static const rb_data_type_t JSON_Parser_type = {
+ "JSON/Parser",
+ {JSON_mark, JSON_free, JSON_memsize,},
+#ifdef RUBY_TYPED_FREE_IMMEDIATELY
+ 0, 0,
+ RUBY_TYPED_FREE_IMMEDIATELY,
+#endif
+};
+
static VALUE cJSON_parser_s_allocate(VALUE klass)
{
JSON_Parser *json = JSON_allocate();
- return Data_Wrap_Struct(klass, JSON_mark, JSON_free, json);
+ return TypedData_Wrap_Struct(klass, &JSON_Parser_type, json);
}
/*
diff --git a/ext/json/extconf.rb b/ext/json/extconf.rb
new file mode 100644
index 0000000..850798c
--- /dev/null
+++ b/ext/json/extconf.rb
@@ -0,0 +1,3 @@
+require 'mkmf'
+create_makefile('json')
+