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