home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / m_init.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  6KB  |  268 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.  
  16. #ifndef lint
  17. static
  18. char rcs_hdr[] = "$Id: m_init.c,v 1.22 1992/08/22 16:27:13 cpcahil Exp $";
  19. #endif
  20.  
  21. #include <stdio.h>
  22. #include "mallocin.h"
  23.  
  24. /*
  25.  * Function:    malloc_init()
  26.  *
  27.  * Purpose:    to initialize the pointers and variables use by the
  28.  *        malloc() debugging library
  29.  *
  30.  * Arguments:    none
  31.  *
  32.  * Returns:    nothing of any value
  33.  *
  34.  * Narrative:    Just initialize all the needed variables.  Use dbmallopt
  35.  *        to set options taken from the environment.
  36.  *
  37.  */
  38. VOIDTYPE
  39. malloc_init()
  40. {
  41.     char            * cptr;
  42.     union dbmalloptarg      m;
  43.     int              size;
  44.     int              round;
  45.  
  46.     /*
  47.       * If already initialized...
  48.      */
  49.     if( malloc_data_start != (char *) 0)
  50.     {
  51.         return;
  52.     }
  53.  
  54.  
  55.     malloc_data_start = sbrk(0);
  56.     malloc_data_end = malloc_data_start;
  57.     malloc_start.s.size = 0;
  58.     malloc_end = &malloc_start;
  59.  
  60.     /*
  61.      * test to see what rounding we need to use for this system
  62.      */
  63.     size = M_SIZE;
  64.     round = M_RND;
  65.     while( round > 0 )
  66.     {
  67.     
  68.         if( (size & (round-1)) == 0 )
  69.         {
  70.             malloc_round = round-1;
  71.             break;
  72.         }
  73.         round >>= 1;
  74.     }
  75.  
  76.     if( round == 0 )
  77.     {
  78.         malloc_errno = M_CODE_NOBOUND;
  79.         malloc_fatal("malloc_init",__FILE__,__LINE__,(struct mlist*)0);
  80.     }
  81.  
  82.     /*
  83.      * the following settings can only be set in the environment.  They
  84.      * cannot be set via calls to dbmallopt().
  85.      */
  86.     if( (cptr=getenv("MALLOC_BOUNDSIZE")) != NULL )
  87.     {
  88.         malloc_boundsize = atoi(cptr);
  89.  
  90.         if( malloc_boundsize < 1 )
  91.         {
  92.             malloc_boundsize = M_DFLT_BSIZE;
  93.         }
  94.     }
  95.  
  96.     
  97.     if( (cptr=getenv("MALLOC_CKCHAIN")) != NULL)
  98.     {
  99.         m.i = atoi(cptr);
  100.         VOIDCAST dbmallopt(MALLOC_CKCHAIN,&m);
  101.     }
  102.  
  103.     if( (cptr=getenv("MALLOC_CKDATA")) != NULL)
  104.     {
  105.         m.i = atoi(cptr);
  106.         VOIDCAST dbmallopt(MALLOC_CKDATA,&m);
  107.     }
  108.  
  109.     if( (cptr=getenv("MALLOC_DETAIL")) != NULL)
  110.     {
  111.         m.i = atoi(cptr);
  112.         VOIDCAST dbmallopt(MALLOC_DETAIL,&m);
  113.     }
  114.  
  115.     if( (cptr=getenv("MALLOC_ERRFILE")) != NULL)
  116.     {
  117.         m.str = cptr;
  118.         VOIDCAST dbmallopt(MALLOC_ERRFILE,&m);
  119.     }
  120.  
  121.     if( (cptr=getenv("MALLOC_FATAL")) != NULL)
  122.     {
  123.         m.i = atoi(cptr);
  124.         VOIDCAST dbmallopt(MALLOC_FATAL,&m);
  125.     }
  126.  
  127.     if( (cptr=getenv("MALLOC_FILLAREA")) != NULL)
  128.     {
  129.         m.i = atoi(cptr);
  130.         VOIDCAST dbmallopt(MALLOC_FILLAREA,&m);
  131.     }
  132.  
  133.     if( (cptr=getenv("MALLOC_FILLBYTE")) != NULL )
  134.     {
  135.         malloc_fillbyte = atoi(cptr);
  136.  
  137.         if( (malloc_fillbyte < 0) || (malloc_fillbyte > 255) )
  138.         {
  139.             malloc_fillbyte = M_DFLT_FILL;
  140.         }
  141.     }
  142.  
  143.     if( (cptr=getenv("MALLOC_FREEBYTE")) != NULL )
  144.     {
  145.         malloc_freebyte = atoi(cptr);
  146.  
  147.         if( (malloc_freebyte < 0) || (malloc_freebyte > 255) )
  148.         {
  149.             malloc_freebyte = M_DFLT_FREE_FILL;
  150.         }
  151.     }
  152.  
  153.     if( (cptr=getenv("MALLOC_FREEMARK")) != NULL)
  154.     {
  155.         m.i = atoi(cptr);
  156.         VOIDCAST dbmallopt(MALLOC_FREEMARK,&m);
  157.     }
  158.  
  159.     if( (cptr=getenv("MALLOC_LOWFRAG")) != NULL)
  160.     {
  161.         m.i = atoi(cptr);
  162.         VOIDCAST dbmallopt(MALLOC_LOWFRAG,&m);
  163.     }
  164.  
  165.     if( (cptr=getenv("MALLOC_REUSE")) != NULL)
  166.     {
  167.         m.i = atoi(cptr);
  168.         VOIDCAST dbmallopt(MALLOC_REUSE,&m);
  169.     }
  170.  
  171.     if( (cptr=getenv("MALLOC_SHOWLINKS")) != NULL)
  172.     {
  173.         m.i = atoi(cptr);
  174.         VOIDCAST dbmallopt(MALLOC_SHOWLINKS,&m);
  175.     }
  176.  
  177.     if( (cptr=getenv("MALLOC_WARN")) != NULL )
  178.     {
  179.         m.i = atoi(cptr);
  180.         VOIDCAST dbmallopt(MALLOC_WARN,&m);
  181.     }
  182.  
  183.     if( (cptr=getenv("MALLOC_ZERO")) != NULL )
  184.     {
  185.         m.i = atoi(cptr);
  186.         VOIDCAST dbmallopt(MALLOC_ZERO,&m);
  187.     }
  188.  
  189.     /*
  190.      * set the malloc_fill initial value 
  191.      */
  192.     if( (malloc_opts & (MOPT_MFILL | MOPT_FFILL | MOPT_DFILL)) != 0 )
  193.     {
  194.         malloc_fill = 1;
  195.     }
  196.  
  197. } /* malloc_init(... */
  198.  
  199. /*
  200.  * $Log: m_init.c,v $
  201.  * Revision 1.22  1992/08/22  16:27:13  cpcahil
  202.  * final changes for pl14
  203.  *
  204.  * Revision 1.21  1992/07/03  00:03:25  cpcahil
  205.  * more fixes for pl13, several suggestons from Rich Salz.
  206.  *
  207.  * Revision 1.20  1992/07/02  15:35:52  cpcahil
  208.  * misc cleanups for PL13
  209.  *
  210.  * Revision 1.19  1992/06/30  13:06:39  cpcahil
  211.  * added support for aligned allocations
  212.  *
  213.  * Revision 1.18  1992/06/22  23:40:10  cpcahil
  214.  * many fixes for working on small int systems
  215.  *
  216.  * Revision 1.17  1992/05/08  02:30:35  cpcahil
  217.  * minor cleanups from minix/atari port
  218.  *
  219.  * Revision 1.16  1992/05/06  05:37:44  cpcahil
  220.  * added overriding of fill characters and boundary size
  221.  *
  222.  * Revision 1.15  1992/05/06  04:53:29  cpcahil
  223.  * performance enhancments
  224.  *
  225.  * Revision 1.14  1992/04/13  03:06:33  cpcahil
  226.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  227.  *
  228.  * Revision 1.13  1992/03/01  12:42:38  cpcahil
  229.  * added support for managing freed areas and fixed doublword bndr problems
  230.  *
  231.  * Revision 1.12  1992/01/30  12:23:06  cpcahil
  232.  * renamed mallocint.h -> mallocin.h
  233.  *
  234.  * Revision 1.11  1992/01/10  17:28:03  cpcahil
  235.  * Added support for overriding void datatype
  236.  *
  237.  * Revision 1.10  1991/12/31  21:31:26  cpcahil
  238.  * changes for patch 6.  See CHANGES file for more info
  239.  *
  240.  * Revision 1.9  1991/12/04  09:23:38  cpcahil
  241.  * several performance enhancements including addition of free list
  242.  *
  243.  * Revision 1.8  91/11/25  14:41:54  cpcahil
  244.  * Final changes in preparation for patch 4 release
  245.  * 
  246.  * Revision 1.7  91/11/24  00:49:26  cpcahil
  247.  * first cut at patch 4
  248.  * 
  249.  * Revision 1.6  90/08/29  22:23:21  cpcahil
  250.  * fixed mallopt to use a union as an argument.
  251.  * 
  252.  * Revision 1.5  90/08/29  21:22:50  cpcahil
  253.  * miscellaneous lint fixes
  254.  * 
  255.  * Revision 1.4  90/05/11  15:53:35  cpcahil
  256.  * fixed bug in initialization code.
  257.  * 
  258.  * Revision 1.3  90/05/11  00:13:08  cpcahil
  259.  * added copyright statment
  260.  * 
  261.  * Revision 1.2  90/02/24  21:50:20  cpcahil
  262.  * lots of lint fixes
  263.  * 
  264.  * Revision 1.1  90/02/24  17:10:53  cpcahil
  265.  * Initial revision
  266.  * 
  267.  */
  268.