Skip to content

Commit 7f60191

Browse files
hsbtflori
authored andcommitted
use ZALLOC if it can be used, and defined ZALLOC macro.
1 parent fcf3a8b commit 7f60191

File tree

5 files changed

+54
-39
lines changed

5 files changed

+54
-39
lines changed

ext/json/ext/generator/generator.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,7 @@ static const rb_data_type_t JSON_Generator_State_type = {
528528

529529
static JSON_Generator_State *State_allocate(void)
530530
{
531-
JSON_Generator_State *state = ALLOC(JSON_Generator_State);
532-
MEMZERO(state, JSON_Generator_State, 1);
531+
JSON_Generator_State *state = ZALLOC(JSON_Generator_State);
533532
return state;
534533
}
535534

ext/json/ext/generator/generator.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ static VALUE cState_ascii_only_p(VALUE self);
147147
static VALUE cState_depth(VALUE self);
148148
static VALUE cState_depth_set(VALUE self, VALUE depth);
149149
static FBuffer *cState_prepare_buffer(VALUE self);
150+
#ifndef ZALLOC
151+
#define ZALLOC(type) ((type *)ruby_zalloc(sizeof(type)))
152+
static inline void *ruby_zalloc(size_t n)
153+
{
154+
void *p = ruby_xmalloc(n);
155+
memset(p, 0, n);
156+
return p;
157+
}
158+
#endif
150159
#ifdef TypedData_Wrap_Struct
151160
static const rb_data_type_t JSON_Generator_State_type;
152161
#define NEW_TYPEDDATA_WRAPPER 1

ext/json/ext/parser/parser.c

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
8989

9090

9191
#line 92 "parser.c"
92-
static const int JSON_object_start = 1;
93-
static const int JSON_object_first_final = 27;
94-
static const int JSON_object_error = 0;
92+
enum {JSON_object_start = 1};
93+
enum {JSON_object_first_final = 27};
94+
enum {JSON_object_error = 0};
9595

96-
static const int JSON_object_en_main = 1;
96+
enum {JSON_object_en_main = 1};
9797

9898

9999
#line 151 "parser.rl"
@@ -467,11 +467,11 @@ case 26:
467467

468468

469469
#line 470 "parser.c"
470-
static const int JSON_value_start = 1;
471-
static const int JSON_value_first_final = 21;
472-
static const int JSON_value_error = 0;
470+
enum {JSON_value_start = 1};
471+
enum {JSON_value_first_final = 21};
472+
enum {JSON_value_error = 0};
473473

474-
static const int JSON_value_en_main = 1;
474+
enum {JSON_value_en_main = 1};
475475

476476

477477
#line 271 "parser.rl"
@@ -776,11 +776,11 @@ case 20:
776776

777777

778778
#line 779 "parser.c"
779-
static const int JSON_integer_start = 1;
780-
static const int JSON_integer_first_final = 3;
781-
static const int JSON_integer_error = 0;
779+
enum {JSON_integer_start = 1};
780+
enum {JSON_integer_first_final = 3};
781+
enum {JSON_integer_error = 0};
782782

783-
static const int JSON_integer_en_main = 1;
783+
enum {JSON_integer_en_main = 1};
784784

785785

786786
#line 295 "parser.rl"
@@ -875,11 +875,11 @@ case 5:
875875

876876

877877
#line 878 "parser.c"
878-
static const int JSON_float_start = 1;
879-
static const int JSON_float_first_final = 8;
880-
static const int JSON_float_error = 0;
878+
enum {JSON_float_start = 1};
879+
enum {JSON_float_first_final = 8};
880+
enum {JSON_float_error = 0};
881881

882-
static const int JSON_float_en_main = 1;
882+
enum {JSON_float_en_main = 1};
883883

884884

885885
#line 329 "parser.rl"
@@ -1041,11 +1041,11 @@ case 7:
10411041

10421042

10431043
#line 1044 "parser.c"
1044-
static const int JSON_array_start = 1;
1045-
static const int JSON_array_first_final = 17;
1046-
static const int JSON_array_error = 0;
1044+
enum {JSON_array_start = 1};
1045+
enum {JSON_array_first_final = 17};
1046+
enum {JSON_array_error = 0};
10471047

1048-
static const int JSON_array_en_main = 1;
1048+
enum {JSON_array_en_main = 1};
10491049

10501050

10511051
#line 381 "parser.rl"
@@ -1373,11 +1373,11 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
13731373

13741374

13751375
#line 1376 "parser.c"
1376-
static const int JSON_string_start = 1;
1377-
static const int JSON_string_first_final = 8;
1378-
static const int JSON_string_error = 0;
1376+
enum {JSON_string_start = 1};
1377+
enum {JSON_string_first_final = 8};
1378+
enum {JSON_string_error = 0};
13791379

1380-
static const int JSON_string_en_main = 1;
1380+
enum {JSON_string_en_main = 1};
13811381

13821382

13831383
#line 494 "parser.rl"
@@ -1730,11 +1730,11 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
17301730

17311731

17321732
#line 1733 "parser.c"
1733-
static const int JSON_start = 1;
1734-
static const int JSON_first_final = 10;
1735-
static const int JSON_error = 0;
1733+
enum {JSON_start = 1};
1734+
enum {JSON_first_final = 10};
1735+
enum {JSON_error = 0};
17361736

1737-
static const int JSON_en_main = 1;
1737+
enum {JSON_en_main = 1};
17381738

17391739

17401740
#line 740 "parser.rl"
@@ -1904,11 +1904,11 @@ case 9:
19041904

19051905

19061906
#line 1907 "parser.c"
1907-
static const int JSON_quirks_mode_start = 1;
1908-
static const int JSON_quirks_mode_first_final = 10;
1909-
static const int JSON_quirks_mode_error = 0;
1907+
enum {JSON_quirks_mode_start = 1};
1908+
enum {JSON_quirks_mode_first_final = 10};
1909+
enum {JSON_quirks_mode_error = 0};
19101910

1911-
static const int JSON_quirks_mode_en_main = 1;
1911+
enum {JSON_quirks_mode_en_main = 1};
19121912

19131913

19141914
#line 778 "parser.rl"
@@ -2094,8 +2094,7 @@ static VALUE cParser_parse(VALUE self)
20942094

20952095
static JSON_Parser *JSON_allocate(void)
20962096
{
2097-
JSON_Parser *json = ALLOC(JSON_Parser);
2098-
MEMZERO(json, JSON_Parser, 1);
2097+
JSON_Parser *json = ZALLOC(JSON_Parser);
20992098
json->fbuffer = fbuffer_alloc(0);
21002099
return json;
21012100
}
@@ -2164,7 +2163,7 @@ static VALUE cParser_quirks_mode_p(VALUE self)
21642163
}
21652164

