ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bmalloc.c
Revision: 2.4
Committed: Tue Feb 25 02:47:21 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.3: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# User Rev Content
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.4 #include "copyright.h"
15 greg 2.3
16     #include <stdlib.h>
17 greg 1.2
18 greg 1.1 #ifndef MBLKSIZ
19     #define MBLKSIZ 16376 /* size of memory allocation block */
20     #endif
21 greg 1.3 #define WASTEFRAC 12 /* don't waste more than a fraction */
22 greg 2.3 #ifndef ALIGNT
23     #define ALIGNT double /* type for alignment */
24 greg 1.1 #endif
25 greg 2.3 #define BYTES_WORD sizeof(ALIGNT)
26 greg 1.1
27 greg 1.3 static char *bposition = NULL;
28 greg 2.3 static unsigned int nremain = 0;
29 greg 1.3
30    
31 greg 1.1 char *
32 greg 2.2 bmalloc(n) /* allocate a block of n bytes */
33 greg 2.3 register unsigned int n;
34 greg 1.1 {
35 greg 1.3 if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC))
36 greg 1.2 return(malloc(n)); /* too big */
37 greg 1.1
38     n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align */
39    
40 greg 2.3 if (n > nremain && (bposition = (char *)malloc(nremain = MBLKSIZ)) == NULL) {
41 greg 2.2 nremain = 0;
42     return(NULL);
43 greg 1.1 }
44 greg 1.3 bposition += n;
45     nremain -= n;
46     return(bposition - n);
47 greg 1.1 }
48    
49    
50 greg 2.3 void
51 greg 1.1 bfree(p, n) /* free random memory */
52 greg 1.3 register char *p;
53 greg 2.3 register unsigned int n;
54 greg 1.1 {
55 greg 2.3 register unsigned int bsiz;
56 greg 1.3 /* check alignment */
57 greg 2.3 bsiz = BYTES_WORD - ((unsigned int)p&(BYTES_WORD-1));
58 greg 1.3 if (bsiz < BYTES_WORD) {
59     p += bsiz;
60     n -= bsiz;
61     }
62     if (p + n == bposition) { /* just allocated? */
63     bposition = p;
64     nremain += n;
65     return;
66     }
67     if (n > nremain) { /* better than what we've got? */
68     bposition = p;
69     nremain = n;
70     return;
71     }
72     /* just throw it away, then */
73 greg 1.1 }