Skip to content

Commit

Permalink
Merge pull request #393 from larskanis/fatkodima-gc-compaction-followup
Browse files Browse the repository at this point in the history
Add memsize callbacks and improve callback definition
  • Loading branch information
larskanis authored Jul 29, 2021
2 parents 5e0f9ae + f59d11a commit 1e7c5b4
Show file tree
Hide file tree
Showing 24 changed files with 259 additions and 85 deletions.
9 changes: 5 additions & 4 deletions ext/pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ typedef long suseconds_t;
#endif

#ifdef HAVE_RB_GC_MARK_MOVABLE
#define pg_compact_callback(x) ((void (*)(void*))(x))
#define pg_compact_callback(x) (x)
#define pg_gc_location(x) x = rb_gc_location(x)
#else
#define rb_gc_mark_movable(x) rb_gc_mark(x)
Expand Down Expand Up @@ -315,7 +315,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_compact _(( t_pg_coder * ));
void pg_coder_compact _(( void * ));
char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));

#define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
Expand All @@ -335,8 +335,9 @@ int pg_typemap_fit_to_copy_get _(( VALUE ));
VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
VALUE pg_typemap_typecast_copy_get _(( t_typemap *, VALUE, int, int, int ));
void pg_typemap_mark _(( t_typemap * ));
void pg_typemap_compact _(( t_typemap * ));
void pg_typemap_mark _(( void * ));
size_t pg_typemap_memsize _(( const void * ));
void pg_typemap_compact _(( void * ));

PGconn *pg_get_pgconn _(( VALUE ));
t_pg_connection *pg_get_connection _(( VALUE ));
Expand Down
36 changes: 26 additions & 10 deletions ext/pg_coder.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,40 @@ pg_coder_init_decoder( VALUE self )
rb_iv_set( self, "@name", Qnil );
}

static size_t
pg_coder_memsize(const void *_this)
{
const t_pg_coder *this = (const t_pg_coder *)_this;
return sizeof(*this);
}

static size_t
pg_composite_coder_memsize(const void *_this)
{
const t_pg_composite_coder *this = (const t_pg_composite_coder *)_this;
return sizeof(*this);
}

void
pg_coder_compact(t_pg_coder *this)
pg_coder_compact(void *_this)
{
t_pg_coder *this = (t_pg_coder *)_this;
pg_gc_location(this->coder_obj);
}

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

