home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xibm.zip / common / ibmPlumber.c < prev    next >
C/C++ Source or Header  |  1991-09-20  |  4KB  |  170 lines

  1. /*
  2.  * $Id: ibmPlumber.c,v 1.1 1991/09/20 18:24:03 mtranle Exp $
  3.  *
  4.  * Copyright IBM Corporation 1987,1988,1989
  5.  *
  6.  * All Rights Reserved
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and that 
  11.  * both that copyright notice and this permission notice appear in
  12.  * supporting documentation, and that the name of IBM not be
  13.  * used in advertising or publicity pertaining to distribution of the
  14.  * software without specific, written prior permission.
  15.  *
  16.  * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18.  * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22.  * SOFTWARE.
  23.  *
  24. */
  25. /*
  26.     plumber.c - check malloc arena for coreleaks
  27.  
  28.     author: WJHansen, CMU/ITC
  29.     (c) Copyright IBM Corporation, 1986
  30.  
  31. Uses the IDENTIFY version of malloc, which stores the return address in the 
  32. header of each block.  
  33.  
  34. Produces a table showing a description of the set of blocks produced from each calling address.
  35.  
  36.  
  37.     WARNING:  if an application does sbrk's of its own, the arena
  38.     will be in multiple segments.  The code here only processes the last of these.
  39.  
  40. */
  41. #define IDENTIFY
  42.  
  43. #include <stdio.h>
  44. #include "ibmMalloc.h"
  45.  
  46. static    void storedata();
  47. static    void printandfreedata();
  48.  
  49. #ifndef IDENTIFY
  50.  
  51. plumber () {
  52. }
  53.  
  54. #else
  55.  
  56. static struct callerdata {
  57.     struct callerdata *left, *right;
  58.     char *caller;
  59.     int nblks;
  60.     long totsize;
  61.     int loseq, hiseq, losize, hisize;
  62. } *data;
  63.  
  64. plumber (outf) 
  65.     FILE *outf;
  66. {
  67.     register struct arenastate *A = GetMallocArena();
  68.     register int nextseq = A->SeqNo;
  69.     register struct freehdr *t, *f;
  70.     /* scan free list to find segment trailers and scan each segment */
  71.     CheckAllocs("plumber start");
  72.  
  73.     data = NULL;
  74.     t = A->arenaend;
  75.     f = ((struct freetrlr *)(t+1))->Front;
  76.     /* scan the last arena segment for active blocks */
  77.     for (; f<t; f = (struct freehdr *)((char *)f 
  78.             + clearbits(f->Size))) 
  79.         if (testbit(f->Size, ACTIVE)
  80.                 && f->seqno<nextseq) 
  81.             storedata(f, &data);
  82.     /* output data from tree */
  83.     fprintf(outf, "%10s%10s%10s%20s%20s\n\n", 
  84.         "caller", "#blocks", "tot size", "size range    ", "seq# range     ");
  85.     printandfreedata(data, outf);
  86. }
  87.  
  88.     static void
  89. storedata (p, d)
  90.     register struct hdr *p;
  91.     struct callerdata **d;
  92. {
  93.     register struct callerdata *td = *d;
  94.     if (td==NULL) {
  95.         *d = td = (struct callerdata *)malloc(sizeof(struct callerdata));
  96.         td->caller = p->caller;
  97.         td->nblks = 1;
  98.         td->loseq = td->hiseq = p->seqno;
  99.         td->losize = td->hisize = td->totsize = clearbits(p->Size);
  100.     }
  101.     else if (p->caller < td->caller)
  102.         storedata (p, &(td->left));
  103.     else if (p->caller > td->caller)
  104.         storedata (p, &(td->right)); 
  105.     else {    /* == */
  106.         int size = clearbits(p->Size);
  107.         td->nblks++;
  108.         if (p->seqno<td->loseq)  td->loseq = p->seqno;
  109.         else if (p->seqno>td->hiseq)  td->hiseq = p->seqno;
  110.         if (size<td->losize)  td->losize = size;
  111.         else if (size>td->hisize)  td->hisize = size;
  112.         td->totsize += size;
  113.     }
  114. }
  115.  
  116.     static void
  117. printandfreedata(d, outf)
  118.     register struct callerdata *d;
  119.     FILE *outf;
  120. {
  121.     if (d) {
  122.         printandfreedata(d->left, outf);
  123.         fprintf (outf, "0x%-8lx%10d%10d%9d-%-10d%9d-%-10d\n",
  124.             d->caller, d->nblks, d->totsize, 
  125.             d->losize, d->hisize, d->loseq, d->hiseq);
  126.         printandfreedata(d->right, outf);
  127.         free(d);
  128.     }
  129. }
  130.  
  131. /*
  132.  * calloc - allocate and clear memory block
  133.  */
  134.  
  135. #define CHARPERINT (sizeof(int)/sizeof(char))
  136. #define NULL 0
  137.  
  138. char *
  139. calloc(num, size)
  140.     unsigned num, size;
  141. {
  142. #define FIRSTARG    num
  143.     register char *mp;
  144.     register int *q;
  145.     register m;
  146.  
  147.     num *= size;
  148.     mp = malloc(num);
  149.     if (mp == NULL)
  150.         return(NULL);
  151.  
  152.     /* Stamp pc of caller of calloc in malloc header */
  153.     (((struct hdr *)mp)-1)->caller = *(((char **)&FIRSTARG)-RETADDROFF);
  154.  
  155.     q = (int *) mp;
  156.     m = (num+CHARPERINT-1)/CHARPERINT;
  157.     while (--m>=0)
  158.         *q++ = 0;
  159.     return (mp);
  160. }
  161.  
  162. cfree(p, num, size)
  163.     char *p;
  164.     unsigned num, size;
  165. {
  166.  
  167.     free(p);
  168. }
  169. #endif
  170.