Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: namespace nodes behave with compaction #2658

Merged
merged 6 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions ext/nokogiri/html4_sax_parser_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ VALUE cNokogiriHtml4SaxParserContext ;
static void
deallocate(xmlParserCtxtPtr ctxt)
{
NOKOGIRI_DEBUG_START(ctxt);

ctxt->sax = NULL;

htmlFreeParserCtxt(ctxt);

NOKOGIRI_DEBUG_END(ctxt);
}

static VALUE
Expand Down
9 changes: 1 addition & 8 deletions ext/nokogiri/nokogiri.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,6 @@ xmlNodePtr xmlLastElementChild(xmlNodePtr parent);
#define NOKOGIRI_STR_NEW(str, len) rb_external_str_new_with_enc((const char *)(str), (long)(len), rb_utf8_encoding())
#define RBSTR_OR_QNIL(_str) (_str ? NOKOGIRI_STR_NEW2(_str) : Qnil)

#ifdef DEBUG
# define NOKOGIRI_DEBUG_START(p) if (getenv("NOKOGIRI_NO_FREE")) return ; if (getenv("NOKOGIRI_DEBUG")) fprintf(stderr,"nokogiri: %s:%d %p start\n", __FILE__, __LINE__, p);
# define NOKOGIRI_DEBUG_END(p) if (getenv("NOKOGIRI_DEBUG")) fprintf(stderr,"nokogiri: %s:%d %p end\n", __FILE__, __LINE__, p);
#else
# define NOKOGIRI_DEBUG_START(p)
# define NOKOGIRI_DEBUG_END(p)
#endif

#ifndef NORETURN_DECL
# if defined(__GNUC__)
# define NORETURN_DECL __attribute__ ((noreturn))
Expand Down Expand Up @@ -181,6 +173,7 @@ int noko_io_write(void *ctx, char *buffer, int len);
int noko_io_close(void *ctx);

#define Noko_Node_Get_Struct(obj,type,sval) ((sval) = (type*)DATA_PTR(obj))
#define Noko_Namespace_Get_Struct(obj,type,sval) ((sval) = (type*)DATA_PTR(obj))

VALUE noko_xml_node_wrap(VALUE klass, xmlNodePtr node) ;
VALUE noko_xml_node_wrap_node_set_result(xmlNodePtr node, VALUE node_set) ;
Expand Down
10 changes: 5 additions & 5 deletions ext/nokogiri/xml_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ dealloc(xmlDocPtr doc)
{
st_table *node_hash;

NOKOGIRI_DEBUG_START(doc);

node_hash = DOC_UNLINKED_NODE_HASH(doc);

st_foreach(node_hash, dealloc_node_i, (st_data_t)doc);
Expand All @@ -84,8 +82,6 @@ dealloc(xmlDocPtr doc)
}

xmlFreeDoc(doc);

NOKOGIRI_DEBUG_END(doc);
}

static void
Expand All @@ -104,7 +100,11 @@ recursively_remove_namespaces_from_node(xmlNodePtr node)
(node->type == XML_XINCLUDE_START) ||
(node->type == XML_XINCLUDE_END)) &&
node->nsDef) {
xmlFreeNsList(node->nsDef);
xmlNsPtr curr = node->nsDef;
while (curr) {
noko_xml_document_pin_namespace(curr, node->doc);
curr = curr->next;
}
node->nsDef = NULL;
}

Expand Down
48 changes: 41 additions & 7 deletions ext/nokogiri/xml_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,53 @@
VALUE cNokogiriXmlNamespace ;

static void
dealloc_namespace(xmlNsPtr ns)
_xml_namespace_dealloc(void *ptr)
{
/*
* this deallocator is only used for namespace nodes that are part of an xpath
* node set. see noko_xml_namespace_wrap().
*/
NOKOGIRI_DEBUG_START(ns) ;
xmlNsPtr ns = ptr;

if (ns->href) {
xmlFree(DISCARD_CONST_QUAL_XMLCHAR(ns->href));
}
if (ns->prefix) {
xmlFree(DISCARD_CONST_QUAL_XMLCHAR(ns->prefix));
}
xmlFree(ns);
NOKOGIRI_DEBUG_END(ns) ;
}

#ifdef HAVE_RB_GC_LOCATION
static void
_xml_namespace_update_references(void *ptr)
{
xmlNsPtr ns = ptr;
if (ns->_private) {
ns->_private = (void *)rb_gc_location((VALUE)ns->_private);
}
}
#else
# define _xml_namespace_update_references 0
#endif

