diff options
author | Robin Watts <robin.watts@artifex.com> | 2011-12-14 19:08:56 +0000 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2011-12-15 00:43:24 +0000 |
commit | 313e8f82816c839b3de47e1137a91414bb95a327 (patch) | |
tree | f680cd6ebc26e51dab5cd0b9a5337ce49197f6ac /pdf/pdf_xobject.c | |
parent | 09c8ebeea83f11ccac07bad76e516460e2c8e0f7 (diff) | |
download | mupdf-313e8f82816c839b3de47e1137a91414bb95a327.tar.xz |
Rework pdf_store to fz_store, a part of fz_context.
Firstly, we rename pdf_store to fz_store, reflecting the fact that
there are no pdf specific dependencies on it.
Next, we rework it so that all the objects that can be stored in
the store start with an fz_storable structure. This consists of
a reference count, and a function used to free the object when
the reference count reaches zero.
All the keep/drop functions are then reimplemented by calling
fz_keep_sharable/fz_drop_sharable. The 'drop' functions as supplied
by the callers are thus now 'free' functions, only called if
the reference count drops to 0.
The store changes to keep all the items in the store in the linked
list (which becomes a doubly linked one). We still make use of
the hashtable to index into this list quickly, but we now have
the objects in an LRU ordering within the list.
Every object is put into the store, with a size record; this is
an estimate of how much memory would be freed by freeing that
object.
The store is moved into the context and given a maximum size;
when new things are inserted into the store, care is taken to
ensure that we do not expand beyond this size. We evict any
stored items (that are not in use) starting from the least
recently used.
Finding an object in the store now takes a reference to it already.
LOCK and UNLOCK comments are used to indicate where locks need to
be taken and released to ensure thread safety.
Diffstat (limited to 'pdf/pdf_xobject.c')
-rw-r--r-- | pdf/pdf_xobject.c | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/pdf/pdf_xobject.c b/pdf/pdf_xobject.c index b4c03b8b..913ded37 100644 --- a/pdf/pdf_xobject.c +++ b/pdf/pdf_xobject.c @@ -2,25 +2,57 @@ #include "mupdf.h" pdf_xobject * +pdf_keep_xobject(pdf_xobject *xobj) +{ + return (pdf_xobject *)fz_keep_storable(&xobj->storable); +} + +void +pdf_drop_xobject(fz_context *ctx, pdf_xobject *xobj) +{ + fz_drop_storable(ctx, &xobj->storable); +} + +static void +pdf_free_xobject_imp(fz_context *ctx, pdf_xobject *xobj) +{ + if (xobj->colorspace) + fz_drop_colorspace(ctx, xobj->colorspace); + if (xobj->resources) + fz_drop_obj(xobj->resources); + if (xobj->contents) + fz_drop_buffer(ctx, xobj->contents); + fz_free(ctx, xobj); +} + +static unsigned int +pdf_xobject_size(pdf_xobject *xobj) +{ + if (xobj == NULL) + return 0; + return sizeof(*xobj) + (xobj->colorspace ? xobj->colorspace->size : 0) + (xobj->contents ? xobj->contents->len : 0); +} + +pdf_xobject * pdf_load_xobject(pdf_xref *xref, fz_obj *dict) { pdf_xobject *form; fz_obj *obj; fz_context *ctx = xref->ctx; - if ((form = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_xobject, dict))) + if ((form = fz_find_item(ctx, pdf_free_xobject_imp, dict))) { - return pdf_keep_xobject(form); + return form; } form = fz_malloc(ctx, sizeof(pdf_xobject)); - form->refs = 1; + FZ_INIT_STORABLE(form, 1, pdf_free_xobject_imp); form->resources = NULL; form->contents = NULL; form->colorspace = NULL; /* Store item immediately, to avoid possible recursion if objects refer back to this one */ - pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)pdf_keep_xobject, (pdf_store_drop_fn *)pdf_drop_xobject, dict, form); + fz_store_item(ctx, dict, form, pdf_xobject_size(form)); obj = fz_dict_gets(dict, "BBox"); form->bbox = pdf_to_rect(ctx, obj); @@ -66,32 +98,10 @@ pdf_load_xobject(pdf_xref *xref, fz_obj *dict) } fz_catch(ctx) { - pdf_remove_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_xobject, dict); + fz_remove_item(ctx, pdf_free_xobject_imp, dict); pdf_drop_xobject(ctx, form); fz_throw(ctx, "cannot load xobject content stream (%d %d R)", fz_to_num(dict), fz_to_gen(dict)); } return form; } - -pdf_xobject * -pdf_keep_xobject(pdf_xobject *xobj) -{ - xobj->refs ++; - return xobj; -} - -void -pdf_drop_xobject(fz_context *ctx, pdf_xobject *xobj) -{ - if (xobj && --xobj->refs == 0) - { - if (xobj->colorspace) - fz_drop_colorspace(ctx, xobj->colorspace); - if (xobj->resources) - fz_drop_obj(xobj->resources); - if (xobj->contents) - fz_drop_buffer(ctx, xobj->contents); - fz_free(ctx, xobj); - } -} |