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.15 by greg, Mon Aug 26 10:49:08 1991 UTC vs.
Revision 2.6 by greg, Fri Sep 4 09:57:43 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24  
25   #include  <errno.h>
26  
27 + extern int      errno;
28 +
29   #ifdef MSTATS
30   #include  <stdio.h>
31 < static unsigned b_nsbrked = 0;
31 > static unsigned b_nsbrked = 0;
32   static unsigned b_nalloced = 0;
33   static unsigned b_nfreed = 0;
34   static unsigned b_nscrounged = 0;
# Line 43 | Line 45 | static unsigned        m_nwasted = 0;
45   #endif
46   #define  BYTES_WORD     sizeof(ALIGN)
47  
48 + #ifndef  MAXINCR
49   #define  MAXINCR        (1<<16)                 /* largest sbrk(2) increment */
47
48 #ifdef  NOVMEM
49 #define  getpagesize()  1024
50   #endif
51                                          /* malloc free lists */
52   typedef union m_head {
# Line 67 | Line 67 | static M_HEAD  *free_list[NBUCKETS];
67  
68   static ALIGN    dummy_mem;
69  
70 + static char     *memlim[2];
71 +
72   #define DUMMYLOC        ((char *)&dummy_mem)
73  
74 + #define BADPTR(p)       ((p) < memlim[0] | (p) >= memlim[1])
75 +
76   #ifdef  MCOMP                   /* memory compaction routines */
77   static char     seedtab[1024];          /* seed for compaction table */
78  
# Line 235 | Line 239 | register unsigned  n;
239                  pagesz = amnt = getpagesize();
240                  nrem = (int)sbrk(0);                    /* page align break */
241                  nrem = pagesz - (nrem&(pagesz-1));
242 <                bpos = sbrk(nrem);                      /* word aligned! */
242 >                bpos = sbrk(nrem);
243                  if ((int)bpos == -1)
244                          return(NULL);
245   #ifdef MSTATS
246                  b_nsbrked += nrem;
247   #endif
248 +                bpos += nrem & (BYTES_WORD-1);          /* align pointer */
249 +                nrem &= ~(BYTES_WORD-1);
250 +                memlim[0] = bpos;
251 +                memlim[1] = bpos + nrem;
252          }
253  
254          n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1);         /* word align rqst. */
# Line 268 | Line 276 | register unsigned  n;
276                          nrem = thisamnt;
277                  } else                                  /* otherwise tack on */
278                          nrem += thisamnt;
279 +                if (bpos < memlim[0])
280 +                        memlim[0] = bpos;
281 +                if (bpos + nrem > memlim[1])
282 +                        memlim[1] = bpos + nrem;
283          }
284          p = bpos;
285          bpos += n;                                      /* advance */
# Line 297 | Line 309 | register unsigned  n;
309                  p += bsiz;
310                  n -= bsiz;
311          }
312 +        if (p < memlim[0])
313 +                memlim[0] = p;
314 +        if (p + n > memlim[1])
315 +                memlim[1] = p + n;
316                                          /* fill big buckets first */
317          for (bucket = NBUCKETS-1, bsiz = 1<<(NBUCKETS-1);
318                          bucket >= FIRSTBUCKET; bucket--, bsiz >>= 1)
# Line 313 | Line 329 | char *
329   malloc(n)                       /* allocate n bytes of memory */
330   unsigned        n;
331   {
316        extern int  errno;
332          register M_HEAD *mp;
333          register int    bucket;
334          register unsigned       bsiz;
# Line 355 | Line 370 | unsigned       n;
370          char    *p;
371          register unsigned       on;
372                                          /* get old size */
373 <        if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC)
373 >        if (op != DUMMYLOC && !BADPTR(op) &&
374 >                        ((M_HEAD *)op-1)->a.magic == MAGIC)
375                  on = 1 << ((M_HEAD *)op-1)->a.bucket;
376          else
377                  on = 0;
378          if (n <= on && (n > on>>1 || on == 1<<FIRSTBUCKET))
379                  return(op);             /* same bucket */
380          if ((p = malloc(n)) == NULL)
381 <                return(NULL);
381 >                return(n<=on ? op : NULL);
382          if (on) {
383   #ifdef  BSD
384                  bcopy(op, p, n>on ? on : n);
# Line 381 | Line 397 | char   *p;
397          register M_HEAD *mp;
398          register int    bucket;
399  
400 <        if (p == NULL || p == DUMMYLOC)
400 >        if (p == DUMMYLOC)
401                  return(1);
402 +        if (BADPTR(p))
403 +                goto invalid;
404          mp = (M_HEAD *)p - 1;
405          if (mp->a.magic != MAGIC)               /* sanity check */
406 <                return(0);
406 >                goto invalid;
407          bucket = mp->a.bucket;
408 +        if (bucket < FIRSTBUCKET | bucket >= NBUCKETS)
409 +                goto invalid;
410          mp->next = free_list[bucket];
411          free_list[bucket] = mp;
412   #ifdef MSTATS
413          m_nfreed += (1 << bucket) + sizeof(M_HEAD);
414   #endif
415          return(1);
416 + invalid:
417 +        errno = EINVAL;
418 +        return(0);
419   }
397
398
399 #ifndef NOVMEM
400 #ifndef BSD
401 #include <sys/var.h>
402 int
403 getpagesize()                   /* use SYSV var structure to get page size */
404 {
405        struct var  v;
406
407        uvar(&v);
408        return(1 << v.v_pageshift);
409 }
410 #endif
411 #endif
420  
421  
422   #ifdef MSTATS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines