1 |
– |
/* Copyright (c) 1990 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 |
< |
* Simple memory allocation without overhead |
5 |
> |
* 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 |
|
* |
11 |
+ |
* External symbols declared in standard.h |
12 |
|
*/ |
13 |
|
|
14 |
+ |
/* ==================================================================== |
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 |
+ |
|
73 |
|
#ifndef MBLKSIZ |
74 |
|
#define MBLKSIZ 16376 /* size of memory allocation block */ |
75 |
|
#endif |
76 |
< |
#ifndef ALIGN |
77 |
< |
#define ALIGN int /* type for alignment */ |
76 |
> |
#define WASTEFRAC 12 /* don't waste more than a fraction */ |
77 |
> |
#ifndef ALIGNT |
78 |
> |
#define ALIGNT double /* type for alignment */ |
79 |
|
#endif |
80 |
< |
#define BYTES_WORD sizeof(ALIGN) |
80 |
> |
#define BYTES_WORD sizeof(ALIGNT) |
81 |
|
|
82 |
+ |
static char *bposition = NULL; |
83 |
+ |
static unsigned int nremain = 0; |
84 |
|
|
85 |
+ |
|
86 |
|
char * |
87 |
< |
bmalloc(n) /* allocate a block of n bytes, no refunds */ |
88 |
< |
register unsigned n; |
87 |
> |
bmalloc(n) /* allocate a block of n bytes */ |
88 |
> |
register unsigned int n; |
89 |
|
{ |
90 |
< |
static char *bpos = NULL; |
91 |
< |
static unsigned nrem = 0; |
90 |
> |
if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC)) |
91 |
> |
return(malloc(n)); /* too big */ |
92 |
|
|
28 |
– |
if (n > MBLKSIZ/2) /* too big for me */ |
29 |
– |
return(malloc(n)); |
30 |
– |
|
93 |
|
n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align */ |
94 |
|
|
95 |
< |
if (n > nrem) { |
96 |
< |
if ((bpos = malloc((unsigned)MBLKSIZ)) == NULL) { |
97 |
< |
nrem = 0; |
36 |
< |
return(NULL); |
37 |
< |
} |
38 |
< |
nrem = MBLKSIZ; |
95 |
> |
if (n > nremain && (bposition = (char *)malloc(nremain = MBLKSIZ)) == NULL) { |
96 |
> |
nremain = 0; |
97 |
> |
return(NULL); |
98 |
|
} |
99 |
< |
bpos += n; |
100 |
< |
nrem -= n; |
101 |
< |
return(bpos - n); |
99 |
> |
bposition += n; |
100 |
> |
nremain -= n; |
101 |
> |
return(bposition - n); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
+ |
void |
106 |
|
bfree(p, n) /* free random memory */ |
107 |
< |
char *p; |
108 |
< |
unsigned n; |
107 |
> |
register char *p; |
108 |
> |
register unsigned int n; |
109 |
|
{ |
110 |
< |
/* not implemented */ |
110 |
> |
register unsigned int bsiz; |
111 |
> |
/* check alignment */ |
112 |
> |
bsiz = BYTES_WORD - ((unsigned int)p&(BYTES_WORD-1)); |
113 |
> |
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 |
|
} |