context.c

filter: Statementstmt 37.5% 65 / 173 Branchbran 30.4% 25 / 82 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 35.2% 90 / 255 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« quant.c ↑ Summary io.c »
1 #include "imager.h"
2 #include "imageri.h"
3 #include <stdio.h>
4  
5 static volatile im_slot_t slot_count = 1;
6 static im_slot_destroy_t *volatile slot_destructors;
7 static volatile i_mutex_t slot_mutex;
8  
9 /*
10 =item im_context_new()
11  
12 Create a new Imager context object.
13  
14 =cut
15 */
16  
17 im_context_t
18 79 im_context_new(void) {
19 79 im_context_t ctx = malloc(sizeof(im_context_struct));
20 int i;
21  
22 79 ▶ if (!slot_mutex)
Branch: 1 branch
BranchTrueFalseTotal
if (!slot_mutex) 79 0 79
23 79 slot_mutex = i_mutex_new();
24  
25 79 ▶ if (!ctx)
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx) 0 79 79
26 0 return NULL;
27
28 79 ctx->error_sp = IM_ERROR_COUNT-1;
29 1659 ▶ for (i = 0; i < IM_ERROR_COUNT; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < IM_ERROR_COUNT; ++i) { 1580 79 1659
30 1580 ctx->error_alloc[i] = 0;
31 1580 ctx->error_stack[i].msg = NULL;
32 1580 ctx->error_stack[i].code = 0;
33 }
34 #ifdef IMAGER_LOG
35 79 ctx->log_level = 0;
36 79 ctx->lg_file = NULL;
37 #endif
38 79 ctx->max_width = 0;
39 79 ctx->max_height = 0;
40 79 ctx->max_bytes = DEF_BYTES_LIMIT;
41  
42 79 ctx->slot_alloc = slot_count;
43 79 ctx->slots = calloc(ctx->slot_alloc, sizeof(void *));
44 79 ▶ if (!ctx->slots) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->slots) { 0 79 79
45 0 free(ctx);
46 0 return NULL;
47 }
48  
49 79 ctx->file_magic = NULL;
50  
51 79 ctx->out_of_memory = im_def_out_of_memory;
52 79 ctx->out_of_memory_userdata = NULL;
53  
54 #if IMAGER_PTR_SIZE > 4
55 79 ctx->max_mmap_size = 0x80000000UL; /* 2GB */
56 #else
57 ctx->max_mmap_size = 0x01000000UL; /* 16MB */
58 #endif
59  
60 79 ctx->refcount = 1;
61  
62 #ifdef IMAGER_TRACE_CONTEXT
63 fprintf(stderr, "im_context: created %p\n", ctx);
64 #endif
65  
66  
67 79 return ctx;
68 }
69  
70 /*
71 =item im_context_refinc(ctx, where)
72 X<im_context_refinc API>
73 =section Context objects
74 =synopsis im_context_refinc(aIMCTX, "a description");
75  
76 Add a new reference to the context.
77  
78 =cut
79 */
80  
81 void
82 2267 im_context_refinc(im_context_t ctx, const char *where) {
83 2267 ++ctx->refcount;
84  
85 #ifdef IMAGER_TRACE_CONTEXT
86 fprintf(stderr, "im_context:%s: refinc %p (count now %lu)\n", where,
87 ctx, (unsigned long)ctx->refcount);
88 #else
89 (void)where;
90 #endif
91 2267 }
92  
93 /*
94 =item im_context_refdec(ctx, where)
95 X<im_context_refdec API>
96 =section Context objects
97 =synopsis im_context_refdec(aIMCTX, "a description");
98  
99 Remove a reference to the context, releasing it if all references have
100 been removed.
101  
102 =cut
103 */
104  
105 void
106 2346 im_context_refdec(im_context_t ctx, const char *where) {
107 int i;
108 size_t slot;
109  
110 2346 ▶ im_assert(ctx->refcount > 0);
Branch: 1 branch
BranchTrueFalseTotal
im_assert(ctx->refcount > 0); 0 2346 2346
111  
112 2346 --ctx->refcount;
113  
114 #ifdef IMAGER_TRACE_CONTEXT
115 fprintf(stderr, "im_context:%s: delete %p (count now %lu)\n", where,
116 ctx, (unsigned long)ctx->refcount);
117 #else
118 (void)where;
119 #endif
120  
121 2346 ▶ if (ctx->refcount != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->refcount != 0) 2267 79 2346
122 2267 return;
123  
124 /* lock here to avoid slot_destructors from being moved under us */
125 79 i_mutex_lock(slot_mutex);
126 158 ▶ for (slot = 0; slot < ctx->slot_alloc; ++slot) {
Branch: 1 branch
BranchTrueFalseTotal
for (slot = 0; slot < ctx->slot_alloc; ++slot) { 79 79 158
127 79 ▶ if (ctx->slots[slot] && slot_destructors[slot])
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->slots[slot] && slot_destructors[slot]) 0 79 79
if (ctx->slots[slot] && slot_destructors[slot]) 0 0 0
128 0 slot_destructors[slot](ctx->slots[slot]);
129 }
130 79 i_mutex_unlock(slot_mutex);
131  
132 79 free(ctx->slots);
133  
134 1659 ▶ for (i = 0; i < IM_ERROR_COUNT; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < IM_ERROR_COUNT; ++i) { 1580 79 1659
135 1580 ▶ if (ctx->error_stack[i].msg)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->error_stack[i].msg) 8 1572 1580
136 8 myfree(ctx->error_stack[i].msg);
137 }
138  
139 {
140 79 im_file_magic *p = ctx->file_magic;
141 80 ▶ while (p != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (p != NULL) { 1 79 80
142 1 im_file_magic *n = p->next;
143 1 free(p->m.name);
144 1 free(p->m.magic);
145 1 free(p->m.mask);
146 1 free(p);
147 1 p = n;
148 }
149 }
150 #ifdef IMAGER_LOG
151 79 ▶ if (ctx->lg_file && ctx->own_log)
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->lg_file && ctx->own_log) 18 61 79
if (ctx->lg_file && ctx->own_log) 18 0 18
152 18 fclose(ctx->lg_file);
153 #endif
154  
155 79 free(ctx);
156 }
157  
158 /*
159 =item im_context_clone(ctx)
160  
161 Clone an Imager context object, returning the result.
162  
163 The error stack is not copied from the original context.
164  
165 =cut
166 */
167  
168 im_context_t
169 0 im_context_clone(im_context_t ctx, const char *where) {
170 0 im_context_t nctx = malloc(sizeof(im_context_struct));
171 int i;
172  
173 0 ▶ if (!nctx)
Branch: 1 branch
BranchTrueFalseTotal
if (!nctx) 0 0 0
174 0 return NULL;
175  
176 0 nctx->slot_alloc = slot_count;
177 0 nctx->slots = calloc(nctx->slot_alloc, sizeof(void *));
178 0 ▶ if (!nctx->slots) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nctx->slots) { 0 0 0
179 0 free(nctx);
180 0 return NULL;
181 }
182  
183 0 nctx->error_sp = IM_ERROR_COUNT-1;
184 0 ▶ for (i = 0; i < IM_ERROR_COUNT; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < IM_ERROR_COUNT; ++i) { 0 0 0
185 0 nctx->error_alloc[i] = 0;
186 0 nctx->error_stack[i].msg = NULL;
187 }
188 #ifdef IMAGER_LOG
189 0 nctx->log_level = ctx->log_level;
190 0 ▶ if (ctx->lg_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->lg_file) { 0 0 0
191 0 ▶ if (ctx->own_log) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->own_log) { 0 0 0
192 0 int newfd = dup(fileno(ctx->lg_file));
193 0 ▶ if (newfd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (newfd >= 0) { 0 0 0
194 0 nctx->own_log = 1;
195 0 nctx->lg_file = fdopen(newfd, "w");
196 0 ▶ if (nctx->lg_file)
Branch: 1 branch
BranchTrueFalseTotal
if (nctx->lg_file) 0 0 0
197 0 setvbuf(nctx->lg_file, NULL, _IONBF, BUFSIZ);
198 }
199 else {
200 #ifdef IMAGER_TRACE_CONTEXT
201 perror("im_context:failed to clone log");
202 #endif
203 0 free(nctx->slots);
204 0 free(nctx);
205 0 return NULL;
206 }
207 }
208 else {
209 /* stderr */
210 0 nctx->lg_file = ctx->lg_file;
211 0 nctx->own_log = 0;
212 }
213 }
214 else {
215 0 nctx->lg_file = NULL;
216 }
217 #endif
218 0 nctx->max_width = ctx->max_width;
219 0 nctx->max_height = ctx->max_height;
220 0 nctx->max_bytes = ctx->max_bytes;
221  
222 0 nctx->out_of_memory = ctx->out_of_memory;
223 0 nctx->out_of_memory_userdata = ctx->out_of_memory_userdata;
224  
225 0 nctx->max_mmap_size = ctx->max_mmap_size;
226  
227 0 nctx->refcount = 1;
228  
229 {
230 0 im_file_magic *inp = ctx->file_magic;
231 0 im_file_magic **outpp = &nctx->file_magic;
232 0 *outpp = NULL;
233 0 ▶ while (inp) {
Branch: 1 branch
BranchTrueFalseTotal
while (inp) { 0 0 0
234 0 im_file_magic *m = malloc(sizeof(im_file_magic));
235 0 ▶ if (!m) {
Branch: 1 branch
BranchTrueFalseTotal
if (!m) { 0 0 0
236 /* should free any parts of the list already allocated */
237 0 im_context_refdec(nctx, "failed cloning");
238 0 return NULL;
239 }
240 0 m->next = NULL;
241 0 m->m.name = strdup(inp->m.name);
242 0 m->m.magic_size = inp->m.magic_size;
243 0 m->m.magic = malloc(inp->m.magic_size);
244 0 m->m.mask = malloc(inp->m.magic_size);
245 0 ▶ if (m->m.name == NULL || m->m.magic == NULL || m->m.mask == NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (m->m.name == NULL || m->m.magic == NULL || m->m.mask == NULL) { 0 0 0
if (m->m.name == NULL || m->m.magic == NULL || m->m.mask == NULL) { 0 0 0
if (m->m.name == NULL || m->m.magic == NULL || m->m.mask == NULL) { 0 0 0
246 0 free(m->m.name);
247 0 free(m->m.magic);
248 0 free(m->m.mask);
249 0 free(m);
250 0 im_context_refdec(nctx, "failed cloning");
251 0 return NULL;
252 }
253 0 memcpy(m->m.magic, inp->m.magic, m->m.magic_size);
254 0 memcpy(m->m.mask, inp->m.mask, m->m.magic_size);
255 0 *outpp = m;
256 0 outpp = &m->next;
257 0 inp = inp->next;
258 }
259 }
260  
261 #ifdef IMAGER_TRACE_CONTEXT
262 fprintf(stderr, "im_context:%s: cloned %p to %p\n", where, ctx, nctx);
263 #else
264 (void)where;
265 #endif
266  
267 0 return nctx;
268 }
269  
270 /*
271 =item im_context_slot_new(destructor)
272  
273 Allocate a new context-local-storage slot.
274  
275 C<desctructor> will be called when the context is destroyed if the
276 corresponding slot is non-NULL.
277  
278 =cut
279 */
280  
281 im_slot_t
282 0 im_context_slot_new(im_slot_destroy_t destructor) {
283 im_slot_t new_slot;
284 im_slot_destroy_t *new_destructors;
285 0 ▶ if (!slot_mutex)
Branch: 1 branch
BranchTrueFalseTotal
if (!slot_mutex) 0 0 0
286 0 slot_mutex = i_mutex_new();
287  
288 0 i_mutex_lock(slot_mutex);
289  
290 0 new_slot = slot_count++;
291 0 new_destructors = realloc(slot_destructors, sizeof(void *) * slot_count);
292 0 ▶ if (!new_destructors)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_destructors) 0 0 0
293 0 i_fatal(1, "Cannot allocate memory for slot destructors");
294 0 slot_destructors = new_destructors;
295  
296 0 slot_destructors[new_slot] = destructor;
297  
298 0 i_mutex_unlock(slot_mutex);
299  
300 0 return new_slot;
301 }
302  
303 /*
304 =item im_context_slot_set(slot, value)
305  
306 Set the value of a slot.
307  
308 Returns true on success.
309  
310 Aborts if the slot supplied is invalid.
311  
312 If reallocation of slot storage fails, returns false.
313  
314 =cut
315 */
316  
317 int
318 0 im_context_slot_set(im_context_t ctx, im_slot_t slot, void *value) {
319 0 ▶ if (slot < 0 || slot >= slot_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (slot < 0 || slot >= slot_count) { 0 0 0
if (slot < 0 || slot >= slot_count) { 0 0 0
320 0 fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n",
321 0 (int)slot, (int)slot_count-1);
322 0 abort();
323 }
324  
325 0 ▶ if ((size_t)slot >= ctx->slot_alloc) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)slot >= ctx->slot_alloc) { 0 0 0
326 size_t slot_index;
327 0 size_t new_alloc = slot_count;
328 0 void **new_slots = realloc(ctx->slots, sizeof(void *) * new_alloc);
329  
330 0 ▶ if (!new_slots)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_slots) 0 0 0
331 0 return 0;
332  
333 0 ▶ for (slot_index = ctx->slot_alloc; slot_index < new_alloc; ++slot_index)
Branch: 1 branch
BranchTrueFalseTotal
for (slot_index = ctx->slot_alloc; slot_index < new_alloc; ++slot_index) 0 0 0
334 0 new_slots[slot_index] = NULL;
335  
336 0 ctx->slots = new_slots;
337 0 ctx->slot_alloc = new_alloc;
338 }
339  
340 0 ctx->slots[slot] = value;
341  
342 0 return 1;
343 }
344  
345 /*
346 =item im_context_slot_get(ctx, slot)
347  
348 Retrieve the value previously stored in the given slot of the context
349 object.
350  
351 =cut
352 */
353  
354 void *
355 0 im_context_slot_get(im_context_t ctx, im_slot_t slot) {
356 0 ▶ if (slot < 0 || slot >= slot_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (slot < 0 || slot >= slot_count) { 0 0 0
if (slot < 0 || slot >= slot_count) { 0 0 0
357 0 fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n",
358 0 (int)slot, (int)slot_count-1);
359 0 abort();
360 }
361  
362 0 ▶ if ((size_t)slot >= ctx->slot_alloc)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)slot >= ctx->slot_alloc) 0 0 0
363 0 return NULL;
364  
365 0 return ctx->slots[slot];
366 }
367  
368 /*
369 =item im_add_file_magic(ctx, name, bits, mask, length)
370  
371 Add file type magic to the given context.
372  
373 =cut
374 */
375  
376 int
377 1 im_add_file_magic(im_context_t ctx, const char *name,
378 const unsigned char *bits, const unsigned char *mask,
379 size_t length) {
380 1 im_file_magic *m = malloc(sizeof(im_file_magic));
381  
382 1 ▶ if (m == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (m == NULL) 0 1 1
383 0 return 0;
384  
385 1 ▶ if (length > 512)
Branch: 1 branch
BranchTrueFalseTotal
if (length > 512) 0 1 1
386 0 length = 512;
387  
388 1 m->m.name = strdup(name);
389 1 m->m.magic = malloc(length);
390 1 m->m.mask = malloc(length);
391 1 m->m.magic_size = length;
392  
393 1 ▶ if (name == NULL || bits == NULL || mask == NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (name == NULL || bits == NULL || mask == NULL) { 1 0 1
if (name == NULL || bits == NULL || mask == NULL) { 1 0 1
if (name == NULL || bits == NULL || mask == NULL) { 0 1 1
394 0 free(m->m.name);
395 0 free(m->m.magic);
396 0 free(m->m.mask);
397 0 free(m);
398 0 return 0;
399 }
400 1 memcpy(m->m.magic, bits, length);
401 1 memcpy(m->m.mask, mask, length);
402 1 m->next = ctx->file_magic;
403 1 ctx->file_magic = m;
404  
405 1 return 1;
406 }
« quant.c ↑ Summary io.c »