const rb_data_type_t pg_coder_type = {
"PG::Coder",
{
(void (*)(void*))NULL,
(void (*)(void*))-1,
(size_t (*)(const void *))NULL,
(RUBY_DATA_FUNC) NULL,
RUBY_TYPED_DEFAULT_FREE,
pg_coder_memsize,
pg_compact_callback(pg_coder_compact),
},
0,
Expand All @@ -98,9 +114,9 @@ pg_simple_encoder_allocate( VALUE klass )
static const rb_data_type_t pg_composite_coder_type = {
"PG::CompositeCoder",
{
(void (*)(void*))NULL,
(void (*)(void*))-1,
(size_t (*)(const void *))NULL,
(RUBY_DATA_FUNC) NULL,
RUBY_TYPED_DEFAULT_FREE,
pg_composite_coder_memsize,
pg_compact_callback(pg_composite_coder_compact),
},
&pg_coder_type,
Expand Down Expand Up @@ -430,8 +446,8 @@ pg_coder_elements_type_set(VALUE self, VALUE elem_type)
static const rb_data_type_t pg_coder_cfunc_type = {
"PG::Coder::CFUNC",
{
(void (*)(void*))NULL,
(void (*)(void*))NULL,
(RUBY_DATA_FUNC)NULL,
(RUBY_DATA_FUNC)NULL,
(size_t (*)(const void *))NULL,
},
0,
Expand Down
36 changes: 25 additions & 11 deletions ext/pg_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ static const char *pg_cstr_enc(VALUE str, int enc_idx){
* GC Mark function
*/
static void
pgconn_gc_mark( t_pg_connection *this )
pgconn_gc_mark( void *_this )
{
t_pg_connection *this = (t_pg_connection *)_this;
rb_gc_mark_movable( this->socket_io );
rb_gc_mark_movable( this->notice_receiver );
rb_gc_mark_movable( this->notice_processor );
Expand All @@ -159,8 +160,9 @@ pgconn_gc_mark( t_pg_connection *this )
}

static void
pgconn_gc_compact( t_pg_connection *this )
pgconn_gc_compact( void *_this )
{
t_pg_connection *this = (t_pg_connection *)_this;
pg_gc_location( this->socket_io );
pg_gc_location( this->notice_receiver );
pg_gc_location( this->notice_processor );
Expand All @@ -176,8 +178,9 @@ pgconn_gc_compact( t_pg_connection *this )
* GC Free function
*/
static void
pgconn_gc_free( t_pg_connection *this )
pgconn_gc_free( void *_this )
{
t_pg_connection *this = (t_pg_connection *)_this;
#if defined(_WIN32)
if ( RTEST(this->socket_io) )
rb_w32_unwrap_io_handle( this->ruby_sd );
Expand All @@ -188,12 +191,22 @@ pgconn_gc_free( t_pg_connection *this )
xfree(this);
}

/*
* Object Size function
*/
static size_t
pgconn_memsize( const void *_this )
{
const t_pg_connection *this = (const t_pg_connection *)_this;
return sizeof(*this);
}

static const rb_data_type_t pg_connection_type = {
"PG::Connection",
{
(void (*)(void*))pgconn_gc_mark,
(void (*)(void*))pgconn_gc_free,
(size_t (*)(const void *))NULL,
pgconn_gc_mark,
pgconn_gc_free,
pgconn_memsize,
pg_compact_callback(pgconn_gc_compact),
},
0,
Expand Down Expand Up @@ -1078,8 +1091,9 @@ struct query_params_data {
};

static void
free_typecast_heap_chain(struct linked_typecast_data *chain_entry)
free_typecast_heap_chain(void *_chain_entry)
{
struct linked_typecast_data *chain_entry = (struct linked_typecast_data *)_chain_entry;
while(chain_entry){
struct linked_typecast_data *next = chain_entry->next;
xfree(chain_entry);
Expand All @@ -1090,8 +1104,8 @@ free_typecast_heap_chain(struct linked_typecast_data *chain_entry)
static const rb_data_type_t pg_typecast_buffer_type = {
"PG::Connection typecast buffer chain",
{
(void (*)(void*))NULL,
(void (*)(void*))free_typecast_heap_chain,
(RUBY_DATA_FUNC) NULL,
free_typecast_heap_chain,
(size_t (*)(const void *))NULL,
},
0,
Expand Down Expand Up @@ -1123,8 +1137,8 @@ alloc_typecast_buf( VALUE *typecast_heap_chain, int len )
static const rb_data_type_t pg_query_heap_pool_type = {
"PG::Connection query heap pool",
{
(void (*)(void*))NULL,
(void (*)(void*))-1,
(RUBY_DATA_FUNC) NULL,
RUBY_TYPED_DEFAULT_FREE,
(size_t (*)(const void *))NULL,
},
0,
Expand Down
19 changes: 14 additions & 5 deletions ext/pg_copy_coder.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ typedef struct {


static void
pg_copycoder_mark( t_pg_copycoder *this )
pg_copycoder_mark( void *_this )
{
t_pg_copycoder *this = (t_pg_copycoder *)_this;
rb_gc_mark_movable(this->typemap);
rb_gc_mark_movable(this->null_string);
}

static size_t
pg_copycoder_memsize( const void *_this )
{
const t_pg_copycoder *this = (const t_pg_copycoder *)_this;
return sizeof(*this);
}

static void
pg_copycoder_compact( t_pg_copycoder *this )
pg_copycoder_compact( void *_this )
{
t_pg_copycoder *this = (t_pg_copycoder *)_this;
pg_coder_compact(&this->comp);
pg_gc_location(this->typemap);
pg_gc_location(this->null_string);
Expand All @@ -38,9 +47,9 @@ pg_copycoder_compact( t_pg_copycoder *this )
static const rb_data_type_t pg_copycoder_type = {
"PG::CopyCoder",
{
(void (*)(void*))pg_copycoder_mark,
(void (*)(void*))-1,
(size_t (*)(const void *))NULL,
pg_copycoder_mark,
RUBY_TYPED_DEFAULT_FREE,
pg_copycoder_memsize,
pg_compact_callback(pg_copycoder_compact),
},
&pg_coder_type,
Expand Down
19 changes: 14 additions & 5 deletions ext/pg_record_coder.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,33 @@ typedef struct {


static void
pg_recordcoder_mark( t_pg_recordcoder *this )
pg_recordcoder_mark( void *_this )
{
t_pg_recordcoder *this = (t_pg_recordcoder *)_this;
rb_gc_mark_movable(this->typemap);
}

static size_t
pg_recordcoder_memsize( const void *_this )
{
const t_pg_recordcoder *this = (const t_pg_recordcoder *)_this;
return sizeof(*this);
}

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

static const rb_data_type_t pg_recordcoder_type = {
"PG::RecordCoder",
{
(void (*)(void*))pg_recordcoder_mark,
(void (*)(void*))-1,
(size_t (*)(const void *))NULL,
pg_recordcoder_mark,
RUBY_TYPED_DEFAULT_FREE,
pg_recordcoder_memsize,
pg_compact_callback(pg_recordcoder_compact),
},
&pg_coder_type,
Expand Down
21 changes: 13 additions & 8 deletions ext/pg_result.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ pgresult_approx_size(const PGresult *result)
* GC Mark function
*/
static void
pgresult_gc_mark( t_pg_result *this )
pgresult_gc_mark( void *_this )
{
t_pg_result *this = (t_pg_result *)_this;
int i;

rb_gc_mark_movable( this->connection );
Expand All @@ -122,8 +123,9 @@ pgresult_gc_mark( t_pg_result *this )
}

static void
pgresult_gc_compact( t_pg_result *this )
pgresult_gc_compact( void *_this )
{
t_pg_result *this = (t_pg_result *)_this;
int i;

pg_gc_location( this->connection );
Expand All @@ -140,8 +142,9 @@ pgresult_gc_compact( t_pg_result *this )
* GC Free function
*/
static void
pgresult_clear( t_pg_result *this )
pgresult_clear( void *_this )
{
t_pg_result *this = (t_pg_result *)_this;
if( this->pgresult && !this->autoclear ){
PQclear(this->pgresult);
#ifdef HAVE_RB_GC_ADJUST_MEMORY_USAGE
Expand All @@ -154,15 +157,17 @@ pgresult_clear( t_pg_result *this )
}

static void
pgresult_gc_free( t_pg_result *this )
pgresult_gc_free( void *_this )
{
t_pg_result *this = (t_pg_result *)_this;
pgresult_clear( this );
xfree(this);
}

static size_t
pgresult_memsize( t_pg_result *this )
pgresult_memsize( const void *_this )
{
const t_pg_result *this = (const t_pg_result *)_this;
/* Ideally the memory 'this' is pointing to should be taken into account as well.
* However we don't want to store two memory sizes in t_pg_result just for reporting by ObjectSpace.memsize_of.
*/
Expand All @@ -172,9 +177,9 @@ pgresult_memsize( t_pg_result *this )
static const rb_data_type_t pgresult_type = {
"PG::Result",
{
(void (*)(void*))pgresult_gc_mark,
(void (*)(void*))pgresult_gc_free,
(size_t (*)(const void *))pgresult_memsize,
pgresult_gc_mark,
pgresult_gc_free,
pgresult_memsize,
pg_compact_callback(pgresult_gc_compact),
},
0, 0,
Expand Down
18 changes: 11 additions & 7 deletions ext/pg_tuple.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ pg_tuple_get_field_names( t_pg_tuple *this )
}

static void
pg_tuple_gc_mark( t_pg_tuple *this )
pg_tuple_gc_mark( void *_this )
{
t_pg_tuple *this = (t_pg_tuple *)_this;
int i;

if( !this ) return;
Expand All @@ -86,8 +87,9 @@ pg_tuple_gc_mark( t_pg_tuple *this )
}

static void
pg_tuple_gc_compact( t_pg_tuple *this )
pg_tuple_gc_compact( void *_this )
{
t_pg_tuple *this = (t_pg_tuple *)_this;
int i;

if( !this ) return;
Expand All @@ -102,25 +104,27 @@ pg_tuple_gc_compact( t_pg_tuple *this )
}

static void
pg_tuple_gc_free( t_pg_tuple *this )
pg_tuple_gc_free( void *_this )
{
t_pg_tuple *this = (t_pg_tuple *)_this;
if( !this ) return;
xfree(this);
}

static size_t
pg_tuple_memsize( t_pg_tuple *this )
pg_tuple_memsize( const void *_this )
{
const t_pg_tuple *this = (const t_pg_tuple *)_this;
if( this==NULL ) return 0;
return sizeof(*this) + sizeof(*this->values) * this->num_fields;
}

static const rb_data_type_t pg_tuple_type = {
"PG::Tuple",
{
(void (*)(void*))pg_tuple_gc_mark,
(void (*)(void*))pg_tuple_gc_free,
(size_t (*)(const void *))pg_tuple_memsize,
pg_tuple_gc_mark,
pg_tuple_gc_free,
pg_tuple_memsize,
pg_compact_callback(pg_tuple_gc_compact),
},
0, 0,
Expand Down
Loading

0 comments on commit 1e7c5b4

Please sign in to comment.