1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.3 |
static const char RCSid[] = "$Id$"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
greg |
1.3 |
* Bmalloc provides basic memory allocation without overhead (no free lists). |
6 |
|
|
* Use only to take the load off of malloc for all those |
7 |
|
|
* piddling little requests that you never expect to free. |
8 |
|
|
* Bmalloc defers to malloc for big requests. |
9 |
|
|
* Bfree should hand memory to bmalloc, but it usually fails here. |
10 |
greg |
2.3 |
* |
11 |
|
|
* External symbols declared in standard.h |
12 |
greg |
1.1 |
*/ |
13 |
|
|
|
14 |
greg |
2.3 |
/* ==================================================================== |
15 |
|
|
* The Radiance Software License, Version 1.0 |
16 |
|
|
* |
17 |
|
|
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
18 |
|
|
* through Lawrence Berkeley National Laboratory. All rights reserved. |
19 |
|
|
* |
20 |
|
|
* Redistribution and use in source and binary forms, with or without |
21 |
|
|
* modification, are permitted provided that the following conditions |
22 |
|
|
* are met: |
23 |
|
|
* |
24 |
|
|
* 1. Redistributions of source code must retain the above copyright |
25 |
|
|
* notice, this list of conditions and the following disclaimer. |
26 |
|
|
* |
27 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
28 |
|
|
* notice, this list of conditions and the following disclaimer in |
29 |
|
|
* the documentation and/or other materials provided with the |
30 |
|
|
* distribution. |
31 |
|
|
* |
32 |
|
|
* 3. The end-user documentation included with the redistribution, |
33 |
|
|
* if any, must include the following acknowledgment: |
34 |
|
|
* "This product includes Radiance software |
35 |
|
|
* (http://radsite.lbl.gov/) |
36 |
|
|
* developed by the Lawrence Berkeley National Laboratory |
37 |
|
|
* (http://www.lbl.gov/)." |
38 |
|
|
* Alternately, this acknowledgment may appear in the software itself, |
39 |
|
|
* if and wherever such third-party acknowledgments normally appear. |
40 |
|
|
* |
41 |
|
|
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
42 |
|
|
* and "The Regents of the University of California" must |
43 |
|
|
* not be used to endorse or promote products derived from this |
44 |
|
|
* software without prior written permission. For written |
45 |
|
|
* permission, please contact [email protected]. |
46 |
|
|
* |
47 |
|
|
* 5. Products derived from this software may not be called "Radiance", |
48 |
|
|
* nor may "Radiance" appear in their name, without prior written |
49 |
|
|
* permission of Lawrence Berkeley National Laboratory. |
50 |
|
|
* |
51 |
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
52 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
53 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
54 |
|
|
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
55 |
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
56 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
57 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
58 |
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
59 |
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
60 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
61 |
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
62 |
|
|
* SUCH DAMAGE. |
63 |
|
|
* ==================================================================== |
64 |
|
|
* |
65 |
|
|
* This software consists of voluntary contributions made by many |
66 |
|
|
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
67 |
|
|
* information on Lawrence Berkeley National Laboratory, please see |
68 |
|
|
* <http://www.lbl.gov/>. |
69 |
|
|
*/ |
70 |
|
|
|
71 |
|
|
#include <stdlib.h> |
72 |
greg |
1.2 |
|
73 |
greg |
1.1 |
#ifndef MBLKSIZ |
74 |
|
|
#define MBLKSIZ 16376 /* size of memory allocation block */ |
75 |
|
|
#endif |
76 |
greg |
1.3 |
#define WASTEFRAC 12 /* don't waste more than a fraction */ |
77 |
greg |
2.3 |
#ifndef ALIGNT |
78 |
|
|
#define ALIGNT double /* type for alignment */ |
79 |
greg |
1.1 |
#endif |
80 |
greg |
2.3 |
#define BYTES_WORD sizeof(ALIGNT) |
81 |
greg |
1.1 |
|
82 |
greg |
1.3 |
static char *bposition = NULL; |
83 |
greg |
2.3 |
static unsigned int nremain = 0; |
84 |
greg |
1.3 |
|
85 |
|
|
|
86 |
greg |
1.1 |
char * |
87 |
greg |
2.2 |
bmalloc(n) /* allocate a block of n bytes */ |
88 |
greg |
2.3 |
register unsigned int n; |
89 |
greg |
1.1 |
{ |
90 |
greg |
1.3 |
if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC)) |
91 |
greg |
1.2 |
return(malloc(n)); /* too big */ |
92 |
greg |
1.1 |
|
93 |
|
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align */ |
94 |
|
|
|
95 |
greg |
2.3 |
if (n > nremain && (bposition = (char *)malloc(nremain = MBLKSIZ)) == NULL) { |
96 |
greg |
2.2 |
nremain = 0; |
97 |
|
|
return(NULL); |
98 |
greg |
1.1 |
} |
99 |
greg |
1.3 |
bposition += n; |
100 |
|
|
nremain -= n; |
101 |
|
|
return(bposition - n); |
102 |
greg |
1.1 |
} |
103 |
|
|
|
104 |
|
|
|
105 |
greg |
2.3 |
void |
106 |
greg |
1.1 |
bfree(p, n) /* free random memory */ |
107 |
greg |
1.3 |
register char *p; |
108 |
greg |
2.3 |
register unsigned int n; |
109 |
greg |
1.1 |
{ |
110 |
greg |
2.3 |
register unsigned int bsiz; |
111 |
greg |
1.3 |
/* check alignment */ |
112 |
greg |
2.3 |
bsiz = BYTES_WORD - ((unsigned int)p&(BYTES_WORD-1)); |
113 |
greg |
1.3 |
if (bsiz < BYTES_WORD) { |
114 |
|
|
p += bsiz; |
115 |
|
|
n -= bsiz; |
116 |
|
|
} |
117 |
|
|
if (p + n == bposition) { /* just allocated? */ |
118 |
|
|
bposition = p; |
119 |
|
|
nremain += n; |
120 |
|
|
return; |
121 |
|
|
} |
122 |
|
|
if (n > nremain) { /* better than what we've got? */ |
123 |
|
|
bposition = p; |
124 |
|
|
nremain = n; |
125 |
|
|
return; |
126 |
|
|
} |
127 |
|
|
/* just throw it away, then */ |
128 |
greg |
1.1 |
} |