home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / ibm / common / ibmPlumber.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-16  |  4.2 KB  |  173 lines

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