Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 390 lines (289 sloc) 7.869 kb
02d1d628 »
2001-03-25 Initial revision
1 #include "io.h"
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 »
2001-05-14 Added more logging to quant.c. Removed global variables from gif.c.
13 #define MAXMAL 102400
02d1d628 »
2001-03-25 Initial revision
14 #define MAXDESC 65
15
a743c0a6 »
2001-05-14 Removed a bunch of unused variables and fixed an attempt to print out a
16 #define UNDRRNVAL 10
17 #define OVERRNVAL 10
02d1d628 »
2001-03-25 Initial revision
18
19 #define PADBYTE 0xaa
20
21
22 static int malloc_need_init = 1;
23
24 typedef struct {
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
25 void* ptr;
02d1d628 »
2001-03-25 Initial revision
26 size_t size;
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
27 char comm[MAXDESC];
02d1d628 »
2001-03-25 Initial revision
28 } malloc_entry;
29
30 malloc_entry malloc_pointers[MAXMAL];
31
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
32
33
34
35 /* Utility functions */
36
37
38 static
39 void
aea697ad »
2001-11-01 fix errors and some warnings on Win32
40 malloc_init(void) {
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
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
48 static
49 int
50 find_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
63 static
64 void *
65 set_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 »
2001-03-25 Initial revision
76
77
78 void
aea697ad »
2001-11-01 fix errors and some warnings on Win32
79 malloc_state(void) {
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
80 int i, total = 0;
81
cd4b0b20 »
2001-11-01 have error.c free a non-leak when using the debug malloc
82 i_clear_error();
02d1d628 »
2001-03-25 Initial revision
83 mm_log((0,"malloc_state()\n"));
84 bndcheck_all();
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
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 »
2001-03-25 Initial revision
88 }
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
89 if (total == 0) mm_log((0,"No memory currently used!\n"))
90 else mm_log((0,"total: %d\n",total));
02d1d628 »
2001-03-25 Initial revision
91 }
92
93
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
94
02d1d628 »
2001-03-25 Initial revision
95 void*
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
96 mymalloc_file_line(size_t size, char* file, int line) {
02d1d628 »
2001-03-25 Initial revision
97 char *buf;
98 int i;
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
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 »
2001-03-25 Initial revision
106 }
107
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
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 »
2001-03-25 Initial revision
111 }
112
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
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 »
2001-03-25 Initial revision
116 }
117
118
119
120
121
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
122
123
02d1d628 »
2001-03-25 Initial revision
124 void*
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
125 myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
126 char *buf;
02d1d628 »
2001-03-25 Initial revision
127 int i;
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
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);
02d1d628 »
2001-03-25 Initial revision
135 }
136
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
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 }
02d1d628 »
2001-03-25 Initial revision
142
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
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
aea697ad »
2001-11-01 fix errors and some warnings on Win32
148 if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
149 mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
150 exit(3);
02d1d628 »
2001-03-25 Initial revision
151 }
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
152
153 buf = set_entry(i, buf, newsize, file, line);
154 mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
155 return buf;
02d1d628 »
2001-03-25 Initial revision
156 }
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
157
158
159
02d1d628 »
2001-03-25 Initial revision
160
161 static
162 void
163 bndcheck(int idx) {
164 int i;
165 size_t s = malloc_pointers[idx].size;
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
166 unsigned char *pp = malloc_pointers[idx].ptr;
02d1d628 »
2001-03-25 Initial revision
167 if (!pp) {
168 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
169 return;
170 }
171
172 for(i=0;i<UNDRRNVAL;i++)
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
173 if (pp[-(1+i)] != PADBYTE)
174 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
02d1d628 »
2001-03-25 Initial revision
175
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
176 for(i=0;i<OVERRNVAL;i++)
02d1d628 »
2001-03-25 Initial revision
177 if (pp[s+i] != PADBYTE)
178 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
179 }
180
181 void
182 bndcheck_all() {
183 int idx;
184 mm_log((1, "bndcheck_all()\n"));
76ff75b8 »
2001-10-22 tga.c should now support reading/writing all variants of targa files.…
185 for(idx=0; idx<MAXMAL; idx++)
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
186 if (malloc_pointers[idx].ptr)
02d1d628 »
2001-03-25 Initial revision
187 bndcheck(idx);
188 }
189
190
191
192
193
194 void
195 myfree_file_line(void *p, char *file, int line) {
196 char *pp = p;
197 int match = 0;
198 int i;
199
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
200 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
02d1d628 »
2001-03-25 Initial revision
201 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
202 bndcheck(i);
fe6163bf »
2001-10-22 Various Changes to the memory accounting code, myrealloc() added.
203 malloc_pointers[i].ptr = NULL;
02d1d628 »
2001-03-25 Initial revision
204 match++;
205 }
97c4effc »
2002-03-06 merge write to gif tags updates
206
207 mm_log((1, "myfree_file_line: freeing address %p (real %p)\n", pp, pp-UNDRRNVAL));
02d1d628 »
2001-03-25 Initial revision
208
209 if (match != 1) {
f1ac5027 »
2001-09-01 support for generic fills for box and arc, with solid, hatched
210 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
aea697ad »
2001-11-01 fix errors and some warnings on Win32
211 fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
a73aeb5f »
2001-11-01 Fixed most outstanding memory leaks that are revealed in the test cases.
212 exit(255);
02d1d628 »
2001-03-25 Initial revision
213 }
214
215
216 free(pp-UNDRRNVAL);
217 }
218
219 #else
220
221 #define malloc_comm(a,b) (mymalloc(a))
222
223 void
224 malloc_state() {
225 printf("malloc_state: not in debug mode\n");
226 }
227
228 void*
229 mymalloc(int size) {
230 void *buf;
231
a743c0a6 »
2001-05-14 Removed a bunch of unused variables and fixed an attempt to print out a
232 if ( (buf = malloc(size)) == NULL ) {
faa9b3e7 »
2001-08-13 Egads
233 mm_log((1, "mymalloc: unable to malloc %d\n", size));
cc6483e0 »
2001-09-02 an extra stipple
234 fprintf(stderr,"Unable to malloc %d.\n", size); exit(3);
02d1d628 »
2001-03-25 Initial revision
235 }
f1ac5027 »
2001-09-01 support for generic fills for box and arc, with solid, hatched
236 mm_log((1, "mymalloc(size %d) -> %p\n", size, buf));
02d1d628 »
2001-03-25 Initial revision
237 return buf;
238 }
239
240 void
241 myfree(void *p) {
dd55acc8 »
2001-06-08 Switched all of jpeg to iolayer functions, removed older functions an…
242 mm_log((1, "myfree(p %p)\n", p));
02d1d628 »
2001-03-25 Initial revision
243 free(p);
244 }
245
faa9b3e7 »
2001-08-13 Egads
246 void *
247 myrealloc(void *block, size_t size) {
248 void *result;
249
250 mm_log((1, "myrealloc(block %p, size %u)\n", block, size));
251 if ((result = realloc(block, size)) == NULL) {
252 mm_log((1, "myrealloc: out of memory\n"));
253 fprintf(stderr, "Out of memory.\n");
254 exit(3);
255 }
256 return result;
257 }
258
02d1d628 »
2001-03-25 Initial revision
259 #endif /* IMAGER_MALLOC_DEBUG */
260
261
262
263
8047cbb5 »
2001-12-28 Added memory pools to make clean up of temporary buffers simpler.
264 /* memory pool implementation */
265
266 void
267 i_mempool_init(i_mempool *mp) {
268 mp->alloc = 10;
269 mp->used = 0;
270 mp->p = mymalloc(sizeof(void*)*mp->alloc);
271 }
272
273 void
274 i_mempool_extend(i_mempool *mp) {
275 mp->p = myrealloc(mp->p, mp->alloc * 2);
276 mp->alloc *=2;
277 }
278
279 void *
280 i_mempool_alloc(i_mempool *mp, size_t size) {
281 if (mp->used == mp->alloc) i_mempool_extend(mp);
282 mp->p[mp->used] = mymalloc(size);
283 mp->used++;
284 return mp->p[mp->used-1];
285 }
286
287
288 void
289 i_mempool_destroy(i_mempool *mp) {
290 unsigned int i;
291 for(i=0; i<mp->used; i++) myfree(mp->p[i]);
292 myfree(mp->p);
293 }
294
02d1d628 »
2001-03-25 Initial revision
295
296
5473b91d »
2001-10-23 Cleaned up io.h, io.c which had functions not used any more, removed …
297 /* Should these really be here? */
02d1d628 »
2001-03-25 Initial revision
298
299 #undef min
300 #undef max
301
302 int
b33c08f8 »
2002-04-01 hide or rename any symbols that are likely to conflict with other
303 i_min(int a,int b) {
02d1d628 »
2001-03-25 Initial revision
304 if (a<b) return a; else return b;
305 }
306
307 int
b33c08f8 »
2002-04-01 hide or rename any symbols that are likely to conflict with other
308 i_max(int a,int b) {
02d1d628 »
2001-03-25 Initial revision
309 if (a>b) return a; else return b;
310 }
311
4f68b48f »
2002-04-18 support UTF with Freetype 1.x
312
313 struct utf8_size {
314 int mask, expect;
315 int size;
316 };
317
318 struct utf8_size utf8_sizes[] =
319 {
320 { 0x80, 0x00, 1 },
321 { 0xE0, 0xC0, 2 },
322 { 0xF0, 0xE0, 3 },
323 { 0xF8, 0xF0, 4 },
324 };
325
326 /*
327 =item utf8_advance(char **p, int *len)
328
329 Retreive a UTF8 character from the stream.
330
331 Modifies *p and *len to indicate the consumed characters.
332
333 This doesn't support the extended UTF8 encoding used by later versions
334 of Perl.
335
336 =cut
337 */
338
339 unsigned long i_utf8_advance(char const **p, int *len) {
340 unsigned char c;
341 int i, ci, clen = 0;
342 unsigned char codes[3];
343 if (*len == 0)
344 return ~0UL;
345 c = *(*p)++; --*len;
346
347 for (i = 0; i < sizeof(utf8_sizes)/sizeof(*utf8_sizes); ++i) {
348 if ((c & utf8_sizes[i].mask) == utf8_sizes[i].expect) {
349 clen = utf8_sizes[i].size;
350 }
351 }
352 if (clen == 0 || *len < clen-1) {
353 --*p; ++*len;
354 return ~0UL;
355 }
356
357 /* check that each character is well formed */
358 i = 1;
359 ci = 0;
360 while (i < clen) {
361 if (((*p)[ci] & 0xC0) != 0x80) {
362 --*p; ++*len;
363 return ~0UL;
364 }
365 codes[ci] = (*p)[ci];
366 ++ci; ++i;
367 }
368 *p += clen-1; *len -= clen-1;
369 if (c & 0x80) {
370 if ((c & 0xE0) == 0xC0) {
371 return ((c & 0x1F) << 6) + (codes[0] & 0x3F);
372 }
373 else if ((c & 0xF0) == 0xE0) {
374 return ((c & 0x0F) << 12) | ((codes[0] & 0x3F) << 6) | (codes[1] & 0x3f);
375 }
376 else if ((c & 0xF8) == 0xF0) {
377 return ((c & 0x07) << 18) | ((codes[0] & 0x3F) << 12)
378 | ((codes[1] & 0x3F) << 6) | (codes[2] & 0x3F);
379 }
380 else {
381 *p -= clen; *len += clen;
382 return ~0UL;
383 }
384 }
385 else {
386 return c;
387 }
388 }
389
Something went wrong with that request. Please try again.