home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / memory.c~ < prev    next >
Text File  |  1993-01-04  |  9KB  |  432 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: memory.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.  * memccpy - copy memory region up to specified byte or length
  26.  */
  27. MEMDATA *
  28. memccpy(ptr1, ptr2, ch, len)
  29.     MEMDATA            * ptr1;
  30.     CONST MEMDATA        * ptr2;
  31.     int              ch;
  32.     MEMSIZE           len;
  33. {
  34.     return( DBmemccpy( (char *) NULL, 0, ptr1, ptr2, ch, len) );
  35. }
  36.  
  37. MEMDATA *
  38. DBmemccpy(file,line,ptr1, ptr2, ch, len)
  39.     CONST char        * file;
  40.     int              line;
  41.     MEMDATA            * ptr1;
  42.     CONST MEMDATA        * ptr2;
  43.     int              ch;
  44.     MEMSIZE           len;
  45. {
  46.     register CONST char    * myptr2;
  47.     register MEMSIZE       i;
  48.     MEMDATA            * rtn;
  49.  
  50.  
  51.     myptr2 = (CONST char *) ptr2;
  52.  
  53.     /*
  54.      * I know that the assignment could be done in the following, but
  55.      * I wanted to perform a check before any assignment, so first I 
  56.      * determine the length, check the pointers and then do the assignment.
  57.      */
  58.     for( i=0; (i < len) && (myptr2[i] != ch); i++)
  59.     {
  60.     }
  61.  
  62.     /*
  63.      * if we found the character...
  64.       */
  65.     if( i < len )
  66.     {
  67.         rtn = ((char *)ptr1)+i+1;
  68.         i++;
  69.     }
  70.     else
  71.     {
  72.         rtn = (char *) 0;
  73.     }
  74.  
  75.     /*
  76.      * make sure we have enough room in both ptr1 and ptr2
  77.      */
  78.     malloc_check_data("memccpy", file, line, ptr1, i);
  79.     malloc_check_data("memccpy", file, line, ptr2, i);
  80.  
  81.     DataMC(ptr1,ptr2,i);
  82.     
  83.     return( rtn );
  84. }
  85.  
  86. /*
  87.  * memchr - find a byte in a memory region
  88.  */
  89. MEMDATA *
  90. memchr(ptr1,ch,len)
  91.     CONST MEMDATA         * ptr1;
  92.     register int          ch;
  93.     MEMSIZE           len;
  94. {
  95.     return( DBmemchr( (char *)NULL, 0, ptr1, ch, len) );
  96. }
  97.  
  98. MEMDATA  *
  99. DBmemchr(file,line,ptr1,ch,len)
  100.     CONST char        * file;
  101.     int              line;
  102.     CONST MEMDATA         * ptr1;
  103.     register int          ch;
  104.     MEMSIZE           len;
  105. {
  106.     register CONST char    * myptr1;
  107.     MEMSIZE           i;
  108.  
  109.     malloc_check_data("memchr", file, line, ptr1, len);
  110.  
  111.     myptr1 = (CONST char *) ptr1;
  112.  
  113.     for( i=0; (i < len) && (myptr1[i] != (char) ch); i++)
  114.     {
  115.     }
  116.  
  117.     if( i < len )
  118.     {
  119.         return( (MEMDATA  *) (myptr1+i) );
  120.     }
  121.     else
  122.     {
  123.         return( (MEMDATA  *) 0);    
  124.     }
  125. }
  126.  
  127. /*
  128.  * memcpy  - copy one memory area to another
  129.  * memmove - copy one memory area to another
  130.  */
  131. MEMDATA  * 
  132. memmove(ptr1, ptr2, len)
  133.     MEMDATA         * ptr1;
  134.     CONST MEMDATA         * ptr2;
  135.     register MEMSIZE       len;
  136. {
  137.     return( DBmemmove( (char *) NULL, 0,ptr1, ptr2, len) );
  138. }
  139.  
  140. MEMDATA  * 
  141. DBmemmove(file,line,ptr1, ptr2, len)
  142.     CONST char        * file;
  143.     int              line;
  144.     MEMDATA         * ptr1;
  145.     CONST MEMDATA         * ptr2;
  146.     register MEMSIZE       len;
  147. {
  148.     return( DBFmemcpy( "memmove", file, line, ptr1, ptr2, len) );
  149. }
  150.  
  151.  
  152. MEMDATA  *
  153. memcpy(ptr1, ptr2, len)
  154.     MEMDATA         * ptr1;
  155.     CONST MEMDATA         * ptr2;
  156.     register MEMSIZE       len;
  157. {
  158.     return( DBmemcpy( (char *) NULL, 0, ptr1, ptr2, len) );
  159. }
  160.  
  161. MEMDATA  *
  162. DBmemcpy(file, line, ptr1, ptr2, len)
  163.     CONST char        * file;
  164.     int              line;
  165.     MEMDATA         * ptr1;
  166.     CONST MEMDATA         * ptr2;
  167.     register MEMSIZE       len;
  168. {
  169.     return( DBFmemcpy( "memcpy", file, line ,ptr1, ptr2, len) );
  170. }
  171.  
  172. MEMDATA  *
  173. DBFmemcpy(func, file, line,ptr1, ptr2, len)
  174.     CONST char        * func;
  175.     CONST char        * file;
  176.     int              line;
  177.     MEMDATA         * ptr1;
  178.     CONST MEMDATA         * ptr2;
  179.     register MEMSIZE       len;
  180. {
  181.     MEMDATA         * rtn = ptr1;
  182.  
  183.     malloc_check_data(func, file, line, ptr1, len);
  184.     malloc_check_data(func, file, line, ptr2, len);
  185.  
  186.     DataMC(ptr1,ptr2,len);
  187.     
  188.     return(rtn);
  189. }
  190.  
  191. /*
  192.  * memcmp - compare two memory regions
  193.  */
  194. int
  195. memcmp(ptr1, ptr2, len)
  196.     CONST MEMDATA         * ptr1;
  197.     CONST MEMDATA         * ptr2;
  198.     register MEMSIZE       len;
  199. {
  200.     return( DBmemcmp((char *)NULL,0,ptr1,ptr2,len) );
  201. }
  202.  
  203. int
  204. DBmemcmp(file,line,ptr1, ptr2, len)
  205.     CONST char        * file;
  206.     int              line;
  207.     CONST MEMDATA         * ptr1;
  208.     CONST MEMDATA         * ptr2;
  209.     register MEMSIZE       len;
  210. {
  211.     return( DBFmemcmp("memcmp",file,line,ptr1,ptr2,len) );
  212. }
  213.  
  214. int
  215. DBFmemcmp(func,file,line,ptr1, ptr2, len)
  216.     CONST char        * func;
  217.     CONST char        * file;
  218.     int              line;
  219.     CONST MEMDATA         * ptr1;
  220.     CONST MEMDATA         * ptr2;
  221.     register MEMSIZE       len;
  222. {
  223.     register CONST char    * myptr1;
  224.     register CONST char    * myptr2;
  225.     
  226.     malloc_check_data(func,file,line, ptr1, len);
  227.     malloc_check_data(func,file,line, ptr2, len);
  228.  
  229.     myptr1 = (CONST char *) ptr1;
  230.     myptr2 = (CONST char *) ptr2;
  231.  
  232.     while( len > 0  && (*myptr1 == *myptr2) )
  233.     {
  234.         len--;
  235.         myptr1++;
  236.         myptr2++;
  237.     }
  238.  
  239.     /* 
  240.      * If stopped by len, return zero
  241.      */
  242.     if( len == 0 )
  243.     {
  244.         return(0);
  245.     }
  246.  
  247.     return( *(CONST MEMCMPTYPE *)myptr1 - *(CONST MEMCMPTYPE *)myptr2 );
  248. }
  249.  
  250. /*
  251.  * memset - set all bytes of a memory block to a specified value
  252.  */
  253. MEMDATA  * 
  254. memset(ptr1, ch, len)
  255.     MEMDATA         * ptr1;
  256.     register int          ch;
  257.     register MEMSIZE       len;
  258. {
  259.     return( DBmemset((char *)NULL,0,ptr1,ch,len) );
  260. }
  261.  
  262. MEMDATA  * 
  263. DBmemset(file,line,ptr1, ch, len)
  264.     CONST char        * file;
  265.     int              line;
  266.     MEMDATA         * ptr1;
  267.     register int          ch;
  268.     register MEMSIZE       len;
  269. {
  270.     return( DBFmemset("memset",file,line,ptr1,ch,len) );
  271. }
  272.  
  273. MEMDATA  * 
  274. DBFmemset(func,file,line,ptr1, ch, len)
  275.     CONST char        * func;
  276.     CONST char        * file;
  277.     int              line;
  278.     MEMDATA         * ptr1;
  279.     register int          ch;
  280.     register MEMSIZE       len;
  281. {
  282.     MEMDATA         * rtn = ptr1;
  283.     malloc_check_data(func, file, line, ptr1, len);
  284.  
  285.     DataMS(ptr1,ch,len);
  286.  
  287.     return(rtn);
  288. }
  289.  
  290. #ifndef ibm032
  291. /*
  292.  * bcopy - copy memory block to another area
  293.  */
  294. MEMDATA  *
  295. bcopy(ptr2,ptr1,len)
  296.     CONST MEMDATA     * ptr2;
  297.     MEMDATA     * ptr1;
  298.     MEMSIZE       len;
  299. {
  300.     return( DBbcopy((char *)NULL,0,ptr2,ptr1,len) );
  301. }
  302. #endif /* ibm032 */
  303.  
  304. MEMDATA  *
  305. DBbcopy(file,line,ptr2,ptr1,len)
  306.     CONST char    * file;
  307.     int          line;
  308.     CONST MEMDATA     * ptr2;
  309.     MEMDATA     * ptr1;
  310.     MEMSIZE       len;
  311. {
  312.     return( DBFmemcpy("bcopy",file,line,ptr1,ptr2,len));
  313. }
  314.  
  315. /*
  316.  * bzero - clear block of memory to zeros
  317.  */
  318. MEMDATA  *
  319. bzero(ptr1,len)
  320.     MEMDATA     * ptr1;
  321.     MEMSIZE       len;
  322. {
  323.     return( DBbzero((char *)NULL,0,ptr1,len) );
  324. }
  325.  
  326. MEMDATA  *
  327. DBbzero(file,line,ptr1,len)
  328.     CONST char    * file;
  329.     int          line;
  330.     MEMDATA     * ptr1;
  331.     MEMSIZE       len;
  332. {
  333.     return( DBFmemset("bzero",file,line,ptr1,'\0',len) );
  334. }
  335.  
  336. /*
  337.  * bcmp - compary memory blocks
  338.  */
  339. int
  340. bcmp(ptr2, ptr1, len)
  341.     CONST MEMDATA     * ptr1;
  342.     CONST MEMDATA     * ptr2;
  343.     MEMSIZE       len;
  344. {
  345.     return( DBbcmp((char *)NULL,0,ptr2, ptr1, len) );
  346. }
  347.  
  348. int
  349. DBbcmp(file, line, ptr2, ptr1, len)
  350.     CONST char    * file;
  351.     int          line;
  352.     CONST MEMDATA     * ptr1;
  353.     CONST MEMDATA     * ptr2;
  354.     MEMSIZE        len;
  355. {
  356.     return( DBFmemcmp("bcmp",file,line,ptr1,ptr2,len) );
  357. }
  358.  
  359. /*
  360.  * $Log: memory.c,v $
  361.  * Revision 1.22  1992/08/22  16:27:13  cpcahil
  362.  * final changes for pl14
  363.  *
  364.  * Revision 1.21  1992/07/12  15:30:58  cpcahil
  365.  * Merged in Jonathan I Kamens' changes
  366.  *
  367.  * Revision 1.20  1992/06/22  23:40:10  cpcahil
  368.  * many fixes for working on small int systems
  369.  *
  370.  * Revision 1.19  1992/05/09  21:27:09  cpcahil
  371.  * final (hopefully) changes for patch 11
  372.  *
  373.  * Revision 1.18  1992/05/09  00:16:16  cpcahil
  374.  * port to hpux and lots of fixes
  375.  *
  376.  * Revision 1.17  1992/05/08  02:30:35  cpcahil
  377.  * minor cleanups from minix/atari port
  378.  *
  379.  * Revision 1.16  1992/05/08  01:44:11  cpcahil
  380.  * more performance enhancements
  381.  *
  382.  * Revision 1.15  1992/04/13  03:06:33  cpcahil
  383.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  384.  *
  385.  * Revision 1.14  1992/01/30  12:23:06  cpcahil
  386.  * renamed mallocint.h -> mallocin.h
  387.  *
  388.  * Revision 1.13  1992/01/24  04:49:05  cpcahil
  389.  * changed memccpy to only check number of chars it will copy.
  390.  *
  391.  * Revision 1.12  1991/12/31  21:31:26  cpcahil
  392.  * changes for patch 6.  See CHANGES file for more info
  393.  *
  394.  * Revision 1.11  1991/12/02  19:10:13  cpcahil
  395.  * changes for patch release 5
  396.  *
  397.  * Revision 1.10  91/11/25  14:42:03  cpcahil
  398.  * Final changes in preparation for patch 4 release
  399.  * 
  400.  * Revision 1.9  91/11/24  00:49:31  cpcahil
  401.  * first cut at patch 4
  402.  * 
  403.  * Revision 1.8  91/05/21  18:33:47  cpcahil
  404.  * fixed bug in memccpy() which checked an extra byte if the first character
  405.  * after the specified length matched the search character.
  406.  * 
  407.  * Revision 1.7  90/08/29  21:27:58  cpcahil
  408.  * fixed value of check in memccpy when character was not found.
  409.  * 
  410.  * Revision 1.6  90/07/16  20:06:26  cpcahil
  411.  * fixed several minor bugs found with Henry Spencer's string/mem tester 
  412.  * program.
  413.  * 
  414.  * 
  415.  * Revision 1.5  90/05/11  15:39:36  cpcahil
  416.  * fixed bug in memccpy().
  417.  * 
  418.  * Revision 1.4  90/05/11  00:13:10  cpcahil
  419.  * added copyright statment
  420.  * 
  421.  * Revision 1.3  90/02/24  21:50:29  cpcahil
  422.  * lots of lint fixes
  423.  * 
  424.  * Revision 1.2  90/02/24  17:29:41  cpcahil
  425.  * changed $Header to $Id so full path wouldnt be included as part of rcs 
  426.  * id string
  427.  * 
  428.  * Revision 1.1  90/02/22  23:17:43  cpcahil
  429.  * Initial revision
  430.  * 
  431.  */
  432.