21662165

2167-
void Init_parser()
2166+
void Init_parser(void)
21682167
{
21692168
rb_require("json/common");
21702169
mJSON = rb_define_module("JSON");

ext/json/ext/parser/parser.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ static void JSON_mark(void *json);
7373
static void JSON_free(void *json);
7474
static VALUE cJSON_parser_s_allocate(VALUE klass);
7575
static VALUE cParser_source(VALUE self);
76+
#ifndef ZALLOC
77+
#define ZALLOC(type) ((type *)ruby_zalloc(sizeof(type)))
78+
static inline void *ruby_zalloc(size_t n)
79+
{
80+
void *p = ruby_xmalloc(n);
81+
memset(p, 0, n);
82+
return p;
83+
}
84+
#endif
7685
#ifdef TypedData_Wrap_Struct
7786
static const rb_data_type_t JSON_Parser_type;
7887
#define NEW_TYPEDDATA_WRAPPER 1

ext/json/ext/parser/parser.rl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,7 @@ static VALUE cParser_parse(VALUE self)
817817

818818
static JSON_Parser *JSON_allocate(void)
819819
{
820-
JSON_Parser *json = ALLOC(JSON_Parser);
821-
MEMZERO(json, JSON_Parser, 1);
820+
JSON_Parser *json = ZALLOC(JSON_Parser);
822821
json->fbuffer = fbuffer_alloc(0);
823822
return json;
824823
}

0 commit comments

Comments
 (0)