Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7640 views
1
#include "mupdf/fitz.h"
2
3
struct fz_id_context_s
4
{
5
int refs;
6
int id;
7
};
8
9
static void
10
fz_drop_id_context(fz_context *ctx)
11
{
12
if (!ctx)
13
return;
14
if (fz_drop_imp(ctx, ctx->id, &ctx->id->refs))
15
fz_free(ctx, ctx->id);
16
}
17
18
static void
19
fz_new_id_context(fz_context *ctx)
20
{
21
ctx->id = fz_malloc_struct(ctx, fz_id_context);
22
ctx->id->refs = 1;
23
ctx->id->id = 0;
24
}
25
26
static fz_id_context *
27
fz_keep_id_context(fz_context *ctx)
28
{
29
if (!ctx)
30
return NULL;
31
return fz_keep_imp(ctx, ctx->id, &ctx->id->refs);
32
}
33
34
void
35
fz_drop_context(fz_context *ctx)
36
{
37
if (!ctx)
38
return;
39
40
/* Other finalisation calls go here (in reverse order) */
41
fz_drop_document_handler_context(ctx);
42
fz_drop_glyph_cache_context(ctx);
43
fz_drop_store_context(ctx);
44
fz_drop_aa_context(ctx);
45
fz_drop_colorspace_context(ctx);
46
fz_drop_font_context(ctx);
47
fz_drop_id_context(ctx);
48
49
if (ctx->warn)
50
{
51
fz_flush_warnings(ctx);
52
fz_free(ctx, ctx->warn);
53
}
54
55
if (ctx->error)
56
{
57
assert(ctx->error->top == -1);
58
fz_free(ctx, ctx->error);
59
}
60
61
/* Free the context itself */
62
ctx->alloc->free(ctx->alloc->user, ctx);
63
}
64
65
/* Allocate new context structure, and initialise allocator, and sections
66
* that aren't shared between contexts.
67
*/
68
static fz_context *
69
new_context_phase1(fz_alloc_context *alloc, fz_locks_context *locks)
70
{
71
fz_context *ctx;
72
73
ctx = alloc->malloc(alloc->user, sizeof(fz_context));
74
if (!ctx)
75
return NULL;
76
memset(ctx, 0, sizeof *ctx);
77
ctx->alloc = alloc;
78
ctx->locks = locks;
79
80
ctx->glyph_cache = NULL;
81
82
ctx->error = Memento_label(fz_malloc_no_throw(ctx, sizeof(fz_error_context)), "fz_error_context");
83
if (!ctx->error)
84
goto cleanup;
85
ctx->error->top = -1;
86
ctx->error->errcode = FZ_ERROR_NONE;
87
ctx->error->message[0] = 0;
88
89
ctx->warn = Memento_label(fz_malloc_no_throw(ctx, sizeof(fz_warn_context)), "fz_warn_context");
90
if (!ctx->warn)
91
goto cleanup;
92
ctx->warn->message[0] = 0;
93
ctx->warn->count = 0;
94
95
/* New initialisation calls for context entries go here */
96
fz_try(ctx)
97
{
98
fz_new_aa_context(ctx);
99
}
100
fz_catch(ctx)
101
{
102
goto cleanup;
103
}
104
105
return ctx;
106
107
cleanup:
108
fprintf(stderr, "cannot create context (phase 1)\n");
109
fz_drop_context(ctx);
110
return NULL;
111
}
112
113
fz_context *
114
fz_new_context_imp(fz_alloc_context *alloc, fz_locks_context *locks, unsigned int max_store, const char *version)
115
{
116
fz_context *ctx;
117
118
if (strcmp(version, FZ_VERSION))
119
{
120
fprintf(stderr, "cannot create context: incompatible header (%s) and library (%s) versions\n", version, FZ_VERSION);
121
return NULL;
122
}
123
124
if (!alloc)
125
alloc = &fz_alloc_default;
126
127
if (!locks)
128
locks = &fz_locks_default;
129
130
ctx = new_context_phase1(alloc, locks);
131
if (!ctx)
132
return NULL;
133
134
/* Now initialise sections that are shared */
135
fz_try(ctx)
136
{
137
fz_new_store_context(ctx, max_store);
138
fz_new_glyph_cache_context(ctx);
139
fz_new_colorspace_context(ctx);
140
fz_new_font_context(ctx);
141
fz_new_id_context(ctx);
142
fz_new_document_handler_context(ctx);
143
}
144
fz_catch(ctx)
145
{
146
fprintf(stderr, "cannot create context (phase 2)\n");
147
fz_drop_context(ctx);
148
return NULL;
149
}
150
return ctx;
151
}
152
153
fz_context *
154
fz_clone_context(fz_context *ctx)
155
{
156
/* We cannot safely clone the context without having locking/
157
* unlocking functions. */
158
if (ctx == NULL || ctx->locks == &fz_locks_default)
159
return NULL;
160
return fz_clone_context_internal(ctx);
161
}
162
163
fz_context *
164
fz_clone_context_internal(fz_context *ctx)
165
{
166
fz_context *new_ctx;
167
168
if (ctx == NULL || ctx->alloc == NULL)
169
return NULL;
170
171
new_ctx = new_context_phase1(ctx->alloc, ctx->locks);
172
if (!new_ctx)
173
return NULL;
174
175
/* Inherit AA defaults from old context. */
176
fz_copy_aa_context(new_ctx, ctx);
177
178
/* Keep thread lock checking happy by copying pointers first and locking under new context */
179
new_ctx->store = ctx->store;
180
new_ctx->store = fz_keep_store_context(new_ctx);
181
new_ctx->glyph_cache = ctx->glyph_cache;
182
new_ctx->glyph_cache = fz_keep_glyph_cache(new_ctx);
183
new_ctx->colorspace = ctx->colorspace;
184
new_ctx->colorspace = fz_keep_colorspace_context(new_ctx);
185
new_ctx->font = ctx->font;
186
new_ctx->font = fz_keep_font_context(new_ctx);
187
new_ctx->id = ctx->id;
188
new_ctx->id = fz_keep_id_context(new_ctx);
189
new_ctx->handler = ctx->handler;
190
new_ctx->handler = fz_keep_document_handler_context(new_ctx);
191
192
return new_ctx;
193
}
194
195
int
196
fz_gen_id(fz_context *ctx)
197
{
198
int id;
199
fz_lock(ctx, FZ_LOCK_ALLOC);
200
/* We'll never wrap around in normal use, but if we do, then avoid 0. */
201
do
202
id = ++ctx->id->id;
203
while (id == 0);
204
fz_unlock(ctx, FZ_LOCK_ALLOC);
205
return id;
206
}
207
208