From 2be507bfb199e8c80e4b2ee1cb2c867b57fc1f6e Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 8 Jul 2016 14:24:54 +0200 Subject: Use fz_keep_imp and fz_drop_imp for all reference counting. --- source/fitz/bitmap.c | 6 ++---- source/fitz/buffer.c | 8 ++------ source/fitz/document.c | 27 ++++++++++----------------- source/fitz/halftone.c | 16 +++++++--------- source/fitz/path.c | 7 ++----- source/fitz/separation.c | 20 +++----------------- source/fitz/store.c | 26 +++++++++++--------------- source/fitz/stream-open.c | 9 ++------- source/gprf/gprf-doc.c | 13 ++----------- source/pdf/pdf-graft.c | 6 ++---- source/pdf/pdf-object.c | 19 ++++++++++--------- source/pdf/pdf-pkcs7.c | 15 +++++---------- 12 files changed, 58 insertions(+), 114 deletions(-) (limited to 'source') diff --git a/source/fitz/bitmap.c b/source/fitz/bitmap.c index b610737a..dc2645ec 100644 --- a/source/fitz/bitmap.c +++ b/source/fitz/bitmap.c @@ -284,15 +284,13 @@ fz_new_bitmap(fz_context *ctx, int w, int h, int n, int xres, int yres) fz_bitmap * fz_keep_bitmap(fz_context *ctx, fz_bitmap *bit) { - if (bit) - bit->refs++; - return bit; + return fz_keep_imp(ctx, bit, &bit->refs); } void fz_drop_bitmap(fz_context *ctx, fz_bitmap *bit) { - if (bit && --bit->refs == 0) + if (fz_drop_imp(ctx, bit, &bit->refs)) { fz_free(ctx, bit->samples); fz_free(ctx, bit); diff --git a/source/fitz/buffer.c b/source/fitz/buffer.c index 190088e3..ba3451bf 100644 --- a/source/fitz/buffer.c +++ b/source/fitz/buffer.c @@ -90,17 +90,13 @@ fz_new_buffer_from_base64(fz_context *ctx, const char *data, size_t size) fz_buffer * fz_keep_buffer(fz_context *ctx, fz_buffer *buf) { - if (buf) - buf->refs ++; - return buf; + return fz_keep_imp(ctx, buf, &buf->refs); } void fz_drop_buffer(fz_context *ctx, fz_buffer *buf) { - if (!buf) - return; - if (--buf->refs == 0) + if (fz_drop_imp(ctx, buf, &buf->refs)) { if (!buf->shared) fz_free(ctx, buf->data); diff --git a/source/fitz/document.c b/source/fitz/document.c index ad602bc2..b2590f07 100644 --- a/source/fitz/document.c +++ b/source/fitz/document.c @@ -142,16 +142,15 @@ fz_new_document_of_size(fz_context *ctx, int size) fz_document * fz_keep_document(fz_context *ctx, fz_document *doc) { - if (doc) - ++doc->refs; - return doc; + return fz_keep_imp(ctx, doc, &doc->refs); } void fz_drop_document(fz_context *ctx, fz_document *doc) { - if (doc && --doc->refs == 0 && doc->drop_document) - doc->drop_document(ctx, doc); + if (fz_drop_imp(ctx, doc, &doc->refs)) + if (doc->drop_document) + doc->drop_document(ctx, doc); } static void @@ -352,15 +351,13 @@ fz_new_annot(fz_context *ctx, int size) fz_annot * fz_keep_annot(fz_context *ctx, fz_annot *annot) { - if (annot) - ++annot->refs; - return annot; + return fz_keep_imp(ctx, annot, &annot->refs); } void fz_drop_annot(fz_context *ctx, fz_annot *annot) { - if (annot && --annot->refs == 0) + if (fz_drop_imp(ctx, annot, &annot->refs)) { if (annot->drop_annot) annot->drop_annot(ctx, annot); @@ -379,21 +376,17 @@ fz_new_page(fz_context *ctx, int size) fz_page * fz_keep_page(fz_context *ctx, fz_page *page) { - if (page) - ++page->refs; - return page; + return fz_keep_imp(ctx, page, &page->refs); } void fz_drop_page(fz_context *ctx, fz_page *page) { - if (page) + if (fz_drop_imp(ctx, page, &page->refs)) { - if (--page->refs == 0 && page->drop_page) - { + if (page->drop_page) page->drop_page(ctx, page); - fz_free(ctx, page); - } + fz_free(ctx, page); } } diff --git a/source/fitz/halftone.c b/source/fitz/halftone.c index eb35dbdf..02353e76 100644 --- a/source/fitz/halftone.c +++ b/source/fitz/halftone.c @@ -18,21 +18,19 @@ fz_new_halftone(fz_context *ctx, int comps) fz_halftone * fz_keep_halftone(fz_context *ctx, fz_halftone *ht) { - if (ht) - ht->refs++; - return ht; + return fz_keep_imp(ctx, ht, &ht->refs); } void fz_drop_halftone(fz_context *ctx, fz_halftone *ht) { int i; - - if (!ht || --ht->refs != 0) - return; - for (i = 0; i < ht->n; i++) - fz_drop_pixmap(ctx, ht->comp[i]); - fz_free(ctx, ht); + if (fz_drop_imp(ctx, ht, &ht->refs)) + { + for (i = 0; i < ht->n; i++) + fz_drop_pixmap(ctx, ht->comp[i]); + fz_free(ctx, ht); + } } /* Default mono halftone, lifted from Ghostscript. */ diff --git a/source/fitz/path.c b/source/fitz/path.c index 0fce853b..3befcf23 100644 --- a/source/fitz/path.c +++ b/source/fitz/path.c @@ -1494,7 +1494,7 @@ fz_clone_stroke_state(fz_context *ctx, fz_stroke_state *stroke) fz_stroke_state * fz_unshare_stroke_state_with_dash_len(fz_context *ctx, fz_stroke_state *shared, int len) { - int single, unsize, shsize, shlen, drop; + int single, unsize, shsize, shlen; fz_stroke_state *unshared; fz_lock(ctx, FZ_LOCK_ALLOC); @@ -1516,10 +1516,7 @@ fz_unshare_stroke_state_with_dash_len(fz_context *ctx, fz_stroke_state *shared, memcpy(unshared, shared, (shsize > unsize ? unsize : shsize)); unshared->refs = 1; - fz_lock(ctx, FZ_LOCK_ALLOC); - drop = (shared->refs > 0 ? --shared->refs == 0 : 0); - fz_unlock(ctx, FZ_LOCK_ALLOC); - if (drop) + if (fz_drop_imp(ctx, shared, &shared->refs)) fz_free(ctx, shared); return unshared; } diff --git a/source/fitz/separation.c b/source/fitz/separation.c index e18691cf..9968b574 100644 --- a/source/fitz/separation.c +++ b/source/fitz/separation.c @@ -22,34 +22,20 @@ fz_separations *fz_new_separations(fz_context *ctx) fz_separations *fz_keep_separations(fz_context *ctx, fz_separations *sep) { - int i; - if (!ctx || !sep) return NULL; - fz_lock(ctx, FZ_LOCK_ALLOC); - i = sep->refs; - if (i > 0) - sep->refs++; - fz_unlock(ctx, FZ_LOCK_ALLOC); - - return sep; + return fz_keep_imp(ctx, sep, &sep->refs); } void fz_drop_separations(fz_context *ctx, fz_separations *sep) { - int i; - if (!ctx || !sep) return; - fz_lock(ctx, FZ_LOCK_ALLOC); - i = sep->refs; - if (i > 0) - sep->refs--; - fz_unlock(ctx, FZ_LOCK_ALLOC); - if (i == 1) + if (fz_drop_imp(ctx, sep, &sep->refs)) { + int i; for (i = 0; i < sep->num_separations; i++) fz_free(ctx, sep->name[i]); fz_free(ctx, sep); diff --git a/source/fitz/store.c b/source/fitz/store.c index 862c179c..f8feb591 100644 --- a/source/fitz/store.c +++ b/source/fitz/store.c @@ -101,8 +101,10 @@ evict(fz_context *ctx, fz_item *item) item->prev->next = item->next; else store->head = item->next; + /* Drop a reference to the value (freeing if required) */ drop = (item->val->refs > 0 && --item->val->refs == 0); + /* Remove from the hash table */ if (item->type->make_hash_key) { @@ -114,6 +116,7 @@ evict(fz_context *ctx, fz_item *item) fz_unlock(ctx, FZ_LOCK_ALLOC); if (drop) item->val->drop(ctx, item->val); + /* Always drops the key and drop the item */ item->type->drop_key(ctx, item->key); fz_free(ctx, item); @@ -457,28 +460,21 @@ fz_keep_store_context(fz_context *ctx) { if (ctx == NULL || ctx->store == NULL) return NULL; - fz_lock(ctx, FZ_LOCK_ALLOC); - ctx->store->refs++; - fz_unlock(ctx, FZ_LOCK_ALLOC); - return ctx->store; + return fz_keep_imp(ctx, ctx->store, &ctx->store->refs); } void fz_drop_store_context(fz_context *ctx) { - int refs; if (ctx == NULL || ctx->store == NULL) return; - fz_lock(ctx, FZ_LOCK_ALLOC); - refs = --ctx->store->refs; - fz_unlock(ctx, FZ_LOCK_ALLOC); - if (refs != 0) - return; - - fz_empty_store(ctx); - fz_drop_hash(ctx, ctx->store->hash); - fz_free(ctx, ctx->store); - ctx->store = NULL; + if (fz_drop_imp(ctx, ctx->store, &ctx->store->refs)) + { + fz_empty_store(ctx); + fz_drop_hash(ctx, ctx->store->hash); + fz_free(ctx, ctx->store); + ctx->store = NULL; + } } static void diff --git a/source/fitz/stream-open.c b/source/fitz/stream-open.c index 142df7c2..05cbc48a 100644 --- a/source/fitz/stream-open.c +++ b/source/fitz/stream-open.c @@ -46,18 +46,13 @@ fz_new_stream(fz_context *ctx, void *state, fz_stream_next_fn *next, fz_stream_c fz_stream * fz_keep_stream(fz_context *ctx, fz_stream *stm) { - if (Memento_takeRef(stm)) - stm->refs ++; - return stm; + return fz_keep_imp(ctx, stm, &stm->refs); } void fz_drop_stream(fz_context *ctx, fz_stream *stm) { - if (!Memento_dropRef(stm)) - return; - stm->refs --; - if (stm->refs == 0) + if (fz_drop_imp(ctx, stm, &stm->refs)) { if (stm->close) stm->close(ctx, stm->state); diff --git a/source/gprf/gprf-doc.c b/source/gprf/gprf-doc.c index 3b97d5ae..e7458043 100644 --- a/source/gprf/gprf-doc.c +++ b/source/gprf/gprf-doc.c @@ -69,25 +69,16 @@ fz_keep_gprf_file(fz_context *ctx, gprf_file *file) if (!ctx || !file) return NULL; - fz_lock(ctx, FZ_LOCK_ALLOC); - file->refs++; - fz_unlock(ctx, FZ_LOCK_ALLOC); - - return file; + return fz_keep_imp(ctx, file, &file->refs); } static void fz_drop_gprf_file(fz_context *ctx, gprf_file *file) { - int i; - if (!ctx || !file) return; - fz_lock(ctx, FZ_LOCK_ALLOC); - i = --file->refs; - fz_unlock(ctx, FZ_LOCK_ALLOC); - if (i == 0) + if (fz_drop_imp(ctx, file, &file->refs)) { unlink(file->filename); fz_free(ctx, file->filename); diff --git a/source/pdf/pdf-graft.c b/source/pdf/pdf-graft.c index d7cb8b42..64072eb0 100644 --- a/source/pdf/pdf-graft.c +++ b/source/pdf/pdf-graft.c @@ -33,15 +33,13 @@ pdf_new_graft_map(fz_context *ctx, pdf_document *src) pdf_graft_map * fz_keep_graft_map(fz_context *ctx, pdf_graft_map *map) { - if (map) - ++map->refs; - return map; + return fz_keep_imp(ctx, map, &map->refs); } void pdf_drop_graft_map(fz_context *ctx, pdf_graft_map *map) { - if (map && --map->refs == 0) + if (fz_drop_imp(ctx, map, &map->refs)) { fz_drop_document(ctx, (fz_document*)map->src); fz_free(ctx, map->dst_from_src); diff --git a/source/pdf/pdf-object.c b/source/pdf/pdf-object.c index b189a125..2ac8ae85 100644 --- a/source/pdf/pdf-object.c +++ b/source/pdf/pdf-object.c @@ -204,7 +204,7 @@ pdf_keep_obj(fz_context *ctx, pdf_obj *obj) if (obj >= PDF_OBJ__LIMIT) { (void)Memento_takeRef(obj); - obj->refs ++; + (void)fz_keep_imp16(ctx, obj, &obj->refs); } return obj; } @@ -1732,14 +1732,15 @@ pdf_drop_obj(fz_context *ctx, pdf_obj *obj) if (obj >= PDF_OBJ__LIMIT) { (void)Memento_dropRef(obj); - if (--obj->refs) - return; - if (obj->kind == PDF_ARRAY) - pdf_drop_array(ctx, obj); - else if (obj->kind == PDF_DICT) - pdf_drop_dict(ctx, obj); - else - fz_free(ctx, obj); + if (fz_drop_imp16(ctx, obj, &obj->refs)) + { + if (obj->kind == PDF_ARRAY) + pdf_drop_array(ctx, obj); + else if (obj->kind == PDF_DICT) + pdf_drop_dict(ctx, obj); + else + fz_free(ctx, obj); + } } } diff --git a/source/pdf/pdf-pkcs7.c b/source/pdf/pdf-pkcs7.c index aaa18300..64938fb3 100644 --- a/source/pdf/pdf-pkcs7.c +++ b/source/pdf/pdf-pkcs7.c @@ -507,21 +507,16 @@ pdf_signer *pdf_read_pfx(fz_context *ctx, const char *pfile, const char *pw) pdf_signer *pdf_keep_signer(fz_context *ctx, pdf_signer *signer) { - if (signer) - signer->refs++; - return signer; + return fz_keep_imp(ctx, signer, &signer->refs); } void pdf_drop_signer(fz_context *ctx, pdf_signer *signer) { - if (signer) + if (fz_drop_imp(ctx, signer, &signer->refs)) { - if (--signer->refs == 0) - { - X509_free(signer->x509); - EVP_PKEY_free(signer->pkey); - fz_free(ctx, signer); - } + X509_free(signer->x509); + EVP_PKEY_free(signer->pkey); + fz_free(ctx, signer); } } -- cgit v1.2.3