home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / leak.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  6KB  |  251 lines

  1. /*
  2.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  3.  *
  4.  * This software may be distributed freely as long as the following conditions
  5.  * are met:
  6.  *         * the distribution, or any derivative thereof, may not be
  7.  *          included as part of a commercial product
  8.  *        * full source code is provided including this copyright
  9.  *        * there is no charge for the software itself (there may be
  10.  *          a minimal charge for the copying or distribution effort)
  11.  *        * this copyright notice is not modified or removed from any
  12.  *          source file
  13.  */
  14.  
  15. #ifndef lint
  16. static
  17. char rcs_hdr[] = "$Id: leak.c,v 1.12 1992/08/22 16:27:13 cpcahil Exp $";
  18. #endif
  19.  
  20. #include <stdio.h>
  21.  
  22. #include "mallocin.h"
  23.  
  24. /*
  25.  * Function:    malloc_inuse()
  26.  *
  27.  * Purpose:    to determine the amount of memory in use
  28.  *
  29.  * Arguments:    histidptr - pointer to hist id area
  30.  *
  31.  * Returns:    Number of bytes currently in use
  32.  *
  33.  * Narrative:    make sure the malloc chain is ok
  34.  *        for each element in the malloc chain
  35.  *            if it is in use
  36.  *                add size to total size
  37.  *         if hist id is wanted
  38.  *                  set it
  39.  *        return total in-use size
  40.  *        
  41.  */
  42.  
  43. unsigned long
  44. malloc_inuse(histptr)
  45.     unsigned long        * histptr;
  46. {
  47.     return( DBmalloc_inuse((char *)NULL,0,histptr) );
  48. }
  49.  
  50. unsigned long
  51. DBmalloc_inuse(file,line,histptr)
  52.     CONST char        * file;
  53.     int               line;
  54.     unsigned long        * histptr;
  55. {
  56.     unsigned long          size = 0;
  57.     struct mlist        * ptr;
  58.  
  59.     MALLOC_INIT();
  60.  
  61.     /*
  62.      * make sure the chain is ok (otherwise we will have a problem
  63.      * parsing through it
  64.      */
  65.     VOIDCAST DBFmalloc_chain_check("malloc_inuse",file,line,1);
  66.  
  67.     /*
  68.      * for each element in the malloc chain
  69.      */
  70.     for(ptr = &malloc_start; ptr; ptr = ptr->next)
  71.     {
  72.         /*
  73.          * if the element is in use and it is not marked and it is
  74.           * not a stack segment (an internal allocation used by the
  75.          * malloc subsystem when tracking program stack)
  76.          */
  77.         if(    ((ptr->flag & M_INUSE)  == M_INUSE)
  78.             && ((ptr->flag & M_MARKED) != M_MARKED) 
  79.             && ( GETTYPE(ptr) != M_T_STACK) )
  80.         {
  81.             /* 
  82.              * add its requested size into the total size
  83.              */
  84.             size += ptr->r_size;
  85.         }
  86.     }
  87.  
  88.     /*
  89.      * if the hist id is desired, give it to em.
  90.      */
  91.     if( histptr != NULL )
  92.     {
  93.         *histptr = malloc_hist_id;
  94.     }
  95.  
  96.     /*
  97.      * return the size
  98.      */
  99.     return(size);
  100.  
  101. } /* DBmalloc_inuse(... */
  102.  
  103.  
  104. /*
  105.  * Function:    malloc_mark()
  106.  *
  107.  * Purpose:    to mark memory as validly in-use. This is used in order to 
  108.  *        exempt verified segments from the leak list/counters so that
  109.  *        once you verify that it is valid to leave the segment around
  110.  *        forever, you can mark the segment and it won't be counted in
  111.  *        the leak memory counts, no the leak segment list
  112.  *
  113.  * Arguments:    ptr    - pointer to data area to mark
  114.  *
  115.  * Returns:    true    - segment has been marked
  116.  *        false    - segment not marked because it is invalid
  117.  *
  118.  * Narrative:
  119.  *        make sure malloc subsystem is initialized
  120.  *        if necessary, check malloc chain
  121.  *        verify pointer is within malloc region
  122.  *        get mlist pointer from passed address
  123.  *        verify magic number
  124.  *        verify inuse flag
  125.  *        verify valid linkage
  126.  *        mark segment
  127.  */
  128.  
  129. VOIDTYPE
  130. malloc_mark(cptr)
  131.     DATATYPE    * cptr;
  132. {
  133.     DBmalloc_mark((char *)NULL, 0, cptr);
  134. }
  135.  
  136. VOIDTYPE
  137. DBmalloc_mark(file,line,cptr)
  138.     CONST char    * file;
  139.     int          line;
  140.     DATATYPE    * cptr;
  141. {
  142.     CONST char        * func = "malloc_mark";
  143.     register struct mlist    * ptr;
  144.  
  145.     /*
  146.      * initialize the malloc sub-system.
  147.      */
  148.     MALLOC_INIT();
  149.  
  150.     /*
  151.      * If malloc chain checking is on, go do it.
  152.      */
  153.     if( malloc_opts & MOPT_CKCHAIN )
  154.     {
  155.         VOIDCAST DBFmalloc_chain_check(func,file,line,1);
  156.     }
  157.  
  158.     /*
  159.      * verify that cptr is within the malloc region and that it is on
  160.      * the correct alignment
  161.      */
  162.     if(        (cptr < malloc_data_start)
  163.         || (cptr > malloc_data_end)
  164.         || ((((long)cptr) & malloc_round) != 0)  )
  165.     {
  166.         malloc_errno = M_CODE_BAD_PTR;
  167.         malloc_warning(func,file,line,(struct mlist *)NULL);
  168.         return;
  169.     }
  170.  
  171.     /* 
  172.      * convert pointer to mlist struct pointer.  To do this we must 
  173.      * move the pointer backwards the correct number of bytes...
  174.      */
  175.     ptr = (struct mlist *) (((char *)cptr) - M_SIZE);
  176.  
  177.     /*
  178.      * check the magic number 
  179.      */    
  180.     if( (ptr->flag&M_MAGIC_BITS) != M_MAGIC )
  181.     {
  182.         malloc_errno = M_CODE_BAD_MAGIC;
  183.         malloc_warning(func,file,line,(struct mlist *)NULL);
  184.         return;
  185.     }
  186.  
  187.     /*
  188.      * if this segment is not flagged as being in use
  189.      */
  190.     if( ! (ptr->flag & M_INUSE) )
  191.     {
  192.         malloc_errno = M_CODE_NOT_INUSE;
  193.         malloc_warning(func,file,line,ptr);
  194.         return;
  195.     }
  196.  
  197.     /*
  198.      * check to see that the pointers are still connected
  199.      */
  200.      if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  201.         (ptr->next && (ptr->next->prev != ptr) ) ||
  202.         ((ptr->next == NULL) && (ptr->prev == NULL)) )
  203.     {
  204.         malloc_errno = M_CODE_BAD_CONNECT;
  205.         malloc_warning(func,file,line,ptr);
  206.         return;
  207.     }
  208.  
  209.     ptr->flag |= M_MARKED;
  210.  
  211. } /* DBmalloc_mark(... */
  212.  
  213. /*
  214.  * $Log: leak.c,v $
  215.  * Revision 1.12  1992/08/22  16:27:13  cpcahil
  216.  * final changes for pl14
  217.  *
  218.  * Revision 1.11  1992/07/02  13:49:54  cpcahil
  219.  * added support for new malloc_size function and additional tests to testerr
  220.  *
  221.  * Revision 1.10  1992/06/30  13:06:39  cpcahil
  222.  * added support for aligned allocations
  223.  *
  224.  * Revision 1.9  1992/06/27  22:48:48  cpcahil
  225.  * misc fixes per bug reports from first week of reviews
  226.  *
  227.  * Revision 1.8  1992/06/22  23:40:10  cpcahil
  228.  * many fixes for working on small int systems
  229.  *
  230.  * Revision 1.7  1992/04/13  19:57:15  cpcahil
  231.  * more patch 8 fixes
  232.  *
  233.  * Revision 1.6  1992/04/13  03:06:33  cpcahil
  234.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  235.  *
  236.  * Revision 1.5  1992/01/30  12:23:06  cpcahil
  237.  * renamed mallocint.h -> mallocin.h
  238.  *
  239.  * Revision 1.4  1992/01/10  17:28:03  cpcahil
  240.  * Added support for overriding void datatype
  241.  *
  242.  * Revision 1.3  1991/12/02  19:10:09  cpcahil
  243.  * changes for patch release 5
  244.  *
  245.  * Revision 1.2  91/11/25  14:41:54  cpcahil
  246.  * Final changes in preparation for patch 4 release
  247.  * 
  248.  * Revision 1.1  91/11/24  00:49:26  cpcahil
  249.  * first cut at patch 4
  250.  */
  251.