9 /* FIXME: make allocation dynamic */
12 #ifdef IMAGER_DEBUG_MALLOC
23 static int malloc_need_init = 1;
31 malloc_entry malloc_pointers[MAXMAL];
36 /* Utility functions */
43 for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
54 if (malloc_pointers[i].ptr == p)
60 /* Takes a pointer to real start of array,
61 * sets the entries in the table, returns
62 * the offset corrected pointer */
66 set_entry(int i, char *buf, size_t size, char *file, int line) {
67 memset( buf, PADBYTE, UNDRRNVAL );
68 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
70 malloc_pointers[i].ptr = buf;
71 malloc_pointers[i].size = size;
72 sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
81 mm_log((0,"malloc_state()\n"));
83 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
84 mm_log((0,"%d: %d (0x%x) : %s\n", i, malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].comm));
85 total += malloc_pointers[i].size;
87 if (total == 0) mm_log((0,"No memory currently used!\n"))
88 else mm_log((0,"total: %d\n",total));
94 mymalloc_file_line(size_t size, char* file, int line) {
97 if (malloc_need_init) malloc_init();
99 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
101 if ( (i = find_ptr(NULL)) < 0 ) {
102 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
106 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
107 mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line));
111 buf = set_entry(i, buf, size, file, line);
112 mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
117 (mymalloc)(int size) {
118 return mymalloc_file_line(size, "unknown", 0);
122 myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
126 if (malloc_need_init) malloc_init();
127 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
130 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
131 return mymalloc_file_line(newsize, file, line);
135 mm_log((1, "newsize = 0, sending request to free\n"));
136 myfree_file_line(ptr, file, line);
140 if ( (i = find_ptr(ptr)) == -1) {
141 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
145 if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
146 mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
150 buf = set_entry(i, buf, newsize, file, line);
151 mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
156 (myrealloc)(void *ptr, size_t newsize) {
157 return myrealloc_file_line(ptr, newsize, "unknown", 0);
164 size_t s = malloc_pointers[idx].size;
165 unsigned char *pp = malloc_pointers[idx].ptr;
167 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
171 for(i=0;i<UNDRRNVAL;i++)
172 if (pp[-(1+i)] != PADBYTE)
173 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
175 for(i=0;i<OVERRNVAL;i++)
176 if (pp[s+i] != PADBYTE)
177 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
183 mm_log((1, "bndcheck_all()\n"));
184 for(idx=0; idx<MAXMAL; idx++)
185 if (malloc_pointers[idx].ptr)
190 myfree_file_line(void *p, char *file, int line) {
198 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
199 mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
201 malloc_pointers[i].ptr = NULL;
205 mm_log((1, "myfree_file_line: freeing address %p (real %p)\n", pp, pp-UNDRRNVAL));
208 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
209 fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
218 (myfree)(void *block) {
219 myfree_file_line(block, "unknown", 0);
224 #define malloc_comm(a,b) (mymalloc(a))
235 fprintf(stderr, "Attempt to allocate size %d\n", size);
239 if ( (buf = malloc(size)) == NULL ) {
240 mm_log((1, "mymalloc: unable to malloc %d\n", size));
241 fprintf(stderr,"Unable to malloc %d.\n", size); exit(3);
243 mm_log((1, "mymalloc(size %d) -> %p\n", size, buf));
248 mymalloc_file_line(size_t size, char *file, int line) {
249 return mymalloc(size);
254 mm_log((1, "myfree(p %p)\n", p));
259 myfree_file_line(void *p, char *file, int line) {
264 myrealloc(void *block, size_t size) {
267 mm_log((1, "myrealloc(block %p, size %u)\n", block, size));
268 if ((result = realloc(block, size)) == NULL) {
269 mm_log((1, "myrealloc: out of memory\n"));
270 fprintf(stderr, "Out of memory.\n");
277 myrealloc_file_line(void *block, size_t newsize, char *file, int size) {
278 return myrealloc(block, newsize);
281 #endif /* IMAGER_MALLOC_DEBUG */
286 /* memory pool implementation */
289 i_mempool_init(i_mempool *mp) {
292 mp->p = mymalloc(sizeof(void*)*mp->alloc);
296 i_mempool_extend(i_mempool *mp) {
297 mp->p = myrealloc(mp->p, mp->alloc * 2);
302 i_mempool_alloc(i_mempool *mp, size_t size) {
303 if (mp->used == mp->alloc) i_mempool_extend(mp);
304 mp->p[mp->used] = mymalloc(size);
306 return mp->p[mp->used-1];
311 i_mempool_destroy(i_mempool *mp) {
313 for(i=0; i<mp->used; i++) myfree(mp->p[i]);
319 /* Should these really be here? */
325 i_minx(i_img_dim a, i_img_dim b) {
326 if (a<b) return a; else return b;
330 i_maxx(i_img_dim a, i_img_dim b) {
331 if (a>b) return a; else return b;
340 struct utf8_size utf8_sizes[] =
349 =item i_utf8_advance(char **p, size_t *len)
351 Retrieve a C<UTF-8> character from the stream.
353 Modifies *p and *len to indicate the consumed characters.
355 This doesn't support the extended C<UTF-8> encoding used by later
358 This doesn't check that the C<UTF-8> character is using the shortest
359 possible representation.
365 i_utf8_advance(char const **p, size_t *len) {
368 unsigned char codes[3];
373 for (i = 0; i < sizeof(utf8_sizes)/sizeof(*utf8_sizes); ++i) {
374 if ((c & utf8_sizes[i].mask) == utf8_sizes[i].expect) {
375 clen = utf8_sizes[i].size;
379 if (clen == 0 || *len < clen-1) {
384 /* check that each character is well formed */
388 if (((*p)[ci] & 0xC0) != 0x80) {
392 codes[ci] = (*p)[ci];
395 *p += clen-1; *len -= clen-1;
397 if ((c & 0xE0) == 0xC0) {
398 return ((c & 0x1F) << 6) + (codes[0] & 0x3F);
400 else if ((c & 0xF0) == 0xE0) {
401 return ((c & 0x0F) << 12) | ((codes[0] & 0x3F) << 6) | (codes[1] & 0x3f);
403 else if ((c & 0xF8) == 0xF0) {
404 return ((c & 0x07) << 18) | ((codes[0] & 0x3F) << 12)
405 | ((codes[1] & 0x3F) << 6) | (codes[2] & 0x3F);
408 *p -= clen; *len += clen;