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 2.5 by greg, Mon Jun 29 22:39:51 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 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 15 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15   *      call mscrounge() if sbrk fails.
16   * mscrounge() returns whatever free memory it can find to satisfy the
17   *      request along with the number of bytes (modified).
18 + * mcompact() takes the same arguments as mscrounge() (and is in fact
19 + *      called by mscrounge() under dire circumstances), but it uses
20 + *      memory compaction to return a larger block.  (MCOMP)
21   *
22   *      Greg Ward       Lawrence Berkeley Laboratory
23   */
# Line 23 | Line 26 | static char SCCSid[] = "$SunId$ LBL";
26  
27   #ifdef MSTATS
28   #include  <stdio.h>
29 + static unsigned b_nsbrked = 0;
30   static unsigned b_nalloced = 0;
31   static unsigned b_nfreed = 0;
32   static unsigned b_nscrounged = 0;
33 + static unsigned b_ncompacted = 0;
34   static unsigned m_nalloced = 0;
35   static unsigned m_nfreed = 0;
36   static unsigned m_nwasted = 0;
# Line 38 | Line 43 | static unsigned        m_nwasted = 0;
43   #endif
44   #define  BYTES_WORD     sizeof(ALIGN)
45  
46 + #ifndef  MAXINCR
47   #define  MAXINCR        (1<<16)                 /* largest sbrk(2) increment */
42
43 #ifdef  NOVMEM
44 #define  getpagesize()  BYTES_WORD
48   #endif
49                                          /* malloc free lists */
50   typedef union m_head {
51          union m_head    *next;
52 <        int             bucket;
52 >        struct {
53 >                short           magic;
54 >                short           bucket;
55 >        }       a;
56          ALIGN           dummy;
57   } M_HEAD;
58  
59 + #define MAGIC           0x1a2           /* magic number for allocated memory */
60 +
61   #define FIRSTBUCKET     3
62   #define NBUCKETS        30
63  
64   static M_HEAD   *free_list[NBUCKETS];
65  
66 < static char     DUMMYLOC[BYTES_WORD];
66 > static ALIGN    dummy_mem;
67  
68 + #define DUMMYLOC        ((char *)&dummy_mem)
69  
70 + #ifdef  MCOMP                   /* memory compaction routines */
71 + static char     seedtab[1024];          /* seed for compaction table */
72 +
73 + static struct mblk {
74 +        char            *ptr;   /* pointer to memory block */
75 +        unsigned        siz;    /* size of memory block */
76 + }       cptab = {seedtab, sizeof(seedtab)};
77 +
78 + #define mtab(mb)        ((struct mblk *)(mb)->ptr)
79 + #define mtablen(mb)     ((mb)->siz/sizeof(struct mblk))
80 +
81 +
82 + static int
83 + mbcmp(m1, m2)           /* compare memory block locations */
84 + register struct mblk    *m1, *m2;
85 + {
86 +                                /* put empty blocks at end */
87 +        if (m1->siz == 0)
88 +                return(m2->siz == 0 ? 0 : 1);
89 +        if (m2->siz == 0)
90 +                return(-1);
91 +                                /* otherwise, ascending order */
92 +        return(m1->ptr - m2->ptr);
93 + }
94 +
95 +
96 + int
97 + compactfree()           /* compact free lists (moving to table) */
98 + {
99 +        register struct mblk    *tp;
100 +        register int    bucket;
101 +        unsigned        n, bsiz, ncomp;
102 +                                /* fill table from free lists */
103 +        tp = mtab(&cptab); n = mtablen(&cptab);
104 +        bucket = NBUCKETS-1; bsiz = 1<<(NBUCKETS-1);
105 +        ncomp = 0;
106 +        for ( ; ; ) {
107 +                while (tp->siz) {       /* find empty slot */
108 +                        if (--n == 0)
109 +                                goto loopexit;
110 +                        tp++;
111 +                }
112 +                while (free_list[bucket] == NULL) {     /* find block */
113 +                        if (--bucket < FIRSTBUCKET)
114 +                                goto loopexit;
115 +                        bsiz >>= 1;
116 +                }
117 +                tp->ptr = (char *)free_list[bucket];
118 +                ncomp += (tp->siz = bsiz + sizeof(M_HEAD));
119 +                free_list[bucket] = free_list[bucket]->next;
120 +        }
121 + loopexit:
122 +        if (ncomp == 0)
123 +                return(0);              /* nothing new to compact */
124 + #ifdef MSTATS
125 +        b_ncompacted += ncomp;
126 + #endif
127 +        tp = mtab(&cptab); n = mtablen(&cptab);         /* sort table */
128 +        qsort((char *)tp, n, sizeof(struct mblk), mbcmp);
129 +        ncomp = 0;                                      /* collect blocks */
130 +        while (--n && tp[1].siz) {
131 +                if (tp[0].ptr + tp[0].siz == tp[1].ptr) {
132 +                        tp[1].ptr = tp[0].ptr;
133 +                        tp[1].siz += tp[0].siz;
134 +                        ncomp += tp[0].siz;
135 +                        tp[0].siz = 0;
136 +                }
137 +                tp++;
138 +        }
139 +        return(ncomp);
140 + }
141 +
142 +
143   char *
144 + mcompact(np)            /* compact free lists to satisfy request */
145 + unsigned        *np;
146 + {
147 +        struct mblk     *tab;
148 +        unsigned        tablen;
149 +        register struct mblk    *bp, *big;
150 +
151 +        for ( ; ; ) {
152 +                                        /* compact free lists */
153 +                compactfree();
154 +                                        /* find largest block */
155 +                tab = mtab(&cptab); tablen = mtablen(&cptab);
156 +                big = tab;
157 +                bp = tab + tablen;
158 +                while (--bp > tab)
159 +                        if (bp->siz > big->siz)
160 +                                big = bp;
161 +                if (big->siz >= *np) {          /* big enough? */
162 +                        *np = big->siz;
163 +                        big->siz = 0;           /* remove from table */
164 +                        return(big->ptr);       /* return it */
165 +                }
166 +                if (mtablen(big) < tablen+1) {
167 +                        *np = 0;                /* cannot grow table */
168 +                        return(NULL);           /* report failure */
169 +                }
170 +                                /* enlarge table, free old one */
171 +                mtab(big)[0].ptr = cptab.ptr;
172 +                mtab(big)[0].siz = cptab.siz;
173 +                cptab.ptr = big->ptr;
174 +                cptab.siz = big->siz;
175 +                big->siz = 0;                   /* clear and copy */
176 + #ifdef BSD
177 +                bcopy((char *)tab, (char *)(mtab(&cptab)+1),
178 +                                tablen*sizeof(struct mblk));
179 +                bzero((char *)(mtab(&cptab)+tablen+1),
180 +                        (mtablen(&cptab)-tablen-1)*sizeof(struct mblk));
181 + #else
182 +                (void)memcpy((char *)(mtab(&cptab)+1), (char *)tab,
183 +                                tablen*sizeof(struct mblk));
184 +                memset((char *)(mtab(&cptab)+tablen+1), 0,
185 +                        (mtablen(&cptab)-tablen-1)*sizeof(struct mblk));
186 + #endif
187 +        }                       /* next round */
188 + }
189 + #endif          /* MCOMP */
190 +
191 +
192 + char *
193   mscrounge(np)           /* search free lists to satisfy request */
194   register unsigned       *np;
195   {
# Line 77 | Line 208 | register unsigned      *np;
208   #endif
209                          return(p);
210                  }
211 + #ifdef MCOMP
212 +        return(mcompact(np));           /* try compaction */
213 + #else
214          *np = 0;
215          return(NULL);
216 + #endif
217   }
218  
219  
# Line 94 | Line 229 | register unsigned  n;
229          unsigned  thisamnt;
230          register char   *p;
231  
97 #ifdef MSTATS
98        b_nalloced += n;
99 #endif
232          if (pagesz == 0) {                              /* initialize */
233                  pagesz = amnt = getpagesize();
234                  nrem = (int)sbrk(0);                    /* page align break */
235                  nrem = pagesz - (nrem&(pagesz-1));
236 <                bpos = sbrk(nrem);                      /* word aligned! */
236 >                bpos = sbrk(nrem);
237                  if ((int)bpos == -1)
238                          return(NULL);
239 + #ifdef MSTATS
240 +                b_nsbrked += nrem;
241 + #endif
242 +                bpos += nrem & (BYTES_WORD-1);          /* align pointer */
243 +                nrem &= ~(BYTES_WORD-1);
244          }
245  
246          n = (n+(BYTES_WORD-1))&~(BYTES_WORD-1);         /* word align rqst. */
# Line 122 | Line 259 | register unsigned  n;
259                          if (p == NULL)                  /* we're really out */
260                                  return(NULL);
261                  }
262 + #ifdef MSTATS
263 +                else b_nsbrked += thisamnt;
264 + #endif
265                  if (p != bpos+nrem) {                   /* not an increment */
266                          bfree(bpos, nrem);              /* free what we have */
267                          bpos = p;
# Line 132 | Line 272 | register unsigned  n;
272          p = bpos;
273          bpos += n;                                      /* advance */
274          nrem -= n;
275 + #ifdef MSTATS
276 +        b_nalloced += n;
277 + #endif
278          return(p);
279   }
280  
# Line 143 | Line 286 | register unsigned  n;
286          register int    bucket;
287          register unsigned       bsiz;
288  
289 +        if (n < 1<<FIRSTBUCKET)
290 +                return;
291   #ifdef MSTATS
292          b_nfreed += n;
293   #endif
# Line 184 | Line 329 | unsigned       n;
329                  errno = EINVAL;
330                  return(NULL);
331          }
187 #ifdef MSTATS
188        m_nalloced += bsiz + sizeof(M_HEAD);
189        m_nwasted += bsiz + sizeof(M_HEAD) - n;
190 #endif
332          if (free_list[bucket] == NULL) {        /* need more core */
333                  mp = (M_HEAD *)bmalloc(bsiz+sizeof(M_HEAD));
334                  if (mp == NULL)
# Line 196 | Line 337 | unsigned       n;
337                  mp = free_list[bucket];
338                  free_list[bucket] = mp->next;
339          }
340 <        mp->bucket = bucket;                    /* tag block */
340 > #ifdef MSTATS
341 >        m_nalloced += bsiz + sizeof(M_HEAD);
342 >        m_nwasted += bsiz + sizeof(M_HEAD) - n;
343 > #endif
344 >        mp->a.magic = MAGIC;                    /* tag block */
345 >        mp->a.bucket = bucket;
346          return((char *)(mp+1));
347   }
348  
# Line 209 | Line 355 | unsigned       n;
355          char    *p;
356          register unsigned       on;
357                                          /* get old size */
358 <        if (op != NULL && op != DUMMYLOC)
359 <                on = 1 << ((M_HEAD *)op-1)->bucket;
358 >        if (op != NULL && op != DUMMYLOC && ((M_HEAD *)op-1)->a.magic == MAGIC)
359 >                on = 1 << ((M_HEAD *)op-1)->a.bucket;
360          else
361                  on = 0;
362          if (n <= on && (n > on>>1 || on == 1<<FIRSTBUCKET))
363                  return(op);             /* same bucket */
364 <        p = malloc(n);
365 <        if (p != NULL)
364 >        if ((p = malloc(n)) == NULL)
365 >                return(n<=on ? op : NULL);
366 >        if (on) {
367   #ifdef  BSD
368                  bcopy(op, p, n>on ? on : n);
369   #else
370                  (void)memcpy(p, op, n>on ? on : n);
371   #endif
372 <        free(op);
372 >                free(op);
373 >        }
374          return(p);
375   }
376  
# Line 233 | Line 381 | char   *p;
381          register M_HEAD *mp;
382          register int    bucket;
383  
384 <        if (p == NULL || p == DUMMYLOC)
385 <                return;
384 >        if (p == NULL | p == DUMMYLOC)
385 >                return(1);
386          mp = (M_HEAD *)p - 1;
387 <        bucket = mp->bucket;
387 >        if (mp->a.magic != MAGIC)               /* sanity check */
388 >                return(0);
389 >        bucket = mp->a.bucket;
390 >        if (bucket < FIRSTBUCKET | bucket >= NBUCKETS)
391 >                return(0);
392          mp->next = free_list[bucket];
393          free_list[bucket] = mp;
394   #ifdef MSTATS
395          m_nfreed += (1 << bucket) + sizeof(M_HEAD);
396   #endif
397 +        return(1);
398   }
399  
400  
248 #ifndef NOVMEM
249 #ifndef BSD
250 #include <sys/var.h>
251 int
252 getpagesize()                   /* use SYSV var structure to get page size */
253 {
254        struct var  v;
255
256        uvar(&v);
257        return(1 << v.v_pageshift);
258 }
259 #endif
260 #endif
261
262
401   #ifdef MSTATS
402   printmemstats(fp)               /* print memory statistics to stream */
403   FILE    *fp;
# Line 270 | Line 408 | FILE   *fp;
408          unsigned int    total = 0;
409  
410          fprintf(fp, "Memory statistics:\n");
411 +        fprintf(fp, "\tbmalloc: %d bytes from sbrk\n", b_nsbrked);
412          fprintf(fp, "\tbmalloc: %d bytes allocated\n", b_nalloced);
413          fprintf(fp, "\tbmalloc: %d bytes freed\n", b_nfreed);
414          fprintf(fp, "\tbmalloc: %d bytes scrounged\n", b_nscrounged);
415 +        fprintf(fp, "\tbmalloc: %d bytes compacted\n", b_ncompacted);
416          fprintf(fp, "\tmalloc: %d bytes allocated\n", m_nalloced);
417          fprintf(fp, "\tmalloc: %d bytes wasted (%.1f%%)\n", m_nwasted,
418                          100.0*m_nwasted/m_nalloced);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines