-#include "io.h"
+#include "imio.h"
#include <stdlib.h>
#ifndef _MSC_VER
#include <unistd.h>
static
void
-malloc_init() {
+malloc_init(void) {
int i;
for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
malloc_need_init = 0;
void
-malloc_state() {
+malloc_state(void) {
int i, total = 0;
+ i_clear_error();
mm_log((0,"malloc_state()\n"));
bndcheck_all();
for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
return buf;
}
-
-
-
-
-
-
void*
myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
char *buf;
exit(3);
}
- if ( (buf = realloc(ptr-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
+ if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
exit(3);
}
return buf;
}
-
-
-
static
void
bndcheck(int idx) {
malloc_pointers[i].ptr = NULL;
match++;
}
+
+ mm_log((1, "myfree_file_line: freeing address %p (real %p)\n", pp, pp-UNDRRNVAL));
if (match != 1) {
mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
+ fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
+ exit(255);
}
- mm_log((1, "myfree_file_line: freeing address %p\n", pp-UNDRRNVAL));
free(pp-UNDRRNVAL);
}
mymalloc(int size) {
void *buf;
+ if (size < 0) {
+ fprintf(stderr, "Attempt to allocate size %d\n", size);
+ exit(3);
+ }
+
if ( (buf = malloc(size)) == NULL ) {
mm_log((1, "mymalloc: unable to malloc %d\n", size));
fprintf(stderr,"Unable to malloc %d.\n", size); exit(3);
+/* memory pool implementation */
+
+void
+i_mempool_init(i_mempool *mp) {
+ mp->alloc = 10;
+ mp->used = 0;
+ mp->p = mymalloc(sizeof(void*)*mp->alloc);
+}
+
+void
+i_mempool_extend(i_mempool *mp) {
+ mp->p = myrealloc(mp->p, mp->alloc * 2);
+ mp->alloc *=2;
+}
+
+void *
+i_mempool_alloc(i_mempool *mp, size_t size) {
+ if (mp->used == mp->alloc) i_mempool_extend(mp);
+ mp->p[mp->used] = mymalloc(size);
+ mp->used++;
+ return mp->p[mp->used-1];
+}
+
+
+void
+i_mempool_destroy(i_mempool *mp) {
+ unsigned int i;
+ for(i=0; i<mp->used; i++) myfree(mp->p[i]);
+ myfree(mp->p);
+}
+
/* Should these really be here? */
#undef max
int
-min(int a,int b) {
+i_min(int a,int b) {
if (a<b) return a; else return b;
}
int
-max(int a,int b) {
+i_max(int a,int b) {
if (a>b) return a; else return b;
}
+
+struct utf8_size {
+ int mask, expect;
+ int size;
+};
+
+struct utf8_size utf8_sizes[] =
+{
+ { 0x80, 0x00, 1 },
+ { 0xE0, 0xC0, 2 },
+ { 0xF0, 0xE0, 3 },
+ { 0xF8, 0xF0, 4 },
+};
+
+/*
+=item utf8_advance(char **p, int *len)
+
+Retreive a UTF8 character from the stream.
+
+Modifies *p and *len to indicate the consumed characters.
+
+This doesn't support the extended UTF8 encoding used by later versions
+of Perl.
+
+=cut
+*/
+
+unsigned long i_utf8_advance(char const **p, int *len) {
+ unsigned char c;
+ int i, ci, clen = 0;
+ unsigned char codes[3];
+ if (*len == 0)
+ return ~0UL;
+ c = *(*p)++; --*len;
+
+ for (i = 0; i < sizeof(utf8_sizes)/sizeof(*utf8_sizes); ++i) {
+ if ((c & utf8_sizes[i].mask) == utf8_sizes[i].expect) {
+ clen = utf8_sizes[i].size;
+ }
+ }
+ if (clen == 0 || *len < clen-1) {
+ --*p; ++*len;
+ return ~0UL;
+ }
+
+ /* check that each character is well formed */
+ i = 1;
+ ci = 0;
+ while (i < clen) {
+ if (((*p)[ci] & 0xC0) != 0x80) {
+ --*p; ++*len;
+ return ~0UL;
+ }
+ codes[ci] = (*p)[ci];
+ ++ci; ++i;
+ }
+ *p += clen-1; *len -= clen-1;
+ if (c & 0x80) {
+ if ((c & 0xE0) == 0xC0) {
+ return ((c & 0x1F) << 6) + (codes[0] & 0x3F);
+ }
+ else if ((c & 0xF0) == 0xE0) {
+ return ((c & 0x0F) << 12) | ((codes[0] & 0x3F) << 6) | (codes[1] & 0x3f);
+ }
+ else if ((c & 0xF8) == 0xF0) {
+ return ((c & 0x07) << 18) | ((codes[0] & 0x3F) << 12)
+ | ((codes[1] & 0x3F) << 6) | (codes[2] & 0x3F);
+ }
+ else {
+ *p -= clen; *len += clen;
+ return ~0UL;
+ }
+ }
+ else {
+ return c;
+ }
+}
+