Skip to content

Commit 5061020

Browse files
committed
Add write barriers to PG::Result
1 parent c6ecc4a commit 5061020

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

ext/pg_result.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static const rb_data_type_t pgresult_type = {
183183
pg_compact_callback(pgresult_gc_compact),
184184
},
185185
0, 0,
186-
RUBY_TYPED_FREE_IMMEDIATELY,
186+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
187187
};
188188

189189
/* Needed by sequel_pg gem, do not delete */
@@ -208,14 +208,12 @@ pg_new_result2(PGresult *result, VALUE rb_pgconn)
208208

209209
this = (t_pg_result *)xmalloc(sizeof(*this) + sizeof(*this->fnames) * nfields);
210210
this->pgresult = result;
211-
this->connection = rb_pgconn;
212-
this->typemap = pg_typemap_all_strings;
213-
this->p_typemap = RTYPEDDATA_DATA( this->typemap );
214211
this->nfields = -1;
215212
this->tuple_hash = Qnil;
216213
this->field_map = Qnil;
217214
this->flags = 0;
218215
self = TypedData_Wrap_Struct(rb_cPGresult, &pgresult_type, this);
216+
RB_OBJ_WRITE(self, &this->connection, rb_pgconn);
219217

220218
if( result ){
221219
t_pg_connection *p_conn = pg_get_connection(rb_pgconn);
@@ -224,10 +222,13 @@ pg_new_result2(PGresult *result, VALUE rb_pgconn)
224222
t_typemap *p_typemap = RTYPEDDATA_DATA(typemap);
225223

226224
this->enc_idx = p_conn->enc_idx;
227-
this->typemap = p_typemap->funcs.fit_to_result( typemap, self );
225+
typemap = p_typemap->funcs.fit_to_result( typemap, self );
226+
RB_OBJ_WRITE(self, &this->typemap, typemap);
228227
this->p_typemap = RTYPEDDATA_DATA( this->typemap );
229228
this->flags = p_conn->flags;
230229
} else {
230+
RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
231+
this->p_typemap = RTYPEDDATA_DATA( this->typemap );
231232
this->enc_idx = rb_locale_encindex();
232233
}
233234

@@ -477,7 +478,8 @@ static void pgresult_init_fnames(VALUE self)
477478

478479
for( i=0; i<nfields; i++ ){
479480
char *cfname = PQfname(this->pgresult, i);
480-
this->fnames[i] = pg_cstr_to_sym(cfname, this->flags, this->enc_idx);
481+
VALUE fname = pg_cstr_to_sym(cfname, this->flags, this->enc_idx);
482+
RB_OBJ_WRITE(self, &this->fnames[i], fname);
481483
this->nfields = i + 1;
482484
}
483485
this->nfields = nfields;
@@ -1126,7 +1128,7 @@ pgresult_aref(VALUE self, VALUE index)
11261128
}
11271129
/* Store a copy of the filled hash for use at the next row. */
11281130
if( num_tuples > 10 )
1129-
this->tuple_hash = rb_hash_dup(tuple);
1131+
RB_OBJ_WRITE(self, &this->tuple_hash, rb_hash_dup(tuple));
11301132

11311133
return tuple;
11321134
}
@@ -1308,7 +1310,7 @@ static void ensure_init_for_tuple(VALUE self)
13081310
rb_hash_aset(field_map, this->fnames[i], INT2FIX(i));
13091311
}
13101312
rb_obj_freeze(field_map);
1311-
this->field_map = field_map;
1313+
RB_OBJ_WRITE(self, &this->field_map, field_map);
13121314
}
13131315
}
13141316

@@ -1399,7 +1401,7 @@ pgresult_type_map_set(VALUE self, VALUE typemap)
13991401
/* Check type of method param */
14001402
TypedData_Get_Struct(typemap, t_typemap, &pg_typemap_type, p_typemap);
14011403

1402-
this->typemap = p_typemap->funcs.fit_to_result( typemap, self );
1404+
RB_OBJ_WRITE(self, &this->typemap, p_typemap->funcs.fit_to_result( typemap, self ));
14031405
this->p_typemap = RTYPEDDATA_DATA( this->typemap );
14041406

14051407
return typemap;

0 commit comments

Comments
 (0)