Skip to content

Commit 67c20f5

Browse files
casperisfinebyroot
andauthored
Stop using the deprecated Data_* API (#905)
* Convert cache objects to TypedData API The replacement API was introduced in Ruby 1.9.2 (2010), and the old untyped data API was marked a deprecated in the documentation as of Ruby 2.3.0 (2015) Ref: https://bugs.ruby-lang.org/issues/19998 * Convert parser objects to TypedData API The replacement API was introduced in Ruby 1.9.2 (2010), and the old untyped data API was marked a deprecated in the documentation as of Ruby 2.3.0 (2015) Ref: https://bugs.ruby-lang.org/issues/19998 * Convert encoder objects to TypedData API The replacement API was introduced in Ruby 1.9.2 (2010), and the old untyped data API was marked a deprecated in the documentation as of Ruby 2.3.0 (2015) Ref: https://bugs.ruby-lang.org/issues/19998 * Convert StringWriter and StreamWriter objects to TypedData API The replacement API was introduced in Ruby 1.9.2 (2010), and the old untyped data API was marked a deprecated in the documentation as of Ruby 2.3.0 (2015) Ref: https://bugs.ruby-lang.org/issues/19998 * Convert Stack objects to TypedData API The replacement API was introduced in Ruby 1.9.2 (2010), and the old untyped data API was marked a deprecated in the documentation as of Ruby 2.3.0 (2015) Ref: https://bugs.ruby-lang.org/issues/19998 * Remove useless DATA_PTR call and appease clang-format --------- Co-authored-by: Jean Boussier <[email protected]>
1 parent e2825f7 commit 67c20f5

File tree

11 files changed

+202
-69
lines changed

11 files changed

+202
-69
lines changed

ext/oj/cache.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ void cache_set_expunge_rate(Cache c, int rate) {
260260
c->xrate = (uint8_t)rate;
261261
}
262262

263-
void cache_free(Cache c) {
263+
void cache_free(void *data) {
264+
Cache c = (Cache)data;
264265
uint64_t i;
265266

266267
for (i = 0; i < c->size; i++) {
@@ -276,7 +277,8 @@ void cache_free(Cache c) {
276277
OJ_FREE(c);
277278
}
278279

279-
void cache_mark(Cache c) {
280+
void cache_mark(void *data) {
281+
Cache c = (Cache)data;
280282
uint64_t i;
281283

282284
#if !HAVE_PTHREAD_MUTEX_INIT

ext/oj/cache.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
#define CACHE_MAX_KEY 35
1111

1212
struct _cache;
13+
typedef struct _cache *Cache;
1314

1415
extern struct _cache *cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking);
15-
extern void cache_free(struct _cache *c);
16-
extern void cache_mark(struct _cache *c);
16+
extern void cache_free(void *data);
17+
extern void cache_mark(void *data);
1718
extern void cache_set_form(struct _cache *c, VALUE (*form)(const char *str, size_t len));
1819
extern VALUE cache_intern(struct _cache *c, const char *key, size_t len);
1920
extern void cache_set_expunge_rate(struct _cache *c, int rate);

ext/oj/dump.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,11 @@ static void debug_raise(const char *orig, size_t cnt, int line) {
727727

728728
void oj_dump_raw_json(VALUE obj, int depth, Out out) {
729729
if (oj_string_writer_class == rb_obj_class(obj)) {
730-
StrWriter sw = (StrWriter)DATA_PTR(obj);
731-
size_t len = sw->out.cur - sw->out.buf;
730+
StrWriter sw;
731+
size_t len;
732+
733+
sw = oj_str_writer_unwrap(obj);
734+
len = sw->out.cur - sw->out.buf;
732735

733736
if (0 < len) {
734737
len--;

ext/oj/fast.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,10 @@ static VALUE parse_json(VALUE clas, char *json, bool given) {
780780
}
781781
}
782782
#endif
783-
doc->json = json;
784-
self = TypedData_Wrap_Struct(clas, &oj_doc_type, doc);
785-
doc->self = self;
786-
DATA_PTR(doc->self) = doc;
787-
result = rb_protect(protect_open_proc, (VALUE)&pi, &ex);
783+
doc->json = json;
784+
self = TypedData_Wrap_Struct(clas, &oj_doc_type, doc);
785+
doc->self = self;
786+
result = rb_protect(protect_open_proc, (VALUE)&pi, &ex);
788787
if (given || 0 != ex) {
789788
DATA_PTR(doc->self) = NULL;
790789
// TBD is this needed?
@@ -1609,7 +1608,9 @@ static VALUE doc_dump(int argc, VALUE *argv, VALUE self) {
16091608
* Oj::Doc.open('[1,2,3]') { |doc| doc.size() } #=> 4
16101609
*/
16111610
static VALUE doc_size(VALUE self) {
1612-
return ULONG2NUM(((Doc)DATA_PTR(self))->size);
1611+
Doc d;
1612+
TypedData_Get_Struct(self, struct _doc, &oj_doc_type, d);
1613+
return ULONG2NUM(d->size);
16131614
}
16141615

16151616
/* @overload close() => nil

ext/oj/intern.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,31 @@ static VALUE form_attr(const char *str, size_t len) {
8585
return (VALUE)rb_intern3(buf, len + 1, oj_utf8_encoding);
8686
}
8787

88+
static const rb_data_type_t oj_cache_type = {
89+
"Oj/cache",
90+
{
91+
cache_mark,
92+
cache_free,
93+
NULL,
94+
},
95+
0,
96+
0,
97+
};
98+
8899
void oj_hash_init(void) {
89100
VALUE cache_class = rb_define_class_under(Oj, "Cache", rb_cObject);
90101
rb_undef_alloc_func(cache_class);
91102

92103
struct _cache *str_cache = cache_create(0, form_str, true, true);
93-
str_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, str_cache);
104+
str_cache_obj = TypedData_Wrap_Struct(cache_class, &oj_cache_type, str_cache);
94105
rb_gc_register_address(&str_cache_obj);
95106

96107
struct _cache *sym_cache = cache_create(0, form_sym, true, true);
97-
sym_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, sym_cache);
108+
sym_cache_obj = TypedData_Wrap_Struct(cache_class, &oj_cache_type, sym_cache);
98109
rb_gc_register_address(&sym_cache_obj);
99110

100111
struct _cache *attr_cache = cache_create(0, form_attr, false, true);
101-
attr_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, attr_cache);
112+
attr_cache_obj = TypedData_Wrap_Struct(cache_class, &oj_cache_type, attr_cache);
102113
rb_gc_register_address(&attr_cache_obj);
103114

104115
memset(class_hash.slots, 0, sizeof(class_hash.slots));
@@ -118,17 +129,23 @@ oj_str_intern(const char *key, size_t len) {
118129
#if HAVE_RB_ENC_INTERNED_STR && 0
119130
return rb_enc_interned_str(key, len, rb_utf8_encoding());
120131
#else
121-
return cache_intern(DATA_PTR(str_cache_obj), key, len);
132+
Cache c;
133+
TypedData_Get_Struct(str_cache_obj, struct _cache, &oj_cache_type, c);
134+
return cache_intern(c, key, len);
122135
#endif
123136
}
124137

125138
VALUE
126139
oj_sym_intern(const char *key, size_t len) {
127-
return cache_intern(DATA_PTR(sym_cache_obj), key, len);
140+
Cache c;
141+
TypedData_Get_Struct(sym_cache_obj, struct _cache, &oj_cache_type, c);
142+
return cache_intern(c, key, len);
128143
}
129144

130145
ID oj_attr_intern(const char *key, size_t len) {
131-
return cache_intern(DATA_PTR(attr_cache_obj), key, len);
146+
Cache c;
147+
TypedData_Get_Struct(attr_cache_obj, struct _cache, &oj_cache_type, c);
148+
return cache_intern(c, key, len);
132149
}
133150

134151
static uint64_t hash_calc(const uint8_t *key, size_t len) {

ext/oj/oj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ extern void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out);
262262
extern void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts);
263263
extern char *oj_longlong_to_string(long long num, bool negative, char *buf);
264264

265+
extern StrWriter oj_str_writer_unwrap(VALUE writer);
266+
265267
extern void oj_str_writer_push_key(StrWriter sw, const char *key);
266268
extern void oj_str_writer_push_object(StrWriter sw, const char *key);
267269
extern void oj_str_writer_push_array(StrWriter sw, const char *key);

ext/oj/parser.c

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,17 @@ static void parser_mark(void *ptr) {
11641164
}
11651165
}
11661166

1167+
static const rb_data_type_t oj_parser_type = {
1168+
"Oj/parser",
1169+
{
1170+
parser_mark,
1171+
parser_free,
1172+
NULL,
1173+
},
1174+
0,
1175+
0,
1176+
};
1177+
11671178
extern void oj_set_parser_validator(ojParser p);
11681179
extern void oj_set_parser_saj(ojParser p);
11691180
extern void oj_set_parser_usual(ojParser p);
@@ -1255,7 +1266,7 @@ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
12551266
rb_hash_foreach(ropts, opt_cb, (VALUE)p);
12561267
}
12571268
}
1258-
return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1269+
return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
12591270
}
12601271

12611272
// Create a new parser without setting the delegate. The parser is
@@ -1275,7 +1286,7 @@ VALUE oj_parser_new(void) {
12751286
buf_init(&p->buf);
12761287
p->map = value_map;
12771288

1278-
return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1289+
return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
12791290
}
12801291

12811292
// Set set the options from a hash (ropts).
@@ -1322,11 +1333,13 @@ void oj_parser_set_option(ojParser p, VALUE ropts) {
13221333
* - _symbol_keys_ is a flag that indicates Hash keys should be parsed to Symbols versus Strings.
13231334
*/
13241335
static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
1325-
ojParser p = (ojParser)DATA_PTR(self);
1336+
ojParser p;
13261337
const char *key = NULL;
13271338
volatile VALUE rkey = *argv;
13281339
volatile VALUE rv = Qnil;
13291340

1341+
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1342+
13301343
#if HAVE_RB_EXT_RACTOR_SAFE
13311344
// This doesn't seem to do anything.
13321345
rb_ext_ractor_safe(true);
@@ -1352,9 +1365,11 @@ static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
13521365
* Returns the result according to the delegate of the parser.
13531366
*/
13541367
static VALUE parser_parse(VALUE self, VALUE json) {
1355-
ojParser p = (ojParser)DATA_PTR(self);
1368+
ojParser p;
13561369
const byte *ptr = (const byte *)StringValuePtr(json);
13571370

1371+
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1372+
13581373
parser_reset(p);
13591374
p->start(p);
13601375
parse(p, ptr);
@@ -1368,9 +1383,11 @@ static VALUE load_rescue(VALUE self, VALUE x) {
13681383
}
13691384

13701385
static VALUE load(VALUE self) {
1371-
ojParser p = (ojParser)DATA_PTR(self);
1386+
ojParser p;
13721387
volatile VALUE rbuf = rb_str_new2("");
13731388

1389+
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1390+
13741391
p->start(p);
13751392
while (true) {
13761393
rb_funcall(p->reader, oj_readpartial_id, 2, INT2NUM(16385), rbuf);
@@ -1389,7 +1406,9 @@ static VALUE load(VALUE self) {
13891406
* Returns the result according to the delegate of the parser.
13901407
*/
13911408
static VALUE parser_load(VALUE self, VALUE reader) {
1392-
ojParser p = (ojParser)DATA_PTR(self);
1409+
ojParser p;
1410+
1411+
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
13931412

13941413
parser_reset(p);
13951414
p->reader = reader;
@@ -1406,10 +1425,12 @@ static VALUE parser_load(VALUE self, VALUE reader) {
14061425
* Returns the result according to the delegate of the parser.
14071426
*/
14081427
static VALUE parser_file(VALUE self, VALUE filename) {
1409-
ojParser p = (ojParser)DATA_PTR(self);
1428+
ojParser p;
14101429
const char *path;
14111430
int fd;
14121431

1432+
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
1433+
14131434
path = StringValuePtr(filename);
14141435

14151436
parser_reset(p);
@@ -1453,7 +1474,9 @@ static VALUE parser_file(VALUE self, VALUE filename) {
14531474
* Returns the current state of the just_one [_Boolean_] option.
14541475
*/
14551476
static VALUE parser_just_one(VALUE self) {
1456-
ojParser p = (ojParser)DATA_PTR(self);
1477+
ojParser p;
1478+
1479+
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
14571480

14581481
return p->just_one ? Qtrue : Qfalse;
14591482
}
@@ -1467,7 +1490,9 @@ static VALUE parser_just_one(VALUE self) {
14671490
* Returns the current state of the just_one [_Boolean_] option.
14681491
*/
14691492
static VALUE parser_just_one_set(VALUE self, VALUE v) {
1470-
ojParser p = (ojParser)DATA_PTR(self);
1493+
ojParser p;
1494+
1495+
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
14711496

14721497
p->just_one = (Qtrue == v);
14731498

@@ -1491,7 +1516,7 @@ static VALUE parser_usual(VALUE self) {
14911516
buf_init(&p->buf);
14921517
p->map = value_map;
14931518
oj_set_parser_usual(p);
1494-
usual_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1519+
usual_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
14951520
rb_gc_register_address(&usual_parser);
14961521
}
14971522
return usual_parser;
@@ -1514,7 +1539,7 @@ static VALUE parser_saj(VALUE self) {
15141539
buf_init(&p->buf);
15151540
p->map = value_map;
15161541
oj_set_parser_saj(p);
1517-
saj_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1542+
saj_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
15181543
rb_gc_register_address(&saj_parser);
15191544
}
15201545
return saj_parser;
@@ -1536,7 +1561,7 @@ static VALUE parser_validate(VALUE self) {
15361561
buf_init(&p->buf);
15371562
p->map = value_map;
15381563
oj_set_parser_validator(p);
1539-
validate_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1564+
validate_parser = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
15401565
rb_gc_register_address(&validate_parser);
15411566
}
15421567
return validate_parser;

ext/oj/rails.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,17 @@ static void encoder_mark(void *ptr) {
639639
}
640640
}
641641

642+
static const rb_data_type_t oj_encoder_type = {
643+
"Oj/encoder",
644+
{
645+
encoder_mark,
646+
encoder_free,
647+
NULL,
648+
},
649+
0,
650+
0,
651+
};
652+
642653
/* Document-method: new
643654
* call-seq: new(options=nil)
644655
*
@@ -656,7 +667,7 @@ static VALUE encoder_new(int argc, VALUE *argv, VALUE self) {
656667
oj_parse_options(*argv, &e->opts);
657668
e->arg = *argv;
658669
}
659-
return Data_Wrap_Struct(encoder_class, encoder_mark, encoder_free, e);
670+
return TypedData_Wrap_Struct(encoder_class, &oj_encoder_type, e);
660671
}
661672

662673
static VALUE resolve_classpath(const char *name) {
@@ -748,7 +759,8 @@ static void optimize(int argc, VALUE *argv, ROptTable rot, bool on) {
748759
* - *classes* [_Class_] a list of classes to optimize
749760
*/
750761
static VALUE encoder_optimize(int argc, VALUE *argv, VALUE self) {
751-
Encoder e = (Encoder)DATA_PTR(self);
762+
Encoder e;
763+
TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
752764

753765
optimize(argc, argv, &e->ropts, true);
754766

@@ -804,7 +816,8 @@ rails_mimic_json(VALUE self) {
804816
* - *classes* [_Class_] a list of classes to deoptimize
805817
*/
806818
static VALUE encoder_deoptimize(int argc, VALUE *argv, VALUE self) {
807-
Encoder e = (Encoder)DATA_PTR(self);
819+
Encoder e;
820+
TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
808821

809822
optimize(argc, argv, &e->ropts, false);
810823

@@ -833,8 +846,11 @@ static VALUE rails_deoptimize(int argc, VALUE *argv, VALUE self) {
833846
* @return true if the class is being optimized for rails and false otherwise
834847
*/
835848
static VALUE encoder_optimized(VALUE self, VALUE clas) {
836-
Encoder e = (Encoder)DATA_PTR(self);
837-
ROpt ro = oj_rails_get_opt(&e->ropts, clas);
849+
Encoder e;
850+
ROpt ro;
851+
852+
TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
853+
ro = oj_rails_get_opt(&e->ropts, clas);
838854

839855
if (NULL == ro) {
840856
return Qfalse;
@@ -940,7 +956,8 @@ static VALUE encode(VALUE obj, ROptTable ropts, Options opts, int argc, VALUE *a
940956
* Returns encoded object as a JSON string.
941957
*/
942958
static VALUE encoder_encode(VALUE self, VALUE obj) {
943-
Encoder e = (Encoder)DATA_PTR(self);
959+
Encoder e;
960+
TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
944961

945962
if (Qnil != e->arg) {
946963
VALUE argv[1] = {e->arg};

0 commit comments

Comments
 (0)