@@ -31,9 +31,13 @@ static rb_encoding *binaryEncoding;
3131#define MYSQL_TYPE_JSON 245
3232#endif
3333
34+ #ifndef NEW_TYPEDDATA_WRAPPER
35+ #define TypedData_Get_Struct (obj , type , ignore , sval ) Data_Get_Struct(obj, type, sval)
36+ #endif
37+
3438#define GET_RESULT (self ) \
3539 mysql2_result_wrapper *wrapper; \
36- Data_Get_Struct (self, mysql2_result_wrapper, wrapper);
40+ TypedData_Get_Struct (self, mysql2_result_wrapper, &rb_mysql_result_type , wrapper);
3741
3842typedef struct {
3943 int symbolizeKeys ;
@@ -61,11 +65,11 @@ static VALUE sym_symbolize_keys, sym_as, sym_array, sym_database_timezone,
6165static void rb_mysql_result_mark (void * wrapper ) {
6266 mysql2_result_wrapper * w = wrapper ;
6367 if (w ) {
64- rb_gc_mark (w -> fields );
65- rb_gc_mark (w -> rows );
66- rb_gc_mark (w -> encoding );
67- rb_gc_mark (w -> client );
68- rb_gc_mark (w -> statement );
68+ rb_gc_mark_movable (w -> fields );
69+ rb_gc_mark_movable (w -> rows );
70+ rb_gc_mark_movable (w -> encoding );
71+ rb_gc_mark_movable (w -> client );
72+ rb_gc_mark_movable (w -> statement );
6973 }
7074}
7175
@@ -127,6 +131,46 @@ static void rb_mysql_result_free(void *ptr) {
127131 xfree (wrapper );
128132}
129133
134+ static size_t rb_mysql_result_memsize (const void * wrapper ) {
135+ const mysql2_result_wrapper * w = wrapper ;
136+ size_t memsize = sizeof (* w );
137+ if (w -> stmt_wrapper ) {
138+ memsize += sizeof (* w -> stmt_wrapper );
139+ }
140+ if (w -> client_wrapper ) {
141+ memsize += sizeof (* w -> client_wrapper );
142+ }
143+ return memsize ;
144+ }
145+
146+ static void rb_mysql_result_compact (void * wrapper ) {
147+ mysql2_result_wrapper * w = wrapper ;
148+ if (w ) {
149+ rb_mysql2_gc_location (w -> fields );
150+ rb_mysql2_gc_location (w -> rows );
151+ rb_mysql2_gc_location (w -> encoding );
152+ rb_mysql2_gc_location (w -> client );
153+ rb_mysql2_gc_location (w -> statement );
154+ }
155+ }
156+
157+ static const rb_data_type_t rb_mysql_result_type = {
158+ "rb_mysql_result" ,
159+ {
160+ rb_mysql_result_mark ,
161+ rb_mysql_result_free ,
162+ rb_mysql_result_memsize ,
163+ #ifdef HAVE_RB_GC_MARK_MOVABLE
164+ rb_mysql_result_compact ,
165+ #endif
166+ },
167+ 0 ,
168+ 0 ,
169+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
170+ RUBY_TYPED_FREE_IMMEDIATELY ,
171+ #endif
172+ };
173+
130174static VALUE rb_mysql_result_free_ (VALUE self ) {
131175 GET_RESULT (self );
132176 rb_mysql_result_free_result (wrapper );
@@ -365,7 +409,7 @@ static VALUE mysql2_set_field_string_encoding(VALUE val, MYSQL_FIELD field, rb_e
365409 int enc_index ;
366410
367411 enc_name = (field .charsetnr - 1 < MYSQL2_CHARSETNR_SIZE ) ? mysql2_mysql_enc_to_rb [field .charsetnr - 1 ] : NULL ;
368-
412+
369413 if (enc_name != NULL ) {
370414 /* use the field encoding we were able to match */
371415 enc_index = rb_enc_find_index (enc_name );
@@ -1129,7 +1173,11 @@ VALUE rb_mysql_result_to_obj(VALUE client, VALUE encoding, VALUE options, MYSQL_
11291173 VALUE obj ;
11301174 mysql2_result_wrapper * wrapper ;
11311175
1176+ #ifdef NEW_TYPEDDATA_WRAPPER
1177+ obj = TypedData_Make_Struct (cMysql2Result , mysql2_result_wrapper , & rb_mysql_result_type , wrapper );
1178+ #else
11321179 obj = Data_Make_Struct (cMysql2Result , mysql2_result_wrapper , rb_mysql_result_mark , rb_mysql_result_free , wrapper );
1180+ #endif
11331181 wrapper -> numberOfFields = 0 ;
11341182 wrapper -> numberOfRows = 0 ;
11351183 wrapper -> lastRowProcessed = 0 ;
@@ -1176,7 +1224,7 @@ void init_mysql2_result() {
11761224 cMysql2Result = rb_define_class_under (mMysql2 , "Result" , rb_cObject );
11771225 rb_undef_alloc_func (cMysql2Result );
11781226 rb_global_variable (& cMysql2Result );
1179-
1227+
11801228 rb_define_method (cMysql2Result , "each" , rb_mysql_result_each , -1 );
11811229 rb_define_method (cMysql2Result , "fields" , rb_mysql_result_fetch_fields , 0 );
11821230 rb_define_method (cMysql2Result , "field_types" , rb_mysql_result_fetch_field_types , 0 );
0 commit comments