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.10 by greg, Tue Nov 13 14:39:05 1990 UTC vs.
Revision 1.13 by greg, Wed Jul 17 11:35:47 1991 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";
# Line 41 | Line 41 | static unsigned        m_nwasted = 0;
41   #define  MAXINCR        (1<<16)                 /* largest sbrk(2) increment */
42  
43   #ifdef  NOVMEM
44 < #define  getpagesize()  BYTES_WORD
44 > #define  getpagesize()  1024
45   #endif
46                                          /* malloc free lists */
47   typedef union m_head {
48          union m_head    *next;
49 <        int             bucket;
49 >        struct {
50 >                short           magic;
51 >                short           bucket;
52 >        }       a;
53          ALIGN           dummy;
54   } M_HEAD;
55  
56 + #define MAGIC           0x1a2           /* magic number for allocated memory */
57 +
58   #define FIRSTBUCKET     3
59   #define NBUCKETS        30
60  
61   static M_HEAD   *free_list[NBUCKETS];
62  
63 < static char     DUMMYLOC[BYTES_WORD];
63 > static ALIGN    dummy_mem;
64  
65 + #define DUMMYLOC        ((char *)&dummy_mem)
66  
67 +
68   char *
69   mscrounge(np)           /* search free lists to satisfy request */
70   register unsigned       *np;
# Line 94 | Line 101 | register unsigned  n;
101          unsigned  thisamnt;
102          register char   *p;
103  
97 #ifdef MSTATS
98        b_nalloced += n;
99 #endif
104          if (pagesz == 0) {                              /* initialize */
105                  pagesz = amnt = getpagesize();
106                  nrem = (int)sbrk(0);                    /* page align break */
# Line 132 | Line 136 | register unsigned  n;
136          p = bpos;
137          bpos += n;                                      /* advance */
138          nrem -= n;
139 + #ifdef MSTATS
140 +        b_nalloced += n;
141 + #endif
142          return(p);
143   }
144  
# Line 184 | Line 191 | unsigned       n;
191                  errno = EINVAL;
192                  return(NULL);
193          }
187 #ifdef MSTATS
188        m_nalloced += bsiz + sizeof(M_HEAD);
189        m_nwasted += bsiz + sizeof(M_HEAD) - n;
190 #endif
194          if (free_list[bucket] == NULL) {        /* need more core */
195                  mp = (M_HEAD *)bmalloc(bsiz+sizeof(M_HEAD));
196                  if (mp == NULL)
# Line 196 | Line 199 | unsigned       n;
199                  mp = free_list[bucket];
200                  free_list[bucket] = mp->next;
201          }
202 <        mp->bucket = bucket;                    /* tag block */
202 > #ifdef MSTATS
203 >        m_nalloced += bsiz + sizeof(M_HEAD);
204 >        m_nwasted += bsiz + sizeof(M_HEAD) - n;
205 > #endif
206 >        mp->a.magic = MAGIC;                    /* tag block */
207 >        mp->a.bucket = bucket;
208          return((char *)(mp+1));
209   }
210  
# Line 209 | Line 217 | unsigned       n;
217          char    *p;
218          register unsigned       on;
219                                          /* get old size */
220 <        if (op != NULL && op != DUMMYLOC)
221 <                on = 1 << ((M_HEAD *)op-1)->bucket;
220 >        if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC)
221 >                on = 1 << ((M_HEAD *)op-1)->a.bucket;
222          else
223                  on = 0;
224          if (n <= on && (n > on>>1 || on == 1<<FIRSTBUCKET))
225                  return(op);             /* same bucket */
226 <        p = malloc(n);
227 <        if (p != NULL)
226 >        if ((p = malloc(n)) == NULL)
227 >                return(NULL);
228 >        if (on) {
229   #ifdef  BSD
230                  bcopy(op, p, n>on ? on : n);
231   #else
232                  (void)memcpy(p, op, n>on ? on : n);
233   #endif
234 <        free(op);
234 >                free(op);
235 >        }
236          return(p);
237   }
238  
# Line 234 | Line 244 | char   *p;
244          register int    bucket;
245  
246          if (p == NULL || p == DUMMYLOC)
247 <                return;
247 >                return(1);
248          mp = (M_HEAD *)p - 1;
249 <        bucket = mp->bucket;
249 >        if (mp->a.magic != MAGIC)               /* sanity check */
250 >                return(0);
251 >        bucket = mp->a.bucket;
252          mp->next = free_list[bucket];
253          free_list[bucket] = mp;
254   #ifdef MSTATS
255          m_nfreed += (1 << bucket) + sizeof(M_HEAD);
256   #endif
257 +        return(1);
258   }
259  
260  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines