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 |
+ |
/* ==================================================================== |
23 |
+ |
* The Radiance Software License, Version 1.0 |
24 |
+ |
* |
25 |
+ |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
26 |
+ |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
27 |
+ |
* |
28 |
+ |
* Redistribution and use in source and binary forms, with or without |
29 |
+ |
* modification, are permitted provided that the following conditions |
30 |
+ |
* are met: |
31 |
+ |
* |
32 |
+ |
* 1. Redistributions of source code must retain the above copyright |
33 |
+ |
* notice, this list of conditions and the following disclaimer. |
34 |
+ |
* |
35 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
36 |
+ |
* notice, this list of conditions and the following disclaimer in |
37 |
+ |
* the documentation and/or other materials provided with the |
38 |
+ |
* distribution. |
39 |
+ |
* |
40 |
+ |
* 3. The end-user documentation included with the redistribution, |
41 |
+ |
* if any, must include the following acknowledgment: |
42 |
+ |
* "This product includes Radiance software |
43 |
+ |
* (http://radsite.lbl.gov/) |
44 |
+ |
* developed by the Lawrence Berkeley National Laboratory |
45 |
+ |
* (http://www.lbl.gov/)." |
46 |
+ |
* Alternately, this acknowledgment may appear in the software itself, |
47 |
+ |
* if and wherever such third-party acknowledgments normally appear. |
48 |
+ |
* |
49 |
+ |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
50 |
+ |
* and "The Regents of the University of California" must |
51 |
+ |
* not be used to endorse or promote products derived from this |
52 |
+ |
* software without prior written permission. For written |
53 |
+ |
* permission, please contact [email protected]. |
54 |
+ |
* |
55 |
+ |
* 5. Products derived from this software may not be called "Radiance", |
56 |
+ |
* nor may "Radiance" appear in their name, without prior written |
57 |
+ |
* permission of Lawrence Berkeley National Laboratory. |
58 |
+ |
* |
59 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
60 |
+ |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
61 |
+ |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
62 |
+ |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
63 |
+ |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
64 |
+ |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
65 |
+ |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
66 |
+ |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
67 |
+ |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
68 |
+ |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
69 |
+ |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
70 |
+ |
* SUCH DAMAGE. |
71 |
+ |
* ==================================================================== |
72 |
+ |
* |
73 |
+ |
* This software consists of voluntary contributions made by many |
74 |
+ |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
75 |
+ |
* information on Lawrence Berkeley National Laboratory, please see |
76 |
+ |
* <http://www.lbl.gov/>. |
77 |
+ |
*/ |
78 |
+ |
|
79 |
|
#include <errno.h> |
80 |
|
|
27 |
– |
extern int errno; |
28 |
– |
|
81 |
|
#ifndef BSD |
82 |
|
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
83 |
|
#define bzero(d,n) (void)memset(d,0,n) |
32 |
– |
extern char *memcpy(), *memset(); |
84 |
|
#endif |
85 |
|
|
86 |
|
#ifdef MSTATS |
97 |
|
#define NULL 0 |
98 |
|
#endif |
99 |
|
|
100 |
< |
#ifndef ALIGN |
101 |
< |
#define ALIGN int /* align type */ |
100 |
> |
#ifndef ALIGNT |
101 |
> |
#define ALIGNT int /* align type */ |
102 |
|
#endif |
103 |
< |
#define BYTES_WORD sizeof(ALIGN) |
103 |
> |
#define BYTES_WORD sizeof(ALIGNT) |
104 |
|
|
105 |
|
#ifndef MAXINCR |
106 |
|
#define MAXINCR (1<<16) /* largest sbrk(2) increment */ |
112 |
|
short magic; |
113 |
|
short bucket; |
114 |
|
} a; |
115 |
< |
ALIGN dummy; |
115 |
> |
ALIGNT dummy; |
116 |
|
} M_HEAD; |
117 |
|
|
118 |
|
#define MAGIC 0x1a2 /* magic number for allocated memory */ |
122 |
|
|
123 |
|
static M_HEAD *free_list[NBUCKETS]; |
124 |
|
|
125 |
< |
static ALIGN dummy_mem; |
125 |
> |
static ALIGNT dummy_mem; |
126 |
|
|
127 |
|
static char *memlim[2]; |
128 |
|
|
213 |
|
|
214 |
|
for ( ; ; ) { |
215 |
|
/* compact free lists */ |
216 |
< |
compactfree(); |
216 |
> |
while (compactfree()) |
217 |
> |
; |
218 |
|
/* find largest block */ |
219 |
|
tab = mtab(&cptab); tablen = mtablen(&cptab); |
220 |
|
big = tab; |
305 |
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align rqst. */ |
306 |
|
|
307 |
|
if (n > nrem) { /* need more core */ |
308 |
+ |
tryagain: |
309 |
|
if (n > amnt) { /* big chunk */ |
310 |
|
thisamnt = (n+(pagesz-1))&~(pagesz-1); |
311 |
|
if (thisamnt <= MAXINCR) /* increase amnt */ |
314 |
|
thisamnt = amnt; |
315 |
|
p = sbrk(thisamnt); |
316 |
|
if ((int)p == -1) { /* uh-oh, ENOMEM */ |
317 |
< |
thisamnt = n; /* search free lists */ |
318 |
< |
p = mscrounge(&thisamnt); |
319 |
< |
if (p == NULL) /* we're really out */ |
317 |
> |
errno = 0; /* call cavalry */ |
318 |
> |
if (thisamnt >= n+pagesz) { |
319 |
> |
amnt = pagesz; /* minimize request */ |
320 |
> |
goto tryagain; |
321 |
> |
} |
322 |
> |
thisamnt = n; |
323 |
> |
p = mscrounge(&thisamnt); /* search free lists */ |
324 |
> |
if (p == NULL) { /* we're really out */ |
325 |
> |
errno = ENOMEM; |
326 |
|
return(NULL); |
327 |
+ |
} |
328 |
|
} |
329 |
|
#ifdef MSTATS |
330 |
|
else b_nsbrked += thisamnt; |
357 |
|
register int bucket; |
358 |
|
register unsigned bsiz; |
359 |
|
|
360 |
< |
if (n < 1<<FIRSTBUCKET) |
360 |
> |
if (n < 1<<FIRSTBUCKET || p == NULL) |
361 |
|
return; |
362 |
|
#ifdef MSTATS |
363 |
|
b_nfreed += n; |