diff --git a/ext/-test-/marshal/usr/usrmarshal.c b/ext/-test-/marshal/usr/usrmarshal.c index b30bd52c13..056f0326c0 100644 --- a/ext/-test-/marshal/usr/usrmarshal.c +++ b/ext/-test-/marshal/usr/usrmarshal.c @@ -1,23 +1,38 @@ #include +static size_t +usr_size(const void *ptr) +{ + return sizeof(int); +} + +static const rb_data_type_t usrmarshal_type = { + "UsrMarshal", + {0, RUBY_DEFAULT_FREE, usr_size,}, + NULL, NULL, + RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED, +}; + static VALUE usr_alloc(VALUE klass) { int *p; - return Data_Make_Struct(klass, int, 0, RUBY_DEFAULT_FREE, p); + return TypedData_Make_Struct(klass, int, &usrmarshal_type, p); } static VALUE usr_init(VALUE self, VALUE val) { - *(int *)DATA_PTR(self) = NUM2INT(val); + int *ptr = Check_TypedStruct(self, &usrmarshal_type); + *ptr = NUM2INT(val); return self; } static VALUE usr_value(VALUE self) { - int val = *(int *)DATA_PTR(self); + int *ptr = Check_TypedStruct(self, &usrmarshal_type); + int val = *ptr; return INT2NUM(val); } diff --git a/ext/-test-/st/numhash/numhash.c b/ext/-test-/st/numhash/numhash.c index d4dbd1a0ce..a903edd541 100644 --- a/ext/-test-/st/numhash/numhash.c +++ b/ext/-test-/st/numhash/numhash.c @@ -7,16 +7,29 @@ numhash_free(void *ptr) if (ptr) st_free_table(ptr); } +static size_t +numhash_memsize(const void *ptr) +{ + return ptr ? st_memsize(ptr) : 0; +} + +static const rb_data_type_t numhash_type = { + "numhash", + {0, numhash_free, numhash_memsize,}, + NULL, NULL, + RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED, +}; + static VALUE numhash_alloc(VALUE klass) { - return Data_Wrap_Struct(klass, 0, numhash_free, 0); + return TypedData_Wrap_Struct(klass, &numhash_type, 0); } static VALUE numhash_init(VALUE self) { - st_table *tbl = (st_table *)DATA_PTR(self); + st_table *tbl = (st_table *)Check_TypedStruct(self, &numhash_type); if (tbl) st_free_table(tbl); DATA_PTR(self) = st_init_numtable(); return self; @@ -26,8 +39,9 @@ static VALUE numhash_aref(VALUE self, VALUE key) { st_data_t data; + st_table *tbl = (st_table *)Check_TypedStruct(self, &numhash_type); if (!SPECIAL_CONST_P(key)) rb_raise(rb_eArgError, "not a special const"); - if (st_lookup((st_table *)DATA_PTR(self), (st_data_t)key, &data)) + if (st_lookup(tbl, (st_data_t)key, &data)) return (VALUE)data; return Qnil; } @@ -35,9 +49,10 @@ numhash_aref(VALUE self, VALUE key) static VALUE numhash_aset(VALUE self, VALUE key, VALUE data) { + st_table *tbl = (st_table *)Check_TypedStruct(self, &numhash_type); if (!SPECIAL_CONST_P(key)) rb_raise(rb_eArgError, "not a special const"); if (!SPECIAL_CONST_P(data)) rb_raise(rb_eArgError, "not a special const"); - st_insert((st_table *)DATA_PTR(self), (st_data_t)key, (st_data_t)data); + st_insert(tbl, (st_data_t)key, (st_data_t)data); return self; } @@ -53,7 +68,7 @@ numhash_i(st_data_t key, st_data_t value, st_data_t arg) static VALUE numhash_each(VALUE self) { - st_table *table = DATA_PTR(self); + st_table *table = (st_table *)Check_TypedStruct(self, &numhash_type); st_data_t data = (st_data_t)self; return st_foreach_check(table, numhash_i, data, data) ? Qtrue : Qfalse; } @@ -76,7 +91,8 @@ update_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing) static VALUE numhash_update(VALUE self, VALUE key) { - if (st_update((st_table *)DATA_PTR(self), (st_data_t)key, update_func, 0)) + st_table *table = (st_table *)Check_TypedStruct(self, &numhash_type); + if (st_update(table, (st_data_t)key, update_func, 0)) return Qtrue; else return Qfalse; @@ -91,14 +107,16 @@ numhash_update(VALUE self, VALUE key) static VALUE numhash_size(VALUE self) { - return ST2NUM(((st_table *)DATA_PTR(self))->num_entries); + st_table *table = (st_table *)Check_TypedStruct(self, &numhash_type); + return ST2NUM(table->num_entries); } static VALUE numhash_delete_safe(VALUE self, VALUE key) { + st_table *table = (st_table *)Check_TypedStruct(self, &numhash_type); st_data_t val, k = (st_data_t)key; - if (st_delete_safe((st_table *)DATA_PTR(self), &k, &val, (st_data_t)self)) { + if (st_delete_safe(table, &k, &val, (st_data_t)self)) { return val; } return Qnil; diff --git a/gc.c b/gc.c index 5ee5beea26..ade6a7dda4 100644 --- a/gc.c +++ b/gc.c @@ -31,6 +31,8 @@ #include #include +#undef rb_data_object_alloc + #ifndef __has_feature # define __has_feature(x) 0 #endif diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 6a3949ab8e..59c2d54756 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -973,7 +973,23 @@ struct RTypedData { */ typedef void (*RUBY_DATA_FUNC)(void*); +#ifndef RUBY_DEPRECATE_DATA_WRAP_STRUCT +# ifdef RUBY_EXPORT +# define RUBY_DEPRECATE_DATA_WRAP_STRUCT 1 +# else +# define RUBY_DEPRECATE_DATA_WRAP_STRUCT 0 +# endif +#endif VALUE rb_data_object_alloc(VALUE,void*,RUBY_DATA_FUNC,RUBY_DATA_FUNC); +#if RUBY_DEPRECATE_DATA_WRAP_STRUCT +DEPRECATED(static inline VALUE rb_data_object_alloc_deprecated(VALUE,void*,RUBY_DATA_FUNC,RUBY_DATA_FUNC)); +static inline VALUE +rb_data_object_alloc_deprecated(VALUE klass, void *ptr, RUBY_DATA_FUNC mark, RUBY_DATA_FUNC free) +{ + return rb_data_object_alloc(klass, ptr, mark, free); +} +#define rb_data_object_alloc rb_data_object_alloc_deprecated +#endif VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *); int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent); int rb_typeddata_is_kind_of(VALUE, const rb_data_type_t *); diff --git a/marshal.c b/marshal.c index 598f90f187..26fd0bcc0b 100644 --- a/marshal.c +++ b/marshal.c @@ -28,6 +28,8 @@ #include #endif +#undef rb_data_object_alloc + #define BITSPERSHORT (2*CHAR_BIT) #define SHORTMASK ((1<