From 2ccb4fe72b9af0241d16a0d206fc8dcf3c2497df Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 26 Apr 2017 14:34:31 +0200 Subject: Clean up store debug printing. Replace fz_print_hash with fz_hash_for_each iterator. Use string formatting callback. --- include/mupdf/fitz/hash.h | 5 ++--- include/mupdf/fitz/store.h | 17 +++++++-------- platform/gl/gl-main.c | 5 +++++ source/fitz/draw-device.c | 24 +++++++++++++-------- source/fitz/hash.c | 31 +++++---------------------- source/fitz/image.c | 6 +++--- source/fitz/store.c | 52 +++++++++++++++++++++++++++++----------------- source/pdf/pdf-font.c | 7 ++++--- source/pdf/pdf-store.c | 9 ++++---- 9 files changed, 79 insertions(+), 77 deletions(-) diff --git a/include/mupdf/fitz/hash.h b/include/mupdf/fitz/hash.h index 89a56bc3..b5077554 100644 --- a/include/mupdf/fitz/hash.h +++ b/include/mupdf/fitz/hash.h @@ -19,6 +19,7 @@ typedef struct fz_hash_table_s fz_hash_table; typedef void (*fz_hash_table_drop_fn)(fz_context *ctx, void *val); +typedef void (*fz_hash_table_for_each_fn)(fz_context *ctx, void *state, void *key, int keylen, void *val); fz_hash_table *fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock, fz_hash_table_drop_fn drop_val); void fz_drop_hash_table(fz_context *ctx, fz_hash_table *table); @@ -26,8 +27,6 @@ void fz_drop_hash_table(fz_context *ctx, fz_hash_table *table); void *fz_hash_find(fz_context *ctx, fz_hash_table *table, const void *key); void *fz_hash_insert(fz_context *ctx, fz_hash_table *table, const void *key, void *val); void fz_hash_remove(fz_context *ctx, fz_hash_table *table, const void *key); - -void fz_print_hash(fz_context *ctx, fz_output *out, fz_hash_table *table); -void fz_print_hash_details(fz_context *ctx, fz_output *out, fz_hash_table *table, void (*details)(fz_context*, fz_output*, void*), int compact); +void fz_hash_for_each(fz_context *ctx, fz_hash_table *table, void *state, fz_hash_table_for_each_fn callback); #endif diff --git a/include/mupdf/fitz/store.h b/include/mupdf/fitz/store.h index 57b817fc..81c7c1e2 100644 --- a/include/mupdf/fitz/store.h +++ b/include/mupdf/fitz/store.h @@ -140,12 +140,12 @@ typedef struct fz_store_hash_s typedef struct fz_store_type_s { - int (*make_hash_key)(fz_context *ctx, fz_store_hash *, void *); - void *(*keep_key)(fz_context *,void *); - void (*drop_key)(fz_context *,void *); - int (*cmp_key)(fz_context *ctx, void *, void *); - void (*print)(fz_context *ctx, fz_output *out, void *); - int (*needs_reap)(fz_context *ctx, void *); + int (*make_hash_key)(fz_context *ctx, fz_store_hash *hash, void *key); + void *(*keep_key)(fz_context *ctx, void *key); + void (*drop_key)(fz_context *ctx, void *key); + int (*cmp_key)(fz_context *ctx, void *a, void *b); + void (*format_key)(fz_context *ctx, char *buf, int size, void *key); + int (*needs_reap)(fz_context *ctx, void *key); } fz_store_type; /* @@ -250,10 +250,9 @@ typedef int (fz_store_filter_fn)(fz_context *ctx, void *arg, void *key); void fz_filter_store(fz_context *ctx, fz_store_filter_fn *fn, void *arg, const fz_store_type *type); /* - fz_print_store: Dump the contents of the store for debugging. + fz_debug_store: Dump the contents of the store for debugging. */ -void fz_print_store(fz_context *ctx, fz_output *out); -void fz_print_store_locked(fz_context *ctx, fz_output *out); +void fz_debug_store(fz_context *ctx); /* fz_defer_reap_start: Increment the defer reap count. diff --git a/platform/gl/gl-main.c b/platform/gl/gl-main.c index 060b8e13..25b9d664 100644 --- a/platform/gl/gl-main.c +++ b/platform/gl/gl-main.c @@ -1573,6 +1573,11 @@ int main(int argc, char **argv) ui_finish_fonts(ctx); +#ifndef NDEBUG + if (fz_atoi(getenv("FZ_DEBUG_STORE"))) + fz_debug_store(ctx); +#endif + fz_drop_link(ctx, links); fz_drop_page(ctx, page); fz_drop_outline(ctx, outline); diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c index f4425558..4e10829f 100644 --- a/source/fitz/draw-device.c +++ b/source/fitz/draw-device.c @@ -1899,7 +1899,7 @@ typedef struct static int fz_make_hash_tile_key(fz_context *ctx, fz_store_hash *hash, void *key_) { - tile_key *key = (tile_key *)key_; + tile_key *key = key_; hash->u.im.id = key->id; hash->u.im.m[0] = key->ctm[0]; @@ -1912,14 +1912,14 @@ fz_make_hash_tile_key(fz_context *ctx, fz_store_hash *hash, void *key_) static void * fz_keep_tile_key(fz_context *ctx, void *key_) { - tile_key *key = (tile_key *)key_; + tile_key *key = key_; return fz_keep_imp(ctx, key, &key->refs); } static void fz_drop_tile_key(fz_context *ctx, void *key_) { - tile_key *key = (tile_key *)key_; + tile_key *key = key_; if (fz_drop_imp(ctx, key, &key->refs)) fz_free(ctx, key); } @@ -1927,16 +1927,21 @@ fz_drop_tile_key(fz_context *ctx, void *key_) static int fz_cmp_tile_key(fz_context *ctx, void *k0_, void *k1_) { - tile_key *k0 = (tile_key *)k0_; - tile_key *k1 = (tile_key *)k1_; - return k0->id == k1->id && k0->ctm[0] == k1->ctm[0] && k0->ctm[1] == k1->ctm[1] && k0->ctm[2] == k1->ctm[2] && k0->ctm[3] == k1->ctm[3]; + tile_key *k0 = k0_; + tile_key *k1 = k1_; + return k0->id == k1->id && + k0->ctm[0] == k1->ctm[0] && + k0->ctm[1] == k1->ctm[1] && + k0->ctm[2] == k1->ctm[2] && + k0->ctm[3] == k1->ctm[3]; } static void -fz_print_tile(fz_context *ctx, fz_output *out, void *key_) +fz_format_tile_key(fz_context *ctx, char *s, int n, void *key_) { tile_key *key = (tile_key *)key_; - fz_write_printf(ctx, out, "(tile id=%x, ctm=%g %g %g %g) ", key->id, key->ctm[0], key->ctm[1], key->ctm[2], key->ctm[3]); + fz_snprintf(s, n, "(tile id=%x, ctm=%g %g %g %g)", + key->id, key->ctm[0], key->ctm[1], key->ctm[2], key->ctm[3]); } static const fz_store_type fz_tile_store_type = @@ -1945,7 +1950,8 @@ static const fz_store_type fz_tile_store_type = fz_keep_tile_key, fz_drop_tile_key, fz_cmp_tile_key, - fz_print_tile + fz_format_tile_key, + NULL }; static void diff --git a/source/fitz/hash.c b/source/fitz/hash.c index 38393caf..245ebbd8 100644 --- a/source/fitz/hash.c +++ b/source/fitz/hash.c @@ -283,31 +283,10 @@ fz_hash_remove(fz_context *ctx, fz_hash_table *table, const void *key) } void -fz_print_hash(fz_context *ctx, fz_output *out, fz_hash_table *table) +fz_hash_for_each(fz_context *ctx, fz_hash_table *table, void *state, fz_hash_table_for_each_fn callback) { - fz_print_hash_details(ctx, out, table, NULL, 0); -} - -void -fz_print_hash_details(fz_context *ctx, fz_output *out, fz_hash_table *table, void (*details)(fz_context*,fz_output*,void*), int compact) -{ - int i, k; - - fz_write_printf(ctx, out, "cache load %d / %d\n", table->load, table->size); - - for (i = 0; i < table->size; i++) - { - if (!table->ents[i].val && !compact) - fz_write_printf(ctx, out, "table %04d: empty\n", i); - else if (table->ents[i].val) - { - fz_write_printf(ctx, out, "table %04d: key=", i); - for (k = 0; k < MAX_KEY_LEN; k++) - fz_write_printf(ctx, out, "%02x", ((unsigned char*)table->ents[i].key)[k]); - if (details) - details(ctx, out, table->ents[i].val); - else - fz_write_printf(ctx, out, " val=$%p\n", table->ents[i].val); - } - } + int i; + for (i = 0; i < table->size; ++i) + if (table->ents[i].val) + callback(ctx, state, table->ents[i].key, table->keylen, table->ents[i].val); } diff --git a/source/fitz/image.c b/source/fitz/image.c index 057d6480..cedb0180 100644 --- a/source/fitz/image.c +++ b/source/fitz/image.c @@ -87,10 +87,10 @@ fz_cmp_image_key(fz_context *ctx, void *k0_, void *k1_) } static void -fz_print_image_key(fz_context *ctx, fz_output *out, void *key_) +fz_format_image_key(fz_context *ctx, char *s, int n, void *key_) { fz_image_key *key = (fz_image_key *)key_; - fz_write_printf(ctx, out, "(image %d x %d sf=%d) ", key->image->w, key->image->h, key->l2factor); + fz_snprintf(s, n, "(image %d x %d sf=%d)", key->image->w, key->image->h, key->l2factor); } static int @@ -107,7 +107,7 @@ static const fz_store_type fz_image_store_type = fz_keep_image_key, fz_drop_image_key, fz_cmp_image_key, - fz_print_image_key, + fz_format_image_key, fz_needs_reap_image_key }; diff --git a/source/fitz/store.c b/source/fitz/store.c index dbff08fc..e2ce8d01 100644 --- a/source/fitz/store.c +++ b/source/fitz/store.c @@ -1,5 +1,8 @@ #include "mupdf/fitz.h" +#include +#include + typedef struct fz_item_s fz_item; struct fz_item_s @@ -669,43 +672,54 @@ fz_drop_store_context(fz_context *ctx) } static void -print_item(fz_context *ctx, fz_output *out, void *item_) +fz_debug_store_item(fz_context *ctx, void *state, void *key_, int keylen, void *item_) { - fz_item *item = (fz_item *)item_; - fz_write_printf(ctx, out, " val=%p item=%p\n", item->val, item); + unsigned char *key = key_; + fz_item *item = item_; + int i; + char buf[256]; + fz_unlock(ctx, FZ_LOCK_ALLOC); + item->type->format_key(ctx, buf, sizeof buf, item->key); + fz_lock(ctx, FZ_LOCK_ALLOC); + printf("hash["); + for (i=0; i < keylen; ++i) + printf("%02x", key[i]); + printf("][refs=%d][size=%d] key=%s val=%p\n", item->val->refs, (int)item->size, buf, item->val); } -void -fz_print_store_locked(fz_context *ctx, fz_output *out) +static void +fz_debug_store_locked(fz_context *ctx) { fz_item *item, *next; + char buf[256]; fz_store *store = ctx->store; - fz_write_printf(ctx, out, "-- resource store contents --\n"); + printf("-- resource store contents --\n"); for (item = store->head; item; item = next) { next = item->next; if (next) next->val->refs++; - fz_write_printf(ctx, out, "store[*][refs=%d][size=%d] ", item->val->refs, item->size); fz_unlock(ctx, FZ_LOCK_ALLOC); - item->type->print(ctx, out, item->key); - fz_write_printf(ctx, out, " = %p\n", item->val); + item->type->format_key(ctx, buf, sizeof buf, item->key); fz_lock(ctx, FZ_LOCK_ALLOC); + printf("store[*][refs=%d][size=%d] key=%s val=%p\n", + item->val->refs, (int)item->size, buf, item->val); if (next) next->val->refs--; } - fz_write_printf(ctx, out, "-- resource store hash contents --\n"); - fz_print_hash_details(ctx, out, store->hash, print_item, 1); - fz_write_printf(ctx, out, "-- end --\n"); + + printf("-- resource store hash contents --\n"); + fz_hash_for_each(ctx, store->hash, NULL, fz_debug_store_item); + printf("-- end --\n"); } void -fz_print_store(fz_context *ctx, fz_output *out) +fz_debug_store(fz_context *ctx) { fz_lock(ctx, FZ_LOCK_ALLOC); - fz_print_store_locked(ctx, out); + fz_debug_store_locked(ctx); fz_unlock(ctx, FZ_LOCK_ALLOC); } @@ -751,7 +765,7 @@ int fz_store_scavenge(fz_context *ctx, size_t size, int *phase) #ifdef DEBUG_SCAVENGING printf("Scavenging: store=" FZ_FMT_zu " size=" FZ_FMT_zu " phase=%d\n", store->size, size, *phase); - fz_print_store_locked(ctx, stderr); + fz_debug_store_locked(ctx); Memento_stats(); #endif do @@ -779,7 +793,7 @@ int fz_store_scavenge(fz_context *ctx, size_t size, int *phase) { #ifdef DEBUG_SCAVENGING printf("scavenged: store=" FZ_FMT_zu "\n", store->size); - fz_print_store(ctx, stderr); + fz_debug_store(ctx); Memento_stats(); #endif return 1; @@ -789,7 +803,7 @@ int fz_store_scavenge(fz_context *ctx, size_t size, int *phase) #ifdef DEBUG_SCAVENGING printf("scavenging failed\n"); - fz_print_store(ctx, stderr); + fz_debug_store(ctx); Memento_listBlocks(); #endif return 0; @@ -810,7 +824,7 @@ fz_shrink_store(fz_context *ctx, unsigned int percent) return 0; #ifdef DEBUG_SCAVENGING - fprintf(stderr, "fz_shrink_store: " FZ_FMT_zu "\n", store->size/(1024*1024)); + printf("fz_shrink_store: " FZ_FMT_zu "\n", store->size/(1024*1024)); #endif fz_lock(ctx, FZ_LOCK_ALLOC); @@ -821,7 +835,7 @@ fz_shrink_store(fz_context *ctx, unsigned int percent) success = (store->size <= new_size) ? 1 : 0; fz_unlock(ctx, FZ_LOCK_ALLOC); #ifdef DEBUG_SCAVENGING - fprintf(stderr, "fz_shrink_store after: " FZ_FMT_zu "\n", store->size/(1024*1024)); + printf("fz_shrink_store after: " FZ_FMT_zu "\n", store->size/(1024*1024)); #endif return success; diff --git a/source/pdf/pdf-font.c b/source/pdf/pdf-font.c index cee094b9..43390247 100644 --- a/source/pdf/pdf-font.c +++ b/source/pdf/pdf-font.c @@ -961,9 +961,9 @@ hail_mary_cmp_key(fz_context *ctx, void *k0, void *k1) } static void -hail_mary_print_key(fz_context *ctx, fz_output *out, void *key_) +hail_mary_format_key(fz_context *ctx, char *s, int n, void *key_) { - fz_write_printf(ctx, out, "hail mary "); + fz_strlcpy(s, "(hail mary font)", n); } static int hail_mary_store_key; /* Dummy */ @@ -974,7 +974,8 @@ static const fz_store_type hail_mary_store_type = hail_mary_keep_key, hail_mary_drop_key, hail_mary_cmp_key, - hail_mary_print_key + hail_mary_format_key, + NULL }; pdf_font_desc * diff --git a/source/pdf/pdf-store.c b/source/pdf/pdf-store.c index 15828d79..7b562d1a 100644 --- a/source/pdf/pdf-store.c +++ b/source/pdf/pdf-store.c @@ -34,14 +34,13 @@ pdf_cmp_key(fz_context *ctx, void *k0, void *k1) } static void -pdf_print_key(fz_context *ctx, fz_output *out, void *key_) +pdf_format_key(fz_context *ctx, char *s, int n, void *key_) { pdf_obj *key = (pdf_obj *)key_; - if (pdf_is_indirect(ctx, key)) - fz_write_printf(ctx, out, "(%d 0 R) ", pdf_to_num(ctx, key)); + fz_snprintf(s, n, "(%d 0 R)", pdf_to_num(ctx, key)); else - pdf_print_obj(ctx, out, key, 0); + pdf_sprint_obj(ctx, s, n, key, 1); } static const fz_store_type pdf_obj_store_type = @@ -50,7 +49,7 @@ static const fz_store_type pdf_obj_store_type = pdf_keep_key, pdf_drop_key, pdf_cmp_key, - pdf_print_key, + pdf_format_key, NULL }; -- cgit v1.2.3