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.4 by greg, Wed Sep 26 08:26:11 1990 UTC vs.
Revision 1.9 by greg, Wed Oct 3 21:01:59 1990 UTC

# Line 13 | Line 13 | static char SCCSid[] = "$SunId$ LBL";
13   * bmalloc() doesn't keep track of free lists -- it's usually
14   *      just a buffered call to sbrk(2).  However, bmalloc will
15   *      call mscrounge() if sbrk fails.
16 < * mscrounge() returns whatever memory it can find to satisfy the
16 > * mscrounge() returns whatever free memory it can find to satisfy the
17   *      request along with the number of bytes (modified).
18   *
19   *      Greg Ward       Lawrence Berkeley Laboratory
20   */
21  
22 + #include  <errno.h>
23 +
24 + #ifdef MSTATS
25 + #include  <stdio.h>
26 +
27 + static unsigned b_nalloced = 0;
28 + static unsigned b_nfreed = 0;
29 + static unsigned b_nscrounged = 0;
30 + static unsigned m_nalloced = 0;
31 + static unsigned m_nfreed = 0;
32 + static unsigned m_nwasted = 0;
33 + #else
34   #define  NULL           0
35 + #endif
36  
37   #ifndef ALIGN
38   #define  ALIGN          int                     /* align type */
39   #endif
40   #define  BYTES_WORD     sizeof(ALIGN)
41  
42 < #define  MAXINCR        (1<<18)                 /* largest sbrk(2) increment */
42 > #define  MAXINCR        (1<<16)                 /* largest sbrk(2) increment */
43  
44   #ifdef  NOVMEM
45   #define  getpagesize()  BYTES_WORD
# Line 58 | Line 71 | register unsigned      *np;
71                          *np = bsiz+sizeof(M_HEAD);
72                          p = (char *)free_list[bucket];
73                          free_list[bucket] = free_list[bucket]->next;
74 + #ifdef MSTATS
75 +                        b_nscrounged += *np;
76 + #endif
77                          return(p);
78                  }
79          *np = 0;
# Line 77 | Line 93 | register unsigned  n;
93          unsigned  thisamnt;
94          register char   *p;
95  
96 + #ifdef MSTATS
97 +        b_nalloced += n;
98 + #endif
99          if (pagesz == 0) {                              /* initialize */
100                  pagesz = amnt = getpagesize();
101                  nrem = (int)sbrk(0);                    /* page align break */
# Line 122 | Line 141 | register unsigned  n;
141   {
142          register int    bucket;
143          register unsigned       bsiz;
144 +
145 + #ifdef MSTATS
146 +        b_nfreed += n;
147 + #endif
148                                          /* align pointer */
149 <        bsiz = BYTES_WORD - ((unsigned)p&~(BYTES_WORD-1));
149 >        bsiz = BYTES_WORD - ((unsigned)p&(BYTES_WORD-1));
150          if (bsiz < BYTES_WORD) {
151                  p += bsiz;
152                  n -= bsiz;
# Line 144 | Line 167 | char *
167   malloc(n)                       /* allocate n bytes of memory */
168   unsigned        n;
169   {
170 +        extern int  errno;
171          register M_HEAD *mp;
172          register int    bucket;
173          register unsigned       bsiz;
150
151        if (n == 0)
152                return(NULL);
174                                          /* find first bucket that fits */
175 <        bucket = FIRSTBUCKET;
176 <        for (bsiz = 1<<FIRSTBUCKET; bsiz < n; bsiz <<= 1)
177 <                bucket++;
175 >        for (bucket = FIRSTBUCKET, bsiz = 1<<FIRSTBUCKET;
176 >                        bucket < NBUCKETS; bucket++, bsiz <<= 1)
177 >                if (bsiz >= n)
178 >                        break;
179 >        if (bucket >= NBUCKETS) {
180 >                errno = EINVAL;
181 >                return(NULL);
182 >        }
183 > #ifdef MSTATS
184 >        m_nalloced += bsiz + sizeof(M_HEAD);
185 >        m_nwasted += bsiz + sizeof(M_HEAD) - n;
186 > #endif
187          if (free_list[bucket] == NULL) {        /* need more core */
188                  mp = (M_HEAD *)bmalloc(bsiz+sizeof(M_HEAD));
189                  if (mp == NULL)
# Line 169 | Line 199 | unsigned       n;
199  
200   char *
201   realloc(op, n)                  /* reallocate memory using malloc() */
202 < char    *op;
202 > register char   *op;
203   unsigned        n;
204   {
205 <        extern char     *memcpy();
176 <        register char   *p;
205 >        char    *p;
206          register unsigned       on;
207 <
208 <        free(op);                       /* free it first */
209 <        p = malloc(n);                  /* p==op if same bucket */
210 <        if (p == NULL)
211 <                return(NULL);
212 <        if (p != op)                    /* different bucket, do copy */
207 >                                        /* get old size */
208 >        if (op != NULL)
209 >                on = 1 << ((M_HEAD *)op-1)->bucket;
210 >        else
211 >                on = 0;
212 >        if (n <= on && (n > on>>1 || on == 1<<FIRSTBUCKET))
213 >                return(op);             /* same bucket */
214 >        p = malloc(n);
215 >        if (p != NULL)
216   #ifdef  BSD
217                  bcopy(op, p, n>on ? on : n);
218   #else
219                  (void)memcpy(p, op, n>on ? on : n);
220   #endif
221 +        free(op);
222          return(p);
223   }
224  
# Line 202 | Line 235 | char   *p;
235          bucket = mp->bucket;
236          mp->next = free_list[bucket];
237          free_list[bucket] = mp;
238 + #ifdef MSTATS
239 +        m_nfreed += (1 << bucket) + sizeof(M_HEAD);
240 + #endif
241   }
242  
243  
# Line 211 | Line 247 | char   *p;
247   int
248   getpagesize()                   /* use SYSV var structure to get page size */
249   {
214        static int  pagesz = 0;
250          struct var  v;
251  
252 <        if (pagesz == 0) {
253 <                uvar(&v);
219 <                pagesz = 1 << v.v_pageshift;
220 <        }
221 <        return(pagesz);
252 >        uvar(&v);
253 >        return(1 << v.v_pageshift);
254   }
255   #endif
256 + #endif
257 +
258 +
259 + #ifdef MSTATS
260 + printmemstats(fp)               /* print memory statistics to stream */
261 + FILE    *fp;
262 + {
263 +        register int    i;
264 +        int     n;
265 +        register M_HEAD *mp;
266 +        unsigned int    total = 0;
267 +
268 +        fprintf(fp, "Memory statistics:\n");
269 +        fprintf(fp, "\tbmalloc: %d bytes allocated\n", b_nalloced);
270 +        fprintf(fp, "\tbmalloc: %d bytes freed\n", b_nfreed);
271 +        fprintf(fp, "\tbmalloc: %d bytes scrounged\n", b_nscrounged);
272 +        fprintf(fp, "\tmalloc: %d bytes allocated\n", m_nalloced);
273 +        fprintf(fp, "\tmalloc: %d bytes wasted (%.1f%%)\n", m_nwasted,
274 +                        100.0*m_nwasted/m_nalloced);
275 +        fprintf(fp, "\tmalloc: %d bytes freed\n", m_nfreed);
276 +        for (i = NBUCKETS-1; i >= FIRSTBUCKET; i--) {
277 +                n = 0;
278 +                for (mp = free_list[i]; mp != NULL; mp = mp->next)
279 +                        n++;
280 +                if (n) {
281 +                        fprintf(fp, "\t%d * %u\n", n, 1<<i);
282 +                        total += n * ((1<<i) + sizeof(M_HEAD));
283 +                }
284 +        }
285 +        fprintf(fp, "\t %u total bytes in free list\n", total);
286 + }
287   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines