@@ -47,8 +47,8 @@ static VALUE mCoolio = Qnil;
4747static VALUE cCoolio_Buffer = Qnil ;
4848
4949static VALUE Coolio_Buffer_allocate (VALUE klass );
50- static void Coolio_Buffer_mark (struct buffer * );
51- static void Coolio_Buffer_free (struct buffer * );
50+ static void Coolio_Buffer_mark (void * );
51+ static void Coolio_Buffer_free (void * );
5252
5353static VALUE Coolio_Buffer_default_node_size (VALUE klass );
5454static VALUE Coolio_Buffer_set_default_node_size (VALUE klass , VALUE size );
@@ -64,7 +64,7 @@ static VALUE Coolio_Buffer_to_str(VALUE self);
6464static VALUE Coolio_Buffer_read_from (VALUE self , VALUE io );
6565static VALUE Coolio_Buffer_write_to (VALUE self , VALUE io );
6666
67- static struct buffer * buffer_new ( void );
67+ static struct buffer * buffer_init ( struct buffer * );
6868static void buffer_clear (struct buffer * buf );
6969static void buffer_free (struct buffer * buf );
7070static void buffer_free_pool (struct buffer * buf );
@@ -86,8 +86,6 @@ static int buffer_write_to(struct buffer * buf, int fd);
8686void
8787Init_coolio_buffer ()
8888{
89- VALUE cCoolio_IO ;
90-
9189 mCoolio = rb_define_module ("Coolio" );
9290 cCoolio_Buffer = rb_define_class_under (mCoolio , "Buffer" , rb_cObject );
9391 rb_define_alloc_func (cCoolio_Buffer , Coolio_Buffer_allocate );
@@ -114,21 +112,33 @@ Init_coolio_buffer()
114112 rb_define_const (cCoolio_Buffer , "MAX_SIZE" , INT2NUM (MAX_BUFFER_SIZE ));
115113}
116114
115+ static const rb_data_type_t Coolio_Buffer_type = {
116+ "Coolio::Buffer" ,
117+ {
118+ Coolio_Buffer_mark ,
119+ Coolio_Buffer_free ,
120+ }
121+ };
122+
117123static VALUE
118124Coolio_Buffer_allocate (VALUE klass )
119125{
120- return Data_Wrap_Struct (klass , Coolio_Buffer_mark , Coolio_Buffer_free , buffer_new ());
126+ struct buffer * buf ;
127+ VALUE buffer = TypedData_Make_Struct (klass , struct buffer , & Coolio_Buffer_type , buf );
128+
129+ buffer_init (buf );
130+ return buffer ;
121131}
122132
123133static void
124- Coolio_Buffer_mark (struct buffer * buf )
134+ Coolio_Buffer_mark (void * buf )
125135{
126136 /* Naively discard the memory pool whenever Ruby garbage collects */
127137 buffer_free_pool (buf );
128138}
129139
130140static void
131- Coolio_Buffer_free (struct buffer * buf )
141+ Coolio_Buffer_free (void * buf )
132142{
133143 buffer_free (buf );
134144}
@@ -188,7 +198,7 @@ Coolio_Buffer_initialize(int argc, VALUE * argv, VALUE self)
188198 struct buffer * buf ;
189199
190200 if (rb_scan_args (argc , argv , "01" , & node_size_obj ) == 1 ) {
191- Data_Get_Struct (self , struct buffer , buf );
201+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
192202
193203 /*
194204 * Make sure we're not changing the buffer size after data
@@ -212,7 +222,7 @@ static VALUE
212222Coolio_Buffer_clear (VALUE self )
213223{
214224 struct buffer * buf ;
215- Data_Get_Struct (self , struct buffer , buf );
225+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
216226
217227 buffer_clear (buf );
218228
@@ -229,7 +239,7 @@ static VALUE
229239Coolio_Buffer_size (VALUE self )
230240{
231241 struct buffer * buf ;
232- Data_Get_Struct (self , struct buffer , buf );
242+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
233243
234244 return INT2NUM (buf -> size );
235245}
@@ -244,7 +254,7 @@ static VALUE
244254Coolio_Buffer_empty (VALUE self )
245255{
246256 struct buffer * buf ;
247- Data_Get_Struct (self , struct buffer , buf );
257+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
248258
249259 return buf -> size > 0 ? Qfalse : Qtrue ;
250260}
@@ -259,7 +269,7 @@ static VALUE
259269Coolio_Buffer_append (VALUE self , VALUE data )
260270{
261271 struct buffer * buf ;
262- Data_Get_Struct (self , struct buffer , buf );
272+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
263273
264274 /* Is this needed? Never seen anyone else do it... */
265275 data = rb_convert_type (data , T_STRING , "String" , "to_str" );
@@ -278,7 +288,7 @@ static VALUE
278288Coolio_Buffer_prepend (VALUE self , VALUE data )
279289{
280290 struct buffer * buf ;
281- Data_Get_Struct (self , struct buffer , buf );
291+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
282292
283293 data = rb_convert_type (data , T_STRING , "String" , "to_str" );
284294 buffer_prepend (buf , RSTRING_PTR (data ), RSTRING_LEN (data ));
@@ -304,7 +314,7 @@ Coolio_Buffer_read(int argc, VALUE * argv, VALUE self)
304314 int length ;
305315 struct buffer * buf ;
306316
307- Data_Get_Struct (self , struct buffer , buf );
317+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
308318
309319 if (rb_scan_args (argc , argv , "01" , & length_obj ) == 1 ) {
310320 length = NUM2INT (length_obj );
@@ -340,7 +350,7 @@ Coolio_Buffer_read_frame(VALUE self, VALUE data, VALUE mark)
340350 char mark_c = (char ) NUM2INT (mark );
341351 struct buffer * buf ;
342352
343- Data_Get_Struct (self , struct buffer , buf );
353+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
344354
345355 if (buffer_read_frame (buf , data , mark_c )) {
346356 return Qtrue ;
@@ -361,7 +371,7 @@ Coolio_Buffer_to_str(VALUE self)
361371 VALUE str ;
362372 struct buffer * buf ;
363373
364- Data_Get_Struct (self , struct buffer , buf );
374+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
365375
366376 str = rb_str_new (0 , buf -> size );
367377 buffer_copy (buf , RSTRING_PTR (str ), buf -> size );
@@ -388,7 +398,7 @@ Coolio_Buffer_read_from(VALUE self, VALUE io)
388398 OpenFile * fptr ;
389399#endif
390400
391- Data_Get_Struct (self , struct buffer , buf );
401+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
392402 io = rb_convert_type (io , T_FILE , "IO" , "to_io" );
393403 GetOpenFile (io , fptr );
394404 rb_io_set_nonblock (fptr );
@@ -419,7 +429,7 @@ Coolio_Buffer_write_to(VALUE self, VALUE io)
419429 OpenFile * fptr ;
420430#endif
421431
422- Data_Get_Struct (self , struct buffer , buf );
432+ TypedData_Get_Struct (self , struct buffer , & Coolio_Buffer_type , buf );
423433 io = rb_convert_type (io , T_FILE , "IO" , "to_io" );
424434 GetOpenFile (io , fptr );
425435 rb_io_set_nonblock (fptr );
@@ -438,11 +448,8 @@ Coolio_Buffer_write_to(VALUE self, VALUE io)
438448
439449/* Create a new buffer */
440450static struct buffer *
441- buffer_new ( void )
451+ buffer_init ( struct buffer * buf )
442452{
443- struct buffer * buf ;
444-
445- buf = (struct buffer * ) xmalloc (sizeof (struct buffer ));
446453 buf -> head = buf -> tail = buf -> pool_head = buf -> pool_tail = 0 ;
447454 buf -> size = 0 ;
448455 buf -> node_size = default_node_size ;
0 commit comments