Skip to content

Commit

Permalink
Make PG::Coder and derivations friendly to GC.compact
Browse files Browse the repository at this point in the history
This way the VALUE references may be relocated.
Since self is always referenced indirectly, marking simple coders can be omitted entirely.

This partly reverts 7c1756f which was introduced as a simple fix for GC.compact compatbility.
  • Loading branch information
larskanis committed Aug 9, 2020
1 parent 3e8d343 commit a24a64e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
have_func 'PQresultMemorySize' # since PostgreSQL-12
have_func 'timegm'
have_func 'rb_gc_adjust_memory_usage' # since ruby-2.4
have_func 'rb_gc_mark_movable' # since ruby-2.7

# unistd.h confilicts with ruby/win32.h when cross compiling for win32 and ruby 1.9.1
have_header 'unistd.h'
Expand Down
10 changes: 9 additions & 1 deletion ext/pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ typedef long suseconds_t;
#define RARRAY_AREF(a, i) (RARRAY_PTR(a)[i])
#endif

#ifdef HAVE_RB_GC_MARK_MOVABLE
#define pg_compact_callback(x) ((void (*)(void*))(x))
#else
#define rb_gc_mark_movable(x) rb_gc_mark(x)
#define rb_gc_location(x) (x)
#define pg_compact_callback(x) {(x)}
#endif

#define PG_ENC_IDX_BITS 28

/* The data behind each PG::Connection object */
Expand Down Expand Up @@ -306,7 +314,7 @@ VALUE pg_obj_to_i _(( VALUE ));
VALUE pg_tmbc_allocate _(( void ));
void pg_coder_init_encoder _(( VALUE ));
void pg_coder_init_decoder _(( VALUE ));
void pg_coder_mark _(( t_pg_coder * ));
void pg_coder_compact _(( t_pg_coder * ));
char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));

#define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
Expand Down
14 changes: 8 additions & 6 deletions ext/pg_coder.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,24 @@ pg_coder_init_decoder( VALUE self )
}

void
pg_coder_mark(t_pg_coder *this)
pg_coder_compact(t_pg_coder *this)
{
rb_gc_mark(this->coder_obj);
this->coder_obj = rb_gc_location(this->coder_obj);
}

static void
pg_composite_coder_mark(t_pg_composite_coder *this)
pg_composite_coder_compact(t_pg_composite_coder *this)
{
pg_coder_mark(&this->comp);
pg_coder_compact(&this->comp);
}

const rb_data_type_t pg_coder_type = {
"PG::Coder",
{
(void (*)(void*))pg_coder_mark,
(void (*)(void*))NULL,
(void (*)(void*))-1,
(size_t (*)(const void *))NULL,
pg_compact_callback(pg_coder_compact),
},
0,
0,
Expand All @@ -99,9 +100,10 @@ pg_simple_encoder_allocate( VALUE klass )
static const rb_data_type_t pg_composite_coder_type = {
"PG::CompositeCoder",
{
(void (*)(void*))pg_composite_coder_mark,
(void (*)(void*))NULL,
(void (*)(void*))-1,
(size_t (*)(const void *))NULL,
pg_compact_callback(pg_composite_coder_compact),
},
&pg_coder_type,
0,
Expand Down
14 changes: 11 additions & 3 deletions ext/pg_copy_coder.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ typedef struct {
static void
pg_copycoder_mark( t_pg_copycoder *this )
{
pg_coder_mark(&this->comp);
rb_gc_mark(this->typemap);
rb_gc_mark(this->null_string);
rb_gc_mark_movable(this->typemap);
rb_gc_mark_movable(this->null_string);
}

static void
pg_copycoder_compact( t_pg_copycoder *this )
{
pg_coder_compact(&this->comp);
this->typemap = rb_gc_location(this->typemap);
this->null_string = rb_gc_location(this->null_string);
}

static const rb_data_type_t pg_copycoder_type = {
Expand All @@ -34,6 +41,7 @@ static const rb_data_type_t pg_copycoder_type = {
(void (*)(void*))pg_copycoder_mark,
(void (*)(void*))-1,
(size_t (*)(const void *))NULL,
pg_compact_callback(pg_copycoder_compact),
},
&pg_coder_type,
0,
Expand Down
11 changes: 9 additions & 2 deletions ext/pg_record_coder.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ typedef struct {
static void
pg_recordcoder_mark( t_pg_recordcoder *this )
{
pg_coder_mark(&this->comp);
rb_gc_mark(this->typemap);
rb_gc_mark_movable(this->typemap);
}

static void
pg_recordcoder_compact( t_pg_recordcoder *this )
{
pg_coder_compact(&this->comp);
this->typemap = rb_gc_location(this->typemap);
}

static const rb_data_type_t pg_recordcoder_type = {
Expand All @@ -28,6 +34,7 @@ static const rb_data_type_t pg_recordcoder_type = {
(void (*)(void*))pg_recordcoder_mark,
(void (*)(void*))-1,
(size_t (*)(const void *))NULL,
pg_compact_callback(pg_recordcoder_compact),
},
&pg_coder_type,
0,
Expand Down

0 comments on commit a24a64e

Please sign in to comment.