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 |
+ |
#include <string.h> |
26 |
|
|
27 |
– |
extern int errno; |
27 |
|
|
29 |
– |
#ifndef BSD |
30 |
– |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
31 |
– |
#define bzero(d,n) (void)memset(d,0,n) |
32 |
– |
extern char *memcpy(), *memset(); |
33 |
– |
#endif |
34 |
– |
|
28 |
|
#ifdef MSTATS |
29 |
|
#include <stdio.h> |
30 |
|
static unsigned b_nsbrked = 0; |
39 |
|
#define NULL 0 |
40 |
|
#endif |
41 |
|
|
42 |
< |
#ifndef ALIGN |
43 |
< |
#define ALIGN int /* align type */ |
42 |
> |
#ifndef ALIGNT |
43 |
> |
#define ALIGNT int /* align type */ |
44 |
|
#endif |
45 |
< |
#define BYTES_WORD sizeof(ALIGN) |
45 |
> |
#define BYTES_WORD sizeof(ALIGNT) |
46 |
|
|
47 |
|
#ifndef MAXINCR |
48 |
|
#define MAXINCR (1<<16) /* largest sbrk(2) increment */ |
54 |
|
short magic; |
55 |
|
short bucket; |
56 |
|
} a; |
57 |
< |
ALIGN dummy; |
57 |
> |
ALIGNT dummy; |
58 |
|
} M_HEAD; |
59 |
|
|
60 |
|
#define MAGIC 0x1a2 /* magic number for allocated memory */ |
64 |
|
|
65 |
|
static M_HEAD *free_list[NBUCKETS]; |
66 |
|
|
67 |
< |
static ALIGN dummy_mem; |
67 |
> |
static ALIGNT dummy_mem; |
68 |
|
|
69 |
|
static char *memlim[2]; |
70 |
|
|
155 |
|
|
156 |
|
for ( ; ; ) { |
157 |
|
/* compact free lists */ |
158 |
< |
compactfree(); |
158 |
> |
while (compactfree()) |
159 |
> |
; |
160 |
|
/* find largest block */ |
161 |
|
tab = mtab(&cptab); tablen = mtablen(&cptab); |
162 |
|
big = tab; |
179 |
|
cptab.ptr = big->ptr; |
180 |
|
cptab.siz = big->siz; |
181 |
|
big->siz = 0; /* clear and copy */ |
182 |
< |
bcopy((char *)tab, (char *)(mtab(&cptab)+1), |
182 |
> |
memcpy((char *)(mtab(&cptab)+1), (char *)tab, |
183 |
|
tablen*sizeof(struct mblk)); |
184 |
< |
bzero((char *)(mtab(&cptab)+tablen+1), |
184 |
> |
memset((char *)(mtab(&cptab)+tablen+1), '\0', |
185 |
|
(mtablen(&cptab)-tablen-1)*sizeof(struct mblk)); |
186 |
|
} /* next round */ |
187 |
|
} |
247 |
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align rqst. */ |
248 |
|
|
249 |
|
if (n > nrem) { /* need more core */ |
250 |
+ |
tryagain: |
251 |
|
if (n > amnt) { /* big chunk */ |
252 |
|
thisamnt = (n+(pagesz-1))&~(pagesz-1); |
253 |
|
if (thisamnt <= MAXINCR) /* increase amnt */ |
256 |
|
thisamnt = amnt; |
257 |
|
p = sbrk(thisamnt); |
258 |
|
if ((int)p == -1) { /* uh-oh, ENOMEM */ |
259 |
< |
thisamnt = n; /* search free lists */ |
260 |
< |
p = mscrounge(&thisamnt); |
261 |
< |
if (p == NULL) /* we're really out */ |
259 |
> |
errno = 0; /* call cavalry */ |
260 |
> |
if (thisamnt >= n+pagesz) { |
261 |
> |
amnt = pagesz; /* minimize request */ |
262 |
> |
goto tryagain; |
263 |
> |
} |
264 |
> |
thisamnt = n; |
265 |
> |
p = mscrounge(&thisamnt); /* search free lists */ |
266 |
> |
if (p == NULL) { /* we're really out */ |
267 |
> |
errno = ENOMEM; |
268 |
|
return(NULL); |
269 |
+ |
} |
270 |
|
} |
271 |
|
#ifdef MSTATS |
272 |
|
else b_nsbrked += thisamnt; |
299 |
|
register int bucket; |
300 |
|
register unsigned bsiz; |
301 |
|
|
302 |
< |
if (n < 1<<FIRSTBUCKET) |
302 |
> |
if (n < 1<<FIRSTBUCKET || p == NULL) |
303 |
|
return; |
304 |
|
#ifdef MSTATS |
305 |
|
b_nfreed += n; |
381 |
|
if ((p = malloc(n)) == NULL) |
382 |
|
return(n<=on ? op : NULL); |
383 |
|
if (on) { |
384 |
< |
bcopy(op, p, n>on ? on : n); |
384 |
> |
memcpy(p, op, n>on ? on : n); |
385 |
|
free(op); |
386 |
|
} |
387 |
|
return(p); |