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.8 by greg, Wed Oct 3 16:20:38 1990 UTC vs.
Revision 1.10 by greg, Tue Nov 13 14:39:05 1990 UTC

# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21  
22   #include  <errno.h>
23  
24 + #ifdef MSTATS
25 + #include  <stdio.h>
26 + static unsigned b_nalloced = 0;
27 + static unsigned b_nfreed = 0;
28 + static unsigned b_nscrounged = 0;
29 + static unsigned m_nalloced = 0;
30 + static unsigned m_nfreed = 0;
31 + static unsigned m_nwasted = 0;
32 + #else
33   #define  NULL           0
34 + #endif
35  
36   #ifndef ALIGN
37   #define  ALIGN          int                     /* align type */
38   #endif
39   #define  BYTES_WORD     sizeof(ALIGN)
40  
41 < #define  MAXINCR        (1<<18)                 /* largest sbrk(2) increment */
41 > #define  MAXINCR        (1<<16)                 /* largest sbrk(2) increment */
42  
43   #ifdef  NOVMEM
44   #define  getpagesize()  BYTES_WORD
# Line 45 | Line 55 | typedef union m_head {
55  
56   static M_HEAD   *free_list[NBUCKETS];
57  
58 + static char     DUMMYLOC[BYTES_WORD];
59  
60 +
61   char *
62   mscrounge(np)           /* search free lists to satisfy request */
63   register unsigned       *np;
# Line 60 | Line 72 | register unsigned      *np;
72                          *np = bsiz+sizeof(M_HEAD);
73                          p = (char *)free_list[bucket];
74                          free_list[bucket] = free_list[bucket]->next;
75 + #ifdef MSTATS
76 +                        b_nscrounged += *np;
77 + #endif
78                          return(p);
79                  }
80          *np = 0;
# Line 79 | Line 94 | register unsigned  n;
94          unsigned  thisamnt;
95          register char   *p;
96  
97 + #ifdef MSTATS
98 +        b_nalloced += n;
99 + #endif
100          if (pagesz == 0) {                              /* initialize */
101                  pagesz = amnt = getpagesize();
102                  nrem = (int)sbrk(0);                    /* page align break */
# Line 124 | Line 142 | register unsigned  n;
142   {
143          register int    bucket;
144          register unsigned       bsiz;
145 +
146 + #ifdef MSTATS
147 +        b_nfreed += n;
148 + #endif
149                                          /* align pointer */
150          bsiz = BYTES_WORD - ((unsigned)p&(BYTES_WORD-1));
151          if (bsiz < BYTES_WORD) {
# Line 150 | Line 172 | unsigned       n;
172          register M_HEAD *mp;
173          register int    bucket;
174          register unsigned       bsiz;
175 +                                        /* don't return NULL on 0 request */
176 +        if (n == 0)
177 +                return(DUMMYLOC);
178                                          /* find first bucket that fits */
179          for (bucket = FIRSTBUCKET, bsiz = 1<<FIRSTBUCKET;
180                          bucket < NBUCKETS; bucket++, bsiz <<= 1)
# Line 159 | Line 184 | unsigned       n;
184                  errno = EINVAL;
185                  return(NULL);
186          }
187 + #ifdef MSTATS
188 +        m_nalloced += bsiz + sizeof(M_HEAD);
189 +        m_nwasted += bsiz + sizeof(M_HEAD) - n;
190 + #endif
191          if (free_list[bucket] == NULL) {        /* need more core */
192                  mp = (M_HEAD *)bmalloc(bsiz+sizeof(M_HEAD));
193                  if (mp == NULL)
# Line 180 | Line 209 | unsigned       n;
209          char    *p;
210          register unsigned       on;
211                                          /* get old size */
212 <        if (op != NULL)
212 >        if (op != NULL && op != DUMMYLOC)
213                  on = 1 << ((M_HEAD *)op-1)->bucket;
214          else
215                  on = 0;
# Line 204 | Line 233 | char   *p;
233          register M_HEAD *mp;
234          register int    bucket;
235  
236 <        if (p == NULL)
236 >        if (p == NULL || p == DUMMYLOC)
237                  return;
238          mp = (M_HEAD *)p - 1;
239          bucket = mp->bucket;
240          mp->next = free_list[bucket];
241          free_list[bucket] = mp;
242 + #ifdef MSTATS
243 +        m_nfreed += (1 << bucket) + sizeof(M_HEAD);
244 + #endif
245   }
246  
247  
# Line 225 | Line 257 | getpagesize()                  /* use SYSV var structure to get page
257          return(1 << v.v_pageshift);
258   }
259   #endif
260 + #endif
261 +
262 +
263 + #ifdef MSTATS
264 + printmemstats(fp)               /* print memory statistics to stream */
265 + FILE    *fp;
266 + {
267 +        register int    i;
268 +        int     n;
269 +        register M_HEAD *mp;
270 +        unsigned int    total = 0;
271 +
272 +        fprintf(fp, "Memory statistics:\n");
273 +        fprintf(fp, "\tbmalloc: %d bytes allocated\n", b_nalloced);
274 +        fprintf(fp, "\tbmalloc: %d bytes freed\n", b_nfreed);
275 +        fprintf(fp, "\tbmalloc: %d bytes scrounged\n", b_nscrounged);
276 +        fprintf(fp, "\tmalloc: %d bytes allocated\n", m_nalloced);
277 +        fprintf(fp, "\tmalloc: %d bytes wasted (%.1f%%)\n", m_nwasted,
278 +                        100.0*m_nwasted/m_nalloced);
279 +        fprintf(fp, "\tmalloc: %d bytes freed\n", m_nfreed);
280 +        for (i = NBUCKETS-1; i >= FIRSTBUCKET; i--) {
281 +                n = 0;
282 +                for (mp = free_list[i]; mp != NULL; mp = mp->next)
283 +                        n++;
284 +                if (n) {
285 +                        fprintf(fp, "\t%d * %u\n", n, 1<<i);
286 +                        total += n * ((1<<i) + sizeof(M_HEAD));
287 +                }
288 +        }
289 +        fprintf(fp, "\t %u total bytes in free list\n", total);
290 + }
291   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines