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