home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / calloc.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  5KB  |  215 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.  
  17. #include "mallocin.h"
  18.  
  19. #ifndef lint
  20. static char rcs_header[] = "$Id: calloc.c,v 1.17 1992/08/22 16:27:13 cpcahil Exp $";
  21. #endif
  22.  
  23. /*
  24.  * Function:    calloc()
  25.  *
  26.  * Purpose:    to call debug_calloc to do the allocation
  27.  *
  28.  * Arguments:    nelem    - number of elements
  29.  *        elsize    - size of each element
  30.  *
  31.  * Returns:    whatever debug_calloc returns
  32.  *
  33.  * Narrative:    call debug_calloc and return it's return
  34.  */
  35. DATATYPE *
  36. calloc(nelem,elsize)
  37.     SIZETYPE       nelem;
  38.     SIZETYPE      elsize;
  39. {
  40.     return( debug_calloc((char *)NULL,(int)-1,nelem,elsize) );
  41. }
  42.  
  43. /*
  44.  * Function:    debug_calloc()
  45.  *
  46.  * Purpose:    to allocate and nullify a data area
  47.  *
  48.  * Arguments:    nelem    - number of elements
  49.  *        elsize    - size of each element
  50.  *
  51.  * Returns:    NULL    - if malloc fails
  52.  *        or pointer to allocated space
  53.  *
  54.  * Narrative:    determine size of area to malloc
  55.  *        malloc area.
  56.  *        if malloc succeeds
  57.  *            fill area with nulls
  58.  *        return ptr to malloc'd region
  59.  */
  60.  
  61. DATATYPE *
  62. debug_calloc(file,line,nelem,elsize)
  63.     CONST char    * file;
  64.     int          line;
  65.     SIZETYPE       nelem;
  66.     SIZETYPE       elsize;
  67. {
  68.     static IDTYPE      call_counter;
  69.  
  70.     /*
  71.      * increment call counter
  72.      */
  73.     call_counter++;
  74.  
  75.     return( DBFcalloc("calloc",M_T_CALLOC,call_counter,
  76.                file,line,nelem,elsize) );
  77.  
  78. }
  79.  
  80. char *
  81. DBFcalloc(func,type,call_counter,file,line,nelem,elsize)
  82.     CONST char    * func;
  83.     int          type;
  84.     IDTYPE          call_counter;
  85.     CONST char    * file;
  86.     int          line;
  87.     SIZETYPE       nelem;
  88.     SIZETYPE       elsize;
  89. {
  90.     DATATYPE    * ptr;
  91.     SIZETYPE      size;
  92.  
  93.     /*
  94.      * make sure malloc sub-system is initialized.
  95.      */
  96.     MALLOC_INIT();
  97.  
  98.     /*
  99.      * calculate the size to allocate
  100.      */
  101.     size = elsize * nelem;
  102.  
  103.     /*
  104.      * if the user wants to be warned about zero length mallocs, do so
  105.      */
  106.     if( ((malloc_opts & MOPT_ZERO) != 0) && (size == 0) )
  107.     {
  108.         malloc_errno = M_CODE_ZERO_ALLOC;
  109.         malloc_warning(func,file,line,(struct mlist *) NULL);
  110.     }
  111.  
  112.     ptr = DBFmalloc(func,type,call_counter,file,line,size);
  113.  
  114.     if( ptr != NULL )
  115.     {
  116.         /*
  117.          * clear the area that was allocated
  118.          */
  119.         VOIDCAST memset(ptr,'\0',size);
  120.     }
  121.  
  122.     return(ptr);
  123.  
  124. } /* DBFcalloc(... */
  125.  
  126. /*
  127.  * Function:    cfree()
  128.  *
  129.  * Purpose:    to free an area allocated by calloc (actually frees any
  130.  *        allocated area)
  131.  *
  132.  * Arguments:    cptr    - pointer to area to be freed
  133.  *
  134.  * Returns:    nothing of any use
  135.  *
  136.  * Narrative:    just call the appropriate function to perform the free
  137.  *
  138.  * Note:    most systems do not have such a call, but for the few that do,
  139.  *        it is here.
  140.  */
  141. FREETYPE
  142. cfree( cptr )
  143.     DATATYPE    * cptr;
  144. {
  145.     debug_cfree((CONST char *)NULL,(int)-1, cptr);
  146. }
  147.  
  148. FREETYPE
  149. debug_cfree(file,line,cptr)
  150.     CONST char    * file;
  151.     int          line;
  152.     DATATYPE    * cptr;
  153. {
  154.     static IDTYPE      call_counter;
  155.  
  156.     call_counter++;    
  157.  
  158.     DBFfree("cfree",F_T_CFREE,call_counter,file,line,cptr);
  159. }
  160.  
  161. /*
  162.  * $Log: calloc.c,v $
  163.  * Revision 1.17  1992/08/22  16:27:13  cpcahil
  164.  * final changes for pl14
  165.  *
  166.  * Revision 1.16  1992/07/12  15:30:58  cpcahil
  167.  * Merged in Jonathan I Kamens' changes
  168.  *
  169.  * Revision 1.15  1992/07/03  00:03:25  cpcahil
  170.  * more fixes for pl13, several suggestons from Rich Salz.
  171.  *
  172.  * Revision 1.14  1992/04/22  18:17:32  cpcahil
  173.  * added support for Xt Alloc functions, linted code
  174.  *
  175.  * Revision 1.13  1992/04/13  03:06:33  cpcahil
  176.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  177.  *
  178.  * Revision 1.12  1992/01/30  12:23:06  cpcahil
  179.  * renamed mallocint.h -> mallocin.h
  180.  *
  181.  * Revision 1.11  1992/01/10  17:28:03  cpcahil
  182.  * Added support for overriding void datatype
  183.  *
  184.  * Revision 1.10  1991/12/06  17:58:42  cpcahil
  185.  * added cfree() for compatibility with some wierd systems
  186.  *
  187.  * Revision 1.9  91/12/02  19:10:08  cpcahil
  188.  * changes for patch release 5
  189.  * 
  190.  * Revision 1.8  91/11/25  14:41:51  cpcahil
  191.  * Final changes in preparation for patch 4 release
  192.  * 
  193.  * Revision 1.7  91/11/24  00:49:21  cpcahil
  194.  * first cut at patch 4
  195.  * 
  196.  * Revision 1.6  90/05/11  00:13:07  cpcahil
  197.  * added copyright statment
  198.  * 
  199.  * Revision 1.5  90/02/24  20:41:57  cpcahil
  200.  * lint changes.
  201.  * 
  202.  * Revision 1.4  90/02/24  17:25:47  cpcahil
  203.  * changed $header to $id so full path isn't included.
  204.  * 
  205.  * Revision 1.3  90/02/24  13:32:24  cpcahil
  206.  * added function header.  moved log to end of file.
  207.  * 
  208.  * Revision 1.2  90/02/22  23:08:26  cpcahil
  209.  * fixed rcs_header line
  210.  * 
  211.  * Revision 1.1  90/02/22  23:07:38  cpcahil
  212.  * Initial revision
  213.  * 
  214.  */
  215.