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

Comparing ray/src/common/malloc.c (file contents):
Revision 1.2 by greg, Tue Sep 25 18:56:37 1990 UTC vs.
Revision 1.3 by greg, Wed Sep 26 06:23:10 1990 UTC

# Line 8 | Line 8 | static char SCCSid[] = "$SunId$ LBL";
8   * Fast malloc for memory hogs and VM environments.
9   * Performs a minimum of searching through free lists.
10   * bmalloc() doesn't keep track of free lists -- it's basically
11 < *      just a buffered call to sbrk().
12 < * bfree() puts memory from any source into malloc() free lists.
11 > *      just a buffered call to sbrk(2).  However, bmalloc will
12 > *      call mscrounge() if sbrk fails.
13 > * bfree() puts memory from any source into malloc free lists.
14   * malloc(), realloc() and free() use buckets
15   *      containing blocks in powers of two, similar to CIT routines.
16 + * mscrounge() returns whatever memory it can find to satisfy the
17 + *      request along with the number of bytes (modified).
18   *
19   *      Greg Ward       Lawrence Berkeley Laboratory
20   */
# Line 23 | Line 26 | static char SCCSid[] = "$SunId$ LBL";
26   #endif
27   #define  BYTES_WORD     sizeof(ALIGN)
28  
29 + #define  MAXINCR        (1<<18)                 /* largest sbrk(2) increment */
30 +
31   #ifdef  NOVMEM
32   #define  getpagesize()  BYTES_WORD
33   #endif
29
34                                          /* malloc free lists */
35   typedef union m_head {
36          union m_head    *next;
# Line 41 | Line 45 | static M_HEAD  *free_list[NBUCKETS];
45  
46  
47   char *
48 + mscrounge(np)           /* search free lists trying to satisfy request */
49 + register unsigned       *np;
50 + {
51 +        char    *p;
52 +        register int    bucket;
53 +        register unsigned       bsiz;
54 +
55 +        for (bucket = FIRSTBUCKET, bsiz = 1<<FIRSTBUCKET;
56 +                        bucket < NBUCKETS; bucket++, bsiz <<= 1)
57 +                if (free_list[bucket] != NULL && bsiz+sizeof(M_HEAD) >= *np) {
58 +                        *np = bsiz+sizeof(M_HEAD);
59 +                        p = (char *)free_list[bucket];
60 +                        free_list[bucket] = free_list[bucket]->next;
61 +                        return(p);
62 +                }
63 +        *np = 0;
64 +        return(NULL);
65 + }
66 +
67 +
68 + char *
69   bmalloc(n)              /* allocate a block of n bytes, sans header */
70   register unsigned  n;
71   {
72          extern char  *sbrk();
73 <        static int  pagesz = 0;
74 <        static int  amnt;
73 >        static unsigned  pagesz = 0;
74 >        static unsigned  amnt;
75          static char  *bpos;
76 <        static int  nrem;
76 >        static unsigned  nrem;
77 >        unsigned  thisamnt;
78          register char   *p;
79  
80          if (pagesz == 0) {                              /* initialize */
# Line 63 | Line 89 | register unsigned  n;
89          n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1);         /* word align rqst. */
90  
91          if (n > nrem) {                                 /* need more core */
92 <                if (n > amnt)                   /* increase amount */
93 <                        amnt = (n+(pagesz-1))&~(pagesz-1);
94 <                p = sbrk(amnt);
95 <                if ((int)p == -1)
96 <                        return(NULL);
97 <                if (p != bpos+nrem) {           /* someone called sbrk()! */
92 >                if (n > amnt) {                         /* big chunk */
93 >                        thisamnt = (n+(pagesz-1))&~(pagesz-1);
94 >                        if (thisamnt <= MAXINCR)        /* increase amnt */
95 >                                amnt = thisamnt;
96 >                } else
97 >                        thisamnt = amnt;
98 >                p = sbrk(thisamnt);
99 >                if ((int)p == -1) {                     /* uh-oh */
100 >                        thisamnt = n;                   /* search free lists */
101 >                        p = mscrounge(&thisamnt);
102 >                        if (p == NULL)                  /* we're really out */
103 >                                return(NULL);
104 >                }
105 >                if (p != bpos+nrem) {                   /* not an increment */
106                          bfree(bpos, nrem);
107                          bpos = p;
108 <                        nrem = amnt;
108 >                        nrem = thisamnt;
109                  } else
110 <                        nrem += amnt;
110 >                        nrem += thisamnt;
111          }
112          p = bpos;
113          bpos += n;                                      /* advance */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines