@@ -67,13 +67,30 @@ VALUE xxhash_xxh32(VALUE mod, VALUE input, VALUE seed)
67
67
return ULL2NUM (XXH32 (StringValuePtr (input ), (size_t )RSTRING_LEN (input ), (unsigned int )NUM2ULL (seed )));
68
68
}
69
69
70
- void xxhash32_streaming_hash_free (xxhash_xxh32_t * storage )
70
+ static void xxhash32_streaming_hash_free (void * ptr )
71
71
{
72
+ xxhash_xxh32_t * storage = (xxhash_xxh32_t * )ptr ;
72
73
// Digest frees the memory.
73
74
XXH32_freeState (storage -> state );
74
75
xfree (storage );
75
76
}
76
77
78
+ static size_t xxhash32_streaming_hash_memsize (const void * ptr )
79
+ {
80
+ // Ideally we'd include sizeof(XXH32_state_t) too, but the type is opaque.
81
+ return sizeof (xxhash_xxh32_t );
82
+ }
83
+
84
+ static const rb_data_type_t xxhash_xxh32_type = {
85
+ .wrap_struct_name = "xxhash/xxhash32_streaming_hash" ,
86
+ .function = {
87
+ .dmark = NULL ,
88
+ .dfree = xxhash32_streaming_hash_free ,
89
+ .dsize = xxhash32_streaming_hash_memsize ,
90
+ },
91
+ .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
92
+ };
93
+
77
94
VALUE xxhash32_streaming_hash_new (VALUE klass , VALUE seed )
78
95
{
79
96
XXH_errorcode code ;
@@ -86,14 +103,14 @@ VALUE xxhash32_streaming_hash_new(VALUE klass, VALUE seed)
86
103
rb_raise (rb_eRuntimeError , "Error during reset." );
87
104
return Qnil ;
88
105
}
89
- return Data_Wrap_Struct (klass , 0 , xxhash32_streaming_hash_free , storage );
106
+ return TypedData_Wrap_Struct (klass , & xxhash_xxh32_type , storage );
90
107
}
91
108
92
109
VALUE xxhash32_streaming_hash_reset (VALUE self )
93
110
{
94
111
XXH_errorcode code ;
95
112
xxhash_xxh32_t * storage ;
96
- Data_Get_Struct (self , xxhash_xxh32_t , storage );
113
+ TypedData_Get_Struct (self , xxhash_xxh32_t , & xxhash_xxh32_type , storage );
97
114
98
115
code = XXH32_reset (storage -> state , storage -> seed );
99
116
if (code != XXH_OK ) {
@@ -107,7 +124,7 @@ VALUE xxhash32_streaming_hash_update(VALUE self, VALUE data)
107
124
{
108
125
XXH_errorcode code ;
109
126
xxhash_xxh32_t * storage ;
110
- Data_Get_Struct (self , xxhash_xxh32_t , storage );
127
+ TypedData_Get_Struct (self , xxhash_xxh32_t , & xxhash_xxh32_type , storage );
111
128
112
129
code = XXH32_update (storage -> state , StringValuePtr (data ), (size_t )RSTRING_LEN (data ));
113
130
if (code != XXH_OK ) {
@@ -119,7 +136,7 @@ VALUE xxhash32_streaming_hash_update(VALUE self, VALUE data)
119
136
VALUE xxhash32_streaming_hash_digest (VALUE self )
120
137
{
121
138
xxhash_xxh32_t * storage ;
122
- Data_Get_Struct (self , xxhash_xxh32_t , storage );
139
+ TypedData_Get_Struct (self , xxhash_xxh32_t , & xxhash_xxh32_type , storage );
123
140
124
141
// Do not free memory now.
125
142
return ULL2NUM (XXH32_digest (storage -> state ));
@@ -130,13 +147,30 @@ VALUE xxhash_xxh64(VALUE mod, VALUE input, VALUE seed)
130
147
return ULL2NUM (XXH64 (StringValuePtr (input ), (size_t )RSTRING_LEN (input ), (unsigned int )NUM2ULL (seed )));
131
148
}
132
149
133
- void xxhash64_streaming_hash_free (xxhash_xxh64_t * storage )
150
+ static void xxhash64_streaming_hash_free (void * ptr )
134
151
{
152
+ xxhash_xxh64_t * storage = (xxhash_xxh64_t * )ptr ;
135
153
// Digest frees the memory.
136
154
XXH64_freeState (storage -> state );
137
155
xfree (storage );
138
156
}
139
157
158
+ static size_t xxhash64_streaming_hash_memsize (const void * ptr )
159
+ {
160
+ // Ideally we'd include sizeof(XXH64_state_t) too, but the type is opaque.
161
+ return sizeof (xxhash_xxh64_t );
162
+ }
163
+
164
+ static const rb_data_type_t xxhash_xxh64_type = {
165
+ .wrap_struct_name = "xxhash/xxhash64_streaming_hash" ,
166
+ .function = {
167
+ .dmark = NULL ,
168
+ .dfree = xxhash64_streaming_hash_free ,
169
+ .dsize = xxhash64_streaming_hash_memsize ,
170
+ },
171
+ .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
172
+ };
173
+
140
174
VALUE xxhash64_streaming_hash_new (VALUE klass , VALUE seed )
141
175
{
142
176
XXH_errorcode code ;
@@ -150,14 +184,14 @@ VALUE xxhash64_streaming_hash_new(VALUE klass, VALUE seed)
150
184
rb_raise (rb_eRuntimeError , "Error during reset." );
151
185
return Qnil ;
152
186
}
153
- return Data_Wrap_Struct (klass , 0 , xxhash64_streaming_hash_free , storage );
187
+ return TypedData_Wrap_Struct (klass , & xxhash_xxh64_type , storage );
154
188
}
155
189
156
190
VALUE xxhash64_streaming_hash_reset (VALUE self )
157
191
{
158
192
XXH_errorcode code ;
159
193
xxhash_xxh64_t * storage ;
160
- Data_Get_Struct (self , xxhash_xxh64_t , storage );
194
+ TypedData_Get_Struct (self , xxhash_xxh64_t , & xxhash_xxh64_type , storage );
161
195
162
196
code = XXH64_reset (storage -> state , storage -> seed );
163
197
if (code != XXH_OK ) {
@@ -170,7 +204,7 @@ VALUE xxhash64_streaming_hash_update(VALUE self, VALUE data)
170
204
{
171
205
XXH_errorcode code ;
172
206
xxhash_xxh64_t * storage ;
173
- Data_Get_Struct (self , xxhash_xxh64_t , storage );
207
+ TypedData_Get_Struct (self , xxhash_xxh64_t , & xxhash_xxh64_type , storage );
174
208
175
209
code = XXH64_update (storage -> state , StringValuePtr (data ), (size_t )RSTRING_LEN (data ));
176
210
if (code != XXH_OK ) {
@@ -182,7 +216,7 @@ VALUE xxhash64_streaming_hash_update(VALUE self, VALUE data)
182
216
VALUE xxhash64_streaming_hash_digest (VALUE self )
183
217
{
184
218
xxhash_xxh64_t * storage ;
185
- Data_Get_Struct (self , xxhash_xxh64_t , storage );
219
+ TypedData_Get_Struct (self , xxhash_xxh64_t , & xxhash_xxh64_type , storage );
186
220
187
221
// Do not free memory now.
188
222
return ULL2NUM (XXH64_digest (storage -> state ));
@@ -209,13 +243,15 @@ void Init_xxhash(void)
209
243
rb_define_singleton_method (mInternal , "xxh64_file" , (ruby_method * ) & xxhash_xxh64_file , 2 );
210
244
211
245
cStreamingHash = rb_define_class_under (mInternal , "StreamingHash32" , rb_cObject );
246
+ rb_undef_alloc_func (cStreamingHash );
212
247
213
248
rb_define_singleton_method (cStreamingHash , "new" , (ruby_method * ) & xxhash32_streaming_hash_new , 1 );
214
249
rb_define_method (cStreamingHash , "update" , (ruby_method * ) & xxhash32_streaming_hash_update , 1 );
215
250
rb_define_method (cStreamingHash , "digest" , (ruby_method * ) & xxhash32_streaming_hash_digest , 0 );
216
251
rb_define_method (cStreamingHash , "reset" , (ruby_method * ) & xxhash32_streaming_hash_reset , 0 );
217
252
218
253
cStreamingHash64 = rb_define_class_under (mInternal , "StreamingHash64" , rb_cObject );
254
+ rb_undef_alloc_func (cStreamingHash64 );
219
255
220
256
rb_define_singleton_method (cStreamingHash64 , "new" , (ruby_method * ) & xxhash64_streaming_hash_new , 1 );
221
257
rb_define_method (cStreamingHash64 , "update" , (ruby_method * ) & xxhash64_streaming_hash_update , 1 );
0 commit comments