Debian Codegraph @

io.c

raw
1 #include "imager.h"
2 #include "imageri.h"
3 #include <stdlib.h>
4 #ifndef _MSC_VER
5 #include <unistd.h>
6 #endif
7
8
9 /* FIXME: make allocation dynamic */
10
11
12 #ifdef IMAGER_DEBUG_MALLOC
13
14 #define MAXMAL 102400
15 #define MAXDESC 65
16
17 #define UNDRRNVAL 10
18 #define OVERRNVAL 10
19
20 #define PADBYTE 0xaa
21
22
23 static int malloc_need_init = 1;
24
25 typedef struct {
26 void* ptr;
27 size_t size;
28 const char *file;
29 int line;
30 } malloc_entry;
31
32 malloc_entry malloc_pointers[MAXMAL];
33
34
35
36
37 /* Utility functions */
38
39
40 static
41 void
42 malloc_init(void) {
43 int i;
44 for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
45 malloc_need_init = 0;
46 atexit(malloc_state);
47 }
48
49
50 static
51 int
52 find_ptr(void *p) {
53 int i;
54 for(i=0;i<MAXMAL;i++)
55 if (malloc_pointers[i].ptr == p)
56 return i;
57 return -1;
58 }
59
60
61 /* Takes a pointer to real start of array,
62 * sets the entries in the table, returns
63 * the offset corrected pointer */
64
65 static
66 void *
67 set_entry(int i, char *buf, size_t size, char *file, int line) {
68 memset( buf, PADBYTE, UNDRRNVAL );
69 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
70 buf += UNDRRNVAL;
71 malloc_pointers[i].ptr = buf;
72 malloc_pointers[i].size = size;
73 malloc_pointers[i].file = file;
74 malloc_pointers[i].line = line;
75 return buf;
76 }
77
78 void
79 malloc_state(void) {
80 int i;
81 size_t total = 0;
82
83 i_clear_error();
84 mm_log((0,"malloc_state()\n"));
85 bndcheck_all();
86 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
87 mm_log((0,"%d: %lu (%p) : %s (%d)\n", i, (unsigned long)malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].file, malloc_pointers[i].line));
88 total += malloc_pointers[i].size;
89 }
90 if (total == 0) mm_log((0,"No memory currently used!\n"))
91 else mm_log((0,"total: %lu\n", (unsigned long)total));
92 }
93
94
95
96 void*
97 mymalloc_file_line(size_t size, char* file, int line) {
98 char *buf;
99 int i;
100 if (malloc_need_init) malloc_init();
101
102 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
103
104 if ( (i = find_ptr(NULL)) < 0 ) {
105 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
106 exit(3);
107 }
108
109 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
110 mm_log((1,"Unable to allocate %ld for %s (%i)\n", (long)size, file, line));
111 exit(3);
112 }
113
114 buf = set_entry(i, buf, size, file, line);
115 mm_log((1,"mymalloc_file_line: slot <%d> %ld bytes allocated at %p for %s (%d)\n", i, (long)size, buf, file, line));
116 return buf;
117 }
118
119 void *
120 (mymalloc)(size_t size) {
121 return mymalloc_file_line(size, "unknown", 0);
122 }
123
124 void*
125 myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
126 char *buf;
127 int i;
128
129 if (malloc_need_init) malloc_init();
130 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
131
132 if (!ptr) {
133 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
134 return mymalloc_file_line(newsize, file, line);
135 }
136
137 if (!newsize) {
138 mm_log((1, "newsize = 0, sending request to free\n"));
139 myfree_file_line(ptr, file, line);
140 return NULL;
141 }
142
143 if ( (i = find_ptr(ptr)) == -1) {
144 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
145 exit(3);
146 }
147
148 if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
149 mm_log((1,"Unable to reallocate %ld bytes at %p for %s (%i)\n", (long)
150 newsize, ptr, file, line));
151 exit(3);
152 }
153
154 buf = set_entry(i, buf, newsize, file, line);
155 mm_log((1,"realloc_file_line: slot <%d> %ld bytes allocated at %p for %s (%d)\n", i, (long)newsize, buf, file, line));
156 return buf;
157 }
158
159 void *
160 (myrealloc)(void *ptr, size_t newsize) {
161 return myrealloc_file_line(ptr, newsize, "unknown", 0);
162 }
163
164 static
165 void
166 bndcheck(int idx) {
167 int i;
168 size_t s = malloc_pointers[idx].size;
169 unsigned char *pp = malloc_pointers[idx].ptr;
170 if (!pp) {
171 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
172 return;
173 }
174
175 for(i=0;i<UNDRRNVAL;i++) {
176 if (pp[-(1+i)] != PADBYTE)
177 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %ld\n", i+1, idx, pp, (long)s ));
178 }
179
180 for(i=0;i<OVERRNVAL;i++) {
181 if (pp[s+i] != PADBYTE)
182 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %ld\n", i+1, idx, pp, (long)s ));
183 }
184 }
185
186 void
187 bndcheck_all() {
188 int idx;
189 mm_log((1, "bndcheck_all()\n"));
190 for(idx=0; idx<MAXMAL; idx++)
191 if (malloc_pointers[idx].ptr)
192 bndcheck(idx);
193 }
194
195 void
196 myfree_file_line(void *p, char *file, int line) {
197 char *pp = p;
198 int match = 0;
199 int i;
200
201 if (p == NULL)
202 return;
203
204 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
205 mm_log((1,"myfree_file_line: pointer %i (%s (%d)) freed at %s (%i)\n", i, malloc_pointers[i].file, malloc_pointers[i].line, file, line));
206 bndcheck(i);
207 malloc_pointers[i].ptr = NULL;
208 match++;
209 }
210
211 mm_log((1, "myfree_file_line: freeing address %p (real %p)\n", pp, pp-UNDRRNVAL));
212
213 if (match != 1) {
214 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
215 fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
216 exit(255);
217 }
218
219
220 free(pp-UNDRRNVAL);
221 }
222
223 void
224 (myfree)(void *block) {
225 myfree_file_line(block, "unknown", 0);
226 }
227
228 #else
229
230 void
231 malloc_state() {
232 }
233
234 void*
235 mymalloc(size_t size) {
236 void *buf;
237
238 if ( (buf = malloc(size)) == NULL ) {
239 mm_log((1, "mymalloc: unable to malloc %ld\n", (long)size));
240 fprintf(stderr,"Unable to malloc %ld.\n", (long)size); exit(3);
241 }
242 mm_log((1, "mymalloc(size %ld) -> %p\n", (long)size, buf));
243 return buf;
244 }
245
246 void *
247 mymalloc_file_line(size_t size, char *file, int line) {
248 (void)file;
249 (void)line;
250 return mymalloc(size);
251 }
252
253 void
254 myfree(void *p) {
255 mm_log((1, "myfree(p %p)\n", p));
256 free(p);
257 }
258
259 void
260 myfree_file_line(void *p, char *file, int line) {
261 (void)file;
262 (void)line;
263 myfree(p);
264 }
265
266 void *
267 myrealloc(void *block, size_t size) {
268 void *result;
269
270 mm_log((1, "myrealloc(block %p, size %ld)\n", block, (long)size));
271 if ((result = realloc(block, size)) == NULL) {
272 mm_log((1, "myrealloc: out of memory\n"));
273 fprintf(stderr, "Out of memory.\n");
274 exit(3);
275 }
276 return result;
277 }
278
279 void *
280 myrealloc_file_line(void *block, size_t newsize, char *file, int line) {
281 (void)file;
282 (void)line;
283 return myrealloc(block, newsize);
284 }
285
286 #endif /* IMAGER_MALLOC_DEBUG */
287
288
289
290
291 /* memory pool implementation */
292
293 void
294 i_mempool_init(i_mempool *mp) {
295 mp->alloc = 10;
296 mp->used = 0;
297 mp->p = mymalloc(sizeof(void*)*mp->alloc);
298 }
299
300 void
301 i_mempool_extend(i_mempool *mp) {
302 mp->p = myrealloc(mp->p, mp->alloc * 2);
303 mp->alloc *=2;
304 }
305
306 void *
307 i_mempool_alloc(i_mempool *mp, size_t size) {
308 if (mp->used == mp->alloc) i_mempool_extend(mp);
309 mp->p[mp->used] = mymalloc(size);
310 mp->used++;
311 return mp->p[mp->used-1];
312 }
313
314
315 void
316 i_mempool_destroy(i_mempool *mp) {
317 unsigned int i;
318 for(i=0; i<mp->used; i++) myfree(mp->p[i]);
319 myfree(mp->p);
320 }
321
322
323
324 /* Should these really be here? */
325
326 #undef min
327 #undef max
328
329 i_img_dim
330 i_minx(i_img_dim a, i_img_dim b) {
331 if (a<b) return a; else return b;
332 }
333
334 i_img_dim
335 i_maxx(i_img_dim a, i_img_dim b) {
336 if (a>b) return a; else return b;
337 }
338
339
340 struct utf8_size {
341 int mask, expect;
342 int size;
343 };
344
345 struct utf8_size utf8_sizes[] =
346 {
347 { 0x80, 0x00, 1 },
348 { 0xE0, 0xC0, 2 },
349 { 0xF0, 0xE0, 3 },
350 { 0xF8, 0xF0, 4 },
351 };
352
353 /*
354 =item i_utf8_advance(char **p, size_t *len)
355
356 Retrieve a C<UTF-8> character from the stream.
357
358 Modifies *p and *len to indicate the consumed characters.
359
360 This doesn't support the extended C<UTF-8> encoding used by later
361 versions of Perl. Since this is typically used to implement text
362 output by font drivers, the strings supplied shouldn't have such out
363 of range characters.
364
365 This doesn't check that the C<UTF-8> character is using the shortest
366 possible representation.
367
368 Returns ~0UL on failure.
369
370 =cut
371 */
372
373 unsigned long
374 i_utf8_advance(char const **p, size_t *len) {
375 unsigned char c;
376 unsigned i;
377 int ci;
378 unsigned clen = 0;
379 unsigned char codes[3];
380 if (*len == 0)
381 return ~0UL;
382 c = *(*p)++; --*len;
383
384 for (i = 0; i < sizeof(utf8_sizes)/sizeof(*utf8_sizes); ++i) {
385 if ((c & utf8_sizes[i].mask) == utf8_sizes[i].expect) {
386 clen = utf8_sizes[i].size;
387 break;
388 }
389 }
390 if (clen == 0 || *len < clen-1) {
391 --*p; ++*len;
392 return ~0UL;
393 }
394
395 /* check that each character is well formed */
396 i = 1;
397 ci = 0;
398 while (i < clen) {
399 if (((*p)[ci] & 0xC0) != 0x80) {
400 --*p; ++*len;
401 return ~0UL;
402 }
403 codes[ci] = (*p)[ci];
404 ++ci; ++i;
405 }
406 *p += clen-1; *len -= clen-1;
407 if (c & 0x80) {
408 if ((c & 0xE0) == 0xC0) {
409 return ((c & 0x1F) << 6) + (codes[0] & 0x3F);
410 }
411 else if ((c & 0xF0) == 0xE0) {
412 return ((c & 0x0F) << 12) | ((codes[0] & 0x3F) << 6) | (codes[1] & 0x3f);
413 }
414 else if ((c & 0xF8) == 0xF0) {
415 return ((c & 0x07) << 18) | ((codes[0] & 0x3F) << 12)
416 | ((codes[1] & 0x3F) << 6) | (codes[2] & 0x3F);
417 }
418 else {
419 *p -= clen; *len += clen;
420 return ~0UL;
421 }
422 }
423 else {
424 return c;
425 }
426 }
427