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