1 /* $NetBSD: malloc.c,v 1.10 2005/12/11 12:25:16 christos Exp $ */
2
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 */
8
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
13 */
14
15 #include <sys/cdefs.h>
16 __KERNEL_RCSID(0, "$NetBSD: malloc.c,v 1.10 2005/12/11 12:25:16 christos Exp $");
17
18 #include <stdio.h>
19 #include "malloc.h"
20 #include "debug.h"
21 #include "main.h"
22 #define CHUNKSIZE 4096*2
23
24 static char *hiwat, *highend;
25 int bytesmalloced=0;
26 int byteswasted = 0;
27
28 void
29 init_alloc()
30 {
31 #ifdef LINT
32 hiwat = 0;
33 highend = 0;
34 #else /* !LINT */
35 extern char *sbrk();
36
37 hiwat = (char *) sbrk(0);
38 hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
39 highend = hiwat;
40 #endif /* LINT */
41 }
42
43 void
44 HIWAT(s)
45 char *s;
46 {
47 IFDEBUG(M)
48 fprintf(stdout, "HIWAT %p %s\n", hiwat,s);
49 fflush(stdout);
50 ENDDEBUG
51 }
52
53 #define MIN(x,y) ((x<y)?x:y)
54
55 char *Malloc(x)
56 int x;
57 {
58 char *c;
59 extern char *sbrk();
60 static int firsttime=1;
61 int total = x;
62 int first_iter = 1;
63 char *returnvalue;
64
65 IFDEBUG(N)
66 fprintf(stdout, "Malloc 0x%x, %d, bytesmalloced %d\n",
67 total,total, bytesmalloced);
68 fflush(stdout);
69 ENDDEBUG
70 IFDEBUG(M)
71 fprintf(stdout, "Malloc 0x%x, %d, hiwat %p\n",
72 total,total, hiwat);
73 fflush(stdout);
74 ENDDEBUG
75 if(firsttime) {
76 hiwat = sbrk(0);
77 if(((unsigned)(hiwat) & 0x3)) {
78 bytesmalloced = 4 - (int) ((unsigned)(hiwat) & 0x3);
79 hiwat = sbrk( bytesmalloced );
80 } else
81 bytesmalloced = 0;
82 firsttime = 0;
83 highend = hiwat;
84 }
85 while( total ) {
86 x = MIN(CHUNKSIZE, total);
87 if(total != x) {
88 IFDEBUG(N)
89 fprintf(stdout, "BIG Malloc tot %d, x %d, left %d net %d\n",
90 total,x, total-x, bytesmalloced);
91 fflush(stdout);
92 ENDDEBUG
93 }
94 if ( (hiwat + x) > highend) {
95 c = sbrk(CHUNKSIZE);
96 IFDEBUG(M)
97 fprintf(stdout, "hiwat %p, x 0x%x, highend %p, c %p\n",
98 hiwat, x, highend, c);
99 fflush(stdout);
100 ENDDEBUG
101 if( c == (char *) -1 ) {
102 fprintf(stderr, "Ran out of memory!\n");
103 Exit(-1);
104 }
105 if(first_iter) {
106 returnvalue = c;
107 first_iter = 0;
108 }
109 bytesmalloced += CHUNKSIZE;
110 IFDEBUG(m)
111 if (highend != c) {
112 fprintf(OUT, "warning: %d wasted bytes!\n", highend - hiwat);
113 fprintf(OUT, " chunksize 0x%x, x 0x%x \n", CHUNKSIZE, x);
114 }
115 ENDDEBUG
116 highend = c + CHUNKSIZE;
117 hiwat = c;
118 }
119 c = hiwat;
120 if(first_iter) {
121 returnvalue = c;
122 first_iter = 0;
123 }
124 hiwat += x;
125 total -= x;
126 }
127 if((unsigned)hiwat & 0x3) {
128 byteswasted += (int)((unsigned)(hiwat) & 0x3);
129 hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
130 }
131 IFDEBUG(M)
132 fprintf(stdout, "Malloc = %p, bytesm 0x%x, wasted 0x%x, hiwat %p\n",
133 returnvalue, bytesmalloced, byteswasted, hiwat);
134 ENDDEBUG
135 IFDEBUG(N)
136 fprintf(stdout, "Malloc returns %p, sbrk(0) %p\n", returnvalue, sbrk(0));
137 fflush(stdout);
138 ENDDEBUG
139 return(returnvalue);
140 }
141
Cache object: be1e4b6abb61d87a2438b8748403531c
|