1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Fast malloc for memory hogs and VM environments. |
6 |
|
* Performs a minimum of searching through free lists. |
19 |
|
* Greg Ward Lawrence Berkeley Laboratory |
20 |
|
*/ |
21 |
|
|
22 |
+ |
#include "copyright.h" |
23 |
+ |
|
24 |
|
#include <errno.h> |
25 |
|
|
26 |
< |
extern int errno; |
26 |
> |
#ifndef BSD |
27 |
> |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
28 |
> |
#define bzero(d,n) (void)memset(d,0,n) |
29 |
> |
#endif |
30 |
|
|
31 |
|
#ifdef MSTATS |
32 |
|
#include <stdio.h> |
42 |
|
#define NULL 0 |
43 |
|
#endif |
44 |
|
|
45 |
< |
#ifndef ALIGN |
46 |
< |
#define ALIGN int /* align type */ |
45 |
> |
#ifndef ALIGNT |
46 |
> |
#define ALIGNT int /* align type */ |
47 |
|
#endif |
48 |
< |
#define BYTES_WORD sizeof(ALIGN) |
48 |
> |
#define BYTES_WORD sizeof(ALIGNT) |
49 |
|
|
50 |
|
#ifndef MAXINCR |
51 |
|
#define MAXINCR (1<<16) /* largest sbrk(2) increment */ |
57 |
|
short magic; |
58 |
|
short bucket; |
59 |
|
} a; |
60 |
< |
ALIGN dummy; |
60 |
> |
ALIGNT dummy; |
61 |
|
} M_HEAD; |
62 |
|
|
63 |
|
#define MAGIC 0x1a2 /* magic number for allocated memory */ |
67 |
|
|
68 |
|
static M_HEAD *free_list[NBUCKETS]; |
69 |
|
|
70 |
< |
static ALIGN dummy_mem; |
70 |
> |
static ALIGNT dummy_mem; |
71 |
|
|
72 |
|
static char *memlim[2]; |
73 |
|
|
158 |
|
|
159 |
|
for ( ; ; ) { |
160 |
|
/* compact free lists */ |
161 |
< |
compactfree(); |
161 |
> |
while (compactfree()) |
162 |
> |
; |
163 |
|
/* find largest block */ |
164 |
|
tab = mtab(&cptab); tablen = mtablen(&cptab); |
165 |
|
big = tab; |
172 |
|
big->siz = 0; /* remove from table */ |
173 |
|
return(big->ptr); /* return it */ |
174 |
|
} |
175 |
< |
if (mtablen(big) < tablen+1) { |
175 |
> |
if (mtablen(big) <= tablen) { |
176 |
|
*np = 0; /* cannot grow table */ |
177 |
|
return(NULL); /* report failure */ |
178 |
|
} |
182 |
|
cptab.ptr = big->ptr; |
183 |
|
cptab.siz = big->siz; |
184 |
|
big->siz = 0; /* clear and copy */ |
182 |
– |
#ifdef BSD |
185 |
|
bcopy((char *)tab, (char *)(mtab(&cptab)+1), |
186 |
|
tablen*sizeof(struct mblk)); |
187 |
|
bzero((char *)(mtab(&cptab)+tablen+1), |
188 |
|
(mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); |
187 |
– |
#else |
188 |
– |
(void)memcpy((char *)(mtab(&cptab)+1), (char *)tab, |
189 |
– |
tablen*sizeof(struct mblk)); |
190 |
– |
memset((char *)(mtab(&cptab)+tablen+1), 0, |
191 |
– |
(mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); |
192 |
– |
#endif |
189 |
|
} /* next round */ |
190 |
|
} |
191 |
|
#endif /* MCOMP */ |
250 |
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align rqst. */ |
251 |
|
|
252 |
|
if (n > nrem) { /* need more core */ |
253 |
+ |
tryagain: |
254 |
|
if (n > amnt) { /* big chunk */ |
255 |
|
thisamnt = (n+(pagesz-1))&~(pagesz-1); |
256 |
|
if (thisamnt <= MAXINCR) /* increase amnt */ |
259 |
|
thisamnt = amnt; |
260 |
|
p = sbrk(thisamnt); |
261 |
|
if ((int)p == -1) { /* uh-oh, ENOMEM */ |
262 |
< |
thisamnt = n; /* search free lists */ |
263 |
< |
p = mscrounge(&thisamnt); |
264 |
< |
if (p == NULL) /* we're really out */ |
262 |
> |
errno = 0; /* call cavalry */ |
263 |
> |
if (thisamnt >= n+pagesz) { |
264 |
> |
amnt = pagesz; /* minimize request */ |
265 |
> |
goto tryagain; |
266 |
> |
} |
267 |
> |
thisamnt = n; |
268 |
> |
p = mscrounge(&thisamnt); /* search free lists */ |
269 |
> |
if (p == NULL) { /* we're really out */ |
270 |
> |
errno = ENOMEM; |
271 |
|
return(NULL); |
272 |
+ |
} |
273 |
|
} |
274 |
|
#ifdef MSTATS |
275 |
|
else b_nsbrked += thisamnt; |
302 |
|
register int bucket; |
303 |
|
register unsigned bsiz; |
304 |
|
|
305 |
< |
if (n < 1<<FIRSTBUCKET) |
305 |
> |
if (n < 1<<FIRSTBUCKET || p == NULL) |
306 |
|
return; |
307 |
|
#ifdef MSTATS |
308 |
|
b_nfreed += n; |
384 |
|
if ((p = malloc(n)) == NULL) |
385 |
|
return(n<=on ? op : NULL); |
386 |
|
if (on) { |
383 |
– |
#ifdef BSD |
387 |
|
bcopy(op, p, n>on ? on : n); |
385 |
– |
#else |
386 |
– |
(void)memcpy(p, op, n>on ? on : n); |
387 |
– |
#endif |
388 |
|
free(op); |
389 |
|
} |
390 |
|
return(p); |