From: Michael Orlitzky Date: Sat, 5 Aug 2023 17:40:28 +0000 (-0400) Subject: io-svg.c: realloc() on each incremental load. X-Git-Tag: 0.0.1~26 X-Git-Url: http://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=2c9c75bf190e03d4bb339dc1f598b6c0aac2c5d3;p=libsvgtiny-pixbuf.git io-svg.c: realloc() on each incremental load. This will reallocate more often -- once per increment, rather than during only those increments where we run out of space -- but it simplifies the logic. And I actually suspect that my computer might be smart sometimes and can avoid some of these reallocations on its own. --- diff --git a/io-svg.c b/io-svg.c index cf8864a..15d3d89 100644 --- a/io-svg.c +++ b/io-svg.c @@ -16,8 +16,6 @@ #define VIEWPORT_WIDTH 512 #define VIEWPORT_HEIGHT 512 -#define SVG_BUFFER_INCREMENT (size_t)4194304 - /* Convenient typedefs for libsvgtiny */ typedef struct svgtiny_diagram diagram_t; typedef struct svgtiny_shape shape_t; @@ -31,8 +29,11 @@ typedef struct { /* The "file" */ char* svg_data; - size_t svg_data_size; - size_t svg_data_max; + + /* How far into the file are we? This should always point to the + next empty byte. If (for example) svg_data_size is 2, then + svg_data[0] and svg_data[1] are used, but svg_data[2] is free. */ + size_t svg_data_size; } SvgTinyContext; @@ -340,36 +341,24 @@ static gpointer gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func, context->user_data = user_data; /* YOLO, no error checking */ - context->svg_data = g_malloc(SVG_BUFFER_INCREMENT); + context->svg_data = g_malloc(0); context->svg_data_size = 0; return context; } static gboolean gdk_pixbuf_load_increment(gpointer data, - const guchar* buf, - guint size, - GError** error) { + const guchar* buf, + guint buf_size, + GError** error) { size_t increment = 0; SvgTinyContext* context = (SvgTinyContext*)data; - if (context->svg_data_size + size > context->svg_data_max) { - if (size > SVG_BUFFER_INCREMENT) { - increment = size; - } - else { - increment = SVG_BUFFER_INCREMENT; - } - - /* YOLO, no error checking */ - context->svg_data = g_realloc(context->svg_data, - context->svg_data_max + increment); - - context->svg_data_max += increment; - } - - memcpy(context->svg_data + context->svg_data_size, buf, size); - context->svg_data_size += size; + /* YOLO, no error checking */ + context->svg_data = g_realloc(context->svg_data, + context->svg_data_size + buf_size); + memcpy(context->svg_data + context->svg_data_size, buf, buf_size); + context->svg_data_size += buf_size; return TRUE; }