static const rb_data_type_t nokogiri_xml_namespace_type_with_dealloc = {
"Nokogiri/XMLNamespace/WithDealloc",
{0, _xml_namespace_dealloc, 0, _xml_namespace_update_references},
0, 0,
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
RUBY_TYPED_FREE_IMMEDIATELY,
#endif
};

static const rb_data_type_t nokogiri_xml_namespace_type_without_dealloc = {
"Nokogiri/XMLNamespace/WithoutDealloc",
{0, 0, 0, _xml_namespace_update_references},
0, 0,
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
RUBY_TYPED_FREE_IMMEDIATELY,
#endif
};

/*
* call-seq:
Expand All @@ -54,7 +84,7 @@ prefix(VALUE self)
{
xmlNsPtr ns;

Data_Get_Struct(self, xmlNs, ns);
Noko_Namespace_Get_Struct(self, xmlNs, ns);
if (!ns->prefix) { return Qnil; }

return NOKOGIRI_STR_NEW2(ns->prefix);
Expand All @@ -71,7 +101,7 @@ href(VALUE self)
{
xmlNsPtr ns;

Data_Get_Struct(self, xmlNs, ns);
Noko_Namespace_Get_Struct(self, xmlNs, ns);
if (!ns->href) { return Qnil; }

return NOKOGIRI_STR_NEW2(ns->href);
Expand All @@ -87,14 +117,18 @@ noko_xml_namespace_wrap(xmlNsPtr c_namespace, xmlDocPtr c_document)
}

if (c_document) {
rb_namespace = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, 0, c_namespace);
rb_namespace = TypedData_Wrap_Struct(cNokogiriXmlNamespace,
&nokogiri_xml_namespace_type_without_dealloc,
c_namespace);

if (DOC_RUBY_OBJECT_TEST(c_document)) {
rb_iv_set(rb_namespace, "@document", DOC_RUBY_OBJECT(c_document));
rb_ary_push(DOC_NODE_CACHE(c_document), rb_namespace);
}
} else {
rb_namespace = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, dealloc_namespace, c_namespace);
rb_namespace = TypedData_Wrap_Struct(cNokogiriXmlNamespace,
&nokogiri_xml_namespace_type_with_dealloc,
c_namespace);
}

c_namespace->_private = (void *)rb_namespace;
Expand Down
30 changes: 9 additions & 21 deletions ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@ static ID id_decorate, id_decorate_bang;

typedef xmlNodePtr(*pivot_reparentee_func)(xmlNodePtr, xmlNodePtr);

#ifdef DEBUG
static void
_xml_node_dealloc(xmlNodePtr x)
_xml_node_mark(void *ptr)
{
NOKOGIRI_DEBUG_START(x)
NOKOGIRI_DEBUG_END(x)
}
#else
# define _xml_node_dealloc 0
#endif
xmlNodePtr node = ptr;

static void
_xml_node_mark(xmlNodePtr node)
{
if (!DOC_RUBY_OBJECT_TEST(node->doc)) {
return;
}
Expand All @@ -39,24 +30,21 @@ _xml_node_mark(xmlNodePtr node)

#ifdef HAVE_RB_GC_LOCATION
static void
_xml_node_update_references(xmlNodePtr node)
_xml_node_update_references(void *ptr)
{
xmlNodePtr node = ptr;

if (node->_private) {
node->_private = (void *)rb_gc_location((VALUE)node->_private);
}
}
#else
# define _xml_node_update_references 0
#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,
#ifdef HAVE_RB_GC_LOCATION
(gc_callback_t)_xml_node_update_references
#endif
},
{_xml_node_mark, 0, 0, _xml_node_update_references},
0, 0,
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
RUBY_TYPED_FREE_IMMEDIATELY,
Expand Down Expand Up @@ -1355,7 +1343,7 @@ set_namespace(VALUE self, VALUE namespace)
Noko_Node_Get_Struct(self, xmlNode, node);

if (!NIL_P(namespace)) {
Data_Get_Struct(namespace, xmlNs, ns);
Noko_Namespace_Get_Struct(namespace, xmlNs, ns);
}

xmlSetNs(node, ns);
Expand Down
2 changes: 0 additions & 2 deletions ext/nokogiri/xml_node_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,11 @@ deallocate(xmlNodeSetPtr node_set)
* For reasons outlined in xml_namespace.c, here we reproduce xmlXPathFreeNodeSet() except for the
* offending call to xmlXPathNodeSetFreeNs().
*/
NOKOGIRI_DEBUG_START(node_set) ;
if (node_set->nodeTab != NULL) {
xmlFree(node_set->nodeTab);
}

