@@ -31,9 +31,13 @@ static rb_encoding *binaryEncoding;
31
31
#define MYSQL_TYPE_JSON 245
32
32
#endif
33
33
34
+ #ifndef NEW_TYPEDDATA_WRAPPER
35
+ #define TypedData_Get_Struct (obj , type , ignore , sval ) Data_Get_Struct(obj, type, sval)
36
+ #endif
37
+
34
38
#define GET_RESULT (self ) \
35
39
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);
37
41
38
42
typedef struct {
39
43
int symbolizeKeys ;
@@ -61,11 +65,11 @@ static VALUE sym_symbolize_keys, sym_as, sym_array, sym_database_timezone,
61
65
static void rb_mysql_result_mark (void * wrapper ) {
62
66
mysql2_result_wrapper * w = wrapper ;
63
67
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 );
69
73
}
70
74
}
71
75
@@ -127,6 +131,46 @@ static void rb_mysql_result_free(void *ptr) {
127
131
xfree (wrapper );
128
132
}
129
133
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
+
130
174
static VALUE rb_mysql_result_free_ (VALUE self ) {
131
175
GET_RESULT (self );
132
176
rb_mysql_result_free_result (wrapper );
@@ -358,7 +402,7 @@ static VALUE mysql2_set_field_string_encoding(VALUE val, MYSQL_FIELD field, rb_e
358
402
int enc_index ;
359
403
360
404
enc_name = (field .charsetnr - 1 < MYSQL2_CHARSETNR_SIZE ) ? mysql2_mysql_enc_to_rb [field .charsetnr - 1 ] : NULL ;
361
-
405
+
362
406
if (enc_name != NULL ) {
363
407
/* use the field encoding we were able to match */
364
408
enc_index = rb_enc_find_index (enc_name );
@@ -1122,7 +1166,11 @@ VALUE rb_mysql_result_to_obj(VALUE client, VALUE encoding, VALUE options, MYSQL_
1122
1166
VALUE obj ;
1123
1167
mysql2_result_wrapper * wrapper ;
1124
1168
1169
+ #ifdef NEW_TYPEDDATA_WRAPPER
1170
+ obj = TypedData_Make_Struct (cMysql2Result , mysql2_result_wrapper , & rb_mysql_result_type , wrapper );
1171
+ #else
1125
1172
obj = Data_Make_Struct (cMysql2Result , mysql2_result_wrapper , rb_mysql_result_mark , rb_mysql_result_free , wrapper );
1173
+ #endif
1126
1174
wrapper -> numberOfFields = 0 ;
1127
1175
wrapper -> numberOfRows = 0 ;
1128
1176
wrapper -> lastRowProcessed = 0 ;
@@ -1168,7 +1216,7 @@ void init_mysql2_result() {
1168
1216
1169
1217
cMysql2Result = rb_define_class_under (mMysql2 , "Result" , rb_cObject );
1170
1218
rb_global_variable (& cMysql2Result );
1171
-
1219
+
1172
1220
rb_define_method (cMysql2Result , "each" , rb_mysql_result_each , -1 );
1173
1221
rb_define_method (cMysql2Result , "fields" , rb_mysql_result_fetch_fields , 0 );
1174
1222
rb_define_method (cMysql2Result , "field_types" , rb_mysql_result_fetch_field_types , 0 );
0 commit comments