summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2013-08-16 20:10:40 +0100
committerRobin Watts <robin.watts@artifex.com>2013-08-21 20:20:20 +0100
commit768a56a43c8104a0292f4a35e85cde12fbb40a5c (patch)
tree9d0c643079be4ce312c116ebea68a2dbfbfd8096
parente412299adea6cc77c649d4c88fd39690b7ecafc6 (diff)
downloadmupdf-768a56a43c8104a0292f4a35e85cde12fbb40a5c.tar.xz
Add simple memory use tracking to mudraw
-rw-r--r--source/tools/mudraw.c73
1 files changed, 71 insertions, 2 deletions
diff --git a/source/tools/mudraw.c b/source/tools/mudraw.c
index 4de0021b..78b3cfba 100644
--- a/source/tools/mudraw.c
+++ b/source/tools/mudraw.c
@@ -154,6 +154,9 @@ static int ignore_errors = 0;
static int output_format;
static int append = 0;
static int out_cs = CS_UNSET;
+static int memtrace_current = 0;
+static int memtrace_peak = 0;
+static int memtrace_total = 0;
static fz_text_sheet *sheet = NULL;
static fz_colorspace *colorspace;
@@ -189,6 +192,7 @@ static void usage(void)
"\t-b -\tnumber of bits of antialiasing (0 to 8)\n"
"\t-g\trender in grayscale\n"
"\t-m\tshow timing information\n"
+ "\t-M\tshow memory use summary\n"
"\t-t\tshow text (-tt for xml, -ttt for more verbose xml)\n"
"\t-x\tshow display list\n"
"\t-d\tdisable use of display list\n"
@@ -856,16 +860,72 @@ parse_colorspace(const char *name)
return -1;
}
+static void *
+trace_malloc(void *arg, unsigned int size)
+{
+ int *p;
+ if (size == 0)
+ return NULL;
+ p = malloc(size + sizeof(unsigned int));
+ if (p == NULL)
+ return NULL;
+ p[0] = size;
+ memtrace_current += size;
+ memtrace_total += size;
+ if (memtrace_current > memtrace_peak)
+ memtrace_peak = memtrace_current;
+ return (void *)&p[1];
+}
+
+static void
+trace_free(void *arg, void *p_)
+{
+ int *p = (int *)p_;
+
+ if (p == NULL)
+ return;
+ memtrace_current -= p[-1];
+ free(&p[-1]);
+}
+
+static void *
+trace_realloc(void *arg, void *p_, unsigned int size)
+{
+ int *p = (int *)p_;
+ unsigned int oldsize;
+
+ if (size == 0)
+ {
+ trace_free(arg, p_);
+ return NULL;
+ }
+ if (p == NULL)
+ return trace_malloc(arg, size);
+ oldsize = p[-1];
+ p = realloc(&p[-1], size + sizeof(unsigned int));
+ if (p == NULL)
+ return NULL;
+ memtrace_current += size - oldsize;
+ if (size > oldsize)
+ memtrace_total += size - oldsize;
+ if (memtrace_current > memtrace_peak)
+ memtrace_peak = memtrace_current;
+ p[0] = size;
+ return &p[1];
+}
+
int main(int argc, char **argv)
{
char *password = "";
fz_document *doc = NULL;
int c;
fz_context *ctx;
+ fz_alloc_context alloc_ctx = { NULL, trace_malloc, trace_realloc, trace_free };
+ int tracememory = 0;
fz_var(doc);
- while ((c = fz_getopt(argc, argv, "lo:F:p:r:R:b:c:dgmtx5G:Iw:h:fij:")) != -1)
+ while ((c = fz_getopt(argc, argv, "lo:F:p:r:R:b:c:dgmtx5G:Iw:h:fij:M")) != -1)
{
switch (c)
{
@@ -877,6 +937,7 @@ int main(int argc, char **argv)
case 'b': alphabits = atoi(fz_optarg); break;
case 'l': showoutline++; break;
case 'm': showtime++; break;
+ case 'M': tracememory++; break;
case 't': showtext++; break;
case 'x': showxml++; break;
case '5': showmd5++; break;
@@ -911,7 +972,7 @@ int main(int argc, char **argv)
mujstest_file = fopen(mujstest_filename, "wb");
}
- ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
+ ctx = fz_new_context((tracememory == 0 ? NULL : &alloc_ctx), NULL, FZ_STORE_DEFAULT);
if (!ctx)
{
fprintf(stderr, "cannot initialise context\n");
@@ -1151,6 +1212,14 @@ int main(int argc, char **argv)
fclose(mujstest_file);
fz_free_context(ctx);
+
+ if (tracememory)
+ {
+ printf("Total memory use = %d bytes\n", memtrace_total);
+ printf("Peak memory use = %d bytes\n", memtrace_peak);
+ printf("Current memory use = %d bytes\n", memtrace_current);
+ }
+
return (errored != 0);
}