diff options
author | Tor Andersson <tor@ghostscript.com> | 2008-03-21 13:28:39 +0100 |
---|---|---|
committer | Tor Andersson <tor@ghostscript.com> | 2008-03-21 13:28:39 +0100 |
commit | 4b9181cdb56a9d2015f8d79e90015038f4792334 (patch) | |
tree | 507876fcec4f91aa591f3cbf2196f4dcc4deeaad /base | |
parent | 65e3374a2415399ded4624326a01bb9dfa77fa9e (diff) | |
download | mupdf-4b9181cdb56a9d2015f8d79e90015038f4792334.tar.xz |
Backport of chained error messages from experimental branch.
Diffstat (limited to 'base')
-rw-r--r-- | base/base_error.c | 118 | ||||
-rw-r--r-- | base/base_hash.c | 26 | ||||
-rw-r--r-- | base/base_memory.c | 38 | ||||
-rw-r--r-- | base/base_rune.c | 42 |
4 files changed, 117 insertions, 107 deletions
diff --git a/base/base_error.c b/base/base_error.c index e0bf1567..fa1c9878 100644 --- a/base/base_error.c +++ b/base/base_error.c @@ -1,74 +1,88 @@ #include "fitz-base.h" -void -fz_warn(char *fmt, ...) +fz_error * +fz_keeperror(fz_error *eo) { - va_list ap; - fprintf(stderr, "warning: "); - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - fprintf(stderr, "\n"); + eo->refs++; + return eo; } -fz_error * -fz_throw1(char *fmt, ...) +void +fz_droperror(fz_error *eo) { - va_list ap; - fz_error *eo; + if (eo->refs > 0) + eo->refs--; + if (eo->refs == 0) + { + if (eo->cause) + fz_droperror(eo->cause); + fz_free(eo); + } +} - eo = fz_malloc(sizeof(fz_error)); - if (!eo) return fz_outofmem; +void +fz_printerror(fz_error *eo) +{ +#if 1 + if (eo->cause) + { + fz_printerror(eo->cause); + fprintf(stderr, "| %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); + } + else + { + fprintf(stderr, "+ %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); + } +#else + fprintf(stderr, "+ %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); + eo = eo->cause; - eo->refs = 1; - strlcpy(eo->func, "unknown", sizeof eo->func); - strlcpy(eo->file, "unknown", sizeof eo->file); - eo->line = 0; + while (eo) + { + fprintf(stderr, "| %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); + eo = eo->cause; + } +#endif +} - va_start(ap, fmt); - vsnprintf(eo->msg, sizeof eo->msg, fmt, ap); - eo->msg[sizeof(eo->msg) - 1] = '\0'; - va_end(ap); - return eo; +void +fz_warn(char *fmt, ...) +{ + va_list ap; + fprintf(stderr, "warning: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); } fz_error * -fz_throw0(const char *func, const char *file, int line, char *fmt, ...) +fz_throwimp(fz_error *cause, const char *func, const char *file, int line, char *fmt, ...) { - va_list ap; - fz_error *eo; + va_list ap; + fz_error *eo; - eo = fz_malloc(sizeof(fz_error)); - if (!eo) return fz_outofmem; + eo = fz_malloc(sizeof(fz_error)); + if (!eo) + return fz_outofmem; /* oops. we're *really* out of memory here. */ - eo->refs = 1; - strlcpy(eo->func, func, sizeof eo->func); - strlcpy(eo->file, file, sizeof eo->file); - eo->line = line; + eo->refs = 1; - va_start(ap, fmt); - vsnprintf(eo->msg, sizeof eo->msg, fmt, ap); - eo->msg[sizeof(eo->msg) - 1] = '\0'; - va_end(ap); + va_start(ap, fmt); + vsnprintf(eo->msg, sizeof eo->msg, fmt, ap); + eo->msg[sizeof(eo->msg) - 1] = '\0'; + va_end(ap); - if (getenv("BOMB")) - { - fflush(stdout); - fprintf(stderr, "%s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); - fflush(stderr); - abort(); - } + strlcpy(eo->func, func, sizeof eo->func); + strlcpy(eo->file, file, sizeof eo->file); + eo->line = line; - return eo; -} + if (cause) + eo->cause = fz_keeperror(cause); + else + eo->cause = nil; -void -fz_droperror(fz_error *eo) -{ - if (eo->refs > 0) - eo->refs--; - if (eo->refs == 0) - fz_free(eo); + return eo; } diff --git a/base/base_hash.c b/base/base_hash.c index dedd1c5e..b43ee611 100644 --- a/base/base_hash.c +++ b/base/base_hash.c @@ -54,7 +54,7 @@ fz_newhash(fz_hashtable **tablep, int initialsize, int keylen) table = *tablep = fz_malloc(sizeof(fz_hashtable)); if (!table) - return fz_outofmem; + return fz_throw("outofmem: hash table struct"); table->keylen = keylen; table->size = initialsize; @@ -65,12 +65,12 @@ fz_newhash(fz_hashtable **tablep, int initialsize, int keylen) { fz_free(table); *tablep = nil; - return fz_outofmem; + return fz_throw("outofmem: hash table entries (size=%d)", initialsize); } memset(table->ents, 0, sizeof(fz_hashentry) * table->size); - return nil; + return fz_okay; } void @@ -120,11 +120,11 @@ fz_resizehash(fz_hashtable *table, int newsize) oldents = table->ents; if (newsize < oldload * 8 / 10) - return fz_throw("rangecheck: resize hash too small"); + return fz_throw("assert: resize hash too small"); newents = fz_malloc(sizeof(fz_hashentry) * newsize); if (!newents) - return fz_outofmem; + return fz_throw("outofmem: hash table (size=%d)", newsize); table->size = newsize; table->load = 0; @@ -142,13 +142,14 @@ fz_resizehash(fz_hashtable *table, int newsize) table->load = oldload; table->ents = oldents; fz_free(newents); - return error; + return fz_rethrow(error, "cannot re-insert old entries"); } } } fz_free(oldents); - return nil; + + return fz_okay; } void * @@ -182,7 +183,7 @@ fz_hashinsert(fz_hashtable *table, void *key, void *val) { error = fz_resizehash(table, table->size * 2); if (error) - return error; + return fz_rethrow(error, "cannot resize hash table"); } ents = table->ents; @@ -200,12 +201,12 @@ fz_hashinsert(fz_hashtable *table, void *key, void *val) } if (memcmp(key, &ents[pos].key, table->keylen) == 0) - return fz_throw("rangecheck: overwrite hash slot"); + return fz_throw("assert: overwrite hash slot"); pos = (pos + 1) % size; } - return nil; + return fz_okay; } fz_error * @@ -219,7 +220,7 @@ fz_hashremove(fz_hashtable *table, void *key) while (1) { if (!ents[pos].val) - return fz_throw("rangecheck: remove inexistant hash entry"); + return fz_throw("assert: remove inexistant hash entry"); if (memcmp(key, &ents[pos].key, table->keylen) == 0) { @@ -244,7 +245,8 @@ fz_hashremove(fz_hashtable *table, void *key) } table->load --; - return nil; + + return fz_okay; } pos = (pos + 1) % size; diff --git a/base/base_memory.c b/base/base_memory.c index a14a3122..1fa918c2 100644 --- a/base/base_memory.c +++ b/base/base_memory.c @@ -4,30 +4,12 @@ static void *stdmalloc(fz_memorycontext *mem, int n) { -#if 0 - void *p = malloc(n); - if (!p) - fprintf(stderr, "failed to malloc %d bytes\n", n); - return p; -#else return malloc(n); -#endif } static void *stdrealloc(fz_memorycontext *mem, void *p, int n) { -#if 0 - void *np = realloc(p, n); - if (np == nil) - fprintf(stderr, "realloc failed %d nytes", n); - else if (np == p) - fprintf(stderr, "realloc kept %d\n", n); - else - fprintf(stderr, "realloc moved %d\n", n); - return np; -#else return realloc(p, n); -#endif } static void stdfree(fz_memorycontext *mem, void *p) @@ -41,9 +23,9 @@ static fz_memorycontext *curmem = &defmem; fz_error fz_koutofmem = { -1, {"out of memory"}, - {"<malloc>"}, - {"memory.c"}, - 0 + {"<internal>"}, + {"<internal>"}, + 0, 0 }; fz_memorycontext * @@ -61,15 +43,21 @@ fz_setmemorycontext(fz_memorycontext *mem) void * fz_malloc(int n) { - fz_memorycontext *mem = fz_currentmemorycontext(); - return mem->malloc(mem, n); + fz_memorycontext *mem = fz_currentmemorycontext(); + void *p = mem->malloc(mem, n); + if (!p) + fz_warn("cannot malloc %d bytes", n); + return p; } void * fz_realloc(void *p, int n) { - fz_memorycontext *mem = fz_currentmemorycontext(); - return mem->realloc(mem, p, n); + fz_memorycontext *mem = fz_currentmemorycontext(); + void *np = mem->realloc(mem, p, n); + if (np == nil) + fz_warn("cannot realloc %d bytes", n); + return np; } void diff --git a/base/base_rune.c b/base/base_rune.c index 4aa81df3..32168792 100644 --- a/base/base_rune.c +++ b/base/base_rune.c @@ -41,7 +41,8 @@ chartorune(int *rune, char *str) * 00000-0007F => T1 */ c = *(unsigned char*)str; - if(c < Tx) { + if (c < Tx) + { *rune = c; return 1; } @@ -51,13 +52,14 @@ chartorune(int *rune, char *str) * 0080-07FF => T2 Tx */ c1 = *(unsigned char*)(str+1) ^ Tx; - if(c1 & Testx) + if (c1 & Testx) goto bad; - if(c < T3) { - if(c < T2) + if (c < T3) + { + if (c < T2) goto bad; l = ((c << Bitx) | c1) & Rune2; - if(l <= Rune1) + if (l <= Rune1) goto bad; *rune = l; return 2; @@ -68,11 +70,12 @@ chartorune(int *rune, char *str) * 0800-FFFF => T3 Tx Tx */ c2 = *(unsigned char*)(str+2) ^ Tx; - if(c2 & Testx) + if (c2 & Testx) goto bad; - if(c < T4) { + if (c < T4) + { l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3; - if(l <= Rune2) + if (l <= Rune2) goto bad; *rune = l; return 3; @@ -96,7 +99,8 @@ runetochar(char *str, int *rune) * 00000-0007F => 00-7F */ c = *rune; - if(c <= Rune1) { + if (c <= Rune1) + { str[0] = c; return 1; } @@ -105,7 +109,8 @@ runetochar(char *str, int *rune) * two character sequence * 0080-07FF => T2 Tx */ - if(c <= Rune2) { + if (c <= Rune2) + { str[0] = T2 | (c >> 1*Bitx); str[1] = Tx | (c & Maskx); return 2; @@ -137,12 +142,12 @@ runenlen(int *r, int nrune) int nb, c; nb = 0; - while(nrune--) { + while (nrune--) + { c = *r++; - if(c <= Rune1) + if (c <= Rune1) nb++; - else - if(c <= Rune2) + else if (c <= Rune2) nb += 2; else nb += 3; @@ -155,12 +160,13 @@ fullrune(char *str, int n) { int c; - if(n > 0) { + if (n > 0) + { c = *(unsigned char*)str; - if(c < Tx) + if (c < Tx) return 1; - if(n > 1) - if(c < T3 || n > 2) + if (n > 1) + if (c < T3 || n > 2) return 1; } return 0; |