From 313e8f82816c839b3de47e1137a91414bb95a327 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Wed, 14 Dec 2011 19:08:56 +0000 Subject: 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. --- pdf/pdf_cmap_load.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'pdf/pdf_cmap_load.c') diff --git a/pdf/pdf_cmap_load.c b/pdf/pdf_cmap_load.c index 5bcf578f..51949907 100644 --- a/pdf/pdf_cmap_load.c +++ b/pdf/pdf_cmap_load.c @@ -1,6 +1,17 @@ #include "fitz.h" #include "mupdf.h" +unsigned int +pdf_cmap_size(pdf_cmap *cmap) +{ + if (cmap == NULL) + return 0; + if (cmap->storable.refs < 0) + return 0; + + return cmap->rcap * sizeof(pdf_range) + cmap->tcap * sizeof(short) + pdf_cmap_size(cmap->usecmap); +} + /* * Load CMap stream in PDF file */ @@ -13,11 +24,12 @@ pdf_load_embedded_cmap(pdf_xref *xref, fz_obj *stmobj) fz_obj *wmode; fz_obj *obj; fz_context *ctx = xref->ctx; - volatile int phase = 0; + int phase = 0; + + fz_var(phase); - if ((cmap = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_cmap, stmobj))) + if ((cmap = fz_find_item(ctx, pdf_free_cmap_imp, stmobj))) { - pdf_keep_cmap(cmap); return cmap; } @@ -49,7 +61,7 @@ pdf_load_embedded_cmap(pdf_xref *xref, fz_obj *stmobj) pdf_drop_cmap(ctx, usecmap); } - pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)pdf_keep_cmap, (pdf_store_drop_fn *)pdf_drop_cmap, stmobj, cmap); + fz_store_item(ctx, stmobj, cmap, pdf_cmap_size(cmap)); } fz_catch(ctx) { -- cgit v1.2.3