Skip to content

Commit

Permalink
Define a memsize for noko_xml_document_data_type
Browse files Browse the repository at this point in the history
Co-authored-by: Jean Boussier <[email protected]>
  • Loading branch information
2 people authored and flavorjones committed Mar 3, 2023
1 parent cb1557d commit 9208336
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ext/nokogiri/xml_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,34 @@ dealloc(void *data)
xmlFreeDoc(doc);
}

static size_t
memsize_node(const xmlNodePtr node)
{
xmlNodePtr child;
size_t memsize = 0;
for (child = node->children; child; child = child->next) {
/* This should count the properties too. */
memsize += sizeof(xmlNode) + memsize_node(child);
}
return memsize;
}

static size_t
memsize(const void *data)
{
xmlDocPtr doc = (const xmlDocPtr)data;
size_t memsize = sizeof(xmlDoc);
/* This may not account for all memory use */
memsize += memsize_node((xmlNodePtr)doc);
return memsize;
}

const rb_data_type_t noko_xml_document_data_type = {
.wrap_struct_name = "Nokogiri::XML::Document",
.function = {
.dmark = mark,
.dfree = dealloc,
.dsize = memsize,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY
};
Expand Down

0 comments on commit 9208336

Please sign in to comment.