home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / size.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  3KB  |  144 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. #include <stdio.h>
  15. #include "mallocin.h"
  16. #include "debug.h"
  17.  
  18. /*
  19.  * Function:    malloc_size()
  20.  *
  21.  * Purpose:    return the size of the allocated segment associated with ptr
  22.  *
  23.  * Arguments:    ptr    - pointer to allocated area 
  24.  *
  25.  * Returns:    the size of the segment
  26.  *
  27.  * Narrative:
  28.  *        verify pointer is within malloc region
  29.  *        get mlist pointer from passed address
  30.  *        verify magic number
  31.  *        verify inuse flag
  32.  *        verify pointer connections with surrounding segments
  33.  *        return size of segment
  34.  */
  35. #ifndef lint
  36. static
  37. char rcs_hdr[] = "$Id: size.c,v 1.4 1992/08/22 16:27:13 cpcahil Exp $";
  38. #endif
  39.  
  40. SIZETYPE
  41. malloc_size(cptr)
  42.     CONST DATATYPE    * cptr;
  43. {
  44.     return( DBmalloc_size((char *)NULL, 0, cptr) );
  45. }
  46.  
  47. SIZETYPE
  48. DBmalloc_size(file,line,cptr)
  49.     CONST char    * file;
  50.     int          line;
  51.     CONST DATATYPE    * cptr;
  52. {
  53.     char            * func = "malloc_size";
  54.     register struct mlist    * ptr;
  55.  
  56.     /*
  57.      * initialize the malloc sub-system.
  58.      */
  59.     MALLOC_INIT();
  60.  
  61.     /*
  62.      * IF malloc chain checking is on, go do it.
  63.      */
  64.     if( malloc_opts & MOPT_CKCHAIN )
  65.     {
  66.         VOIDCAST DBFmalloc_chain_check(func,file,line,1);
  67.     }
  68.  
  69.     /*
  70.      * verify that cptr is within the malloc region and that it is on
  71.      * the correct alignment
  72.      */
  73.     if(        (cptr < malloc_data_start)
  74.         || (cptr > malloc_data_end)
  75.         || ((((long)cptr) & malloc_round) != 0)  )
  76.     {
  77.         malloc_errno = M_CODE_BAD_PTR;
  78.         malloc_warning(func,file,line,(struct mlist *)NULL);
  79.         return( (SIZETYPE) -1);
  80.     }
  81.  
  82.     /* 
  83.      * convert pointer to mlist struct pointer.  To do this we must 
  84.      * move the pointer backwards the correct number of bytes...
  85.      */
  86.     ptr = DATATOMLIST(cptr);
  87.  
  88.     /*
  89.      * check the magic number 
  90.      */    
  91.     if( (ptr->flag&M_MAGIC_BITS) != M_MAGIC )
  92.     {
  93.         malloc_errno = M_CODE_BAD_MAGIC;
  94.         malloc_warning(func,file,line,(struct mlist *)NULL);
  95.         return((SIZETYPE) -1);
  96.     }
  97.  
  98.     /*
  99.      * if this segment is not flagged as being in use
  100.      */
  101.     if( ! (ptr->flag & M_INUSE) )
  102.     {
  103.         malloc_errno = M_CODE_NOT_INUSE;
  104.         malloc_warning(func,file,line,ptr);
  105.         return( (SIZETYPE) -1 );
  106.     }
  107.  
  108.     /*
  109.      * check to see that the pointers are still connected
  110.      */
  111.      if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  112.         (ptr->next && (ptr->next->prev != ptr) ) ||
  113.         ((ptr->next == NULL) && (ptr->prev == NULL)) )
  114.     {
  115.         malloc_errno = M_CODE_BAD_CONNECT;
  116.         malloc_warning(func,file,line,ptr);
  117.         return( (SIZETYPE) -1 );
  118.     }
  119.  
  120.     /*
  121.      * check fill regions for overflow
  122.      */
  123.     VOIDCAST FILLCHECK(func,file,line,ptr,SHOWERRORS);
  124.  
  125.     return(ptr->r_size);
  126.  
  127. } /* DBmalloc_size(... */
  128.  
  129. /*
  130.  * $Log: size.c,v $
  131.  * Revision 1.4  1992/08/22  16:27:13  cpcahil
  132.  * final changes for pl14
  133.  *
  134.  * Revision 1.3  1992/07/03  00:03:25  cpcahil
  135.  * more fixes for pl13, several suggestons from Rich Salz.
  136.  *
  137.  * Revision 1.2  1992/07/02  15:35:52  cpcahil
  138.  * misc cleanups for PL13
  139.  *
  140.  * Revision 1.1  1992/07/02  13:49:54  cpcahil
  141.  * added support for new malloc_size function and additional tests to testerr
  142.  *
  143.  */
  144.