1 /* $NetBSD: malloc.c,v 1.11 2009/03/14 21:04:25 dsl Exp $ */
4 * This code is such a kludge that I don't want to put my name on it.
5 * It was a ridiculously fast hack and needs rewriting.
6 * However it does work...
11 * it might be brain-damaged but for the purposes of xebec
12 * it's a whole lot faster than the c library malloc
15 #include <sys/cdefs.h>
16 __KERNEL_RCSID(0, "$NetBSD: malloc.c,v 1.11 2009/03/14 21:04:25 dsl Exp $");
22 #define CHUNKSIZE 4096*2
24 static char *hiwat
, *highend
;
37 hiwat
= (char *) sbrk(0);
38 hiwat
= (char *)((unsigned)(hiwat
+ 3) & ~0x3);
47 fprintf(stdout
, "HIWAT %p %s\n", hiwat
,s
);
52 #define MIN(x,y) ((x<y)?x:y)
59 static int firsttime
=1;
65 fprintf(stdout
, "Malloc 0x%x, %d, bytesmalloced %d\n",
66 total
,total
, bytesmalloced
);
70 fprintf(stdout
, "Malloc 0x%x, %d, hiwat %p\n",
76 if(((unsigned)(hiwat
) & 0x3)) {
77 bytesmalloced
= 4 - (int) ((unsigned)(hiwat
) & 0x3);
78 hiwat
= sbrk( bytesmalloced
);
85 x
= MIN(CHUNKSIZE
, total
);
88 fprintf(stdout
, "BIG Malloc tot %d, x %d, left %d net %d\n",
89 total
,x
, total
-x
, bytesmalloced
);
93 if ( (hiwat
+ x
) > highend
) {
96 fprintf(stdout
, "hiwat %p, x 0x%x, highend %p, c %p\n",
97 hiwat
, x
, highend
, c
);
100 if( c
== (char *) -1 ) {
101 fprintf(stderr
, "Ran out of memory!\n");
108 bytesmalloced
+= CHUNKSIZE
;
111 fprintf(OUT
, "warning: %d wasted bytes!\n", highend
- hiwat
);
112 fprintf(OUT
, " chunksize 0x%x, x 0x%x \n", CHUNKSIZE
, x
);
115 highend
= c
+ CHUNKSIZE
;
126 if((unsigned)hiwat
& 0x3) {
127 byteswasted
+= (int)((unsigned)(hiwat
) & 0x3);
128 hiwat
= (char *)((unsigned)(hiwat
+ 3) & ~0x3);
131 fprintf(stdout
, "Malloc = %p, bytesm 0x%x, wasted 0x%x, hiwat %p\n",
132 returnvalue
, bytesmalloced
, byteswasted
, hiwat
);
135 fprintf(stdout
, "Malloc returns %p, sbrk(0) %p\n", returnvalue
, sbrk(0));