io.c

filter: Statementstmt 52.3% 44 / 84 Branchbran 27.7% 10 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 45.0% 54 / 120 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« context.c ↑ Summary paste.im »
1 #define IMAGER_NO_CONTEXT
2 #include "imager.h"
3 #include "imageri.h"
4 #include <stdlib.h>
5 #ifndef _MSC_VER
6 #include <unistd.h>
7 #endif
8  
9 /*
10 =item mymalloc(size)
11 =category Memory Management
12  
13 Allocate a block of C<size> bytes of memory.
14  
15 exit()s on failure.
16  
17 Always uses im_get_context() to fetch the current Imager context.
18  
19 =cut
20 */
21  
22 void *
23 22664 mymalloc(size_t size) {
24 22664 dIMCTX;
25 22664 return i_malloc(size);
26 }
27  
28 /*
29 =item im_malloc(aIMCTX, size)
30 =category Memory Management
31 X<i_malloc>
32  
33 Allocate a block of C<size> bytes of memory.
34  
35 exit()s on failure.
36  
37 Callable as C<i_malloc(size)>, and will use the local IMCTX under
38 IMAGER_NO_CONTEXT.
39  
40 =cut
41 */
42  
43 void*
44 27669 im_malloc(pIMCTX, size_t size) {
45 void *buf;
46  
47 27669 ▶ if ( (buf = malloc(size)) == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (buf = malloc(size)) == NULL ) { 0 27669 27669
48 0 im_out_of_memory(aIMCTX, "mymalloc", size);
49 }
50 27669 im_log((aIMCTX, 1, "mymalloc(size %ld) -> %p\n", (long)size, buf));
51 27669 return buf;
52 }
53  
54 /*
55 =item myfree(p)
56 =category Memory Management
57  
58 Release the memory block C<p> points at.
59  
60 This is not suitable for memory allocated by perl itself.
61  
62 Always uses im_get_context() to fetch the current Imager context.
63  
64 =cut
65 */
66  
67 void
68 21866 myfree(void *p) {
69 21866 dIMCTX;
70 21866 i_free(p);
71 21866 }
72  
73 /*
74 =item im_free(aIMCTX, p)
75 =category Memory Management
76  
77 Release the memory block C<p> points at.
78  
79 This is not suitable for memory allocated by perl itself.
80  
81 Callable as i_free(p), and will use the local IMCTX under
82 IMAGER_NO_CONTEXT.
83  
84 =cut
85 */
86  
87 void
88 29182 im_free(pIMCTX, void *p) {
89 29182 im_log((aIMCTX, 1, "myfree(p %p)\n", p));
90 29182 free(p);
91 29182 }
92  
93 /*
94 =item myrealloc(p, size)
95 =category Memory Management
96  
97 Resize the block C<p> to C<size> bytes of memory.
98  
99 exit()s on failure.
100  
101 Always uses im_get_context() to fetch the current Imager context.
102  
103 =cut
104 */
105  
106 void *
107 11 myrealloc(void *block, size_t size) {
108 11 dIMCTX;
109 11 return i_realloc(block, size);
110 }
111  
112 /*
113 =item im_realloc(aIMCTX, p, size)
114 =category Memory Management
115  
116 Resize the block C<p> to C<size> bytes of memory.
117  
118 exit()s on failure.
119  
120 Callable as C<i_realloc(p, size)>, and will use the local IMCTX under
121 IMAGER_NO_CONTEXT.
122  
123 =cut
124 */
125  
126 void *
127 11 im_realloc(pIMCTX, void *block, size_t size) {
128 void *result;
129  
130 11 im_log((aIMCTX, 1, "myrealloc(block %p, size %ld)\n", block, (long)size));
131 11 ▶ if ((result = realloc(block, size)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((result = realloc(block, size)) == NULL) { 0 11 11
132 0 im_out_of_memory(aIMCTX, "myrealloc", size);
133 }
134 11 return result;
135 }
136  
137 /*
138 =item im_malloc_fail(aIMCTX, size)
139 =category Memory Management
140  
141 Allocate C<size> bytes of memory, returning NULL on failure.
142  
143 =cut
144 */
145  
146 void *
147 1500 im_malloc_fail(pIMCTX, size_t size) {
148 void *buf;
149  
150 1500 ▶ if ( (buf = malloc(size)) == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (buf = malloc(size)) == NULL ) { 0 1500 1500
151 0 im_log((aIMCTX, 1, "im_malloc_fail: unable to allocate %zu\n",
152 size));
153 0 return NULL;
154 }
155 1500 im_log((aIMCTX, 1, "im_malloc_fail(size %zu) -> %p\n",
156 size, buf));
157 1500 return buf;
158 }
159  
160 /* memory pool implementation */
161  
162 void
163 5 i_mempool_init(i_mempool *mp) {
164 5 mp->alloc = 10;
165 5 mp->used = 0;
166 5 mp->p = mymalloc(sizeof(void*)*mp->alloc);
167 5 }
168  
169 void
170 0 i_mempool_extend(i_mempool *mp) {
171 0 mp->p = myrealloc(mp->p, mp->alloc * 2);
172 0 mp->alloc *=2;
173 0 }
174  
175 void *
176 10 i_mempool_alloc(i_mempool *mp, size_t size) {
177 10 ▶ if (mp->used == mp->alloc) i_mempool_extend(mp);
Branch: 1 branch
BranchTrueFalseTotal
if (mp->used == mp->alloc) i_mempool_extend(mp); 0 10 10
178 10 mp->p[mp->used] = mymalloc(size);
179 10 mp->used++;
180 10 return mp->p[mp->used-1];
181 }
182  
183  
184 void
185 5 i_mempool_destroy(i_mempool *mp) {
186 unsigned int i;
187 15 ▶ for(i=0; i<mp->used; i++) myfree(mp->p[i]);
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<mp->used; i++) myfree(mp->p[i]); 10 5 15
188 5 myfree(mp->p);
189 5 }
190  
191  
192  
193 /* Should these really be here? */
194  
195 #undef min
196 #undef max
197  
198 i_img_dim
199 173919 i_minx(i_img_dim a, i_img_dim b) {
200 173919 ▶ if (a<b) return a; else return b;
Branch: 1 branch
BranchTrueFalseTotal
if (a<b) return a; else return b; 47886 126033 173919
201 }
202  
203 i_img_dim
204 35871 i_maxx(i_img_dim a, i_img_dim b) {
205 35871 ▶ if (a>b) return a; else return b;
Branch: 1 branch
BranchTrueFalseTotal
if (a>b) return a; else return b; 19171 16700 35871
206 }
207  
208  
209 struct utf8_size {
210 int mask, expect;
211 int size;
212 };
213  
214 struct utf8_size utf8_sizes[] =
215 {
216 { 0x80, 0x00, 1 },
217 { 0xE0, 0xC0, 2 },
218 { 0xF0, 0xE0, 3 },
219 { 0xF8, 0xF0, 4 },
220 };
221  
222 /*
223 =item i_utf8_advance(char **p, size_t *len)
224  
225 Retrieve a C<UTF-8> character from the stream.
226  
227 Modifies *p and *len to indicate the consumed characters.
228  
229 This doesn't support the extended C<UTF-8> encoding used by later
230 versions of Perl. Since this is typically used to implement text
231 output by font drivers, the strings supplied shouldn't have such out
232 of range characters.
233  
234 This doesn't check that the C<UTF-8> character is using the shortest
235 possible representation.
236  
237 Returns ~0UL on failure.
238  
239 =cut
240 */
241  
242 unsigned long
243 0 i_utf8_advance(char const **p, size_t *len) {
244 unsigned char c;
245 unsigned i;
246 int ci;
247 0 unsigned clen = 0;
248 unsigned char codes[3];
249 0 ▶ if (*len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (*len == 0) 0 0 0
250 0 return ~0UL;
251 0 c = *(*p)++; --*len;
252  
253 0 ▶ for (i = 0; i < sizeof(utf8_sizes)/sizeof(*utf8_sizes); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(utf8_sizes)/sizeof(*utf8_sizes); ++i) { 0 0 0
254 0 ▶ if ((c & utf8_sizes[i].mask) == utf8_sizes[i].expect) {
Branch: 1 branch
BranchTrueFalseTotal
if ((c & utf8_sizes[i].mask) == utf8_sizes[i].expect) { 0 0 0
255 0 clen = utf8_sizes[i].size;
256 0 break;
257 }
258 }
259 0 ▶ if (clen == 0 || *len < clen-1) {
Branch: 2 branches
BranchTrueFalseTotal
if (clen == 0 || *len < clen-1) { 0 0 0
if (clen == 0 || *len < clen-1) { 0 0 0
260 0 --*p; ++*len;
261 0 return ~0UL;
262 }
263  
264 /* check that each character is well formed */
265 0 i = 1;
266 0 ci = 0;
267 0 ▶ while (i < clen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < clen) { 0 0 0
268 0 ▶ if (((*p)[ci] & 0xC0) != 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (((*p)[ci] & 0xC0) != 0x80) { 0 0 0
269 0 --*p; ++*len;
270 0 return ~0UL;
271 }
272 0 codes[ci] = (*p)[ci];
273 0 ++ci; ++i;
274 }
275 0 *p += clen-1; *len -= clen-1;
276 0 ▶ if (c & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c & 0x80) { 0 0 0
277 0 ▶ if ((c & 0xE0) == 0xC0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((c & 0xE0) == 0xC0) { 0 0 0
278 0 return ((c & 0x1F) << 6) + (codes[0] & 0x3F);
279 }
280 0 ▶ else if ((c & 0xF0) == 0xE0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((c & 0xF0) == 0xE0) { 0 0 0
281 0 return ((c & 0x0F) << 12) | ((codes[0] & 0x3F) << 6) | (codes[1] & 0x3f);
282 }
283 0 ▶ else if ((c & 0xF8) == 0xF0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((c & 0xF8) == 0xF0) { 0 0 0
284 0 return ((c & 0x07) << 18) | ((codes[0] & 0x3F) << 12)
285 0 | ((codes[1] & 0x3F) << 6) | (codes[2] & 0x3F);
286 }
287 else {
288 0 *p -= clen; *len += clen;
289 0 return ~0UL;
290 }
291 }
292 else {
293 0 return c;
294 }
295 }
296  
« context.c ↑ Summary paste.im »