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_function.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_function.c')
-rw-r--r-- | pdf/pdf_function.c | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/pdf/pdf_function.c b/pdf/pdf_function.c index fcd683ae..26d4bcec 100644 --- a/pdf/pdf_function.c +++ b/pdf/pdf_function.c @@ -19,7 +19,8 @@ enum struct pdf_function_s { - int refs; + fz_storable storable; + unsigned int size; int type; /* 0=sample 2=exponential 3=stitching 4=postscript */ int m; /* number of input values */ int n; /* number of output values */ @@ -849,6 +850,8 @@ load_postscript_func(pdf_function *func, pdf_xref *xref, fz_obj *dict, int num, fz_throw(ctx, "cannot parse calculator function (%d %d R)", num, gen); } + func->size += func->u.p.cap * sizeof(psobj); + fz_close(stream); } @@ -947,6 +950,7 @@ load_sample_func(pdf_function *func, pdf_xref *xref, fz_obj *dict, int num, int samplecount *= func->u.sa.size[i]; func->u.sa.samples = fz_malloc_array(ctx, samplecount, sizeof(float)); + func->size += samplecount * sizeof(float); stream = pdf_open_stream(xref, num, gen); /* RJW: "cannot open samples stream (%d %d R)", num, gen */ @@ -1198,6 +1202,7 @@ load_stitching_func(pdf_function *func, pdf_xref *xref, fz_obj *dict) /* RJW: "cannot load sub function %d (%d %d R)", i, fz_to_num(sub), fz_to_gen(sub) */ if (funcs[i]->m != 1 || funcs[i]->n != funcs[0]->n) fz_throw(ctx, "sub function %d /Domain or /Range mismatch", i); + func->size += pdf_function_size(funcs[i]); func->u.st.k ++; } @@ -1292,36 +1297,46 @@ eval_stitching_func(fz_context *ctx, pdf_function *func, float in, float *out) pdf_function * pdf_keep_function(pdf_function *func) { - func->refs ++; - return func; + return (pdf_function *)fz_keep_storable(&func->storable); } void pdf_drop_function(fz_context *ctx, pdf_function *func) { + fz_drop_storable(ctx, &func->storable); +} + +static void +pdf_free_function_imp(fz_context *ctx, void *func_) +{ + pdf_function *func = (pdf_function *)func_; int i; - if (--func->refs == 0) + + switch(func->type) { - switch(func->type) - { - case SAMPLE: - fz_free(ctx, func->u.sa.samples); - break; - case EXPONENTIAL: - break; - case STITCHING: - for (i = 0; i < func->u.st.k; i++) - pdf_drop_function(ctx, func->u.st.funcs[i]); - fz_free(ctx, func->u.st.funcs); - fz_free(ctx, func->u.st.bounds); - fz_free(ctx, func->u.st.encode); - break; - case POSTSCRIPT: - fz_free(ctx, func->u.p.code); - break; - } - fz_free(ctx, func); + case SAMPLE: + fz_free(ctx, func->u.sa.samples); + break; + case EXPONENTIAL: + break; + case STITCHING: + for (i = 0; i < func->u.st.k; i++) + pdf_drop_function(ctx, func->u.st.funcs[i]); + fz_free(ctx, func->u.st.funcs); + fz_free(ctx, func->u.st.bounds); + fz_free(ctx, func->u.st.encode); + break; + case POSTSCRIPT: + fz_free(ctx, func->u.p.code); + break; } + fz_free(ctx, func); +} + +unsigned int +pdf_function_size(pdf_function *func) +{ + return (func ? func->size : 0); } pdf_function * @@ -1332,15 +1347,15 @@ pdf_load_function(pdf_xref *xref, fz_obj *dict) fz_obj *obj; int i; - if ((func = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_function, dict))) + if ((func = fz_find_item(ctx, pdf_free_function_imp, dict))) { - pdf_keep_function(func); return func; } func = fz_malloc(ctx, sizeof(pdf_function)); memset(func, 0, sizeof *func); - func->refs = 1; + FZ_INIT_STORABLE(func, 1, pdf_free_function_imp); + func->size = sizeof(*func); obj = fz_dict_gets(dict, "FunctionType"); func->type = fz_to_int(obj); @@ -1414,7 +1429,7 @@ pdf_load_function(pdf_xref *xref, fz_obj *dict) "unknown")))), fz_to_num(dict), fz_to_gen(dict)); } - pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)pdf_keep_function, (pdf_store_drop_fn *)pdf_drop_function, dict, func); + fz_store_item(ctx, dict, func, func->size); return func; } |