From 69c379990d83d83682b23eac3ddf390c0d85e976 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 12 Oct 2016 14:45:23 +0200 Subject: Use pool allocator when parsing CSS. Fixes memory leaks when parsing throws exceptions and saves a lot of tiny mallocs for objects that have common life times. --- source/html/html-layout.c | 111 ++++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 47 deletions(-) (limited to 'source/html/html-layout.c') diff --git a/source/html/html-layout.c b/source/html/html-layout.c index 34d6f8ac..fb039061 100644 --- a/source/html/html-layout.c +++ b/source/html/html-layout.c @@ -112,7 +112,7 @@ struct genstate fz_tree *images; int is_fb2; const char *base_uri; - fz_css_rule *css; + fz_css *css; int at_bol; int emit_white; int last_brk_cls; @@ -498,8 +498,11 @@ static void fz_drop_html_box(fz_context *ctx, fz_html_box *box) void fz_drop_html(fz_context *ctx, fz_html *html) { - fz_drop_html_box(ctx, html->root); - fz_drop_pool(ctx, html->pool); + if (html) + { + fz_drop_html_box(ctx, html->root); + fz_drop_pool(ctx, html->pool); + } } static fz_html_box *new_box(fz_context *ctx, fz_pool *pool, fz_bidi_direction markup_dir) @@ -1907,8 +1910,8 @@ static char *concat_text(fz_context *ctx, fz_xml *root) return s; } -static fz_css_rule * -html_load_css(fz_context *ctx, fz_archive *zip, const char *base_uri, fz_css_rule *css, fz_xml *root) +static void +html_load_css(fz_context *ctx, fz_archive *zip, const char *base_uri, fz_css *css, fz_xml *root) { fz_xml *html, *head, *node; fz_buffer *buf; @@ -1942,7 +1945,7 @@ html_load_css(fz_context *ctx, fz_archive *zip, const char *base_uri, fz_css_rul { buf = fz_read_archive_entry(ctx, zip, path); fz_write_buffer_byte(ctx, buf, 0); - css = fz_parse_css(ctx, css, (char*)buf->data, path); + fz_parse_css(ctx, css, (char*)buf->data, path); } fz_always(ctx) fz_drop_buffer(ctx, buf); @@ -1956,17 +1959,16 @@ html_load_css(fz_context *ctx, fz_archive *zip, const char *base_uri, fz_css_rul { char *s = concat_text(ctx, node); fz_try(ctx) - css = fz_parse_css(ctx, css, s, "