[前][次][番号順一覧][スレッド一覧]

ruby-changes:35635

From: nobu <ko1@a...>
Date: Sat, 27 Sep 2014 10:29:01 +0900 (JST)
Subject: [ruby-changes:35635] nobu:r47717 (trunk): ruby.h: deprecate plain Data

nobu	2014-09-27 10:28:47 +0900 (Sat, 27 Sep 2014)

  New Revision: 47717

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47717

  Log:
    ruby.h: deprecate plain Data
    
    * include/ruby/ruby.h (rb_data_object_alloc_deprecated): deprecate
      Data_Make_Struct and Data_Wrap_Struct.  [EXPERIMENTAL]

  Modified files:
    trunk/ext/-test-/marshal/usr/usrmarshal.c
    trunk/ext/-test-/st/numhash/numhash.c
    trunk/gc.c
    trunk/include/ruby/ruby.h
    trunk/marshal.c
Index: include/ruby/ruby.h
===================================================================
--- include/ruby/ruby.h	(revision 47716)
+++ include/ruby/ruby.h	(revision 47717)
@@ -973,7 +973,23 @@ struct RTypedData { https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L973
 */
 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 *);
Index: gc.c
===================================================================
--- gc.c	(revision 47716)
+++ gc.c	(revision 47717)
@@ -31,6 +31,8 @@ https://github.com/ruby/ruby/blob/trunk/gc.c#L31
 #include <sys/types.h>
 #include <assert.h>
 
+#undef rb_data_object_alloc
+
 #ifndef __has_feature
 # define __has_feature(x) 0
 #endif
Index: ext/-test-/st/numhash/numhash.c
===================================================================
--- ext/-test-/st/numhash/numhash.c	(revision 47716)
+++ ext/-test-/st/numhash/numhash.c	(revision 47717)
@@ -7,16 +7,29 @@ numhash_free(void *ptr) https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/numhash/numhash.c#L7
     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 https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/numhash/numhash.c#L39
 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) https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/numhash/numhash.c#L49
 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 https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/numhash/numhash.c#L68
 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 *v https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/numhash/numhash.c#L91
 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) https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/numhash/numhash.c#L107
 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;
Index: ext/-test-/marshal/usr/usrmarshal.c
===================================================================
--- ext/-test-/marshal/usr/usrmarshal.c	(revision 47716)
+++ ext/-test-/marshal/usr/usrmarshal.c	(revision 47717)
@@ -1,23 +1,38 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/marshal/usr/usrmarshal.c#L1
 #include <ruby.h>
 
+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);
 }
 
Index: marshal.c
===================================================================
--- marshal.c	(revision 47716)
+++ marshal.c	(revision 47717)
@@ -28,6 +28,8 @@ https://github.com/ruby/ruby/blob/trunk/marshal.c#L28
 #include <ieeefp.h>
 #endif
 
+#undef rb_data_object_alloc
+
 #define BITSPERSHORT (2*CHAR_BIT)
 #define SHORTMASK ((1<<BITSPERSHORT)-1)
 #define SHORTDN(x) RSHIFT((x),BITSPERSHORT)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]