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.14 by greg, Mon Aug 26 10:12:24 1991 UTC vs.
Revision 2.12 by greg, Sat Feb 22 02:07:22 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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   * Fast malloc for memory hogs and VM environments.
6   * Performs a minimum of searching through free lists.
# Line 22 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
19   *      Greg Ward       Lawrence Berkeley Laboratory
20   */
21  
22 + /* ====================================================================
23 + * The Radiance Software License, Version 1.0
24 + *
25 + * Copyright (c) 1990 - 2002 The Regents of the University of California,
26 + * through Lawrence Berkeley National Laboratory.   All rights reserved.
27 + *
28 + * Redistribution and use in source and binary forms, with or without
29 + * modification, are permitted provided that the following conditions
30 + * are met:
31 + *
32 + * 1. Redistributions of source code must retain the above copyright
33 + *         notice, this list of conditions and the following disclaimer.
34 + *
35 + * 2. Redistributions in binary form must reproduce the above copyright
36 + *       notice, this list of conditions and the following disclaimer in
37 + *       the documentation and/or other materials provided with the
38 + *       distribution.
39 + *
40 + * 3. The end-user documentation included with the redistribution,
41 + *           if any, must include the following acknowledgment:
42 + *             "This product includes Radiance software
43 + *                 (http://radsite.lbl.gov/)
44 + *                 developed by the Lawrence Berkeley National Laboratory
45 + *               (http://www.lbl.gov/)."
46 + *       Alternately, this acknowledgment may appear in the software itself,
47 + *       if and wherever such third-party acknowledgments normally appear.
48 + *
49 + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
50 + *       and "The Regents of the University of California" must
51 + *       not be used to endorse or promote products derived from this
52 + *       software without prior written permission. For written
53 + *       permission, please contact [email protected].
54 + *
55 + * 5. Products derived from this software may not be called "Radiance",
56 + *       nor may "Radiance" appear in their name, without prior written
57 + *       permission of Lawrence Berkeley National Laboratory.
58 + *
59 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
60 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
62 + * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
63 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
64 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
65 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
66 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
67 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
68 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
69 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 + * SUCH DAMAGE.
71 + * ====================================================================
72 + *
73 + * This software consists of voluntary contributions made by many
74 + * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
75 + * information on Lawrence Berkeley National Laboratory, please see
76 + * <http://www.lbl.gov/>.
77 + */
78 +
79   #include  <errno.h>
80  
81 + #ifndef  BSD
82 + #define  bcopy(s,d,n)           (void)memcpy(d,s,n)
83 + #define  bzero(d,n)             (void)memset(d,0,n)
84 + #endif
85 +
86   #ifdef MSTATS
87   #include  <stdio.h>
88 < static unsigned b_nsbrked = 0;
88 > static unsigned b_nsbrked = 0;
89   static unsigned b_nalloced = 0;
90   static unsigned b_nfreed = 0;
91   static unsigned b_nscrounged = 0;
# Line 38 | Line 97 | static unsigned        m_nwasted = 0;
97   #define  NULL           0
98   #endif
99  
100 < #ifndef ALIGN
101 < #define  ALIGN          int                     /* align type */
100 > #ifndef ALIGNT
101 > #define  ALIGNT         int                     /* align type */
102   #endif
103 < #define  BYTES_WORD     sizeof(ALIGN)
103 > #define  BYTES_WORD     sizeof(ALIGNT)
104  
105 + #ifndef  MAXINCR
106   #define  MAXINCR        (1<<16)                 /* largest sbrk(2) increment */
47
48 #ifdef  NOVMEM
49 #define  getpagesize()  1024
107   #endif
108                                          /* malloc free lists */
109   typedef union m_head {
# Line 55 | Line 112 | typedef union m_head {
112                  short           magic;
113                  short           bucket;
114          }       a;
115 <        ALIGN           dummy;
115 >        ALIGNT          dummy;
116   } M_HEAD;
117  
118   #define MAGIC           0x1a2           /* magic number for allocated memory */
# Line 65 | Line 122 | typedef union m_head {
122  
123   static M_HEAD   *free_list[NBUCKETS];
124  
125 < static ALIGN    dummy_mem;
125 > static ALIGNT   dummy_mem;
126  
127 + static char     *memlim[2];
128 +
129   #define DUMMYLOC        ((char *)&dummy_mem)
130  
131 + #define BADPTR(p)       ((p) < memlim[0] | (p) >= memlim[1])
132 +
133   #ifdef  MCOMP                   /* memory compaction routines */
134   static char     seedtab[1024];          /* seed for compaction table */
135  
# Line 152 | Line 213 | unsigned       *np;
213  
214          for ( ; ; ) {
215                                          /* compact free lists */
216 <                compactfree();
216 >                while (compactfree())
217 >                        ;
218                                          /* find largest block */
219                  tab = mtab(&cptab); tablen = mtablen(&cptab);
220                  big = tab;
# Line 165 | Line 227 | unsigned       *np;
227                          big->siz = 0;           /* remove from table */
228                          return(big->ptr);       /* return it */
229                  }
230 <                if (mtablen(big) < tablen+1) {
230 >                if (mtablen(big) <= tablen) {
231                          *np = 0;                /* cannot grow table */
232                          return(NULL);           /* report failure */
233                  }
# Line 175 | Line 237 | unsigned       *np;
237                  cptab.ptr = big->ptr;
238                  cptab.siz = big->siz;
239                  big->siz = 0;                   /* clear and copy */
178 #ifdef BSD
240                  bcopy((char *)tab, (char *)(mtab(&cptab)+1),
241                                  tablen*sizeof(struct mblk));
242                  bzero((char *)(mtab(&cptab)+tablen+1),
243                          (mtablen(&cptab)-tablen-1)*sizeof(struct mblk));
183 #else
184                (void)memcpy((char *)(mtab(&cptab)+1), (char *)tab,
185                                tablen*sizeof(struct mblk));
186                memset((char *)(mtab(&cptab)+tablen+1), 0,
187                        (mtablen(&cptab)-tablen-1)*sizeof(struct mblk));
188 #endif
244          }                       /* next round */
245   }
246   #endif          /* MCOMP */
# Line 235 | Line 290 | register unsigned  n;
290                  pagesz = amnt = getpagesize();
291                  nrem = (int)sbrk(0);                    /* page align break */
292                  nrem = pagesz - (nrem&(pagesz-1));
293 <                bpos = sbrk(nrem);                      /* word aligned! */
293 >                bpos = sbrk(nrem);
294                  if ((int)bpos == -1)
295                          return(NULL);
296   #ifdef MSTATS
297                  b_nsbrked += nrem;
298   #endif
299 +                bpos += nrem & (BYTES_WORD-1);          /* align pointer */
300 +                nrem &= ~(BYTES_WORD-1);
301 +                memlim[0] = bpos;
302 +                memlim[1] = bpos + nrem;
303          }
304  
305          n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1);         /* word align rqst. */
306  
307          if (n > nrem) {                                 /* need more core */
308 +        tryagain:
309                  if (n > amnt) {                         /* big chunk */
310                          thisamnt = (n+(pagesz-1))&~(pagesz-1);
311                          if (thisamnt <= MAXINCR)        /* increase amnt */
# Line 254 | Line 314 | register unsigned  n;
314                          thisamnt = amnt;
315                  p = sbrk(thisamnt);
316                  if ((int)p == -1) {                     /* uh-oh, ENOMEM */
317 <                        thisamnt = n;                   /* search free lists */
318 <                        p = mscrounge(&thisamnt);
319 <                        if (p == NULL)                  /* we're really out */
317 >                        errno = 0;                      /* call cavalry */
318 >                        if (thisamnt >= n+pagesz) {
319 >                                amnt = pagesz;          /* minimize request */
320 >                                goto tryagain;
321 >                        }
322 >                        thisamnt = n;
323 >                        p = mscrounge(&thisamnt);       /* search free lists */
324 >                        if (p == NULL) {                /* we're really out */
325 >                                errno = ENOMEM;
326                                  return(NULL);
327 +                        }
328                  }
329   #ifdef MSTATS
330                  else b_nsbrked += thisamnt;
# Line 268 | Line 335 | register unsigned  n;
335                          nrem = thisamnt;
336                  } else                                  /* otherwise tack on */
337                          nrem += thisamnt;
338 +                if (bpos < memlim[0])
339 +                        memlim[0] = bpos;
340 +                if (bpos + nrem > memlim[1])
341 +                        memlim[1] = bpos + nrem;
342          }
343          p = bpos;
344          bpos += n;                                      /* advance */
# Line 286 | Line 357 | register unsigned  n;
357          register int    bucket;
358          register unsigned       bsiz;
359  
360 +        if (n < 1<<FIRSTBUCKET || p == NULL)
361 +                return;
362   #ifdef MSTATS
363          b_nfreed += n;
364   #endif
# Line 295 | Line 368 | register unsigned  n;
368                  p += bsiz;
369                  n -= bsiz;
370          }
371 +        if (p < memlim[0])
372 +                memlim[0] = p;
373 +        if (p + n > memlim[1])
374 +                memlim[1] = p + n;
375                                          /* fill big buckets first */
376          for (bucket = NBUCKETS-1, bsiz = 1<<(NBUCKETS-1);
377                          bucket >= FIRSTBUCKET; bucket--, bsiz >>= 1)
# Line 311 | Line 388 | char *
388   malloc(n)                       /* allocate n bytes of memory */
389   unsigned        n;
390   {
314        extern int  errno;
391          register M_HEAD *mp;
392          register int    bucket;
393          register unsigned       bsiz;
# Line 353 | Line 429 | unsigned       n;
429          char    *p;
430          register unsigned       on;
431                                          /* get old size */
432 <        if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC)
432 >        if (op != DUMMYLOC && !BADPTR(op) &&
433 >                        ((M_HEAD *)op-1)->a.magic == MAGIC)
434                  on = 1 << ((M_HEAD *)op-1)->a.bucket;
435          else
436                  on = 0;
437          if (n <= on && (n > on>>1 || on == 1<<FIRSTBUCKET))
438                  return(op);             /* same bucket */
439          if ((p = malloc(n)) == NULL)
440 <                return(NULL);
440 >                return(n<=on ? op : NULL);
441          if (on) {
365 #ifdef  BSD
442                  bcopy(op, p, n>on ? on : n);
367 #else
368                (void)memcpy(p, op, n>on ? on : n);
369 #endif
443                  free(op);
444          }
445          return(p);
# Line 379 | Line 452 | char   *p;
452          register M_HEAD *mp;
453          register int    bucket;
454  
455 <        if (p == NULL || p == DUMMYLOC)
455 >        if (p == DUMMYLOC)
456                  return(1);
457 +        if (BADPTR(p))
458 +                goto invalid;
459          mp = (M_HEAD *)p - 1;
460          if (mp->a.magic != MAGIC)               /* sanity check */
461 <                return(0);
461 >                goto invalid;
462          bucket = mp->a.bucket;
463 +        if (bucket < FIRSTBUCKET | bucket >= NBUCKETS)
464 +                goto invalid;
465          mp->next = free_list[bucket];
466          free_list[bucket] = mp;
467   #ifdef MSTATS
468          m_nfreed += (1 << bucket) + sizeof(M_HEAD);
469   #endif
470          return(1);
471 + invalid:
472 +        errno = EINVAL;
473 +        return(0);
474   }
395
396
397 #ifndef NOVMEM
398 #ifndef BSD
399 #include <sys/var.h>
400 int
401 getpagesize()                   /* use SYSV var structure to get page size */
402 {
403        struct var  v;
404
405        uvar(&v);
406        return(1 << v.v_pageshift);
407 }
408 #endif
409 #endif
475  
476  
477   #ifdef MSTATS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines