Skip to content

Commit

Permalink
Only add compaction callback on Rubies that support it
Browse files Browse the repository at this point in the history
We don't have `rb_gc_location` everywhere, so check for it in the
extconf and then conditionally set the compaction callback
  • Loading branch information
tenderlove authored and flavorjones committed Jul 10, 2022
1 parent ff29c7a commit c84a345
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions ext/nokogiri/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ def compile
have_func("xmlRelaxNGSetValidStructuredErrors") # introduced in libxml 2.6.21
have_func("xmlSchemaSetValidStructuredErrors") # introduced in libxml 2.6.23
have_func("xmlSchemaSetParserStructuredErrors") # introduced in libxml 2.6.23
have_func("rb_gc_location") # introduced in Ruby 2.7

other_library_versions_string = OTHER_LIBRARY_VERSIONS.map { |k, v| [k, v].join(":") }.join(",")
append_cppflags(%[-DNOKOGIRI_OTHER_LIBRARY_VERSIONS="\\\"#{other_library_versions_string}\\\""])
Expand Down
15 changes: 11 additions & 4 deletions ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,29 @@ _xml_node_mark(xmlNodePtr node)
}
}

#ifdef HAVE_RB_GC_LOCATION
static void
_xml_node_update_references(xmlNodePtr node)
{
if (node->_private) {
node->_private = (void *)rb_gc_location((VALUE)node->_private);
}
}
#endif

typedef void (*gc_callback_t)(void *);

static const rb_data_type_t nokogiri_node_type = {
"Nokogiri/XMLNode",
{(gc_callback_t)_xml_node_mark, (gc_callback_t)_xml_node_dealloc, 0, (gc_callback_t)_xml_node_update_references},
0, 0,
"Nokogiri/XMLNode",
{
(gc_callback_t)_xml_node_mark, (gc_callback_t)_xml_node_dealloc, 0,
#ifdef HAVE_RB_GC_LOCATION
(gc_callback_t)_xml_node_update_references
#endif
},
0, 0,
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
RUBY_TYPED_FREE_IMMEDIATELY,
RUBY_TYPED_FREE_IMMEDIATELY,
#endif
};

Expand Down

0 comments on commit c84a345

Please sign in to comment.