xmlFree(node_set);
NOKOGIRI_DEBUG_END(node_set) ;
}


Expand Down
2 changes: 0 additions & 2 deletions ext/nokogiri/xml_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ VALUE cNokogiriXmlReader;
static void
dealloc(xmlTextReaderPtr reader)
{
NOKOGIRI_DEBUG_START(reader);
xmlFreeTextReader(reader);
NOKOGIRI_DEBUG_END(reader);
}

static int
Expand Down
2 changes: 0 additions & 2 deletions ext/nokogiri/xml_relax_ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ VALUE cNokogiriXmlRelaxNG;
static void
dealloc(xmlRelaxNGPtr schema)
{
NOKOGIRI_DEBUG_START(schema);
xmlRelaxNGFree(schema);
NOKOGIRI_DEBUG_END(schema);
}

/*
Expand Down
2 changes: 0 additions & 2 deletions ext/nokogiri/xml_sax_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ processing_instruction(void *ctx, const xmlChar *name, const xmlChar *content)
static void
deallocate(xmlSAXHandlerPtr handler)
{
NOKOGIRI_DEBUG_START(handler);
ruby_xfree(handler);
NOKOGIRI_DEBUG_END(handler);
}

static VALUE
Expand Down
5 changes: 0 additions & 5 deletions ext/nokogiri/xml_sax_parser_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ static ID id_read;
static void
deallocate(xmlParserCtxtPtr ctxt)
{
NOKOGIRI_DEBUG_START(ctxt);

ctxt->sax = NULL;

xmlFreeParserCtxt(ctxt);

NOKOGIRI_DEBUG_END(ctxt);
}

/*
Expand Down
2 changes: 0 additions & 2 deletions ext/nokogiri/xml_sax_push_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ VALUE cNokogiriXmlSaxPushParser ;
static void
deallocate(xmlParserCtxtPtr ctx)
{
NOKOGIRI_DEBUG_START(ctx);
if (ctx != NULL) {
NOKOGIRI_SAX_TUPLE_DESTROY(ctx->userData);
xmlFreeParserCtxt(ctx);
}
NOKOGIRI_DEBUG_END(ctx);
}

static VALUE
Expand Down
2 changes: 0 additions & 2 deletions ext/nokogiri/xml_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ VALUE cNokogiriXmlSchema;
static void
dealloc(xmlSchemaPtr schema)
{
NOKOGIRI_DEBUG_START(schema);
xmlSchemaFree(schema);
NOKOGIRI_DEBUG_END(schema);
}

/*
Expand Down
2 changes: 0 additions & 2 deletions ext/nokogiri/xml_xpath_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ static const xmlChar *NOKOGIRI_BUILTIN_URI = (const xmlChar *)"https://www.nokog
static void
xml_xpath_context_deallocate(xmlXPathContextPtr ctx)
{
NOKOGIRI_DEBUG_START(ctx);
xmlXPathFreeContext(ctx);
NOKOGIRI_DEBUG_END(ctx);
}

/* find a CSS class in an HTML element's `class` attribute */
Expand Down
6 changes: 1 addition & 5 deletions ext/nokogiri/xslt_stylesheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ static void
dealloc(nokogiriXsltStylesheetTuple *wrapper)
{
xsltStylesheetPtr doc = wrapper->ss;

NOKOGIRI_DEBUG_START(doc);
xsltFreeStylesheet(doc); /* commented out for now. */
NOKOGIRI_DEBUG_END(doc);

xsltFreeStylesheet(doc);
ruby_xfree(wrapper);
}

Expand Down
7 changes: 0 additions & 7 deletions rakelib/debug.rake
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# frozen_string_literal: true

desc "Set environment variables to build and/or test with debug options"
task :debug do
ENV["NOKOGIRI_DEBUG"] = "true"
ENV["CFLAGS"] ||= ""
ENV["CFLAGS"] += " -DDEBUG"
end

task :java_debug do # rubocop:disable Rake/Desc
ENV["JRUBY_OPTS"] = "#{ENV["JRUBY_OPTS"]} --debug --dev"
if ENV["JAVA_DEBUG"]
Expand Down