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_store.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_store.c')
-rw-r--r-- | pdf/pdf_store.c | 224 |
1 files changed, 0 insertions, 224 deletions
diff --git a/pdf/pdf_store.c b/pdf/pdf_store.c deleted file mode 100644 index 26a2ad9b..00000000 --- a/pdf/pdf_store.c +++ /dev/null @@ -1,224 +0,0 @@ -#include "fitz.h" -#include "mupdf.h" - -typedef struct pdf_item_s pdf_item; - -struct pdf_item_s -{ - pdf_store_drop_fn *drop_func; - fz_obj *key; - void *val; - int age; - pdf_item *next; -}; - -struct refkey -{ - pdf_store_drop_fn *drop_func; - int num; - int gen; -}; - -struct pdf_store_s -{ - fz_hash_table *hash; /* hash for num/gen keys */ - pdf_item *root; /* linked list for everything else */ -}; - -pdf_store * -pdf_new_store(fz_context *ctx) -{ - pdf_store *store; - store = fz_malloc(ctx, sizeof(pdf_store)); - store->hash = fz_new_hash_table(ctx, 4096, sizeof(struct refkey)); - store->root = NULL; - return store; -} - -void -pdf_store_item(fz_context *ctx, pdf_store *store, pdf_store_keep_fn *keep_func, pdf_store_drop_fn *drop_func, fz_obj *key, void *val) -{ - pdf_item *item; - - if (!store) - return; - - item = fz_malloc(ctx, sizeof(pdf_item)); - item->drop_func = drop_func; - item->key = fz_keep_obj(key); - item->val = keep_func(val); - item->age = 0; - item->next = NULL; - - if (fz_is_indirect(key)) - { - struct refkey refkey; - refkey.drop_func = drop_func; - refkey.num = fz_to_num(key); - refkey.gen = fz_to_gen(key); - fz_hash_insert(store->hash, &refkey, item); - } - else - { - item->next = store->root; - store->root = item; - } -} - -void * -pdf_find_item(fz_context *ctx, pdf_store *store, pdf_store_drop_fn *drop_func, fz_obj *key) -{ - struct refkey refkey; - pdf_item *item; - - if (!store) - return NULL; - - if (!key) - return NULL; - - if (fz_is_indirect(key)) - { - refkey.drop_func = drop_func; - refkey.num = fz_to_num(key); - refkey.gen = fz_to_gen(key); - item = fz_hash_find(store->hash, &refkey); - if (item) - { - item->age = 0; - return item->val; - } - } - else - { - for (item = store->root; item; item = item->next) - { - if (item->drop_func == drop_func && !fz_objcmp(item->key, key)) - { - item->age = 0; - return item->val; - } - } - } - - return NULL; -} - -void -pdf_remove_item(fz_context *ctx, pdf_store *store, pdf_store_drop_fn *drop_func, fz_obj *key) -{ - struct refkey refkey; - pdf_item *item, *prev, *next; - - if (fz_is_indirect(key)) - { - refkey.drop_func = drop_func; - refkey.num = fz_to_num(key); - refkey.gen = fz_to_gen(key); - item = fz_hash_find(store->hash, &refkey); - if (item) - { - fz_hash_remove(store->hash, &refkey); - item->drop_func(ctx, item->val); - fz_drop_obj(item->key); - fz_free(ctx, item); - } - } - else - { - prev = NULL; - for (item = store->root; item; item = next) - { - next = item->next; - if (item->drop_func == drop_func && !fz_objcmp(item->key, key)) - { - if (!prev) - store->root = next; - else - prev->next = next; - item->drop_func(ctx, item->val); - fz_drop_obj(item->key); - fz_free(ctx, item); - } - else - prev = item; - } - } -} - -void -pdf_age_store(fz_context *ctx, pdf_store *store, int maxage) -{ - struct refkey *refkey; - pdf_item *item, *prev, *next; - int i, n; - - n = fz_hash_len(store->hash); - for (i = 0; i < n; i++) - { - refkey = fz_hash_get_key(store->hash, i); - item = fz_hash_get_val(store->hash, i); - if (item && ++item->age > maxage) - { - fz_hash_remove(store->hash, refkey); - item->drop_func(ctx, item->val); - fz_drop_obj(item->key); - fz_free(ctx, item); - i--; /* items with same hash may move into place */ - } - } - - prev = NULL; - for (item = store->root; item; item = next) - { - next = item->next; - if (++item->age > maxage) - { - if (!prev) - store->root = next; - else - prev->next = next; - item->drop_func(ctx, item->val); - fz_drop_obj(item->key); - fz_free(ctx, item); - } - else - prev = item; - } -} - -void -pdf_free_store(fz_context *ctx, pdf_store *store) -{ - pdf_age_store(ctx, store, 0); - fz_free_hash(store->hash); - fz_free(ctx, store); -} - -void -pdf_debug_store(fz_context *ctx, pdf_store *store) -{ - pdf_item *item; - pdf_item *next; - struct refkey *refkey; - int i, n; - - printf("-- resource store contents --\n"); - - n = fz_hash_len(store->hash); - for (i = 0; i < n; i++) - { - refkey = fz_hash_get_key(store->hash, i); - item = fz_hash_get_val(store->hash, i); - if (item) - printf("store[%d] (%d %d R) = %p\n", i, refkey->num, refkey->gen, item->val); - } - - for (item = store->root; item; item = next) - { - next = item->next; - printf("store[*] "); - fz_debug_obj(item->key); - printf(" = %p\n", item->val); - } -} |