home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / mallopt.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  6KB  |  275 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 <fcntl.h>
  17. #include "mallocin.h"
  18.  
  19. /*
  20.  * Function:    dbmallopt()
  21.  *
  22.  * Purpose:    to set options for the malloc debugging library
  23.  *
  24.  * Arguments:    none
  25.  *
  26.  * Returns:    nothing of any value
  27.  *
  28.  * Narrative:    
  29.  *
  30.  */
  31.  
  32. #ifndef lint
  33. static
  34. char rcs_hdr[] = "$Id: mallopt.c,v 1.21 1992/08/22 16:27:13 cpcahil Exp $";
  35. #endif
  36.  
  37. int
  38. dbmallopt(cmd,value)
  39.     int              cmd;
  40.     union dbmalloptarg    * value;
  41. {
  42.     int              baseflags = 0;
  43.     int              i;
  44.     int              newflag = 0;
  45.     register char        * s;
  46.     int              turnon = 0;
  47.  
  48.     MALLOC_INIT();
  49.  
  50.     switch(cmd)
  51.     {
  52.         case MALLOC_CKCHAIN:
  53.             newflag = MOPT_CKCHAIN;
  54.             turnon = value->i;
  55.             break;
  56.  
  57.         case MALLOC_CKDATA:
  58.             newflag = MOPT_CKDATA;
  59.             turnon = value->i;
  60.             break;
  61.  
  62.         case MALLOC_DETAIL:
  63.             turnon = value->i;
  64.             newflag = MOPT_DETAIL;
  65.             break;
  66.  
  67.         case MALLOC_ERRFILE:
  68.             
  69.             if( strcmp(value->str,"-") != 0 )
  70.             {
  71.                 if( malloc_errfd != 2 )
  72.                 {
  73.                     close(malloc_errfd);    
  74.                 }
  75.                 i = open(value->str,
  76.                      O_CREAT|O_APPEND|O_WRONLY,0666);
  77.                 if( i == -1 )
  78.                 {
  79.                     VOIDCAST write(2,
  80.                       "Unable to open malloc error file: ",
  81.                       (WRTSIZE) 34);
  82.                     for(s=value->str; *s; s++)
  83.                     {
  84.                         /* do nothing */;
  85.                     }
  86.                     VOIDCAST write(2,value->str,
  87.                              (WRTSIZE)(s-(value->str)));
  88.                     VOIDCAST write(2,"\n",(WRTSIZE)1);
  89.                 }
  90.                 else
  91.                 {
  92.                     if( malloc_errfd != 2 )
  93.                     {
  94.                         VOIDCAST close(malloc_errfd);
  95.                     }
  96.                     malloc_errfd = i;
  97.                 }
  98.             }
  99.             else
  100.             {
  101.                 if( malloc_errfd != 2 )
  102.                 {
  103.                     close(malloc_errfd);    
  104.                 }
  105.                 malloc_errfd = 2;
  106.             }
  107.             
  108.             break;
  109.  
  110.         case MALLOC_FATAL:
  111.             malloc_fatal_level = value->i;
  112.             break;
  113.  
  114.         case MALLOC_FREEMARK:
  115.             turnon = value->i;
  116.             newflag = MOPT_FREEMARK;
  117.             break;
  118.  
  119.         case MALLOC_FILLAREA:
  120.             baseflags = MOPT_MFILL | MOPT_FFILL;
  121.             switch(value->i)
  122.             {
  123.                 case 1:
  124.                     newflag = MOPT_DFILL;
  125.                     break;
  126.                 case 2:
  127.                     newflag = MOPT_MFILL | MOPT_DFILL;
  128.                     break;
  129.                 case 0:
  130.                 case 3:
  131.                 default:
  132.                     newflag = MOPT_MFILL | MOPT_FFILL
  133.                                  | MOPT_DFILL;
  134.                     break;
  135.             }
  136.             turnon = value->i;    
  137.  
  138.             /*
  139.              * if we ever enable malloc_checking, then we set the
  140.              * malloc_check flag to non-zero.  Then it can never be
  141.              * set back to zero.  This is done as a performance 
  142.              * increase if filling is never enabled.
  143.              */
  144.             if( (turnon != 0) && (newflag != 0) )
  145.             {
  146.                 malloc_fill = 1;
  147.             }
  148.             
  149.             break;
  150.  
  151.         case MALLOC_LOWFRAG:
  152.             newflag = MOPT_LOWFRAG;
  153.             turnon = value->i;    
  154.             break;
  155.  
  156.         case MALLOC_REUSE:
  157.             turnon = value->i;
  158.             newflag = MOPT_REUSE;
  159.             break;
  160.  
  161.         case MALLOC_SHOWLINKS:
  162.             turnon = value->i;
  163.             newflag = MOPT_SLINKS;
  164.             break;
  165.  
  166.         case MALLOC_WARN:
  167.             malloc_warn_level = value->i;
  168.             break;
  169.  
  170.         case MALLOC_ZERO:
  171.             turnon = value->i;
  172.             newflag = MOPT_ZERO;
  173.             break;
  174.  
  175.         default:
  176.             return(1);
  177.     }
  178.  
  179.     /*
  180.      * if there are base flags, remove all of them so that they will
  181.      * not remain on forever.
  182.      */
  183.     if( baseflags )
  184.     {
  185.         malloc_opts &= ~baseflags;
  186.     }
  187.  
  188.     /*
  189.      * if we have a new option flag, apply it to the options variable.
  190.      */
  191.     if( newflag )
  192.     {
  193.         if( turnon )
  194.         {
  195.             malloc_opts |= newflag;
  196.         }
  197.         else
  198.         {
  199.             malloc_opts &= ~newflag;
  200.         }
  201.     }
  202.  
  203.     return(0);
  204. }
  205.  
  206. /*
  207.  * $Log: mallopt.c,v $
  208.  * Revision 1.21  1992/08/22  16:27:13  cpcahil
  209.  * final changes for pl14
  210.  *
  211.  * Revision 1.20  1992/07/03  00:03:25  cpcahil
  212.  * more fixes for pl13, several suggestons from Rich Salz.
  213.  *
  214.  * Revision 1.19  1992/07/02  15:35:52  cpcahil
  215.  * misc cleanups for PL13
  216.  *
  217.  * Revision 1.18  1992/06/30  13:06:39  cpcahil
  218.  * added support for aligned allocations
  219.  *
  220.  * Revision 1.17  1992/06/22  23:40:10  cpcahil
  221.  * many fixes for working on small int systems
  222.  *
  223.  * Revision 1.16  1992/05/06  04:53:29  cpcahil
  224.  * performance enhancments
  225.  *
  226.  * Revision 1.15  1992/04/15  12:51:06  cpcahil
  227.  * fixes per testing of patch 8
  228.  *
  229.  * Revision 1.14  1992/04/13  03:06:33  cpcahil
  230.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  231.  *
  232.  * Revision 1.13  1992/03/01  12:42:38  cpcahil
  233.  * added support for managing freed areas and fixed doublword bndr problems
  234.  *
  235.  * Revision 1.12  1992/01/30  12:23:06  cpcahil
  236.  * renamed mallocint.h -> mallocin.h
  237.  *
  238.  * Revision 1.11  1992/01/10  17:28:03  cpcahil
  239.  * Added support for overriding void datatype
  240.  *
  241.  * Revision 1.10  1991/12/31  21:31:26  cpcahil
  242.  * changes for patch 6.  See CHANGES file for more info
  243.  *
  244.  * Revision 1.9  1991/12/04  09:23:42  cpcahil
  245.  * several performance enhancements including addition of free list
  246.  *
  247.  * Revision 1.8  91/11/25  14:42:03  cpcahil
  248.  * Final changes in preparation for patch 4 release
  249.  * 
  250.  * Revision 1.7  91/11/24  00:49:30  cpcahil
  251.  * first cut at patch 4
  252.  * 
  253.  * Revision 1.6  90/08/29  22:23:36  cpcahil
  254.  * fixed mallopt to use a union as an argument.
  255.  * 
  256.  * Revision 1.5  90/08/29  21:22:51  cpcahil
  257.  * miscellaneous lint fixes
  258.  * 
  259.  * Revision 1.4  90/05/11  00:13:10  cpcahil
  260.  * added copyright statment
  261.  * 
  262.  * Revision 1.3  90/02/25  11:03:26  cpcahil
  263.  * changed to return int so that it agrees with l libmalloc.a's mallopt()
  264.  * 
  265.  * Revision 1.2  90/02/25  11:01:21  cpcahil
  266.  * added support for malloc chain checking.
  267.  * 
  268.  * Revision 1.1  90/02/24  21:50:24  cpcahil
  269.  * Initial revision
  270.  * 
  271.  * Revision 1.1  90/02/24  17:10:53  cpcahil
  272.  * Initial revision
  273.  * 
  274.  */
  275.