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

  1.  
  2. /*
  3.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  4.  *
  5.  * This software may be distributed freely as long as the following conditions
  6.  * are met:
  7.  *         * the distribution, or any derivative thereof, may not be
  8.  *          included as part of a commercial product
  9.  *        * full source code is provided including this copyright
  10.  *        * there is no charge for the software itself (there may be
  11.  *          a minimal charge for the copying or distribution effort)
  12.  *        * this copyright notice is not modified or removed from any
  13.  *          source file
  14.  */
  15. #include <stdio.h>
  16. #include "mallocin.h"
  17. #include "debug.h"
  18.  
  19. /*
  20.  * Function:    free()
  21.  *
  22.  * Purpose:    to deallocate malloced data
  23.  *
  24.  * Arguments:    ptr    - pointer to data area to deallocate
  25.  *
  26.  * Returns:    nothing of any value
  27.  *
  28.  * Narrative:
  29.  *        verify pointer is within malloc region
  30.  *        get mlist pointer from passed address
  31.  *        verify magic number
  32.  *        verify inuse flag
  33.  *        verify pointer connections with surrounding segments
  34.  *        turn off inuse flag
  35.  *        verify no data overrun into non-malloced area at end of segment
  36.  *        IF possible join segment with next segment
  37.  *        IF possible join segment with previous segment
  38.  *        Clear all data in segment (to make sure it isn't reused)
  39.  *
  40.  */
  41. #ifndef lint
  42. static
  43. char rcs_hdr[] = "$Id: free.c,v 1.29 1992/08/22 16:27:13 cpcahil Exp $";
  44. #endif
  45.  
  46. FREETYPE
  47. free(cptr)
  48.     DATATYPE    * cptr;
  49. {
  50.     debug_free((char *)NULL, 0, cptr);
  51. }
  52.  
  53. FREETYPE
  54. debug_free(file,line,cptr)
  55.     CONST char    * file;
  56.     int          line;
  57.     DATATYPE    * cptr;
  58. {
  59.     static IDTYPE      counter;
  60.  
  61.     counter++;
  62.  
  63.     DBFfree("free",F_T_FREE,counter,file,line,cptr);
  64. }
  65.  
  66. FREETYPE
  67. DBFfree(func,type,counter,file,line,cptr)
  68.     CONST char    * func;
  69.     int          type;
  70.     IDTYPE          counter;
  71.     CONST char    * file;
  72.     int          line;
  73.     DATATYPE    * cptr;
  74. {
  75.     register struct mlist    * oldptr;
  76.     register struct mlist    * ptr;
  77.  
  78.     /*
  79.      * initialize the malloc sub-system.
  80.      */
  81.     MALLOC_INIT();
  82.  
  83.     /*
  84.      * IF malloc chain checking is on, go do it.
  85.      */
  86.     if( malloc_opts & MOPT_CKCHAIN )
  87.     {
  88.         VOIDCAST DBFmalloc_chain_check(func,file,line,1);
  89.     }
  90.  
  91. #if defined(ANSI_NULLS) || (__STDC__ && ! defined(NO_ANSI_NULLS))
  92.  
  93.     if( cptr == NULL )
  94.     {
  95.         return;
  96.     }
  97.  
  98. #else
  99.  
  100.     if( (cptr == NULL) && (type == F_T_XTFREE) )
  101.     {
  102.         return;
  103.     }
  104.  
  105. #endif
  106.  
  107.     /*
  108.      * verify that cptr is within the malloc region and that it is on
  109.      * the correct alignment
  110.      */
  111.     if(        (cptr < malloc_data_start)
  112.         || (cptr > malloc_data_end)
  113.         || ((((long)cptr) & malloc_round) != 0)  )
  114.     {
  115.         malloc_errno = M_CODE_BAD_PTR;
  116.         malloc_warning(func,file,line,(struct mlist *)NULL);
  117.         return;
  118.     }
  119.  
  120.     /* 
  121.      * convert pointer to mlist struct pointer.  To do this we must 
  122.      * move the pointer backwards the correct number of bytes...
  123.      */
  124.     ptr = DATATOMLIST(cptr);
  125.  
  126.     /*
  127.      * check the magic number 
  128.      */    
  129.     if( (ptr->flag&M_MAGIC_BITS) != M_MAGIC )
  130.     {
  131.         malloc_errno = M_CODE_BAD_MAGIC;
  132.         malloc_warning(func,file,line,(struct mlist *)NULL);
  133.         return;
  134.     }
  135.  
  136.     /*
  137.      * if this segment is not flagged as being in use
  138.      */
  139.     if( ! (ptr->flag & M_INUSE) )
  140.     {
  141.         malloc_errno = M_CODE_NOT_INUSE;
  142.         malloc_warning(func,file,line,ptr);
  143.         return;
  144.     }
  145.  
  146.     /*
  147.      * check to see that the pointers are still connected
  148.      */
  149.      if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  150.         (ptr->next && (ptr->next->prev != ptr) ) ||
  151.         ((ptr->next == NULL) && (ptr->prev == NULL)) )
  152.     {
  153.         malloc_errno = M_CODE_BAD_CONNECT;
  154.         malloc_warning(func,file,line,ptr);
  155.         return;
  156.     }
  157.  
  158.     /*
  159.      * check fill regions for overflow
  160.      */
  161.     VOIDCAST FILLCHECK(func,file,line,ptr,SHOWERRORS);
  162.  
  163.     /*
  164.      * if this block has been marked and we are warning about marked frees
  165.      * give the user a warning about the free.
  166.      */
  167.     if( ((malloc_opts&MOPT_FREEMARK) != 0) && ((ptr->flag & M_MARKED) != 0))
  168.     {
  169.         malloc_errno = M_CODE_FREEMARK;
  170.         malloc_warning(func,file,line,ptr);
  171.     }
  172.  
  173.     /*
  174.      * flag block as freed
  175.      */
  176.     ptr->flag &= ~M_INUSE;
  177.  
  178.     DEBUG3(10,"pointers: prev: 0x%.7x,  ptr: 0x%.7x, next: 0x%.7x",
  179.             ptr->prev, ptr, ptr->next);
  180.     
  181.     DEBUG3(10,"size:     prev: %9d,  ptr: %9d, next: %9d",
  182.             ptr->prev->s.size, ptr->s.size, ptr->next->s.size);
  183.     
  184.     DEBUG3(10,"flags:    prev: 0x%.7x,  ptr: 0x%.7x, next: 0x%.7x",
  185.             ptr->prev->flag, ptr->flag, ptr->next->flag);
  186.     
  187.     /*
  188.      * identify where this section was freed
  189.      */
  190.     ptr->ffile     = file;
  191.     ptr->fline     = line;
  192.     ptr->fid       = counter;
  193.     ptr->freestack = StackCurrent();
  194.     SETFTYPE(ptr,type);
  195.     
  196.     /*
  197.      * Fill in the freed segment
  198.      */
  199.     FILLDATA(ptr,FILL_FREE,0,(struct mlist *)NULL);
  200.  
  201.     /*
  202.      * if we are reusing code
  203.      */
  204.     if( malloc_opts & MOPT_REUSE  )
  205.     {
  206.         /*
  207.          * check to see if this block can be combined with the next
  208.          * and/or previous block.  Since it may be joined with the
  209.           * previous block we will save a pointer to the previous
  210.          * block and test to verify if it is joined (it's next ptr
  211.          * will no longer point to ptr).
  212.           */
  213.         malloc_join(ptr,ptr->next,NOTINUSE,DOFILL);
  214.  
  215.         oldptr = ptr->prev;
  216.  
  217.         malloc_join(ptr->prev, ptr,NOTINUSE,DOFILL);
  218.  
  219.         if( oldptr->next != ptr )
  220.         {
  221.             DEBUG0(10,"Oldptr was changed");
  222.             ptr = oldptr;
  223.         }
  224.  
  225.         /*
  226.          * else, since the oldptr did not change, ptr is now a new free
  227.          * segment that must be added to the free list, so go do it.
  228.          */
  229.         else
  230.         {
  231.             malloc_freeseg(M_FREE_ADD,ptr);
  232.         }
  233.     }
  234.  
  235. } /* DBFfree(... */
  236.  
  237. /*
  238.  * $Log: free.c,v $
  239.  * Revision 1.29  1992/08/22  16:27:13  cpcahil
  240.  * final changes for pl14
  241.  *
  242.  * Revision 1.28  1992/07/12  15:30:58  cpcahil
  243.  * Merged in Jonathan I Kamens' changes
  244.  *
  245.  * Revision 1.27  1992/07/03  00:03:25  cpcahil
  246.  * more fixes for pl13, several suggestons from Rich Salz.
  247.  *
  248.  * Revision 1.26  1992/07/02  13:49:54  cpcahil
  249.  * added support for new malloc_size function and additional tests to testerr
  250.  *
  251.  * Revision 1.25  1992/05/14  23:02:27  cpcahil
  252.  * added support for ANSI NULL behavior even with non-ansi compilers (if
  253.  * chosen at compile time).
  254.  *
  255.  * Revision 1.24  1992/05/06  04:53:29  cpcahil
  256.  * performance enhancments
  257.  *
  258.  * Revision 1.23  1992/04/24  11:18:52  cpcahil
  259.  * Fixes from Denny Page and Better integration of Xt alloc hooks
  260.  *
  261.  * Revision 1.22  1992/04/22  18:17:32  cpcahil
  262.  * added support for Xt Alloc functions, linted code
  263.  *
  264.  * Revision 1.21  1992/04/13  03:06:33  cpcahil
  265.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  266.  *
  267.  * Revision 1.20  1992/03/01  12:42:38  cpcahil
  268.  * added support for managing freed areas and fixed doublword bndr problems
  269.  *
  270.  * Revision 1.19  1992/02/19  01:41:35  cpcahil
  271.  * added check for alignment on the free'd pointer.
  272.  *
  273.  * Revision 1.18  1992/01/30  12:23:06  cpcahil
  274.  * renamed mallocint.h -> mallocin.h
  275.  *
  276.  * Revision 1.17  1992/01/28  14:30:18  cpcahil
  277.  * Changes from the c.s.r second review
  278.  *
  279.  * Revision 1.16  1992/01/10  17:28:03  cpcahil
  280.  * Added support for overriding void datatype
  281.  *
  282.  * Revision 1.15  1991/12/06  17:58:44  cpcahil
  283.  * added cfree() for compatibility with some wierd systems
  284.  *
  285.  * Revision 1.14  91/12/06  08:54:17  cpcahil
  286.  * cleanup of __STDC__ usage and addition of CHANGES file
  287.  * 
  288.  * Revision 1.13  91/12/04  09:23:37  cpcahil
  289.  * several performance enhancements including addition of free list
  290.  * 
  291.  * Revision 1.12  91/12/02  19:10:09  cpcahil
  292.  * changes for patch release 5
  293.  * 
  294.  * Revision 1.11  91/11/25  14:41:53  cpcahil
  295.  * Final changes in preparation for patch 4 release
  296.  * 
  297.  * Revision 1.10  91/11/24  00:49:25  cpcahil
  298.  * first cut at patch 4
  299.  * 
  300.  * Revision 1.9  90/08/29  21:22:48  cpcahil
  301.  * miscellaneous lint fixes
  302.  * 
  303.  * Revision 1.8  90/05/11  00:13:08  cpcahil
  304.  * added copyright statment
  305.  * 
  306.  * Revision 1.7  90/02/25  11:00:18  cpcahil
  307.  * added support for malloc chain checking.
  308.  * 
  309.  * Revision 1.6  90/02/24  21:50:18  cpcahil
  310.  * lots of lint fixes
  311.  * 
  312.  * Revision 1.5  90/02/24  17:29:13  cpcahil
  313.  * changed $Header to $Id so full path wouldnt be included as part of rcs 
  314.  * id string
  315.  * 
  316.  * Revision 1.4  90/02/24  15:15:32  cpcahil
  317.  * 1. changed ALREADY_FREE errno to NOT_INUSE so that the same errno could
  318.  *    be used by both free and realloc (since it was the same error).
  319.  * 2. fixed coding bug
  320.  * 
  321.  * Revision 1.3  90/02/24  14:23:45  cpcahil
  322.  * fixed malloc_warning calls
  323.  * 
  324.  * Revision 1.2  90/02/24  13:59:10  cpcahil
  325.  * added function header.
  326.  * Modified calls to malloc_warning/malloc_fatal to use new code error messages
  327.  * Added support for malloc_errno setting of error codes.
  328.  * 
  329.  * Revision 1.1  90/02/22  23:17:43  cpcahil
  330.  * Initial revision
  331.  * 
  332.  */
  333.