summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mupdf/fitz/context.h30
-rw-r--r--source/fitz/colorspace.c15
-rw-r--r--source/fitz/context.c26
-rw-r--r--source/fitz/draw-device.c23
-rw-r--r--source/fitz/font.c29
-rw-r--r--source/fitz/hash.c8
-rw-r--r--source/fitz/image.c18
-rw-r--r--source/fitz/path.c25
-rw-r--r--source/fitz/store.c36
-rw-r--r--source/fitz/text.c7
10 files changed, 73 insertions, 144 deletions
diff --git a/include/mupdf/fitz/context.h b/include/mupdf/fitz/context.h
index 79406942..f694bc39 100644
--- a/include/mupdf/fitz/context.h
+++ b/include/mupdf/fitz/context.h
@@ -457,4 +457,34 @@ fz_unlock(fz_context *ctx, int lock)
ctx->locks->unlock(ctx->locks->user, lock);
}
+static inline void *
+fz_keep_imp(fz_context *ctx, void *p, int *refs)
+{
+ if (p)
+ {
+ fz_lock(ctx, FZ_LOCK_ALLOC);
+ if (*refs > 0)
+ ++*refs;
+ fz_unlock(ctx, FZ_LOCK_ALLOC);
+ }
+ return p;
+}
+
+static inline int
+fz_drop_imp(fz_context *ctx, void *p, int *refs)
+{
+ if (p)
+ {
+ int drop;
+ fz_lock(ctx, FZ_LOCK_ALLOC);
+ if (*refs > 0)
+ drop = --*refs == 0;
+ else
+ drop = 0;
+ fz_unlock(ctx, FZ_LOCK_ALLOC);
+ return drop;
+ }
+ return 0;
+}
+
#endif
diff --git a/source/fitz/colorspace.c b/source/fitz/colorspace.c
index 42aa6129..d145be27 100644
--- a/source/fitz/colorspace.c
+++ b/source/fitz/colorspace.c
@@ -198,23 +198,16 @@ void fz_new_colorspace_context(fz_context *ctx)
fz_colorspace_context *
fz_keep_colorspace_context(fz_context *ctx)
{
- if (!ctx || !ctx->colorspace)
+ if (!ctx)
return NULL;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- ctx->colorspace->ctx_refs++;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return ctx->colorspace;
+ return fz_keep_imp(ctx, ctx->colorspace, &ctx->colorspace->ctx_refs);
}
void fz_drop_colorspace_context(fz_context *ctx)
{
- int drop;
- if (!ctx || !ctx->colorspace)
+ if (!ctx)
return;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- drop = --ctx->colorspace->ctx_refs;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (drop == 0)
+ if (fz_drop_imp(ctx, ctx->colorspace, &ctx->colorspace->ctx_refs))
fz_free(ctx, ctx->colorspace);
}
diff --git a/source/fitz/context.c b/source/fitz/context.c
index b3ca9b5b..7a2076b7 100644
--- a/source/fitz/context.c
+++ b/source/fitz/context.c
@@ -9,16 +9,10 @@ struct fz_id_context_s
static void
fz_drop_id_context(fz_context *ctx)
{
- int refs;
- fz_id_context *id = ctx->id;
-
- if (id == NULL)
+ if (!ctx)
return;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- refs = --id->refs;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (refs == 0)
- fz_free(ctx, id);
+ if (fz_drop_imp(ctx, ctx->id, &ctx->id->refs))
+ fz_free(ctx, ctx->id);
}
static void
@@ -32,14 +26,9 @@ fz_new_id_context(fz_context *ctx)
static fz_id_context *
fz_keep_id_context(fz_context *ctx)
{
- fz_id_context *id = ctx->id;
-
- if (id == NULL)
+ if (!ctx)
return NULL;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- ++id->refs;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return id;
+ return fz_keep_imp(ctx, ctx->id, &ctx->id->refs);
}
void
@@ -208,12 +197,9 @@ fz_gen_id(fz_context *ctx)
{
int id;
fz_lock(ctx, FZ_LOCK_ALLOC);
- /* We'll never wrap around in normal use, but if we *do*, then avoid
- * 0. */
+ /* We'll never wrap around in normal use, but if we do, then avoid 0. */
do
- {
id = ++ctx->id->id;
- }
while (id == 0);
fz_unlock(ctx, FZ_LOCK_ALLOC);
return id;
diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c
index 18ea53d9..1c093fc6 100644
--- a/source/fitz/draw-device.c
+++ b/source/fitz/draw-device.c
@@ -1691,27 +1691,15 @@ static void *
fz_keep_tile_key(fz_context *ctx, void *key_)
{
tile_key *key = (tile_key *)key_;
-
- fz_lock(ctx, FZ_LOCK_ALLOC);
- key->refs++;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
-
- return (void *)key;
+ return fz_keep_imp(ctx, key, &key->refs);
}
static void
fz_drop_tile_key(fz_context *ctx, void *key_)
{
tile_key *key = (tile_key *)key_;
- int drop;
-
- fz_lock(ctx, FZ_LOCK_ALLOC);
- drop = --key->refs;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (drop == 0)
- {
+ if (fz_drop_imp(ctx, key, &key->refs))
fz_free(ctx, key);
- }
}
static int
@@ -1719,7 +1707,6 @@ fz_cmp_tile_key(fz_context *ctx, void *k0_, void *k1_)
{
tile_key *k0 = (tile_key *)k0_;
tile_key *k1 = (tile_key *)k1_;
-
return k0->id == k1->id && k0->ctm[0] == k1->ctm[0] && k0->ctm[1] == k1->ctm[1] && k0->ctm[2] == k1->ctm[2] && k0->ctm[3] == k1->ctm[3];
}
@@ -1728,7 +1715,6 @@ static void
fz_debug_tile(fz_context *ctx, FILE *out, void *key_)
{
tile_key *key = (tile_key *)key_;
-
fprintf(out, "(tile id=%x, ctm=%g %g %g %g) ", key->id, key->ctm[0], key->ctm[1], key->ctm[2], key->ctm[3]);
}
#endif
@@ -1747,10 +1733,7 @@ static fz_store_type fz_tile_store_type =
static void
fz_drop_tile_record_imp(fz_context *ctx, fz_storable *storable)
{
- tile_record *tr = (tile_record *)(void *)storable;
-
- if (tr == NULL)
- return;
+ tile_record *tr = (tile_record *)storable;
fz_drop_pixmap(ctx, tr->dest);
fz_drop_pixmap(ctx, tr->shape);
fz_free(ctx, tr);
diff --git a/source/fitz/font.c b/source/fitz/font.c
index e35f01fa..5f2f85e1 100644
--- a/source/fitz/font.c
+++ b/source/fitz/font.c
@@ -74,12 +74,7 @@ fz_new_font(fz_context *ctx, const char *name, int use_glyph_bbox, int glyph_cou
fz_font *
fz_keep_font(fz_context *ctx, fz_font *font)
{
- if (!font)
- return NULL;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- font->refs ++;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return font;
+ return fz_keep_imp(ctx, font, &font->refs);
}
static void
@@ -119,12 +114,9 @@ void
fz_drop_font(fz_context *ctx, fz_font *font)
{
int fterr;
- int i, drop;
+ int i;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- drop = (font && --font->refs == 0);
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (!drop)
+ if (!fz_drop_imp(ctx, font, &font->refs))
return;
free_resources(ctx, font);
@@ -214,23 +206,16 @@ void fz_new_font_context(fz_context *ctx)
fz_font_context *
fz_keep_font_context(fz_context *ctx)
{
- if (!ctx || !ctx->font)
+ if (!ctx)
return NULL;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- ctx->font->ctx_refs++;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return ctx->font;
+ return fz_keep_imp(ctx, ctx->font, &ctx->font->ctx_refs);
}
void fz_drop_font_context(fz_context *ctx)
{
- int drop;
- if (!ctx || !ctx->font)
+ if (!ctx)
return;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- drop = --ctx->font->ctx_refs;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (drop == 0)
+ if (fz_drop_imp(ctx, ctx->font, &ctx->font->ctx_refs))
fz_free(ctx, ctx->font);
}
diff --git a/source/fitz/hash.c b/source/fitz/hash.c
index acd22334..f80e79b3 100644
--- a/source/fitz/hash.c
+++ b/source/fitz/hash.c
@@ -159,10 +159,10 @@ fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize)
}
if (table->lock == FZ_LOCK_ALLOC)
- fz_unlock(ctx, FZ_LOCK_ALLOC);
+ fz_unlock(ctx, table->lock);
newents = fz_malloc_array_no_throw(ctx, newsize, sizeof(fz_hash_entry));
if (table->lock == FZ_LOCK_ALLOC)
- fz_lock(ctx, FZ_LOCK_ALLOC);
+ fz_lock(ctx, table->lock);
if (table->lock >= 0)
{
if (table->size >= newsize)
@@ -192,10 +192,10 @@ fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize)
}
if (table->lock == FZ_LOCK_ALLOC)
- fz_unlock(ctx, FZ_LOCK_ALLOC);
+ fz_unlock(ctx, table->lock);
fz_free(ctx, oldents);
if (table->lock == FZ_LOCK_ALLOC)
- fz_lock(ctx, FZ_LOCK_ALLOC);
+ fz_lock(ctx, table->lock);
}
void *
diff --git a/source/fitz/image.c b/source/fitz/image.c
index 4c82e2c7..77999ff1 100644
--- a/source/fitz/image.c
+++ b/source/fitz/image.c
@@ -34,7 +34,6 @@ static int
fz_make_hash_image_key(fz_context *ctx, fz_store_hash *hash, void *key_)
{
fz_image_key *key = (fz_image_key *)key_;
-
hash->u.pi.ptr = key->image;
hash->u.pi.i = key->l2factor;
return 1;
@@ -44,26 +43,14 @@ static void *
fz_keep_image_key(fz_context *ctx, void *key_)
{
fz_image_key *key = (fz_image_key *)key_;
-
- fz_lock(ctx, FZ_LOCK_ALLOC);
- key->refs++;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
-
- return (void *)key;
+ return fz_keep_imp(ctx, key, &key->refs);
}
static void
fz_drop_image_key(fz_context *ctx, void *key_)
{
fz_image_key *key = (fz_image_key *)key_;
- int drop;
-
- if (key == NULL)
- return;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- drop = --key->refs;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (drop == 0)
+ if (fz_drop_imp(ctx, key, &key->refs))
{
fz_drop_image(ctx, key->image);
fz_free(ctx, key);
@@ -75,7 +62,6 @@ fz_cmp_image_key(fz_context *ctx, void *k0_, void *k1_)
{
fz_image_key *k0 = (fz_image_key *)k0_;
fz_image_key *k1 = (fz_image_key *)k1_;
-
return k0->image == k1->image && k0->l2factor == k1->l2factor;
}
diff --git a/source/fitz/path.c b/source/fitz/path.c
index 4712ea35..8a085070 100644
--- a/source/fitz/path.c
+++ b/source/fitz/path.c
@@ -20,16 +20,13 @@ fz_new_path(fz_context *ctx)
fz_path *
fz_keep_path(fz_context *ctx, fz_path *path)
{
- ++path->refs;
- return path;
+ return fz_keep_imp(ctx, path, &path->refs);
}
void
fz_drop_path(fz_context *ctx, fz_path *path)
{
- if (path == NULL)
- return;
- if (--path->refs == 0)
+ if (fz_drop_imp(ctx, path, &path->refs))
{
fz_free(ctx, path->cmds);
fz_free(ctx, path->coords);
@@ -366,25 +363,13 @@ fz_keep_stroke_state(fz_context *ctx, fz_stroke_state *stroke)
if (stroke->refs == -2)
return fz_clone_stroke_state(ctx, stroke);
- fz_lock(ctx, FZ_LOCK_ALLOC);
- if (stroke->refs > 0)
- stroke->refs++;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return stroke;
+ return fz_keep_imp(ctx, stroke, &stroke->refs);
}
void
fz_drop_stroke_state(fz_context *ctx, fz_stroke_state *stroke)
{
- int drop;
-
- if (!stroke)
- return;
-
- fz_lock(ctx, FZ_LOCK_ALLOC);
- drop = (stroke->refs > 0 ? --stroke->refs == 0 : 0);
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (drop)
+ if (fz_drop_imp(ctx, stroke, &stroke->refs))
fz_free(ctx, stroke);
}
@@ -448,10 +433,12 @@ fz_unshare_stroke_state_with_dash_len(fz_context *ctx, fz_stroke_state *shared,
len = 0;
if (single && shlen >= len)
return shared;
+
unsize = sizeof(*unshared) + sizeof(unshared->dash_list[0]) * len;
unshared = Memento_label(fz_malloc(ctx, unsize), "fz_stroke_state");
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);
diff --git a/source/fitz/store.c b/source/fitz/store.c
index d4cafe15..dd1cb784 100644
--- a/source/fitz/store.c
+++ b/source/fitz/store.c
@@ -56,38 +56,20 @@ fz_new_store_context(fz_context *ctx, unsigned int max)
void *
fz_keep_storable(fz_context *ctx, fz_storable *s)
{
- if (s == NULL)
- return NULL;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- if (s->refs > 0)
- s->refs++;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return s;
+ return fz_keep_imp(ctx, s, &s->refs);
}
void
fz_drop_storable(fz_context *ctx, fz_storable *s)
{
- int do_free = 0;
-
- if (s == NULL)
- return;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- if (s->refs < 0)
- {
- /* It's a static object. Dropping does nothing. */
- }
- else if (--s->refs == 0)
- {
- /* If we are dropping the last reference to an object, then
- * it cannot possibly be in the store (as the store always
- * keeps a ref to everything in it, and doesn't drop via
- * this method. So we can simply drop the storable object
- * itself without any operations on the fz_store. */
- do_free = 1;
- }
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (do_free)
+ /*
+ If we are dropping the last reference to an object, then
+ it cannot possibly be in the store (as the store always
+ keeps a ref to everything in it, and doesn't drop via
+ this method. So we can simply drop the storable object
+ itself without any operations on the fz_store.
+ */
+ if (fz_drop_imp(ctx, s, &s->refs))
s->drop(ctx, s);
}
diff --git a/source/fitz/text.c b/source/fitz/text.c
index 4d2b73d9..466c869a 100644
--- a/source/fitz/text.c
+++ b/source/fitz/text.c
@@ -20,16 +20,13 @@ fz_new_text(fz_context *ctx, fz_font *font, const fz_matrix *trm, int wmode)
fz_text *
fz_keep_text(fz_context *ctx, fz_text *text)
{
- ++text->refs;
- return text;
+ return fz_keep_imp(ctx, text, &text->refs);
}
void
fz_drop_text(fz_context *ctx, fz_text *text)
{
- if (text == NULL)
- return;
- if (--text->refs == 0)
+ if (fz_drop_imp(ctx, text, &text->refs))
{
fz_drop_font(ctx, text->font);
fz_free(ctx, text->items);