From b557ece715c9324d4e3cde10bda94d6ae90311cc Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 4 May 2016 21:19:38 +0200 Subject: Reference count fz_device. Language bindings sometimes require objects to be reference counted. --- include/mupdf/fitz/device.h | 3 +++ include/mupdf/fitz/writer.h | 3 ++- source/fitz/device.c | 23 ++++++++++++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/include/mupdf/fitz/device.h b/include/mupdf/fitz/device.h index f9295349..caa01cb2 100644 --- a/include/mupdf/fitz/device.h +++ b/include/mupdf/fitz/device.h @@ -98,6 +98,7 @@ enum struct fz_device_s { + int refs; int hints; int flags; @@ -178,6 +179,8 @@ void fz_close_device(fz_context *ctx, fz_device *dev); */ void fz_drop_device(fz_context *ctx, fz_device *dev); +fz_device *fz_keep_device(fz_context *ctx, fz_device *dev); + /* fz_enable_device_hints : Enable hints in a device. diff --git a/include/mupdf/fitz/writer.h b/include/mupdf/fitz/writer.h index 5d2b06ba..064b8465 100644 --- a/include/mupdf/fitz/writer.h +++ b/include/mupdf/fitz/writer.h @@ -13,7 +13,7 @@ struct fz_document_writer_s { fz_device *(*begin_page)(fz_context *ctx, fz_document_writer *wri, const fz_rect *mediabox, fz_matrix *ctm); void (*end_page)(fz_context *ctx, fz_document_writer *wri, fz_device *dev); - void (*drop_imp)(fz_context *ctx, fz_document_writer *wri); + void (*close)(fz_context *ctx, fz_document_writer *wri); }; int fz_has_option(fz_context *ctx, const char *opts, const char *key, const char **val); @@ -22,6 +22,7 @@ fz_document_writer *fz_new_document_writer(fz_context *ctx, const char *path, co fz_device *fz_begin_page(fz_context *ctx, fz_document_writer *wri, const fz_rect *mediabox, fz_matrix *ctm); void fz_end_page(fz_context *ctx, fz_document_writer *wri, fz_device *dev); +void fz_close_document_writer(fz_context *ctx, fz_document_writer *wri); void fz_drop_document_writer(fz_context *ctx, fz_document_writer *wri); fz_document_writer *fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options); diff --git a/source/fitz/device.c b/source/fitz/device.c index df2d4f86..4379bb21 100644 --- a/source/fitz/device.c +++ b/source/fitz/device.c @@ -3,7 +3,9 @@ void * fz_new_device(fz_context *ctx, int size) { - return Memento_label(fz_calloc(ctx, 1, size), "fz_device"); + fz_device *dev = Memento_label(fz_calloc(ctx, 1, size), "fz_device"); + dev->refs = 1; + return dev; } void @@ -40,15 +42,22 @@ fz_close_device(fz_context *ctx, fz_device *dev) dev->end_tile = NULL; } +fz_device * +fz_keep_device(fz_context *ctx, fz_device *dev) +{ + return fz_keep_imp(ctx, dev, &dev->refs); +} + void fz_drop_device(fz_context *ctx, fz_device *dev) { - if (dev == NULL) - return; - if (dev->close) - dev->close(ctx, dev); - fz_free(ctx, dev->container); - fz_free(ctx, dev); + if (fz_drop_imp(ctx, dev, &dev->refs)) + { + if (dev->close) + dev->close(ctx, dev); + fz_free(ctx, dev->container); + fz_free(ctx, dev); + } } void -- cgit v1.2.3