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.1 by greg, Tue Sep 25 18:56:04 1990 UTC vs.
Revision 2.4 by greg, Tue Feb 25 02:47:21 2003 UTC

# Line 1 | Line 1
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 + #include "copyright.h"
15 +
16 + #include <stdlib.h>
17 +
18   #ifndef  MBLKSIZ
19   #define  MBLKSIZ        16376           /* size of memory allocation block */
20   #endif
21 < #ifndef  ALIGN
22 < #define  ALIGN          int             /* type for alignment */
21 > #define  WASTEFRAC      12              /* don't waste more than a fraction */
22 > #ifndef  ALIGNT
23 > #define  ALIGNT         double          /* type for alignment */
24   #endif
25 < #define  BYTES_WORD     sizeof(ALIGN)
25 > #define  BYTES_WORD     sizeof(ALIGNT)
26  
27 + static char  *bposition = NULL;
28 + static unsigned int  nremain = 0;
29  
30 +
31   char *
32 < bmalloc(n)              /* allocate a block of n bytes, no refunds */
33 < register unsigned  n;
32 > bmalloc(n)              /* allocate a block of n bytes */
33 > register unsigned int  n;
34   {
35 <        static char  *bpos = NULL;
36 <        static unsigned  nrem = 0;
35 >        if (n > nremain && (n > MBLKSIZ || nremain > MBLKSIZ/WASTEFRAC))
36 >                return(malloc(n));                      /* too big */
37  
28        if (n > MBLKSIZ/2)                      /* too big for me */
29                return(malloc(n));
30
38          n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1);         /* word align */
39  
40 <        if (n > nrem) {
41 <                if ((bpos = malloc((unsigned)MBLKSIZ)) == NULL) {
42 <                        nrem = 0;
36 <                        return(NULL);
37 <                }
38 <                nrem = MBLKSIZ;
40 >        if (n > nremain && (bposition = (char *)malloc(nremain = MBLKSIZ)) == NULL) {
41 >                nremain = 0;
42 >                return(NULL);
43          }
44 <        bpos += n;
45 <        nrem -= n;
46 <        return(bpos - n);
44 >        bposition += n;
45 >        nremain -= n;
46 >        return(bposition - n);
47   }
48  
49  
50 + void
51   bfree(p, n)                     /* free random memory */
52 < char    *p;
53 < unsigned        n;
52 > register char   *p;
53 > register unsigned int   n;
54   {
55 <        /* not implemented */
55 >        register unsigned int   bsiz;
56 >                                        /* check alignment */
57 >        bsiz = BYTES_WORD - ((unsigned int)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