13 |
|
* bmalloc() doesn't keep track of free lists -- it's usually |
14 |
|
* just a buffered call to sbrk(2). However, bmalloc will |
15 |
|
* call mscrounge() if sbrk fails. |
16 |
< |
* mscrounge() returns whatever memory it can find to satisfy the |
16 |
> |
* mscrounge() returns whatever free memory it can find to satisfy the |
17 |
|
* request along with the number of bytes (modified). |
18 |
|
* |
19 |
|
* Greg Ward Lawrence Berkeley Laboratory |
20 |
|
*/ |
21 |
|
|
22 |
+ |
#include <errno.h> |
23 |
+ |
|
24 |
+ |
#ifdef MSTATS |
25 |
+ |
#include <stdio.h> |
26 |
+ |
static unsigned b_nalloced = 0; |
27 |
+ |
static unsigned b_nfreed = 0; |
28 |
+ |
static unsigned b_nscrounged = 0; |
29 |
+ |
static unsigned m_nalloced = 0; |
30 |
+ |
static unsigned m_nfreed = 0; |
31 |
+ |
static unsigned m_nwasted = 0; |
32 |
+ |
#else |
33 |
|
#define NULL 0 |
34 |
+ |
#endif |
35 |
|
|
36 |
|
#ifndef ALIGN |
37 |
|
#define ALIGN int /* align type */ |
38 |
|
#endif |
39 |
|
#define BYTES_WORD sizeof(ALIGN) |
40 |
|
|
41 |
< |
#define MAXINCR (1<<18) /* largest sbrk(2) increment */ |
41 |
> |
#define MAXINCR (1<<16) /* largest sbrk(2) increment */ |
42 |
|
|
43 |
|
#ifdef NOVMEM |
44 |
|
#define getpagesize() BYTES_WORD |
55 |
|
|
56 |
|
static M_HEAD *free_list[NBUCKETS]; |
57 |
|
|
58 |
+ |
static char DUMMYLOC[BYTES_WORD]; |
59 |
|
|
60 |
+ |
|
61 |
|
char * |
62 |
|
mscrounge(np) /* search free lists to satisfy request */ |
63 |
|
register unsigned *np; |
72 |
|
*np = bsiz+sizeof(M_HEAD); |
73 |
|
p = (char *)free_list[bucket]; |
74 |
|
free_list[bucket] = free_list[bucket]->next; |
75 |
+ |
#ifdef MSTATS |
76 |
+ |
b_nscrounged += *np; |
77 |
+ |
#endif |
78 |
|
return(p); |
79 |
|
} |
80 |
|
*np = 0; |
94 |
|
unsigned thisamnt; |
95 |
|
register char *p; |
96 |
|
|
97 |
+ |
#ifdef MSTATS |
98 |
+ |
b_nalloced += n; |
99 |
+ |
#endif |
100 |
|
if (pagesz == 0) { /* initialize */ |
101 |
|
pagesz = amnt = getpagesize(); |
102 |
|
nrem = (int)sbrk(0); /* page align break */ |
142 |
|
{ |
143 |
|
register int bucket; |
144 |
|
register unsigned bsiz; |
145 |
+ |
|
146 |
+ |
#ifdef MSTATS |
147 |
+ |
b_nfreed += n; |
148 |
+ |
#endif |
149 |
|
/* align pointer */ |
150 |
< |
bsiz = BYTES_WORD - ((unsigned)p&~(BYTES_WORD-1)); |
150 |
> |
bsiz = BYTES_WORD - ((unsigned)p&(BYTES_WORD-1)); |
151 |
|
if (bsiz < BYTES_WORD) { |
152 |
|
p += bsiz; |
153 |
|
n -= bsiz; |
168 |
|
malloc(n) /* allocate n bytes of memory */ |
169 |
|
unsigned n; |
170 |
|
{ |
171 |
+ |
extern int errno; |
172 |
|
register M_HEAD *mp; |
173 |
|
register int bucket; |
174 |
|
register unsigned bsiz; |
175 |
< |
|
175 |
> |
/* don't return NULL on 0 request */ |
176 |
|
if (n == 0) |
177 |
< |
return(NULL); |
177 |
> |
return(DUMMYLOC); |
178 |
|
/* find first bucket that fits */ |
179 |
< |
bucket = FIRSTBUCKET; |
180 |
< |
for (bsiz = 1<<FIRSTBUCKET; bsiz < n; bsiz <<= 1) |
181 |
< |
bucket++; |
179 |
> |
for (bucket = FIRSTBUCKET, bsiz = 1<<FIRSTBUCKET; |
180 |
> |
bucket < NBUCKETS; bucket++, bsiz <<= 1) |
181 |
> |
if (bsiz >= n) |
182 |
> |
break; |
183 |
> |
if (bucket >= NBUCKETS) { |
184 |
> |
errno = EINVAL; |
185 |
> |
return(NULL); |
186 |
> |
} |
187 |
> |
#ifdef MSTATS |
188 |
> |
m_nalloced += bsiz + sizeof(M_HEAD); |
189 |
> |
m_nwasted += bsiz + sizeof(M_HEAD) - n; |
190 |
> |
#endif |
191 |
|
if (free_list[bucket] == NULL) { /* need more core */ |
192 |
|
mp = (M_HEAD *)bmalloc(bsiz+sizeof(M_HEAD)); |
193 |
|
if (mp == NULL) |
203 |
|
|
204 |
|
char * |
205 |
|
realloc(op, n) /* reallocate memory using malloc() */ |
206 |
< |
char *op; |
206 |
> |
register char *op; |
207 |
|
unsigned n; |
208 |
|
{ |
209 |
< |
extern char *memcpy(); |
176 |
< |
register char *p; |
209 |
> |
char *p; |
210 |
|
register unsigned on; |
211 |
< |
|
212 |
< |
free(op); /* free it first */ |
213 |
< |
p = malloc(n); /* p==op if same bucket */ |
214 |
< |
if (p == NULL) |
215 |
< |
return(NULL); |
216 |
< |
if (p != op) /* different bucket, do copy */ |
211 |
> |
/* get old size */ |
212 |
> |
if (op != NULL && op != DUMMYLOC) |
213 |
> |
on = 1 << ((M_HEAD *)op-1)->bucket; |
214 |
> |
else |
215 |
> |
on = 0; |
216 |
> |
if (n <= on && (n > on>>1 || on == 1<<FIRSTBUCKET)) |
217 |
> |
return(op); /* same bucket */ |
218 |
> |
p = malloc(n); |
219 |
> |
if (p != NULL) |
220 |
|
#ifdef BSD |
221 |
|
bcopy(op, p, n>on ? on : n); |
222 |
|
#else |
223 |
|
(void)memcpy(p, op, n>on ? on : n); |
224 |
|
#endif |
225 |
+ |
free(op); |
226 |
|
return(p); |
227 |
|
} |
228 |
|
|
233 |
|
register M_HEAD *mp; |
234 |
|
register int bucket; |
235 |
|
|
236 |
< |
if (p == NULL) |
236 |
> |
if (p == NULL || p == DUMMYLOC) |
237 |
|
return; |
238 |
|
mp = (M_HEAD *)p - 1; |
239 |
|
bucket = mp->bucket; |
240 |
|
mp->next = free_list[bucket]; |
241 |
|
free_list[bucket] = mp; |
242 |
+ |
#ifdef MSTATS |
243 |
+ |
m_nfreed += (1 << bucket) + sizeof(M_HEAD); |
244 |
+ |
#endif |
245 |
|
} |
246 |
|
|
247 |
|
|
251 |
|
int |
252 |
|
getpagesize() /* use SYSV var structure to get page size */ |
253 |
|
{ |
214 |
– |
static int pagesz = 0; |
254 |
|
struct var v; |
255 |
|
|
256 |
< |
if (pagesz == 0) { |
257 |
< |
uvar(&v); |
219 |
< |
pagesz = 1 << v.v_pageshift; |
220 |
< |
} |
221 |
< |
return(pagesz); |
256 |
> |
uvar(&v); |
257 |
> |
return(1 << v.v_pageshift); |
258 |
|
} |
259 |
|
#endif |
260 |
+ |
#endif |
261 |
+ |
|
262 |
+ |
|
263 |
+ |
#ifdef MSTATS |
264 |
+ |
printmemstats(fp) /* print memory statistics to stream */ |
265 |
+ |
FILE *fp; |
266 |
+ |
{ |
267 |
+ |
register int i; |
268 |
+ |
int n; |
269 |
+ |
register M_HEAD *mp; |
270 |
+ |
unsigned int total = 0; |
271 |
+ |
|
272 |
+ |
fprintf(fp, "Memory statistics:\n"); |
273 |
+ |
fprintf(fp, "\tbmalloc: %d bytes allocated\n", b_nalloced); |
274 |
+ |
fprintf(fp, "\tbmalloc: %d bytes freed\n", b_nfreed); |
275 |
+ |
fprintf(fp, "\tbmalloc: %d bytes scrounged\n", b_nscrounged); |
276 |
+ |
fprintf(fp, "\tmalloc: %d bytes allocated\n", m_nalloced); |
277 |
+ |
fprintf(fp, "\tmalloc: %d bytes wasted (%.1f%%)\n", m_nwasted, |
278 |
+ |
100.0*m_nwasted/m_nalloced); |
279 |
+ |
fprintf(fp, "\tmalloc: %d bytes freed\n", m_nfreed); |
280 |
+ |
for (i = NBUCKETS-1; i >= FIRSTBUCKET; i--) { |
281 |
+ |
n = 0; |
282 |
+ |
for (mp = free_list[i]; mp != NULL; mp = mp->next) |
283 |
+ |
n++; |
284 |
+ |
if (n) { |
285 |
+ |
fprintf(fp, "\t%d * %u\n", n, 1<<i); |
286 |
+ |
total += n * ((1<<i) + sizeof(M_HEAD)); |
287 |
+ |
} |
288 |
+ |
} |
289 |
+ |
fprintf(fp, "\t %u total bytes in free list\n", total); |
290 |
+ |
} |
291 |
|
#endif |