[gs-commits] mupdf 1.16.1.epub-prerelease-29 Add some Memento_labels t

[email protected] (Robin Watts) Mon, 23 Dec 2019 16:41:30 +0000 (UTC)
Newsgroups gmane.comp.printing.ghostscript.cvs
Message-ID <[email protected]>
commit 6896bc2f73ef19e3530283f2ae6e265e754df9d3
Author: Robin Watts <[email protected]>
Date:   Mon Dec 23 00:01:27 2019 +0000

    Add some Memento_labels throughout code.

diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c
index c1d521e..fe0f795 100644
--- a/platform/java/mupdf_native.c
+++ b/platform/java/mupdf_native.c
@@ -1760,7 +1760,7 @@ static inline jobject to_PDFWidget(fz_context *ctx, JNIEnv *env, pdf_widget *wid
 			nopts = pdf_choice_widget_options(ctx, widget, 0, NULL);
 			if (nopts > 0)
 			{
-				opts = fz_malloc(ctx, nopts * sizeof(*opts));
+				opts = Memento_label(fz_malloc(ctx, nopts * sizeof(*opts)), "to_PDFWidget");
 				pdf_choice_widget_options(ctx, widget, 0, opts);
 				jopts = to_StringArray_safe(ctx, env, opts, nopts);
 				if (jopts)
@@ -3568,7 +3568,7 @@ newNativeAndroidDrawDevice(JNIEnv *env, jobject self, fz_context *ctx, jobject o
 	{
 		pixmap = fz_new_pixmap_with_bbox_and_data(ctx, fz_device_rgb(ctx), bbox, NULL, 1, &dummy);
 		pixmap->stride = width * sizeof(int32_t);
-		ninfo = fz_malloc(ctx, sizeof(*ninfo));
+		ninfo = Memento_label(fz_malloc(ctx, sizeof(*ninfo)), "newNativeAndroidDrawDevice");
 		ninfo->pixmap = pixmap;
 		ninfo->lock = lock;
 		ninfo->unlock = unlock;
@@ -5104,7 +5104,7 @@ FUN(Document_openNativeWithStream)(JNIEnv *env, jclass cls, jstring jmagic, jobj
 		if (jdoc)
 		{
 			/* No exceptions can occur from here to stream owning docstate, so we must not free docstate. */
-			docstate = fz_malloc(ctx, sizeof(SeekableStreamState));
+			docstate = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_docstate");
 			docstate->stream = jdoc;
 			docstate->array = docarray;
 
@@ -5120,7 +5120,7 @@ FUN(Document_openNativeWithStream)(JNIEnv *env, jclass cls, jstring jmagic, jobj
 		if (jacc)
 		{
 			/* No exceptions can occur from here to stream owning accstate, so we must not free accstate. */
-			accstate = fz_malloc(ctx, sizeof(SeekableStreamState));
+			accstate = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_accstate");
 			accstate->stream = jacc;
 			accstate->array = accarray;
 
@@ -5261,7 +5261,7 @@ FUN(Document_openNativeWithPathAndStream)(JNIEnv *env, jclass cls, jstring jfile
 		if (jacc)
 		{
 			/* No exceptions can occur from here to stream owning accstate, so we must not free accstate. */
-			accstate = fz_malloc(ctx, sizeof(SeekableStreamState));
+			accstate = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_accstate2");
 			accstate->stream = jacc;
 			accstate->array = accarray;
 
@@ -5466,7 +5466,7 @@ FUN(Document_outputAccelerator)(JNIEnv *env, jobject self, jobject jstream)
 
 	fz_try(ctx)
 	{
-		state = fz_malloc(ctx, sizeof(SeekableStreamState));
+		state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreamState_outputAccelerator");
 		state->stream = stream;
 		state->array = array;
 
@@ -7311,7 +7311,7 @@ FUN(PDFDocument_newByteString)(JNIEnv *env, jobject self, jobject jbs)
 	bslen = (*env)->GetArrayLength(env, jbs);
 
 	fz_try(ctx)
-		bs = fz_malloc(ctx, bslen);
+		bs = Memento_label(fz_malloc(ctx, bslen), "PDFDocument_newByteString");
 	fz_catch(ctx)
 	{
 		jni_rethrow(env, ctx);
@@ -7892,7 +7892,7 @@ FUN(PDFDocument_nativeSaveWithStream)(JNIEnv *env, jobject self, jobject jstream
 		if (jstream)
 		{
 			/* No exceptions can occur from here to stream owning state, so we must not free state. */
-			state = fz_malloc(ctx, sizeof(SeekableStreamState));
+			state = Memento_label(fz_malloc(ctx, sizeof(SeekableStreamState)), "SeekableStreramState_state");
 			state->stream = stream;
 			state->array = array;
 
diff --git a/source/fitz/bidi.c b/source/fitz/bidi.c
index d718f90..6cb9310 100644
--- a/source/fitz/bidi.c
+++ b/source/fitz/bidi.c
@@ -423,7 +423,7 @@ create_levels(fz_context *ctx,
 	const uint32_t *ptext;
 	size_t plen, remaining;
 
-	levels = fz_malloc(ctx, len * sizeof(*levels));
+	levels = Memento_label(fz_malloc(ctx, len * sizeof(*levels)), "bidi_levels");
 
 	fz_var(types);
 
diff --git a/source/fitz/bitmap.c b/source/fitz/bitmap.c
index 8cff296..d176779 100644
--- a/source/fitz/bitmap.c
+++ b/source/fitz/bitmap.c
@@ -295,7 +295,7 @@ fz_new_bitmap(fz_context *ctx, int w, int h, int n, int xres, int yres)
 		bit->xres = xres;
 		bit->yres = yres;
 		bit->stride = stride;
-		bit->samples = fz_malloc(ctx, h * bit->stride);
+		bit->samples = Memento_label(fz_malloc(ctx, h * bit->stride), "bitmap_samples");
 	}
 	fz_catch(ctx)
 	{
diff --git a/source/fitz/color-icc-create.c b/source/fitz/color-icc-create.c
index 285dd80..fdd7447 100644
--- a/source/fitz/color-icc-create.c
+++ b/source/fitz/color-icc-create.c
@@ -361,7 +361,7 @@ fz_new_icc_data_from_cal(fz_context *ctx,
 		TRC_Tags[0] = icSigGrayTRCTag;
 	}
 
-	tag_list = fz_malloc(ctx, sizeof(fz_icc_tag) * num_tags);
+	tag_list = Memento_label(fz_malloc(ctx, sizeof(fz_icc_tag) * num_tags), "icc_tag_list");
 
 	/* precompute sizes and offsets */
 	profile_size += ICC_TAG_SIZE * num_tags;
diff --git a/source/fitz/colorspace.c b/source/fitz/colorspace.c
index 975217c..9288eb0 100644
--- a/source/fitz/colorspace.c
+++ b/source/fitz/colorspace.c
@@ -1010,7 +1010,7 @@ static void fz_cached_color_convert(fz_context *ctx, fz_color_converter *cc_, co
 
 	cc->base.convert(ctx, &cc->base, ss, ds);
 
-	val = fz_malloc_array(ctx, cc->base.ds->n, float);
+	val = Memento_label(fz_malloc_array(ctx, cc->base.ds->n, float), "cached_color_convert");
 	memcpy(val, ds, n);
 	fz_try(ctx)
 		fz_hash_insert(ctx, cc->hash, ss, val);
diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c
index b74978d..a63deec 100644
--- a/source/fitz/draw-device.c
+++ b/source/fitz/draw-device.c
@@ -155,7 +155,7 @@ static void grow_stack(fz_context *ctx, fz_draw_device *dev)
 	{
 		stack = fz_realloc_array(ctx, dev->stack, max, fz_draw_state);
 	}
-	dev->stack = stack;
+	dev->stack = Memento_label(stack, "draw_stack");
 	dev->stack_cap = max;
 }
 
diff --git a/source/fitz/draw-edge.c b/source/fitz/draw-edge.c
index a883058..a996670 100644
--- a/source/fitz/draw-edge.c
+++ b/source/fitz/draw-edge.c
@@ -579,8 +579,8 @@ fz_scan_convert_aa(fz_context *ctx, fz_gel *gel, int eofill, const fz_irect *cli
 		fz_free(ctx, gel->deltas);
 		gel->alphas = NULL;
 		gel->deltas = NULL;
-		alphas = gel->alphas = fz_malloc_array(ctx, bcap, unsigned char);
-		deltas = gel->deltas = fz_malloc_array(ctx, bcap, int);
+		alphas = gel->alphas = Memento_label(fz_malloc_array(ctx, bcap, unsigned char), "gel_alphas");
+		deltas = gel->deltas = Memento_label(fz_malloc_array(ctx, bcap, int), "gel_deltas");
 	}
 	alphas = gel->alphas;
 	deltas = gel->deltas;
@@ -893,11 +893,11 @@ fz_new_gel(fz_context *ctx)
 		gel->edges = NULL;
 		gel->cap = 512;
 		gel->len = 0;
-		gel->edges = fz_malloc_array(ctx, gel->cap, fz_edge);
+		gel->edges = Memento_label(fz_malloc_array(ctx, gel->cap, fz_edge), "gel_edges");
 
 		gel->acap = 64;
 		gel->alen = 0;
-		gel->active = fz_malloc_array(ctx, gel->acap, fz_edge*);
+		gel->active = Memento_label(fz_malloc_array(ctx, gel->acap, fz_edge*), "gel_active");
 	}
 	fz_catch(ctx)
 	{
diff --git a/source/fitz/filter-dct.c b/source/fitz/filter-dct.c
index 7fa58db..68d765a 100644
--- a/source/fitz/filter-dct.c
+++ b/source/fitz/filter-dct.c
@@ -50,7 +50,7 @@ static void *
 fz_dct_mem_alloc(j_common_ptr cinfo, size_t size)
 {
 	fz_dctd *state = JZ_DCT_STATE_FROM_CINFO(cinfo);
-	return fz_malloc_no_throw(state->ctx, size);
+	return Memento_label(fz_malloc_no_throw(state->ctx, size), "dct_alloc");
 }
 
 static void
@@ -234,7 +234,7 @@ next_dctd(fz_context *ctx, fz_stream *stm, size_t max)
 			jpeg_start_decompress(cinfo);
 
 			state->stride = cinfo->output_width * cinfo->output_components;
-			state->scanline = fz_malloc(ctx, state->stride);
+			state->scanline = Memento_label(fz_malloc(ctx, state->stride), "dct_scanline");
 			state->rp = state->scanline;
 			state->wp = state->scanline;
 		}
diff --git a/source/fitz/filter-fax.c b/source/fitz/filter-fax.c
index ae11702..0b62728 100644
--- a/source/fitz/filter-fax.c
+++ b/source/fitz/filter-fax.c
@@ -805,8 +805,8 @@ fz_open_faxd(fz_context *ctx, fz_stream *chain,
 		fax->dim = fax->k < 0 ? 2 : 1;
 		fax->eolc = 0;
 
-		fax->ref = fz_malloc(ctx, fax->stride);
-		fax->dst = fz_malloc(ctx, fax->stride);
+		fax->ref = Memento_label(fz_malloc(ctx, fax->stride), "fax_ref");
+		fax->dst = Memento_label(fz_malloc(ctx, fax->stride), "fax_dst");
 		fax->rp = fax->dst;
 		fax->wp = fax->dst + fax->stride;
 
diff --git a/source/fitz/filter-flate.c b/source/fitz/filter-flate.c
index 8d71014..dbcf0b7 100644
--- a/source/fitz/filter-flate.c
+++ b/source/fitz/filter-flate.c
@@ -15,7 +15,7 @@ struct fz_inflate_state_s
 
 void *fz_zlib_alloc(void *ctx, unsigned int items, unsigned int size)
 {
-	return fz_malloc_no_throw(ctx, items * size);
+	return Memento_label(fz_malloc_no_throw(ctx, items * size), "zlib_alloc");
 }
 
 void fz_zlib_free(void *ctx, void *ptr)
diff --git a/source/fitz/filter-jbig2.c b/source/fitz/filter-jbig2.c
index 8962161..8309bc8 100644
--- a/source/fitz/filter-jbig2.c
+++ b/source/fitz/filter-jbig2.c
@@ -54,7 +54,7 @@ static void * JB2_Callback
 jbig2_alloc(unsigned long size, void *userdata)
 {
 	fz_jbig2d *state = userdata;
-	return fz_malloc(state->ctx, size);
+	return Memento_label(fz_malloc(state->ctx, size), "jbig2_alloc");
 }
 
 static JB2_Error JB2_Callback
@@ -174,7 +174,7 @@ next_jbig2d(fz_context *ctx, fz_stream *stm, size_t len)
 
 			state->stride = (state->width + 7) >> 3;
 			stm->pos = state->stride * state->height;
-			state->output = fz_malloc(state->ctx, stm->pos);
+			state->output = Memento_label(fz_malloc(state->ctx, stm->pos), "jbig2_output");
 			stm->rp = state->output;
 			stm->wp = state->output;
 
@@ -361,7 +361,7 @@ error_callback(void *data, const char *msg, Jbig2Severity severity, int32_t seg_
 static void *fz_jbig2_alloc(Jbig2Allocator *allocator, size_t size)
 {
 	fz_context *ctx = ((struct fz_jbig2_alloc_s *) allocator)->ctx;
-	return fz_malloc_no_throw(ctx, size);
+	return Memento_label(fz_malloc_no_throw(ctx, size), "jbig2_alloc");
 }
 
 static void fz_jbig2_free(Jbig2Allocator *allocator, void *p)
@@ -379,8 +379,8 @@ static void *fz_jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size)
 		return NULL;
 	}
 	if (p == NULL)
-		return fz_malloc(ctx, size);
-	return fz_realloc_no_throw(ctx, p, size);
+		return Memento_label(fz_malloc(ctx, size), "jbig2_realloc");
+	return Memento_label(fz_realloc_no_throw(ctx, p, size), "jbig2_realloc");
 }
 
 fz_jbig2_globals *
diff --git a/source/fitz/filter-predict.c b/source/fitz/filter-predict.c
index e00e07f..f7b4750 100644
--- a/source/fitz/filter-predict.c
+++ b/source/fitz/filter-predict.c
@@ -264,9 +264,9 @@ fz_open_predict(fz_context *ctx, fz_stream *chain, int predictor, int columns, i
 		state->stride = (state->bpc * state->colors * state->columns + 7) / 8;
 		state->bpp = (state->bpc * state->colors + 7) / 8;
 
-		state->in = fz_malloc(ctx, state->stride + 1);
-		state->out = fz_malloc(ctx, state->stride);
-		state->ref = fz_malloc(ctx, state->stride);
+		state->in = Memento_label(fz_malloc(ctx, state->stride + 1), "predict_in");
+		state->out = Memento_label(fz_malloc(ctx, state->stride), "predict_out");
+		state->ref = Memento_label(fz_malloc(ctx, state->stride), "predict_ref");
 		state->rp = state->out;
 		state->wp = state->out;
 
diff --git a/source/fitz/filter-sgi.c b/source/fitz/filter-sgi.c
index ef59ee5..d1dff15 100644
--- a/source/fitz/filter-sgi.c
+++ b/source/fitz/filter-sgi.c
@@ -319,7 +319,7 @@ fz_open_sgilog16(fz_context *ctx, fz_stream *chain, int w)
 		state->n = 0;
 		state->c = 0;
 		state->w = w;
-		state->temp = fz_malloc(ctx, w * sizeof(uint16_t));
+		state->temp = Memento_label(fz_malloc(ctx, w * sizeof(uint16_t)), "sgilog16_temp");
 		state->chain = fz_keep_stream(ctx, chain);
 	}
 	fz_catch(ctx)
@@ -484,7 +484,7 @@ fz_open_sgilog24(fz_context *ctx, fz_stream *chain, int w)
 	{
 		state->err = 0;
 		state->w = w;
-		state->temp = fz_malloc(ctx, w * 3);
+		state->temp = Memento_label(fz_malloc(ctx, w * 3), "sgilog24_temp");
 		state->chain = fz_keep_stream(ctx, chain);
 	}
 	fz_catch(ctx)
@@ -654,7 +654,7 @@ fz_open_sgilog32(fz_context *ctx, fz_stream *chain, int w)
 		state->n = 0;
 		state->c = 0;
 		state->w = w;
-		state->temp = fz_malloc(ctx, w * sizeof(uint32_t));
+		state->temp = Memento_label(fz_malloc(ctx, w * sizeof(uint32_t)), "sgilog32_temp");
 		state->chain = fz_keep_stream(ctx, chain);
 	}
 	fz_catch(ctx)
diff --git a/source/fitz/filter-thunder.c b/source/fitz/filter-thunder.c
index db96fbe..30abd0b 100644
--- a/source/fitz/filter-thunder.c
+++ b/source/fitz/filter-thunder.c
@@ -131,7 +131,7 @@ fz_open_thunder(fz_context *ctx, fz_stream *chain, int w)
 		state->pixel = 0;
 		state->lastpixel = 0;
 		state->len = w / 2;
-		state->buffer = fz_malloc(ctx, state->len);
+		state->buffer = Memento_label(fz_malloc(ctx, state->len), "thunder_buffer");
 		state->chain = fz_keep_stream(ctx, chain);
 	}
 	fz_catch(ctx)
diff --git a/source/fitz/font.c b/source/fitz/font.c
index 2f9a288..2368d9d 100644
--- a/source/fitz/font.c
+++ b/source/fitz/font.c
@@ -108,7 +108,7 @@ fz_new_font(fz_context *ctx, const char *name, int use_glyph_bbox, int glyph_cou
 	if (use_glyph_bbox && glyph_count <= MAX_BBOX_TABLE_SIZE)
 	{
 		fz_try(ctx)
-			font->bbox_table = fz_malloc_array(ctx, glyph_count, fz_rect);
+			font->bbox_table = Memento_label(fz_malloc_array(ctx, glyph_count, fz_rect), "font_bbox_table");
 		fz_catch(ctx)
 		{
 			fz_free(ctx, font);
@@ -1990,7 +1990,7 @@ fz_advance_glyph(fz_context *ctx, fz_font *font, int gid, int wmode)
 			if (!font->advance_cache)
 			{
 				int i;
-				font->advance_cache = fz_malloc_array(ctx, font->glyph_count, float);
+				font->advance_cache = Memento_label(fz_malloc_array(ctx, font->glyph_count, float), "font_advance_cache");
 				for (i = 0; i < font->glyph_count; ++i)
 					font->advance_cache[i] = fz_advance_ft_glyph(ctx, font, i, 0);
 			}
diff --git a/source/fitz/hash.c b/source/fitz/hash.c
index e8f0903..dc5d549 100644
--- a/source/fitz/hash.c
+++ b/source/fitz/hash.c
@@ -62,7 +62,7 @@ fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock, fz_has
 	table->drop_val = drop_val;
 	fz_try(ctx)
 	{
-		table->ents = fz_malloc_array(ctx, table->size, fz_hash_entry);
+		table->ents = Memento_label(fz_malloc_array(ctx, table->size, fz_hash_entry), "hash_entries");
 		memset(table->ents, 0, sizeof(fz_hash_entry) * table->size);
 	}
 	fz_catch(ctx)
diff --git a/source/fitz/image.c b/source/fitz/image.c
index 9d142c5..bc73a8d 100644
--- a/source/fitz/image.c
+++ b/source/fitz/image.c
@@ -371,7 +371,7 @@ fz_decomp_image_from_stream(fz_context *ctx, fz_stream *stm, fz_compressed_image
 		stride = (w * image->n * image->bpc + 7) / 8;
 		if ((size_t)h > (size_t)(SIZE_MAX / stride))
 			fz_throw(ctx, FZ_ERROR_MEMORY, "image too large");
-		samples = fz_malloc(ctx, h * stride);
+		samples = Memento_label(fz_malloc(ctx, h * stride), "pixmap_samples");
 
 		if (subarea)
 		{
diff --git a/source/fitz/load-gif.c b/source/fitz/load-gif.c
index 37175c5..6e139b3 100644
--- a/source/fitz/load-gif.c
+++ b/source/fitz/load-gif.c
@@ -176,7 +176,7 @@ gif_read_gct(fz_context *ctx, struct info *info, const unsigned char *p, const u
 	if (end - p < info->gct_entries * 3)
 		fz_throw(ctx, FZ_ERROR_GENERIC, "premature end in global color table in gif image");
 
-	info->gct = fz_malloc(ctx, info->gct_entries * 3);
+	info->gct = Memento_label(fz_malloc(ctx, info->gct_entries * 3), "gif_gct");
 	memmove(info->gct, p, info->gct_entries * 3);
 
 	return p + info->gct_entries * 3;
@@ -207,7 +207,7 @@ gif_read_lct(fz_context *ctx, struct info *info, const unsigned char *p, const u
 	if (end - p < info->lct_entries * 3)
 		fz_throw(ctx, FZ_ERROR_GENERIC, "premature end in local color table in gif image");
 
-	info->lct = fz_malloc(ctx, info->lct_entries * 3);
+	info->lct = Memento_label(fz_malloc(ctx, info->lct_entries * 3), "gif_lct");
 	memmove(info->lct, p, info->lct_entries * 3);
 
 	return p + info->lct_entries * 3;
diff --git a/source/fitz/load-jbig2.c b/source/fitz/load-jbig2.c
index e273872..21b0156 100644
--- a/source/fitz/load-jbig2.c
+++ b/source/fitz/load-jbig2.c
@@ -25,7 +25,7 @@ static void * JB2_Callback
 jbig2_alloc(unsigned long size, void *userdata)
 {
 	struct info *state = userdata;
-	return fz_malloc(state->ctx, size);
+	return Memento_label(fz_malloc(state->ctx, size), "jbig2_alloc");
 }
 
 static JB2_Error JB2_Callback
@@ -154,7 +154,7 @@ jbig2_read_image(fz_context *ctx, struct info *jbig2, const unsigned char *buf,
 		if (!only_metadata)
 		{
 			state->stride = (state->width + 7) >> 3;
-			state->output = fz_malloc(state->ctx, state->stride * state->height);
+			state->output = Memento_label(fz_malloc(state->ctx, state->stride * state->height), "jbig2_image");
 
 			err = JB2_Document_Decompress_Page(state->doc, scale, rect, jbig2_write, state);
 			if (err != cJB2_Error_OK)
@@ -282,8 +282,8 @@ static void *fz_jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size)
 		return NULL;
 	}
 	if (p == NULL)
-		return fz_malloc(ctx, size);
-	return fz_realloc_no_throw(ctx, p, size);
+		return Memento_label(fz_malloc(ctx, size), "jbig2_realloc");
+	return Memento_label(fz_realloc_no_throw(ctx, p, size), "jbig2_realloc");
 }
 
 static fz_pixmap *
diff --git a/source/fitz/load-jpx.c b/source/fitz/load-jpx.c
index e6c01e8..8091727 100644
--- a/source/fitz/load-jpx.c
+++ b/source/fitz/load-jpx.c
@@ -82,7 +82,7 @@ static void * JP2_Callback_Conv
 jpx_alloc(long size, JP2_Callback_Param param)
 {
 	fz_context *ctx = (fz_context *) param;
-	return fz_malloc(ctx, size);
+	return Memento_label(fz_malloc(ctx, size), "jpx_alloc");
 }
 
 static JP2_Error JP2_Callback_Conv
@@ -554,7 +554,7 @@ void *opj_malloc(size_t size)
 
 	assert(ctx != NULL);
 
-	return fz_malloc_no_throw(ctx, size);
+	return Memento_label(fz_malloc_no_throw(ctx, size), "opj_malloc");
 }
 
 void *opj_calloc(size_t n, size_t size)
diff --git a/source/fitz/load-jxr.c b/source/fitz/load-jxr.c
index 4081e69..3257649 100644
--- a/source/fitz/load-jxr.c
+++ b/source/fitz/load-jxr.c
@@ -328,7 +328,7 @@ jxr_read_image(fz_context *ctx, const unsigned char *data, int size, struct info
 			unsigned char alpha_band;
 
 			info->ctx = ctx;
-			info->samples = fz_malloc(ctx, info->stride * info->height);
+			info->samples = Memento_label(fz_malloc(ctx, info->stride * info->height), "jxr_samples");
 			memset(info->samples, 0xff, info->stride * info->height);
 
 			image_offset = jxrc_image_offset(container, 0);
diff --git a/source/fitz/load-png.c b/source/fitz/load-png.c
index e21f7b7..f3a42ed 100644
--- a/source/fitz/load-png.c
+++ b/source/fitz/load-png.c
@@ -184,7 +184,7 @@ png_deinterlace(fz_context *ctx, struct info *info, unsigned int *passw, unsigne
 
 	if (info->height > UINT_MAX / stride)
 		fz_throw(ctx, FZ_ERROR_MEMORY, "image too large");
-	output = fz_malloc(ctx, info->height * stride);
+	output = Memento_label(fz_malloc(ctx, info->height * stride), "png_deinterlace");
 
 	for (p = 0; p < 7; p++)
 	{
@@ -450,7 +450,7 @@ png_read_image(fz_context *ctx, struct info *info, const unsigned char *p, size_
 			info->size = passofs[7];
 		}
 
-		info->samples = fz_malloc(ctx, info->size);
+		info->samples = Memento_label(fz_malloc(ctx, info->size), "png_samples");
 
 		stm.zalloc = fz_zlib_alloc;
 		stm.zfree = fz_zlib_free;
diff --git a/source/fitz/load-tiff.c b/source/fitz/load-tiff.c
index b13f389..5a23173 100644
--- a/source/fitz/load-tiff.c
+++ b/source/fitz/load-tiff.c
@@ -253,7 +253,7 @@ tiff_expand_colormap(fz_context *ctx, struct tiff *tiff)
 
 	stride = tiff->imagewidth * (tiff->samplesperpixel + 2);
 
-	samples = fz_malloc(ctx, stride * tiff->imagelength);
+	samples = Memento_label(fz_malloc(ctx, stride * tiff->imagelength), "tiff_samples");
 
 	for (y = 0; y < tiff->imagelength; y++)
 	{
@@ -594,7 +594,7 @@ tiff_decode_tiles(fz_context *ctx, struct tiff *tiff)
 		else
 			wlen = tiff->tilestride * tiff->ycbcrsubsamp[1];
 
-		data = tiff->data = fz_malloc(ctx, wlen);
+		data = tiff->data = Memento_label(fz_malloc(ctx, wlen), "tiff_tile_jpg");
 
 		tile = 0;
 		for (x = 0; x < tiff->imagelength; x += tiff->tilelength)
@@ -620,7 +620,7 @@ tiff_decode_tiles(fz_context *ctx, struct tiff *tiff)
 	else
 	{
 		wlen = tiff->tilelength * tiff->tilestride;
-		data = tiff->data = fz_malloc(ctx, wlen);
+		data = tiff->data = Memento_label(fz_malloc(ctx, wlen), "tiff_tile");
 
 		tile = 0;
 		for (x = 0; x < tiff->imagelength; x += tiff->tilelength)
@@ -673,7 +673,7 @@ tiff_decode_strips(fz_context *ctx, struct tiff *tiff)
 			rowsperstrip = tiff->ycbcrsubsamp[1];
 
 		wlen = rowsperstrip * tiff->stride;
-		data = tiff->data = fz_malloc(ctx, wlen);
+		data = tiff->data = Memento_label(fz_malloc(ctx, wlen), "tiff_strip_jpg");
 
 		strip = 0;
 		for (y = 0; y < tiff->imagelength; y += rowsperstrip)
@@ -873,7 +873,7 @@ tiff_read_tag(fz_context *ctx, struct tiff *tiff, unsigned offset)
 	case ICCProfile:
 		if (tiff->profile)
 			fz_throw(ctx, FZ_ERROR_GENERIC, "at most one ICC profile tag allowed");
-		tiff->profile = fz_malloc(ctx, count);
+		tiff->profile = Memento_label(fz_malloc(ctx, count), "tiff_profile");
 		/* ICC profile data type is set to UNDEFINED.
 		 * TBYTE reading not correct in tiff_read_tag_value */
 		tiff_read_bytes(tiff->profile, tiff, value, count);
@@ -891,7 +891,7 @@ tiff_read_tag(fz_context *ctx, struct tiff *tiff, unsigned offset)
 	case StripOffsets:
 		if (tiff->stripoffsets)
 			fz_throw(ctx, FZ_ERROR_GENERIC, "at most one strip offsets tag allowed");
-		tiff->stripoffsets = fz_malloc_array(ctx, count, unsigned);
+		tiff->stripoffsets = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_stripoffsets");
 		tiff_read_tag_value(tiff->stripoffsets, tiff, type, value, count);
 		tiff->stripoffsetslen = count;
 		break;
@@ -899,7 +899,7 @@ tiff_read_tag(fz_context *ctx, struct tiff *tiff, unsigned offset)
 	case StripByteCounts:
 		if (tiff->stripbytecounts)
 			fz_throw(ctx, FZ_ERROR_GENERIC, "at most one strip byte counts tag allowed");
-		tiff->stripbytecounts = fz_malloc_array(ctx, count, unsigned);
+		tiff->stripbytecounts = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_stripbytecounts");
 		tiff_read_tag_value(tiff->stripbytecounts, tiff, type, value, count);
 		tiff->stripbytecountslen = count;
 		break;
@@ -907,7 +907,7 @@ tiff_read_tag(fz_context *ctx, struct tiff *tiff, unsigned offset)
 	case ColorMap:
 		if (tiff->colormap)
 			fz_throw(ctx, FZ_ERROR_GENERIC, "at most one color map allowed");
-		tiff->colormap = fz_malloc_array(ctx, count, unsigned);
+		tiff->colormap = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_colormap");
 		tiff_read_tag_value(tiff->colormap, tiff, type, value, count);
 		tiff->colormaplen = count;
 		break;
@@ -923,7 +923,7 @@ tiff_read_tag(fz_context *ctx, struct tiff *tiff, unsigned offset)
 	case TileOffsets:
 		if (tiff->tileoffsets)
 			fz_throw(ctx, FZ_ERROR_GENERIC, "at most one tile offsets tag allowed");
-		tiff->tileoffsets = fz_malloc_array(ctx, count, unsigned);
+		tiff->tileoffsets = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_tileoffsets");
 		tiff_read_tag_value(tiff->tileoffsets, tiff, type, value, count);
 		tiff->tileoffsetslen = count;
 		break;
@@ -931,7 +931,7 @@ tiff_read_tag(fz_context *ctx, struct tiff *tiff, unsigned offset)
 	case TileByteCounts:
 		if (tiff->tilebytecounts)
 			fz_throw(ctx, FZ_ERROR_GENERIC, "at most one tile byte counts tag allowed");
-		tiff->tilebytecounts = fz_malloc_array(ctx, count, unsigned);
+		tiff->tilebytecounts = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_tilebytecounts");
 		tiff_read_tag_value(tiff->tilebytecounts, tiff, type, value, count);
 		tiff->tilebytecountslen = count;
 		break;
@@ -1010,7 +1010,7 @@ tiff_read_header(fz_context *ctx, struct tiff *tiff, const unsigned char *buf, s
 		fz_throw(ctx, FZ_ERROR_GENERIC, "not a TIFF file, wrong version marker");
 
 	/* get offset of IFD */
-	tiff->ifd_offsets = fz_malloc_array(ctx, 1, unsigned);
+	tiff->ifd_offsets = Memento_label(fz_malloc_array(ctx, 1, unsigned), "tiff_ifd_offsets");
 	tiff->ifd_offsets[0] = tiff_readlong(tiff);
 	tiff->ifds = 1;
 }
@@ -1244,7 +1244,7 @@ tiff_decode_ifd(fz_context *ctx, struct tiff *tiff)
 		if (!tiff->tilelength && !tiff->tilewidth && !tiff->stripbytecounts)
 		{
 			tiff->stripbytecountslen = (tiff->imagelength + tiff->rowsperstrip - 1) / tiff->rowsperstrip;
-			tiff->stripbytecounts = fz_malloc_array(ctx, tiff->stripbytecountslen, unsigned);
+			tiff->stripbytecounts = Memento_label(fz_malloc_array(ctx, tiff->stripbytecountslen, unsigned), "tiff_stripbytecounts");
 			for (i = 0; i < tiff->stripbytecountslen; i++)
 				tiff->stripbytecounts[i] = tiff->rowsperstrip * tiff->stride;
 		}
@@ -1253,7 +1253,7 @@ tiff_decode_ifd(fz_context *ctx, struct tiff *tiff)
 			unsigned tilesdown = (tiff->imagelength + tiff->tilelength - 1) / tiff->tilelength;
 			unsigned tilesacross = (tiff->imagewidth + tiff->tilewidth - 1) / tiff->tilewidth;
 			tiff->tilebytecountslen = tilesacross * tilesdown;
-			tiff->tilebytecounts = fz_malloc_array(ctx, tiff->tilebytecountslen, unsigned);
+			tiff->tilebytecounts = Memento_label(fz_malloc_array(ctx, tiff->tilebytecountslen, unsigned), "tiff_tilebytecounts");
 			for (i = 0; i < tiff->tilebytecountslen; i++)
 				tiff->tilebytecounts[i] = tiff->tilelength * tiff->tilestride;
 		}
@@ -1288,7 +1288,7 @@ tiff_decode_samples(fz_context *ctx, struct tiff *tiff)
 
 	if (tiff->imagelength > UINT_MAX / tiff->stride)
 		fz_throw(ctx, FZ_ERROR_MEMORY, "image too large");
-	tiff->samples = fz_malloc(ctx, tiff->imagelength * tiff->stride);
+	tiff->samples = Memento_label(fz_malloc(ctx, tiff->imagelength * tiff->stride), "tiff_samples");
 	memset(tiff->samples, 0x55, tiff->imagelength * tiff->stride);
 
 	if (tiff->tilelength && tiff->tilewidth && tiff->tileoffsets && tiff->tilebytecounts)
diff --git a/source/fitz/output-pcl.c b/source/fitz/output-pcl.c
index fdc6806..fe28623 100644
--- a/source/fitz/output-pcl.c
+++ b/source/fitz/output-pcl.c
@@ -791,7 +791,7 @@ color_pcl_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *
 	if (n != 3)
 		fz_throw(ctx, FZ_ERROR_GENERIC, "color PCL must be RGB");
 
-	writer->linebuf = fz_malloc(ctx, w * 3 * 2);
+	writer->linebuf = Memento_label(fz_malloc(ctx, w * 3 * 2), "color_pcl_linebuf");
 
 	guess_paper_size(&writer->options, w, h, xres, yres);
 
diff --git a/source/fitz/output-pclm.c b/source/fitz/output-pclm.c
index b8c918a..3baf1b6 100644
--- a/source/fitz/output-pclm.c
+++ b/source/fitz/output-pclm.c
@@ -129,9 +129,9 @@ pclm_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs)
 
 	fz_free(ctx, writer->stripbuf);
 	fz_free(ctx, writer->compbuf);
-	writer->stripbuf = fz_malloc(ctx, w * sh * n);
+	writer->stripbuf = Memento_label(fz_malloc(ctx, w * sh * n), "pclm_stripbuf");
 	writer->complen = fz_deflate_bound(ctx, w * sh * n);
-	writer->compbuf = fz_malloc(ctx, writer->complen);
+	writer->compbuf = Memento_label(fz_malloc(ctx, writer->complen), "pclm_compbuf");
 
 	/* Send the file header on the first page */
 	if (writer->pages == 0)
diff --git a/source/fitz/output-png.c b/source/fitz/output-png.c
index 8fe2c9d..775d3f7 100644
--- a/source/fitz/output-png.c
+++ b/source/fitz/output-png.c
@@ -199,8 +199,8 @@ png_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_st
 		 * larger than compressBound outputted in one go, even if you
 		 * take all the data out each time. */
 		writer->csize = compressBound(writer->usize);
-		writer->udata = fz_malloc(ctx, writer->usize);
-		writer->cdata = fz_malloc(ctx, writer->csize);
+		writer->udata = Memento_label(fz_malloc(ctx, writer->usize), "png_write_udata");
+		writer->cdata = Memento_label(fz_malloc(ctx, writer->csize), "png_write_cdata");
 		writer->stream.opaque = ctx;
 		writer->stream.zalloc = fz_zlib_alloc;
 		writer->stream.zfree = fz_zlib_free;
diff --git a/source/fitz/output-ps.c b/source/fitz/output-ps.c
index cd04a73..a455fd8 100644
--- a/source/fitz/output-ps.c
+++ b/source/fitz/output-ps.c
@@ -219,7 +219,7 @@ ps_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_sta
 	{
 		fz_free(ctx, writer->input);
 		writer->input = NULL;
-		writer->input = fz_malloc(ctx, required_input);
+		writer->input = Memento_label(fz_malloc(ctx, required_input), "pswriter_input");
 		writer->input_size = required_input;
 	}
 
@@ -227,7 +227,7 @@ ps_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_sta
 	{
 		fz_free(ctx, writer->output);
 		writer->output = NULL;
-		writer->output = fz_malloc(ctx, required_output);
+		writer->output = Memento_label(fz_malloc(ctx, required_output), "pswriter_output");
 		writer->output_size = required_output;
 	}
 
diff --git a/source/fitz/output.c b/source/fitz/output.c
index 5964dce..6e25e02 100644
--- a/source/fitz/output.c
+++ b/source/fitz/output.c
@@ -142,7 +142,7 @@ fz_new_output(fz_context *ctx, int bufsiz, void *state, fz_output_write_fn *writ
 		out->drop = drop;
 		if (bufsiz > 0)
 		{
-			out->bp = fz_malloc(ctx, bufsiz);
+			out->bp = Memento_label(fz_malloc(ctx, bufsiz), "output_buf");
 			out->wp = out->bp;
 			out->ep = out->bp + bufsiz;
 		}
diff --git a/source/fitz/path.c b/source/fitz/path.c
index eca4a4a..d67ddd2 100644
--- a/source/fitz/path.c
+++ b/source/fitz/path.c
@@ -217,10 +217,10 @@ fz_pack_path(fz_context *ctx, uint8_t *pack_, size_t max, const fz_path *path)
 			pack->coord_len = path->coord_len;
 			pack->cmd_cap = path->cmd_len;
 			pack->cmd_len = path->cmd_len;
-			pack->coords = fz_malloc_array(ctx, path->coord_len, float);
+			pack->coords = Memento_label(fz_malloc_array(ctx, path->coord_len, float), "path_packed_coords");
 			fz_try(ctx)
 			{
-				pack->cmds = fz_malloc_array(ctx, path->cmd_len, uint8_t);
+				pack->cmds = Memento_label(fz_malloc_array(ctx, path->cmd_len, uint8_t), "path_packed_cmds");
 			}
 			fz_catch(ctx)
 			{
diff --git a/source/fitz/printf.c b/source/fitz/printf.c
index 48d795f..6dde461 100644
--- a/source/fitz/printf.c
+++ b/source/fitz/printf.c
@@ -556,7 +556,7 @@ fz_asprintf(fz_context *ctx, const char *fmt, ...)
 	va_start(ap, fmt);
 	len = fz_vsnprintf(NULL, 0, fmt, ap);
 	va_end(ap);
-	mem = fz_malloc(ctx, len+1);
+	mem = Memento_label(fz_malloc(ctx, len+1), "asprintf");
 	va_start(ap, fmt);
 	fz_vsnprintf(mem, len+1, fmt, ap);
 	va_end(ap);
diff --git a/source/fitz/xml.c b/source/fitz/xml.c
index c2da627..1082d00 100644
--- a/source/fitz/xml.c
+++ b/source/fitz/xml.c
@@ -1056,7 +1056,7 @@ static char *convert_to_utf8(fz_context *ctx, unsigned char *s, size_t n, int *d
 
 	if (s[0] == 0xFE && s[1] == 0xFF) {
 		s += 2;
-		dst = d = fz_malloc(ctx, n * FZ_UTFMAX);
+		dst = d = Memento_label(fz_malloc(ctx, n * FZ_UTFMAX), "utf8_from_be");
 		while (s + 1 < e) {
 			c = s[0] << 8 | s[1];
 			d += fz_runetochar(d, c);
@@ -1069,7 +1069,7 @@ static char *convert_to_utf8(fz_context *ctx, unsigned char *s, size_t n, int *d
 
 	if (s[0] == 0xFF && s[1] == 0xFE) {
 		s += 2;
-		dst = d = fz_malloc(ctx, n * FZ_UTFMAX);
+		dst = d = Memento_label(fz_malloc(ctx, n * FZ_UTFMAX), "utf8_from_le");
 		while (s + 1 < e) {
 			c = s[0] | s[1] << 8;
 			d += fz_runetochar(d, c);
@@ -1082,7 +1082,7 @@ static char *convert_to_utf8(fz_context *ctx, unsigned char *s, size_t n, int *d
 
 	table = find_xml_encoding((char*)s);
 	if (table) {
-		dst = d = fz_malloc(ctx, n * FZ_UTFMAX);
+		dst = d = Memento_label(fz_malloc(ctx, n * FZ_UTFMAX), "utf8");
 		while (*s) {
 			c = table[*s++];
 			d += fz_runetochar(d, c);
diff --git a/source/html/epub-doc.c b/source/html/epub-doc.c
index 9cea040..f292c9a 100644
--- a/source/html/epub-doc.c
+++ b/source/html/epub-doc.c
@@ -274,7 +274,7 @@ static void epub_load_accelerator(fz_context *ctx, epub_document *doc, fz_stream
 			}
 
 			acc = fz_malloc_struct(ctx, epub_accelerator);
-			acc->pages_in_chapter = fz_malloc_array(ctx, num_chapters, int);
+			acc->pages_in_chapter = Memento_label(fz_malloc_array(ctx, num_chapters, int), "accel_pages_in_chapter");
 			acc->max_chapters = acc->num_chapters = num_chapters;
 			acc->layout_w = w;
 			acc->layout_h = h;
diff --git a/source/html/html-parse.c b/source/html/html-parse.c
index 680d3b9..fe4e695 100644
--- a/source/html/html-parse.c
+++ b/source/html/html-parse.c
@@ -941,7 +941,7 @@ static char *concat_text(fz_context *ctx, fz_xml *root)
 		const char *text = fz_xml_text(node);
 		n += text ? strlen(text) : 0;
 	}
-	s = fz_malloc(ctx, n);
+	s = Memento_label(fz_malloc(ctx, n), "concat_html");
 	for (node = fz_xml_down(root); node; node = fz_xml_next(node))
 	{
 		const char *text = fz_xml_text(node);
diff --git a/source/pdf/pdf-cmap.c b/source/pdf/pdf-cmap.c
index acf9438..56976d0 100644
--- a/source/pdf/pdf-cmap.c
+++ b/source/pdf/pdf-cmap.c
@@ -775,11 +775,11 @@ pdf_sort_cmap(fz_context *ctx, pdf_cmap *cmap)
 	counts[2] = 0;
 	walk_splay(cmap->tree, cmap->ttop, count_node_types, &counts);
 
-	cmap->ranges = fz_malloc_array(ctx, counts[0], pdf_range);
+	cmap->ranges = Memento_label(fz_malloc_array(ctx, counts[0], pdf_range), "cmap_range");
 	cmap->rcap = counts[0];
-	cmap->xranges = fz_malloc_array(ctx, counts[1], pdf_xrange);
+	cmap->xranges = Memento_label(fz_malloc_array(ctx, counts[1], pdf_xrange), "cmap_xrange");
 	cmap->xcap = counts[1];
-	cmap->mranges = fz_malloc_array(ctx, counts[2], pdf_mrange);
+	cmap->mranges = Memento_label(fz_malloc_array(ctx, counts[2], pdf_mrange), "cmap_mrange");
 	cmap->mcap = counts[2];
 
 	walk_splay(cmap->tree, cmap->ttop, copy_node_types, cmap);
diff --git a/source/pdf/pdf-colorspace.c b/source/pdf/pdf-colorspace.c
index cac5e43..e9d6778 100644
--- a/source/pdf/pdf-colorspace.c
+++ b/source/pdf/pdf-colorspace.c
@@ -182,7 +182,7 @@ load_indexed(fz_context *ctx, pdf_obj *array)
 		high = pdf_to_int(ctx, highobj);
 		high = fz_clampi(high, 0, 255);
 		n = base->n * (high + 1);
-		lookup = fz_malloc(ctx, n);
+		lookup = Memento_label(fz_malloc(ctx, n), "cs_lookup");
 
 		if (pdf_is_string(ctx, lookupobj))
 		{
diff --git a/source/pdf/pdf-font.c b/source/pdf/pdf-font.c
index d376a93..f13e0fb 100644
--- a/source/pdf/pdf-font.c
+++ b/source/pdf/pdf-font.c
@@ -703,7 +703,8 @@ pdf_load_simple_font(fz_context *ctx, pdf_document *doc, pdf_obj *dict)
 		else
 			fz_warn(ctx, "freetype could not find any cmaps");
 
-		etable = fz_malloc_array(ctx, 256, unsigned short);
+		/* FIXME: etable may leak on error. */
+		etable = Memento_label(fz_malloc_array(ctx, 256, unsigned short), "cid_to_gid");
 		fontdesc->size += 256 * sizeof(unsigned short);
 		for (i = 0; i < 256; i++)
 		{
@@ -1069,7 +1070,7 @@ load_cid_font(fz_context *ctx, pdf_document *doc, pdf_obj *dict, pdf_obj *encodi
 
 			len = fz_buffer_storage(ctx, buf, &data);
 			fontdesc->cid_to_gid_len = len / 2;
-			fontdesc->cid_to_gid = fz_malloc_array(ctx, fontdesc->cid_to_gid_len, unsigned short);
+			fontdesc->cid_to_gid = Memento_label(fz_malloc_array(ctx, fontdesc->cid_to_gid_len, unsigned short), "cid_to_gid_map");
 			fontdesc->size += fontdesc->cid_to_gid_len * sizeof(unsigned short);
 			for (z = 0; z < fontdesc->cid_to_gid_len; z++)
 				fontdesc->cid_to_gid[z] = (data[z * 2] << 8) + data[z * 2 + 1];
@@ -1330,7 +1331,7 @@ pdf_make_width_table(fz_context *ctx, pdf_font_desc *fontdesc)
 	}
 
 	font->width_count = n + 1;
-	font->width_table = fz_malloc_array(ctx, font->width_count, short);
+	font->width_table = Memento_label(fz_malloc_array(ctx, font->width_count, short), "font_widths");
 	fontdesc->size += font->width_count * sizeof(short);
 
 	font->width_default = fontdesc->dhmtx.w;
diff --git a/source/pdf/pdf-form.c b/source/pdf/pdf-form.c
index abde08b..c5ef15d 100644
--- a/source/pdf/pdf-form.c
+++ b/source/pdf/pdf-form.c
@@ -693,7 +693,7 @@ static char *get_field_name(fz_context *ctx, pdf_obj *field, int spare)
 	}
 	else
 	{
-		res = fz_malloc(ctx, spare+1);
+		res = Memento_label(fz_malloc(ctx, spare+1), "form_field_name");
 		res[0] = 0;
 	}
 
@@ -1313,7 +1313,7 @@ int pdf_signature_contents(fz_context *ctx, pdf_document *doc, pdf_obj *signatur
 
 		if (contents)
 		{
-			copy = fz_malloc(ctx, len);
+			copy = Memento_label(fz_malloc(ctx, len), "sig_contents");
 			memcpy(copy, s, len);
 		}
 	}
diff --git a/source/pdf/pdf-function.c b/source/pdf/pdf-function.c
index 7d17b0b..ec988fe 100644
--- a/source/pdf/pdf-function.c
+++ b/source/pdf/pdf-function.c
@@ -977,7 +977,7 @@ load_sample_func(fz_context *ctx, pdf_function *func, pdf_obj *dict)
 	if (samplecount > MAX_SAMPLE_FUNCTION_SIZE)
 		fz_throw(ctx, FZ_ERROR_SYNTAX, "sample function too large");
 
-	func->u.sa.samples = fz_malloc_array(ctx, samplecount, float);
+	func->u.sa.samples = Memento_label(fz_malloc_array(ctx, samplecount, float), "function_samples");
 	func->size += samplecount * sizeof(float);
 
 	stream = pdf_open_stream(ctx, dict);
@@ -1224,9 +1224,9 @@ load_stitching_func(fz_context *ctx, pdf_function *func, pdf_obj *dict)
 			fz_throw(ctx, FZ_ERROR_SYNTAX, "recursive function");
 		k = pdf_array_len(ctx, obj);
 
-		func->u.st.funcs = fz_malloc_array(ctx, k, pdf_function*);
-		func->u.st.bounds = fz_malloc_array(ctx, k - 1, float);
-		func->u.st.encode = fz_malloc_array(ctx, k * 2, float);
+		func->u.st.funcs = Memento_label(fz_malloc_array(ctx, k, pdf_function*), "stitch_fns");
+		func->u.st.bounds = Memento_label(fz_malloc_array(ctx, k - 1, float), "stitch_bounds");
+		func->u.st.encode = Memento_label(fz_malloc_array(ctx, k * 2, float), "stitch_encode");
 		funcs = func->u.st.funcs;
 
 		for (i = 0; i < k; i++)
diff --git a/source/pdf/pdf-image.c b/source/pdf/pdf-image.c
index 6d3553f..9ede910 100644
--- a/source/pdf/pdf-image.c
+++ b/source/pdf/pdf-image.c
@@ -425,7 +425,7 @@ raw_or_unknown_compression:
 			size = image->w * n;
 			h = image->h;
 			s = pixmap->samples;
-			d = fz_malloc(ctx, size * h);
+			d = Memento_label(fz_malloc(ctx, size * h), "pdf_image_samples");
 			buffer = fz_new_buffer_from_data(ctx, d, size * h);
 
 			if (n == pixmap->n)
diff --git a/source/pdf/pdf-lex.c b/source/pdf/pdf-lex.c
index f0e7ea4..bb1b07a 100644
--- a/source/pdf/pdf-lex.c
+++ b/source/pdf/pdf-lex.c
@@ -524,7 +524,7 @@ ptrdiff_t pdf_lexbuf_grow(fz_context *ctx, pdf_lexbuf *lb)
 	int newsize = lb->size * 2;
 	if (lb->size == lb->base_size)
 	{
-		lb->scratch = fz_malloc(ctx, newsize);
+		lb->scratch = Memento_label(fz_malloc(ctx, newsize), "pdf_lexbuf");
 		memcpy(lb->scratch, lb->buffer, lb->size);
 	}
 	else
diff --git a/source/pdf/pdf-link.c b/source/pdf/pdf-link.c
index 3669759..6c686d5 100644
--- a/source/pdf/pdf-link.c
+++ b/source/pdf/pdf-link.c
@@ -197,7 +197,7 @@ pdf_parse_link_action(fz_context *ctx, pdf_document *doc, pdf_obj *action, int p
 		{
 			pdf_obj *uri_base_obj = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/URI/Base");
 			const char *uri_base = uri_base_obj ? pdf_to_text_string(ctx, uri_base_obj) : "file://";
-			char *new_uri = fz_malloc(ctx, strlen(uri_base) + strlen(uri) + 1);
+			char *new_uri = Memento_label(fz_malloc(ctx, strlen(uri_base) + strlen(uri) + 1), "link_action");
 			strcpy(new_uri, uri_base);
 			strcat(new_uri, uri);
 			return new_uri;
diff --git a/source/pdf/pdf-object.c b/source/pdf/pdf-object.c
index 9a49388..ebdf180 100644
--- a/source/pdf/pdf-object.c
+++ b/source/pdf/pdf-object.c
@@ -573,7 +573,7 @@ pdf_new_array(fz_context *ctx, pdf_document *doc, int initialcap)
 
 	fz_try(ctx)
 	{
-		obj->items = fz_malloc_array(ctx, obj->cap, pdf_obj*);
+		obj->items = Memento_label(fz_malloc_array(ctx, obj->cap, pdf_obj*), "pdf_array_items");
 	}
 	fz_catch(ctx)
 	{
@@ -901,7 +901,7 @@ pdf_new_dict(fz_context *ctx, pdf_document *doc, int initialcap)
 
 	fz_try(ctx)
 	{
-		DICT(obj)->items = fz_malloc_array(ctx, DICT(obj)->cap, struct keyval);
+		DICT(obj)->items = Memento_label(fz_malloc_array(ctx, DICT(obj)->cap, struct keyval), "dict_items");
 	}
 	fz_catch(ctx)
 	{
@@ -1822,7 +1822,7 @@ static inline void fmt_putc(fz_context *ctx, struct fmt *fmt, int c)
 		fmt->cap *= 2;
 		if (fmt->buf == fmt->ptr)
 		{
-			fmt->ptr = fz_malloc(ctx, fmt->cap);
+			fmt->ptr = Memento_label(fz_malloc(ctx, fmt->cap), "fmt_ptr");
 			memcpy(fmt->ptr, fmt->buf, fmt->len);
 		}
 		else
@@ -2122,7 +2122,7 @@ pdf_sprint_encrypted_obj(fz_context *ctx, char *buf, int cap, int *len, pdf_obj
 	{
 		fmt.cap = 1024;
 		fmt.buf = NULL;
-		fmt.ptr = fz_malloc(ctx, fmt.cap);
+		fmt.ptr = Memento_label(fz_malloc(ctx, fmt.cap), "fmt_buf");
 	}
 	else
 	{
diff --git a/source/pdf/pdf-page.c b/source/pdf/pdf-page.c
index ec39f50..4924782 100644
--- a/source/pdf/pdf-page.c
+++ b/source/pdf/pdf-page.c
@@ -68,7 +68,7 @@ pdf_load_page_tree(fz_context *ctx, pdf_document *doc)
 	if (!doc->rev_page_map)
 	{
 		doc->rev_page_count = pdf_count_pages(ctx, doc);
-		doc->rev_page_map = fz_malloc_array(ctx, doc->rev_page_count, pdf_rev_page_map);
+		doc->rev_page_map = Memento_label(fz_malloc_array(ctx, doc->rev_page_count, pdf_rev_page_map), "pdf_rev_page_map");
 		pdf_load_page_tree_imp(ctx, doc, pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/Pages"), 0);
 		qsort(doc->rev_page_map, doc->rev_page_count, sizeof *doc->rev_page_map, cmp_rev_page_map);
 	}
diff --git a/source/pdf/pdf-parse.c b/source/pdf/pdf-parse.c
index 80d9a69..1272ca2 100644
--- a/source/pdf/pdf-parse.c
+++ b/source/pdf/pdf-parse.c
@@ -153,7 +153,7 @@ pdf_new_utf8_from_pdf_string(fz_context *ctx, const char *ssrcptr, size_t srclen
 			}
 		}
 
-		dstptr = dst = fz_malloc(ctx, dstlen + 1);
+		dstptr = dst = Memento_label(fz_malloc(ctx, dstlen + 1), "utf8_from_utf16be");
 
 		i = 2;
 		while (i + 2 <= srclen)
@@ -185,7 +185,7 @@ pdf_new_utf8_from_pdf_string(fz_context *ctx, const char *ssrcptr, size_t srclen
 			}
 		}
 
-		dstptr = dst = fz_malloc(ctx, dstlen + 1);
+		dstptr = dst = Memento_label(fz_malloc(ctx, dstlen + 1), "utf8_from_utf16le");
 
 		i = 2;
 		while (i + 2 <= srclen)
@@ -217,7 +217,7 @@ pdf_new_utf8_from_pdf_string(fz_context *ctx, const char *ssrcptr, size_t srclen
 			}
 		}
 
-		dstptr = dst = fz_malloc(ctx, dstlen + 1);
+		dstptr = dst = Memento_label(fz_malloc(ctx, dstlen + 1), "utf8_from_utf8");
 
 		i = 3;
 		while (i < srclen)
@@ -236,7 +236,7 @@ pdf_new_utf8_from_pdf_string(fz_context *ctx, const char *ssrcptr, size_t srclen
 		for (i = 0; i < srclen; i++)
 			dstlen += fz_runelen(fz_unicode_from_pdf_doc_encoding[srcptr[i]]);
 
-		dstptr = dst = fz_malloc(ctx, dstlen + 1);
+		dstptr = dst = Memento_label(fz_malloc(ctx, dstlen + 1), "utf8_from_pdfdocenc");
 
 		for (i = 0; i < srclen; i++)
 		{
diff --git a/source/pdf/pdf-repair.c b/source/pdf/pdf-repair.c
index fc49d88..af9a4dd 100644
--- a/source/pdf/pdf-repair.c
+++ b/source/pdf/pdf-repair.c
@@ -664,14 +664,13 @@ pdf_repair_xref(fz_context *ctx, pdf_document *doc)
 			pdf_drop_obj(ctx, id);
 			id = NULL;
 		}
-
-		fz_free(ctx, list);
 	}
 	fz_always(ctx)
 	{
 		for (i = 0; i < num_roots; i++)
 			pdf_drop_obj(ctx, roots[i]);
 		fz_free(ctx, roots);
+		fz_free(ctx, list);
 	}
 	fz_catch(ctx)
 	{
@@ -679,7 +678,6 @@ pdf_repair_xref(fz_context *ctx, pdf_document *doc)
 		pdf_drop_obj(ctx, id);
 		pdf_drop_obj(ctx, obj);
 		pdf_drop_obj(ctx, info);
-		fz_free(ctx, list);
 		fz_rethrow(ctx);
 	}
 }
diff --git a/source/pdf/pdf-shade.c b/source/pdf/pdf-shade.c
index 092c30e..11afad1 100644
--- a/source/pdf/pdf-shade.c
+++ b/source/pdf/pdf-shade.c
@@ -71,7 +71,7 @@ pdf_load_function_based_shading(fz_context *ctx, pdf_document *doc, fz_shade *sh
 	shade->u.f.matrix = pdf_dict_get_matrix(ctx, dict, PDF_NAME(Matrix));
 	shade->u.f.xdivs = FUNSEGS;
 	shade->u.f.ydivs = FUNSEGS;
-	shade->u.f.fn_vals = fz_malloc(ctx, (FUNSEGS+1)*(FUNSEGS+1)*n*sizeof(float));
+	shade->u.f.fn_vals = Memento_label(fz_malloc(ctx, (FUNSEGS+1)*(FUNSEGS+1)*n*sizeof(float)), "shade_fn_vals");
 	shade->u.f.domain[0][0] = x0;
 	shade->u.f.domain[0][1] = y0;
 	shade->u.f.domain[1][0] = x1;
diff --git a/source/pdf/pdf-unicode.c b/source/pdf/pdf-unicode.c
index 4dd01ba..9fe6b84 100644
--- a/source/pdf/pdf-unicode.c
+++ b/source/pdf/pdf-unicode.c
@@ -110,7 +110,7 @@ pdf_load_to_unicode(fz_context *ctx, pdf_document *doc, pdf_font_desc *font,
 	{
 		/* TODO one-to-many mappings */
 
-		font->cid_to_ucs = fz_malloc_array(ctx, 256, unsigned short);
+		font->cid_to_ucs = Memento_label(fz_malloc_array(ctx, 256, unsigned short), "cid_to_ucs");
 		font->cid_to_ucs_len = 256;
 		font->size += 256 * sizeof *font->cid_to_ucs;
 
diff --git a/source/pdf/pdf-write.c b/source/pdf/pdf-write.c
index 6b2d818..02ae01d 100644
--- a/source/pdf/pdf-write.c
+++ b/source/pdf/pdf-write.c
@@ -911,7 +911,7 @@ static void renumberobjs(fz_context *ctx, pdf_document *doc, pdf_write_state *op
 		}
 
 		/* Create new table for the reordered, compacted xref */
-		newxref = fz_malloc_array(ctx, xref_len + 3, pdf_xref_entry);
+		newxref = Memento_label(fz_malloc_array(ctx, xref_len + 3, pdf_xref_entry), "pdf_xref_entries");
 		newxref[0] = *pdf_get_xref_entry(ctx, doc, 0);
 
 		/* Move used objects into the new compacted xref */
@@ -1578,7 +1578,7 @@ static fz_buffer *hexbuf(fz_context *ctx, const unsigned char *p, size_t n)
 	static const char hex[17] = "0123456789abcdef";
 	int x = 0;
 	size_t len = n * 2 + (n / 32) + 1;
-	unsigned char *data = fz_malloc(ctx, len);
+	unsigned char *data = Memento_label(fz_malloc(ctx, len), "hexbuf");
 	fz_buffer *buf = fz_new_buffer_from_data(ctx, data, len);
 
 	while (n--)
@@ -1660,7 +1660,7 @@ static fz_buffer *deflatebuf(fz_context *ctx, const unsigned char *p, size_t n)
 		fz_throw(ctx, FZ_ERROR_GENERIC, "Buffer too large to deflate");
 
 	cap = compressBound(longN);
-	data = fz_malloc(ctx, cap);
+	data = Memento_label(fz_malloc(ctx, cap), "pdf_write_deflate");
 	buf = fz_new_buffer_from_data(ctx, data, cap);
 	csize = (uLongf)cap;
 	t = compress(data, &csize, p, longN);

http://git.ghostscript.com/?p=mupdf.git;a=commit;h=6896bc2f73ef19e3530283f2ae6e265e754df9d3

--
MuPDF library
Artifex Software, Inc.