Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / netiso / xebec / malloc.c
blob6ce9726d15d716ae23859a489141fe1f0c1a3a47
1 /* $NetBSD: malloc.c,v 1.11 2009/03/14 21:04:25 dsl Exp $ */
3 /*
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...
7 */
9 /*
10 * a simple malloc
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 $");
18 #include <stdio.h>
19 #include "malloc.h"
20 #include "debug.h"
21 #include "main.h"
22 #define CHUNKSIZE 4096*2
24 static char *hiwat, *highend;
25 int bytesmalloced=0;
26 int byteswasted = 0;
28 void
29 init_alloc(void)
31 #ifdef LINT
32 hiwat = 0;
33 highend = 0;
34 #else /* !LINT */
35 extern char *sbrk();
37 hiwat = (char *) sbrk(0);
38 hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
39 highend = hiwat;
40 #endif /* LINT */
43 void
44 HIWAT(char *s)
46 IFDEBUG(M)
47 fprintf(stdout, "HIWAT %p %s\n", hiwat,s);
48 fflush(stdout);
49 ENDDEBUG
52 #define MIN(x,y) ((x<y)?x:y)
54 char *Malloc(x)
55 int x;
57 char *c;
58 extern char *sbrk();
59 static int firsttime=1;
60 int total = x;
61 int first_iter = 1;
62 char *returnvalue;
64 IFDEBUG(N)
65 fprintf(stdout, "Malloc 0x%x, %d, bytesmalloced %d\n",
66 total,total, bytesmalloced);
67 fflush(stdout);
68 ENDDEBUG
69 IFDEBUG(M)
70 fprintf(stdout, "Malloc 0x%x, %d, hiwat %p\n",
71 total,total, hiwat);
72 fflush(stdout);
73 ENDDEBUG
74 if(firsttime) {
75 hiwat = sbrk(0);
76 if(((unsigned)(hiwat) & 0x3)) {
77 bytesmalloced = 4 - (int) ((unsigned)(hiwat) & 0x3);
78 hiwat = sbrk( bytesmalloced );
79 } else
80 bytesmalloced = 0;
81 firsttime = 0;
82 highend = hiwat;
84 while( total ) {
85 x = MIN(CHUNKSIZE, total);
86 if(total != x) {
87 IFDEBUG(N)
88 fprintf(stdout, "BIG Malloc tot %d, x %d, left %d net %d\n",
89 total,x, total-x, bytesmalloced);
90 fflush(stdout);
91 ENDDEBUG
93 if ( (hiwat + x) > highend) {
94 c = sbrk(CHUNKSIZE);
95 IFDEBUG(M)
96 fprintf(stdout, "hiwat %p, x 0x%x, highend %p, c %p\n",
97 hiwat, x, highend, c);
98 fflush(stdout);
99 ENDDEBUG
100 if( c == (char *) -1 ) {
101 fprintf(stderr, "Ran out of memory!\n");
102 Exit(-1);
104 if(first_iter) {
105 returnvalue = c;
106 first_iter = 0;
108 bytesmalloced += CHUNKSIZE;
109 IFDEBUG(m)
110 if (highend != c) {
111 fprintf(OUT, "warning: %d wasted bytes!\n", highend - hiwat);
112 fprintf(OUT, " chunksize 0x%x, x 0x%x \n", CHUNKSIZE, x);
114 ENDDEBUG
115 highend = c + CHUNKSIZE;
116 hiwat = c;
118 c = hiwat;
119 if(first_iter) {
120 returnvalue = c;
121 first_iter = 0;
123 hiwat += x;
124 total -= x;
126 if((unsigned)hiwat & 0x3) {
127 byteswasted += (int)((unsigned)(hiwat) & 0x3);
128 hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
130 IFDEBUG(M)
131 fprintf(stdout, "Malloc = %p, bytesm 0x%x, wasted 0x%x, hiwat %p\n",
132 returnvalue, bytesmalloced, byteswasted, hiwat);
133 ENDDEBUG
134 IFDEBUG(N)
135 fprintf(stdout, "Malloc returns %p, sbrk(0) %p\n", returnvalue, sbrk(0));
136 fflush(stdout);
137 ENDDEBUG
138 return(returnvalue);