ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bmalloc.c
(Generate patch)

Comparing ray/src/common/bmalloc.c (file contents):
Revision 1.2 by greg, Tue Sep 25 19:37:15 1990 UTC vs.
Revision 2.2 by greg, Tue Mar 10 15:32:00 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
5   #endif
6  
7   /*
8 < * Basic memory allocation w/o overhead.
9 < * Calls malloc(3) for big requests.
10 < *
8 > * 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   */
14  
15   #define  NULL           0
# Line 15 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17   #ifndef  MBLKSIZ
18   #define  MBLKSIZ        16376           /* size of memory allocation block */
19   #endif
20 < #define  WASTEFRAC      4               /* don't waste more than this */
20 > #define  WASTEFRAC      12              /* don't waste more than a fraction */
21   #ifndef  ALIGN
22   #define  ALIGN          int             /* type for alignment */
23   #endif
24   #define  BYTES_WORD     sizeof(ALIGN)
25  
26 + extern char  *malloc();
27  
28 + static char  *bposition = NULL;
29 + static unsigned  nremain = 0;
30 +
31 +
32   char *
33 < bmalloc(n)              /* allocate a block of n bytes, no refunds */
33 > bmalloc(n)              /* allocate a block of n bytes */
34   register unsigned  n;
35   {
36 <        extern char  *malloc();
30 <        static char  *bpos = NULL;
31 <        static unsigned  nrem = 0;
32 <
33 <        if (n > nrem && (n > MBLKSIZ || nrem > MBLKSIZ/WASTEFRAC))
36 >        if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC))
37                  return(malloc(n));                      /* too big */
38  
39          n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1);         /* word align */
40  
41 <        if (n > nrem) {
42 <                if ((bpos = malloc((unsigned)MBLKSIZ)) == NULL) {
43 <                        nrem = 0;
41 <                        return(NULL);
42 <                }
43 <                nrem = MBLKSIZ;
41 >        if (n > nremain && (bposition = malloc(nremain = MBLKSIZ)) == NULL) {
42 >                nremain = 0;
43 >                return(NULL);
44          }
45 <        bpos += n;
46 <        nrem -= n;
47 <        return(bpos - n);
45 >        bposition += n;
46 >        nremain -= n;
47 >        return(bposition - n);
48   }
49  
50  
51   bfree(p, n)                     /* free random memory */
52 < char    *p;
53 < unsigned        n;
52 > register char   *p;
53 > register unsigned       n;
54   {
55 <        /* not implemented */
55 >        register unsigned       bsiz;
56 >                                        /* check alignment */
57 >        bsiz = BYTES_WORD - ((unsigned)p&(BYTES_WORD-1));
58 >        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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines