summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2011-09-20 15:50:07 +0200
committerTor Andersson <tor.andersson@artifex.com>2011-09-20 15:50:07 +0200
commit99ba154018b7c4a2c47b4c7e721ffe6d9164f9f3 (patch)
tree228cdc9d4c1af8caa3e815bc43441b0a1a551f5b
parentb51ef0eea028c73b6379e832eaa34fff3fbbb927 (diff)
downloadmupdf-99ba154018b7c4a2c47b4c7e721ffe6d9164f9f3.tar.xz
Reshuffle exception context code to fit Tor's aesthetic sense.
-rw-r--r--apps/pdfclean.c6
-rw-r--r--apps/pdfdraw.c3
-rw-r--r--apps/pdfextract.c6
-rw-r--r--apps/pdfinfo.c5
-rw-r--r--apps/pdfshow.c6
-rw-r--r--apps/x11_main.c4
-rw-r--r--apps/xpsdraw.c4
-rw-r--r--fitz/base_error.c52
-rw-r--r--fitz/base_memory.c11
-rw-r--r--fitz/context.c63
-rw-r--r--fitz/except.c82
-rw-r--r--fitz/except.h51
-rw-r--r--fitz/exceptxxx.h36
-rw-r--r--fitz/fitz.h97
-rw-r--r--pdf/pdf_xref.c2
-rw-r--r--scripts/cmapdump.c5
-rw-r--r--win32/libmupdf.vcproj20
17 files changed, 137 insertions, 316 deletions
diff --git a/apps/pdfclean.c b/apps/pdfclean.c
index 1767ebc5..2a8c54f2 100644
--- a/apps/pdfclean.c
+++ b/apps/pdfclean.c
@@ -720,7 +720,7 @@ int main(int argc, char **argv)
if (argc - fz_optind > 0)
subset = 1;
- ctx = fz_context_init(&fz_alloc_default);
+ ctx = fz_new_context(&fz_alloc_default);
if (ctx == NULL)
die(fz_error_note(1, "failed to initialise context"));
@@ -782,9 +782,7 @@ int main(int argc, char **argv)
fz_free(xref->ctx, renumbermap);
pdf_free_xref(xref);
-
fz_flush_warnings();
- fz_context_fin(ctx);
-
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c
index ddaf10c5..2c2e476b 100644
--- a/apps/pdfdraw.c
+++ b/apps/pdfdraw.c
@@ -343,7 +343,7 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
- ctx = fz_context_init(&fz_alloc_default);
+ ctx = fz_new_context(&fz_alloc_default);
if (ctx == NULL)
{
fprintf(stderr, "Failed to init context\n");
@@ -410,5 +410,6 @@ int main(int argc, char **argv)
fz_flush_warnings();
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/pdfextract.c b/apps/pdfextract.c
index 2f5baec5..96ea7e87 100644
--- a/apps/pdfextract.c
+++ b/apps/pdfextract.c
@@ -198,7 +198,7 @@ int main(int argc, char **argv)
infile = argv[fz_optind++];
- ctx = fz_context_init(&fz_alloc_default);
+ ctx = fz_new_context(&fz_alloc_default);
if (ctx == NULL)
die(fz_error_note(1, "failed to initialise context"));
@@ -221,9 +221,7 @@ int main(int argc, char **argv)
}
pdf_free_xref(xref);
-
fz_flush_warnings();
- fz_context_fin(ctx);
-
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/pdfinfo.c b/apps/pdfinfo.c
index f7ee1800..2006b0be 100644
--- a/apps/pdfinfo.c
+++ b/apps/pdfinfo.c
@@ -981,7 +981,7 @@ int main(int argc, char **argv)
if (fz_optind == argc)
infousage();
- ctx = fz_context_init(&fz_alloc_default);
+ ctx = fz_new_context(&fz_alloc_default);
if (ctx == NULL)
die(fz_error_make("failed to initialise context"));
@@ -1025,7 +1025,6 @@ int main(int argc, char **argv)
showinfo(filename, show, "1-");
closexref();
- fz_context_fin(ctx);
-
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/pdfshow.c b/apps/pdfshow.c
index bbb6747e..653ffff6 100644
--- a/apps/pdfshow.c
+++ b/apps/pdfshow.c
@@ -214,7 +214,7 @@ int main(int argc, char **argv)
filename = argv[fz_optind++];
- ctx = fz_context_init(&fz_alloc_default);
+ ctx = fz_new_context(&fz_alloc_default);
if (ctx == NULL)
die(fz_error_note(1, "failed to initialise context"));
@@ -239,9 +239,7 @@ int main(int argc, char **argv)
}
pdf_free_xref(xref);
-
fz_flush_warnings();
- fz_context_fin(ctx);
-
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/x11_main.c b/apps/x11_main.c
index f0f4f66d..a8c92074 100644
--- a/apps/x11_main.c
+++ b/apps/x11_main.c
@@ -584,7 +584,7 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
- ctx = fz_context_init(&fz_alloc_default);
+ ctx = fz_new_context(&fz_alloc_default);
if (ctx == NULL)
{
fprintf(stderr, "failed to initialise context");
@@ -750,7 +750,7 @@ int main(int argc, char **argv)
XCloseDisplay(xdpy);
- fz_context_fin(ctx);
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/xpsdraw.c b/apps/xpsdraw.c
index 8283216a..719719ea 100644
--- a/apps/xpsdraw.c
+++ b/apps/xpsdraw.c
@@ -311,7 +311,7 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
- fzctx = fz_context_init(&fz_alloc_default);
+ fzctx = fz_new_context(&fz_alloc_default);
if (fzctx == NULL)
{
fprintf(stderr, "failed to initialise context");
@@ -369,7 +369,7 @@ int main(int argc, char **argv)
}
fz_free_glyph_cache(fzctx, glyphcache);
- fz_context_fin(fzctx);
+ fz_free_context(fzctx);
return 0;
}
diff --git a/fitz/base_error.c b/fitz/base_error.c
index 1610d033..6963a69b 100644
--- a/fitz/base_error.c
+++ b/fitz/base_error.c
@@ -1,5 +1,8 @@
#include "fitz.h"
+/* Warning context
+ * TODO: move into fz_context
+ */
enum { LINE_LEN = 160, LINE_COUNT = 25 };
static char warn_message[LINE_LEN] = "";
@@ -35,6 +38,55 @@ void fz_warn(char *fmt, ...)
}
}
+/* Error context */
+
+static void throw(fz_error_context *ex)
+{
+ if (ex->top >= 0)
+ longjmp(ex->stack[ex->top].buffer, 1);
+ else {
+ fprintf(stderr, "uncaught exception: %s\n", ex->message);
+ exit(EXIT_FAILURE);
+ }
+}
+
+void fz_push_try(fz_error_context *ex)
+{
+ assert(ex != NULL);
+ if (ex->top + 1 >= nelem(ex->stack))
+ {
+ fprintf(stderr, "exception stack overflow!\n");
+ exit(EXIT_FAILURE);
+ }
+ ex->top++;
+}
+
+char *fz_caught(fz_context *ctx)
+{
+ assert(ctx != NULL);
+ assert(ctx->error != NULL);
+ return ctx->error->message;
+}
+
+void fz_throw(fz_context *ctx, char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ vsnprintf(ctx->error->message, sizeof ctx->error->message, fmt, args);
+ va_end(args);
+
+ fprintf(stderr, "error: %s\n", ctx->error->message);
+
+ throw(ctx->error);
+}
+
+void fz_rethrow(fz_context *ctx)
+{
+ throw(ctx->error);
+}
+
+/* Deprecated error bubbling */
+
static char error_message[LINE_COUNT][LINE_LEN];
static int error_count = 0;
diff --git a/fitz/base_memory.c b/fitz/base_memory.c
index 9847aa76..643c85b6 100644
--- a/fitz/base_memory.c
+++ b/fitz/base_memory.c
@@ -1,5 +1,4 @@
#include "fitz.h"
-#include "except.h"
void *
fz_malloc(fz_context *ctx, size_t size)
@@ -134,13 +133,3 @@ fz_strdup(fz_context *ctx, char *s)
memcpy(ns, s, len);
return ns;
}
-
-char *
-fz_strdup_nothrow(fz_context *ctx, char *s)
-{
- int len = strlen(s) + 1;
- char *ns = fz_malloc_nothrow(ctx, len);
- if (ns != NULL)
- memcpy(ns, s, len);
- return ns;
-}
diff --git a/fitz/context.c b/fitz/context.c
deleted file mode 100644
index aee521b6..00000000
--- a/fitz/context.c
+++ /dev/null
@@ -1,63 +0,0 @@
-#include "fitz.h"
-#include "except.h"
-
-void fz_context_fin(fz_context *ctx)
-{
- assert(ctx != NULL);
-
- /* Other finalisation calls go here (in reverse order) */
- fz_except_fin(ctx);
- /* Free the context itself */
- ctx->alloc->free(ctx->alloc->opaque, ctx);
- /* We do NOT free the allocator! */
-}
-
-fz_obj *fz_resolve_indirect_null(fz_obj *ref)
-{
- return ref;
-}
-
-fz_context *fz_context_init(fz_alloc_context *alloc)
-{
- fz_context *ctx;
- fz_error error;
-
- assert(alloc != NULL);
- ctx = alloc->calloc(alloc->opaque, 1, sizeof(fz_context));
- if (ctx == NULL)
- return NULL;
- ctx->alloc = alloc;
-
- error = fz_except_init(ctx);
- if (error != fz_okay)
- goto cleanup;
- ctx->fz_resolve_indirect = fz_resolve_indirect_null;
-
- /* New initialisation calls for context entries go here */
- return ctx;
- cleanup:
- fz_context_fin(ctx);
- fz_error_handle(error, "fz_context_init failed");
- return NULL;
-}
-
-fz_context *fz_context_clone(fz_context *ctx)
-{
- fz_context *clone;
- fz_error error;
-
- clone = ctx->alloc->calloc(ctx->alloc->opaque, 1, sizeof(*clone));
- if (clone == NULL)
- return NULL;
- clone->alloc = ctx->alloc;
- error = fz_except_init(ctx);
- if (error != fz_okay)
- goto cleanup;
- /* Other initialisations go here; either a copy (probably refcounted)
- * or a new initialisation. */
- return clone;
- cleanup:
- fz_context_fin(ctx);
- fz_error_handle(error, "fz_context_clone failed");
- return NULL;
-}
diff --git a/fitz/except.c b/fitz/except.c
deleted file mode 100644
index 4055367d..00000000
--- a/fitz/except.c
+++ /dev/null
@@ -1,82 +0,0 @@
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-#include "except.h"
-
-static void do_throw(fz_except_context *ex)
-{
- assert(ex != NULL);
- if(ex->depth >= 0)
- longjmp(ex->stack[ex->depth].buffer, 1);
- else {
- printf("Uncaught exception: %s\n", ex->except.mess);
- exit(EXIT_FAILURE); /* Bale through normal channels */
- }
-}
-
-void fz_throw(fz_context *ctx, char *fmt, ...)
-{
- va_list args;
-
- va_start(args, fmt);
- vsprintf(ctx->except->except.mess, fmt, args);
- va_end(args);
- do_throw(ctx->except);
-}
-
-fz_except *fz_caught(fz_context *ctx)
-{
- assert(ctx != NULL);
- assert(ctx->except != NULL);
- return &ctx->except->except;
-}
-
-void fz_rethrow(fz_context *ctx)
-{
- assert(ctx != NULL);
- do_throw(ctx->except);
-}
-
-void fz_except_xxx_push(fz_except_context *ex)
-{
- assert(ex != NULL);
- if(ex->depth+1 >= MAXDEPTH) {
- fprintf(stderr, "Exception stack overflow!\n");
- exit(EXIT_FAILURE); /* Bale through normal channels */
- }
- ex->depth++;
-}
-
-void fz_var_xxx(void *e)
-{
- /* Do nothing */
-}
-
-fz_error fz_except_init(fz_context *ctx)
-{
- fz_except_context *ex;
-
- assert(ctx != NULL);
- if (ctx->except != NULL)
- return fz_okay;
- ex = fz_malloc_nothrow(ctx, sizeof(fz_except_context));
- if (ex == NULL)
- return fz_error_make("Failed to initialise exception context");
- ex->depth = -1;
- ctx->except = ex;
-
- return fz_okay;
-}
-
-void fz_except_fin(fz_context *ctx)
-{
- assert(ctx != NULL);
-
- if (ctx->except == NULL)
- return;
- assert(ctx->except->depth == -1);
- free(ctx->except);
- ctx->except = NULL;
-}
diff --git a/fitz/except.h b/fitz/except.h
deleted file mode 100644
index 477fbc39..00000000
--- a/fitz/except.h
+++ /dev/null
@@ -1,51 +0,0 @@
-#ifndef EXCEPT_H
-#define EXCEPT_H
-
-#include "fitz.h"
-
-typedef struct fz_context fz_context;
-typedef int fz_error;
-
-typedef struct fz_except {
- char mess[256];
-} fz_except;
-
-#include "exceptxxx.h"
-/*
- Macros for fz_try and fz_catch are defined in exceptxxx.h,
- but their definitions are best ignored. Just use them as follows:
-
- fz_var(..);
- fz_var(..);
-
- fz_try(ctx)
- {
- .
- .
- .
- }
- fz_catch(ctx)
- {
- .
- .
- .
- }
-
- and don't return from within the try clause.
-*/
-
-void fz_throw(fz_context *, char *, ...);
-
-fz_except *fz_caught(fz_context *);
-
-void fz_rethrow(fz_context *);
-
-#define fz_var(A) fz_var_xxx((void *)&(A))
-
-void fz_var_xxx(void *x);
-
-fz_error fz_except_init(fz_context *);
-
-void fz_except_fin(fz_context *);
-
-#endif /* EXCEPT */
diff --git a/fitz/exceptxxx.h b/fitz/exceptxxx.h
deleted file mode 100644
index 285e88b7..00000000
--- a/fitz/exceptxxx.h
+++ /dev/null
@@ -1,36 +0,0 @@
-#ifndef EXCEPTXXX_H
-#define EXCEPTXXX_H
-
-#include <setjmp.h>
-
-#define fz_try(ctx) \
-if(fz_except_xxx_push(ctx->except), \
- !setjmp(ctx->except->stack[ctx->except->depth].buffer)) \
-{
-
-#define fz_catch(ctx) \
- ctx->except->stack[ctx->except->depth].failed = 0;\
-}\
-else\
-{\
- ctx->except->stack[ctx->except->depth].failed = 1;\
-}\
-if(ctx->except->stack[ctx->except->depth--].failed)
-
-typedef struct fz_except_xxx_stack
-{
- int failed;
- jmp_buf buffer;
-} fz_except_xxx_stack;
-
-#define MAXDEPTH (20)
-
-struct fz_except_context {
- fz_except_xxx_stack stack[MAXDEPTH];
- int depth;
- fz_except except;
-};
-
-void fz_except_xxx_push(struct fz_except_context *);
-
-#endif /* EXCEPTXXX */
diff --git a/fitz/fitz.h b/fitz/fitz.h
index 1311f019..841e0f61 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -18,6 +18,8 @@
#include <float.h> /* FLT_EPSILON */
#include <fcntl.h> /* O_RDONLY & co */
+#include <setjmp.h>
+
#include "memento.h"
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
@@ -62,11 +64,6 @@ int gettimeofday(struct timeval *tv, struct timezone *tz);
#define M_SQRT2 1.41421356237309504880
#endif
-/* Context types */
-typedef struct fz_except_context fz_except_context;
-typedef struct fz_alloc_context fz_alloc_context;
-typedef struct fz_context fz_context;
-
/*
* Variadic macros, inline and restrict keywords
*/
@@ -117,7 +114,7 @@ typedef struct fz_context fz_context;
#endif
/*
- * Error handling
+ * Deprecated error handling.
*/
typedef int fz_error;
@@ -139,6 +136,66 @@ void fz_error_handle_impx(fz_error cause, char *fmt, ...) __printflike(2, 3);
int fz_get_error_count(void);
char *fz_get_error_line(int n);
+/* Context types */
+
+typedef struct fz_error_context_s fz_error_context;
+typedef struct fz_alloc_context_s fz_alloc_context;
+typedef struct fz_context_s fz_context;
+
+/* Memory allocator context */
+
+struct fz_alloc_context_s
+{
+ void *opaque;
+ void *(*malloc)(void *, size_t);
+ void *(*realloc)(void *, void *, size_t);
+ void (*free)(void *, void *);
+ void *(*calloc)(void *, size_t, size_t);
+};
+
+extern fz_alloc_context fz_alloc_default;
+
+/* Exception context */
+
+struct fz_error_context_s
+{
+ int top;
+ struct {
+ int failed;
+ jmp_buf buffer;
+ } stack[256];
+ char message[256];
+};
+
+#define fz_try(ctx) \
+ if (fz_push_try(ctx->error), \
+ !setjmp(ctx->error->stack[ctx->error->top].buffer)) \
+ {
+
+#define fz_catch(ctx) \
+ ctx->error->stack[ctx->error->top].failed = 0; \
+ } \
+ else \
+ ctx->error->stack[ctx->error->top].failed = 1; \
+ if (ctx->error->stack[ctx->error->top--].failed)
+
+void fz_push_try(fz_error_context *ex);
+void fz_throw(fz_context *, char *, ...);
+void fz_rethrow(fz_context *);
+
+/* Fitz per-thread context */
+
+struct fz_context_s
+{
+ fz_alloc_context *alloc;
+ fz_error_context *error;
+ struct fz_obj_s *(*resolve_indirect)(struct fz_obj_s *);
+};
+
+fz_context *fz_new_context(fz_alloc_context *alloc);
+fz_context *fz_clone_context(fz_context *ctx);
+void fz_free_context(fz_context *ctx);
+
/*
* Basic runtime and utility functions
*/
@@ -157,7 +214,6 @@ void fz_free(fz_context *ctx, void *p);
void *fz_malloc_nothrow(fz_context *ctx, size_t size);
void *fz_calloc_nothrow(fz_context *ctx, size_t count, size_t size);
void *fz_realloc_nothrow(fz_context *ctx, void *p, size_t size);
-char *fz_strdup_nothrow(fz_context *ctx, char *s);
/* runtime (hah!) test for endian-ness */
int fz_is_big_endian(void);
@@ -371,7 +427,7 @@ void aes_crypt_cbc( fz_aes *ctx, int mode, int length,
typedef struct fz_obj_s fz_obj;
-#define fz_resolve_indirect(ctx, obj) (ctx)->fz_resolve_indirect((obj))
+#define fz_resolve_indirect(ctx, obj) (ctx)->resolve_indirect((obj))
fz_obj *fz_new_null(fz_context *ctx);
fz_obj *fz_new_bool(fz_context *ctx, int b);
@@ -1135,29 +1191,4 @@ enum
FZ_BLEND_KNOCKOUT = 32
};
-/* Fitz allocator context */
-
-struct fz_alloc_context
-{
- void *opaque;
- void *(*malloc)(void *, size_t);
- void *(*realloc)(void *, void *, size_t);
- void (*free)(void *, void *);
- void *(*calloc)(void *, size_t, size_t);
-};
-
-extern fz_alloc_context fz_alloc_default;
-
-/* Fitz context */
-
-struct fz_context
-{
- fz_except_context *except;
- fz_alloc_context *alloc;
- fz_obj *(*fz_resolve_indirect)(fz_obj*);
-};
-
-fz_context *fz_context_init(fz_alloc_context *alloc);
-void fz_context_fin(fz_context *ctx);
-
#endif
diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c
index d9691be5..1dd591ab 100644
--- a/pdf/pdf_xref.c
+++ b/pdf/pdf_xref.c
@@ -527,7 +527,7 @@ pdf_open_xref_with_stream(pdf_xref **xrefp, fz_stream *file, char *password)
fz_context *ctx = file->ctx;
/* install pdf specific callback */
- ctx->fz_resolve_indirect = pdf_resolve_indirect;
+ ctx->resolve_indirect = pdf_resolve_indirect;
xref = fz_calloc(ctx, 1, sizeof(pdf_xref));
diff --git a/scripts/cmapdump.c b/scripts/cmapdump.c
index 06e74beb..23798ec6 100644
--- a/scripts/cmapdump.c
+++ b/scripts/cmapdump.c
@@ -16,7 +16,6 @@
#include "../fitz/stm_open.c"
#include "../fitz/stm_read.c"
#include "../fitz/context.c"
-#include "../fitz/except.c"
#include "../pdf/pdf_lex.c"
#include "../pdf/pdf_cmap.c"
@@ -51,7 +50,7 @@ main(int argc, char **argv)
return 1;
}
- ctx = fz_context_init(&fz_alloc_default);
+ ctx = fz_new_context(&fz_alloc_default);
if (ctx == NULL)
{
fprintf(stderr, "failed to initialise!\n");
@@ -166,6 +165,6 @@ main(int argc, char **argv)
return 1;
}
- fz_context_fin(ctx);
+ fz_free_context(ctx);
return 0;
}
diff --git a/win32/libmupdf.vcproj b/win32/libmupdf.vcproj
index 21852ea0..4ca6be25 100644
--- a/win32/libmupdf.vcproj
+++ b/win32/libmupdf.vcproj
@@ -339,6 +339,10 @@
Name="fitz"
>
<File
+ RelativePath="..\fitz\base_context.c"
+ >
+ </File>
+ <File
RelativePath="..\fitz\base_error.c"
>
</File>
@@ -371,10 +375,6 @@
>
</File>
<File
- RelativePath="..\fitz\context.c"
- >
- </File>
- <File
RelativePath="..\fitz\crypt_aes.c"
>
</File>
@@ -411,18 +411,6 @@
>
</File>
<File
- RelativePath="..\fitz\except.c"
- >
- </File>
- <File
- RelativePath="..\fitz\except.h"
- >
- </File>
- <File
- RelativePath="..\fitz\exceptxxx.h"
- >
- </File>
- <File
RelativePath="..\fitz\filt_basic.c"
>
</File>