ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bmalloc.c
Revision: 1.3
Committed: Mon May 6 16:44:10 1991 UTC (32 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +40 -20 lines
Log Message:
improved bfree()

File Contents

# User Rev Content
1 greg 1.3 /* Copyright (c) 1991 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8 greg 1.3 * Bmalloc provides basic memory allocation without overhead (no free lists).
9     * Use only to take the load off of malloc for all those
10     * piddling little requests that you never expect to free.
11     * Bmalloc defers to malloc for big requests.
12     * Bfree should hand memory to bmalloc, but it usually fails here.
13 greg 1.1 */
14    
15 greg 1.2 #define NULL 0
16    
17 greg 1.1 #ifndef MBLKSIZ
18     #define MBLKSIZ 16376 /* size of memory allocation block */
19     #endif
20 greg 1.3 #define WASTEFRAC 12 /* don't waste more than a fraction */
21 greg 1.1 #ifndef ALIGN
22     #define ALIGN int /* type for alignment */
23     #endif
24     #define BYTES_WORD sizeof(ALIGN)
25    
26 greg 1.3 extern char *malloc();
27 greg 1.1
28 greg 1.3 static char *bposition = NULL;
29     static unsigned nremain = 0;
30    
31    
32 greg 1.1 char *
33     bmalloc(n) /* allocate a block of n bytes, no refunds */
34     register unsigned n;
35     {
36 greg 1.3 if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC))
37 greg 1.2 return(malloc(n)); /* too big */
38 greg 1.1
39     n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1); /* word align */
40    
41 greg 1.3 if (n > nremain) {
42     if ((bposition = malloc((unsigned)MBLKSIZ)) == NULL) {
43     nremain = 0;
44 greg 1.1 return(NULL);
45     }
46 greg 1.3 nremain = MBLKSIZ;
47 greg 1.1 }
48 greg 1.3 bposition += n;
49     nremain -= n;
50     return(bposition - n);
51 greg 1.1 }
52    
53    
54     bfree(p, n) /* free random memory */
55 greg 1.3 register char *p;
56     register unsigned n;
57 greg 1.1 {
58 greg 1.3 register unsigned bsiz;
59     /* check alignment */
60     bsiz = BYTES_WORD - ((unsigned)p&(BYTES_WORD-1));
61     if (bsiz < BYTES_WORD) {
62     p += bsiz;
63     n -= bsiz;
64     }
65     if (p + n == bposition) { /* just allocated? */
66     bposition = p;
67     nremain += n;
68     return;
69     }
70     if (n > nremain) { /* better than what we've got? */
71     bposition = p;
72     nremain = n;
73     return;
74     }
75     /* just throw it away, then */
76 greg 1.1 }