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. --- fitz/res_colorspace.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'fitz/res_colorspace.c') diff --git a/fitz/res_colorspace.c b/fitz/res_colorspace.c index 85556b62..8f145658 100644 --- a/fitz/res_colorspace.c +++ b/fitz/res_colorspace.c @@ -2,11 +2,22 @@ #define SLOWCMYK +void +fz_free_colorspace_imp(fz_context *ctx, void *cs_) +{ + fz_colorspace *cs = (fz_colorspace *)cs_; + + if (cs->free_data && cs->data) + cs->free_data(ctx, cs); + fz_free(ctx, cs); +} + fz_colorspace * fz_new_colorspace(fz_context *ctx, char *name, int n) { fz_colorspace *cs = fz_malloc(ctx, sizeof(fz_colorspace)); - cs->refs = 1; + FZ_INIT_STORABLE(cs, 1, fz_free_colorspace_imp); + cs->size = sizeof(fz_colorspace); fz_strlcpy(cs->name, name, sizeof cs->name); cs->n = n; cs->to_rgb = NULL; @@ -19,23 +30,13 @@ fz_new_colorspace(fz_context *ctx, char *name, int n) fz_colorspace * fz_keep_colorspace(fz_colorspace *cs) { - if (cs->refs < 0) - return cs; - cs->refs ++; - return cs; + return (fz_colorspace *)fz_keep_storable(&cs->storable); } void fz_drop_colorspace(fz_context *ctx, fz_colorspace *cs) { - if (cs && cs->refs < 0) - return; - if (cs && --cs->refs == 0) - { - if (cs->free_data && cs->data) - cs->free_data(ctx, cs); - fz_free(ctx, cs); - } + fz_drop_storable(ctx, &cs->storable); } /* Device colorspace definitions */ @@ -152,10 +153,10 @@ static void rgb_to_cmyk(fz_context *ctx, fz_colorspace *cs, float *rgb, float *c cmyk[3] = k; } -static fz_colorspace k_device_gray = { -1, "DeviceGray", 1, gray_to_rgb, rgb_to_gray }; -static fz_colorspace k_device_rgb = { -1, "DeviceRGB", 3, rgb_to_rgb, rgb_to_rgb }; -static fz_colorspace k_device_bgr = { -1, "DeviceRGB", 3, bgr_to_rgb, rgb_to_bgr }; -static fz_colorspace k_device_cmyk = { -1, "DeviceCMYK", 4, cmyk_to_rgb, rgb_to_cmyk }; +static fz_colorspace k_device_gray = { {-1, fz_free_colorspace_imp}, 0, "DeviceGray", 1, gray_to_rgb, rgb_to_gray }; +static fz_colorspace k_device_rgb = { {-1, fz_free_colorspace_imp}, 0, "DeviceRGB", 3, rgb_to_rgb, rgb_to_rgb }; +static fz_colorspace k_device_bgr = { {-1, fz_free_colorspace_imp}, 0, "DeviceRGB", 3, bgr_to_rgb, rgb_to_bgr }; +static fz_colorspace k_device_cmyk = { {-1, fz_free_colorspace_imp}, 0, "DeviceCMYK", 4, cmyk_to_rgb, rgb_to_cmyk }; fz_colorspace *fz_device_gray = &k_device_gray; fz_colorspace *fz_device_rgb = &k_device_rgb; -- cgit v1.2.3