/* $NetBSD: malloc.c,v 1.12 2009/03/18 10:22:44 cegger Exp $ */ /* * This code is such a kludge that I don't want to put my name on it. * It was a ridiculously fast hack and needs rewriting. * However it does work... */ /* * a simple malloc * it might be brain-damaged but for the purposes of xebec * it's a whole lot faster than the c library malloc */ #include __KERNEL_RCSID(0, "$NetBSD: malloc.c,v 1.12 2009/03/18 10:22:44 cegger Exp $"); #include #include "malloc.h" #include "debug.h" #include "main.h" #define CHUNKSIZE 4096*2 static char *hiwat, *highend; int bytesmalloced=0; int byteswasted = 0; void init_alloc(void) { #ifdef LINT hiwat = 0; highend = 0; #else /* !LINT */ extern char *sbrk(); hiwat = (char *) sbrk(0); hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3); highend = hiwat; #endif /* LINT */ } void HIWAT(char *s) { IFDEBUG(M) fprintf(stdout, "HIWAT %p %s\n", hiwat,s); fflush(stdout); ENDDEBUG } #define MIN(x,y) ((x highend) { c = sbrk(CHUNKSIZE); IFDEBUG(M) fprintf(stdout, "hiwat %p, x 0x%x, highend %p, c %p\n", hiwat, x, highend, c); fflush(stdout); ENDDEBUG if( c == (char *) -1 ) { fprintf(stderr, "Ran out of memory!\n"); Exit(-1); } if(first_iter) { returnvalue = c; first_iter = 0; } bytesmalloced += CHUNKSIZE; IFDEBUG(m) if (highend != c) { fprintf(OUT, "warning: %d wasted bytes!\n", highend - hiwat); fprintf(OUT, " chunksize 0x%x, x 0x%x \n", CHUNKSIZE, x); } ENDDEBUG highend = c + CHUNKSIZE; hiwat = c; } c = hiwat; if(first_iter) { returnvalue = c; first_iter = 0; } hiwat += x; total -= x; } if((unsigned)hiwat & 0x3) { byteswasted += (int)((unsigned)(hiwat) & 0x3); hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3); } IFDEBUG(M) fprintf(stdout, "Malloc = %p, bytesm 0x%x, wasted 0x%x, hiwat %p\n", returnvalue, bytesmalloced, byteswasted, hiwat); ENDDEBUG IFDEBUG(N) fprintf(stdout, "Malloc returns %p, sbrk(0) %p\n", returnvalue, sbrk(0)); fflush(stdout); ENDDEBUG return(returnvalue); }