home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sources / misc / 3904 < prev    next >
Encoding:
Text File  |  1992-09-04  |  56.7 KB  |  2,037 lines

  1. Newsgroups: comp.sources.misc
  2. Path: sparky!kent
  3. From: cpcahil@vti.com (Conor P. Cahill)
  4. Subject:  v32i013:  dbmalloc - Debug Malloc Library PL14, Part08/10
  5. Message-ID: <1992Sep4.152333.13488@sparky.imd.sterling.com>
  6. Followup-To: comp.sources.d
  7. X-Md4-Signature: 3d6cdda915a3a990360ae2a6d368f000
  8. Sender: kent@sparky.imd.sterling.com (Kent Landfield)
  9. Organization: Virtual Technologies, Inc., Dulles VA
  10. References: <csm-v32i005=dbmalloc.101423@sparky.IMD.Sterling.COM>
  11. Date: Fri, 4 Sep 1992 15:23:33 GMT
  12. Approved: kent@sparky.imd.sterling.com
  13. Lines: 2022
  14.  
  15. Submitted-by: cpcahil@vti.com (Conor P. Cahill)
  16. Posting-number: Volume 32, Issue 13
  17. Archive-name: dbmalloc/part08
  18. Environment: C, UNIX
  19.  
  20. #! /bin/sh
  21. # This is a shell archive.  Remove anything before this line, then unpack
  22. # it by saving it into a file and typing "sh file".  To overwrite existing
  23. # files, type "sh file -c".  You can also feed this as standard input via
  24. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  25. # will see the following message at the end:
  26. #        "End of archive 8 (of 10)."
  27. # Contents:  mcheck.c memory.c prototypes.h realloc.c stack.c
  28. # Wrapped by cpcahil@virtech on Thu Sep  3 18:39:21 1992
  29. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  30. if test -f 'mcheck.c' -a "${1}" != "-c" ; then 
  31.   echo shar: Will not clobber existing file \"'mcheck.c'\"
  32. else
  33. echo shar: Extracting \"'mcheck.c'\" \(11212 characters\)
  34. sed "s/^X//" >'mcheck.c' <<'END_OF_FILE'
  35. X
  36. X/*
  37. X * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  38. X *
  39. X * This software may be distributed freely as long as the following conditions
  40. X * are met:
  41. X *         * the distribution, or any derivative thereof, may not be
  42. X *          included as part of a commercial product
  43. X *        * full source code is provided including this copyright
  44. X *        * there is no charge for the software itself (there may be
  45. X *          a minimal charge for the copying or distribution effort)
  46. X *        * this copyright notice is not modified or removed from any
  47. X *          source file
  48. X */
  49. X
  50. X#include <stdio.h>
  51. X#include "mallocin.h"
  52. X#include "debug.h"
  53. X
  54. X#ifndef lint
  55. Xstatic
  56. Xchar rcs_hdr[] = "$Id: mcheck.c,v 1.22 1992/08/22 16:27:13 cpcahil Exp $";
  57. X#endif
  58. X
  59. X#define malloc_in_arena(ptr) (   ((DATATYPE*)(ptr) >= malloc_data_start) \
  60. X                  && ((DATATYPE*)(ptr) <= malloc_data_end) )
  61. X
  62. X/*
  63. X * Function:    malloc_check_str()
  64. X *
  65. X * Arguments:    func    - name of function calling this routine
  66. X *        str    - pointer to area to check
  67. X *
  68. X * Purpose:    to verify that if str is within the malloc arena, the data 
  69. X *        it points to does not extend beyond the applicable region.
  70. X *
  71. X * Returns:    Nothing of any use (function is void).
  72. X *
  73. X * Narrative:
  74. X *   IF pointer is within malloc arena
  75. X *      determin length of string
  76. X *      call malloc_verify() to verify data is withing applicable region
  77. X *   return 
  78. X *
  79. X * Mod History:    
  80. X *   90/01/24    cpcahil        Initial revision.
  81. X *   90/01/29    cpcahil        Added code to ignore recursive calls.
  82. X */
  83. XVOIDTYPE
  84. Xmalloc_check_str(func,file,line,str)
  85. X    CONST char        * func;
  86. X    CONST char        * file;
  87. X    int              line;
  88. X    CONST char        * str;
  89. X{
  90. X    static int          layers;
  91. X    register CONST char    * s;
  92. X
  93. X    MALLOC_INIT();
  94. X
  95. X    /*
  96. X     * if we are already in the malloc library somewhere, don't check
  97. X     * things again.
  98. X     */
  99. X    if( in_malloc_code || (! (malloc_opts & MOPT_CKDATA) ) )
  100. X    {
  101. X        return;
  102. X    }
  103. X
  104. X    if( (layers++ == 0) &&  malloc_in_arena(str) )
  105. X    {
  106. X        for( s=str; *s; s++)
  107. X        {
  108. X        }
  109. X        
  110. X        malloc_verify(func,file,line,str,(SIZETYPE)(s-str+1));
  111. X    }
  112. X
  113. X    layers--;
  114. X}
  115. X
  116. X/*
  117. X * Function:    malloc_check_strn()
  118. X *
  119. X * Arguments:    func    - name of function calling this routine
  120. X *        str    - pointer to area to check
  121. X *         len     - max length of string
  122. X *
  123. X * Purpose:    to verify that if str is within the malloc arena, the data 
  124. X *        it points to does not extend beyond the applicable region.
  125. X *
  126. X * Returns:    Nothing of any use (function is void).
  127. X *
  128. X * Narrative:
  129. X *   IF pointer is within malloc arena
  130. X *      determin length of string
  131. X *      call malloc_verify() to verify data is withing applicable region
  132. X *   return 
  133. X *
  134. X * Mod History:    
  135. X *   90/01/24    cpcahil        Initial revision.
  136. X *   90/01/29    cpcahil        Added code to ignore recursive calls.
  137. X *   90/08/29    cpcahil        added length (for strn* functions)
  138. X */
  139. XVOIDTYPE
  140. Xmalloc_check_strn(func,file,line,str,len)
  141. X    CONST char    * func;
  142. X    CONST char     * file;
  143. X    int          line;
  144. X    CONST char    * str;
  145. X    STRSIZE          len;
  146. X{
  147. X    register STRSIZE      i;
  148. X    static int          layers;
  149. X    register CONST char    * s;
  150. X
  151. X    MALLOC_INIT();
  152. X
  153. X    /*
  154. X     * if we are already in the malloc library somewhere, don't check
  155. X     * things again.
  156. X     */
  157. X    if( in_malloc_code || (! (malloc_opts & MOPT_CKDATA) ) )
  158. X    {
  159. X        return;
  160. X    }
  161. X
  162. X    if( (layers++ == 0) &&  malloc_in_arena(str) )
  163. X    {
  164. X        for( s=str,i=0; (i < len) && (*s != '\0'); i++,s++)
  165. X        {
  166. X        }
  167. X
  168. X        /*
  169. X         * if we found a null byte before len, add one to s so
  170. X         * that we ensure that the null is counted in the bytes to
  171. X         * check.
  172. X         */
  173. X        if( i < len )
  174. X        {
  175. X            s++;
  176. X        }
  177. X        malloc_verify(func,file,line,str,(SIZETYPE)(s-str));
  178. X    }
  179. X
  180. X    layers--;
  181. X}
  182. X
  183. X/*
  184. X * Function:    malloc_check_data()
  185. X *
  186. X * Arguments:    func    - name of function calling this routine
  187. X *        ptr    - pointer to area to check
  188. X *        len     - length to verify
  189. X *
  190. X * Purpose:    to verify that if ptr is within the malloc arena, the data 
  191. X *        it points to does not extend beyond the applicable region.
  192. X *
  193. X * Returns:    Nothing of any use (function is void).
  194. X *
  195. X * Narrative:
  196. X *   IF pointer is within malloc arena
  197. X *      call malloc_verify() to verify data is withing applicable region
  198. X *   return 
  199. X *
  200. X * Mod History:    
  201. X *   90/01/24    cpcahil        Initial revision.
  202. X *   90/01/29    cpcahil        Added code to ignore recursive calls.
  203. X */
  204. XVOIDTYPE
  205. Xmalloc_check_data(func,file,line,ptr,len)
  206. X    CONST char    * func;
  207. X    CONST char    * file;
  208. X    int          line;
  209. X    CONST DATATYPE    * ptr;
  210. X    SIZETYPE      len;
  211. X{
  212. X    static int      layers;
  213. X
  214. X    MALLOC_INIT();
  215. X
  216. X    /*
  217. X     * if we are already in the malloc library somewhere, don't check
  218. X     * things again.
  219. X     */
  220. X    if( in_malloc_code || (! (malloc_opts & MOPT_CKDATA) ) )
  221. X    {
  222. X        return;
  223. X    }
  224. X
  225. X    if( layers++ == 0 )
  226. X    {
  227. X        DEBUG3(40,"malloc_check_data(%s,0x%x,%d) called...",
  228. X            func,ptr,len);
  229. X        if( malloc_in_arena(ptr) )
  230. X        {
  231. X            DEBUG0(10,"pointer in malloc arena, verifying...");
  232. X            malloc_verify(func,file,line,ptr,len);
  233. X        }
  234. X    }
  235. X
  236. X    layers--;
  237. X}
  238. X
  239. X/*
  240. X * Function:    malloc_verify()
  241. X *
  242. X * Arguments:    func    - name of function calling the malloc check routines
  243. X *        ptr    - pointer to area to check
  244. X *        len     - length to verify
  245. X *
  246. X * Purpose:    to verify that the data ptr points to does not extend beyond
  247. X *        the applicable malloc region.  This function is only called 
  248. X *        if it has been determined that ptr points into the malloc arena.
  249. X *
  250. X * Returns:    Nothing of any use (function is void).
  251. X *
  252. X * Narrative:
  253. X *
  254. X * Mod History:    
  255. X *   90/01/24    cpcahil        Initial revision.
  256. X */
  257. XVOIDTYPE
  258. Xmalloc_verify(func,file,line,ptr,len)
  259. X    CONST char        * func;
  260. X    CONST char        * file;
  261. X    int              line;
  262. X    register CONST DATATYPE    * ptr;
  263. X    register SIZETYPE      len;
  264. X{
  265. X    register CONST struct mlist    * mptr = NULL;
  266. X    
  267. X    DEBUG5(40,"malloc_verify(%s, %s, %s, 0x%x,%d) called...",
  268. X        func, file, line, ptr, len);
  269. X
  270. X    /*
  271. X     * since we know we have a pointer that is somewhere within the
  272. X     * malloc region, let's take a guess that the current pointer is
  273. X     * at the begining of a region and see if it has the correct values
  274. X     */
  275. X
  276. X    /*
  277. X     * if we are on the correct boundry for a malloc data area pointer
  278. X     */
  279. X    if(   (((long)ptr) & malloc_round) == 0 )
  280. X    {
  281. X        mptr = (CONST struct mlist *)(((CONST char *) ptr ) - M_SIZE);
  282. X
  283. X        if( ! GoodMlist(mptr) )
  284. X        {
  285. X            mptr = (CONST struct mlist *) NULL;
  286. X        }
  287. X    }
  288. X
  289. X    /*
  290. X     * if we haven't found it yet, let's try to look back just a few bytes
  291. X     * to see if it is nearby
  292. X     */
  293. X    if( mptr == (struct mlist *) NULL)
  294. X    {
  295. X        register long    * lptr;
  296. X        register int      i;
  297. X
  298. X        lptr = (long *) (((long)ptr) & ~malloc_round);
  299. X
  300. X        for(i=0; i < MAX_KLUDGE_CHECKS; i++)
  301. X        {
  302. X            lptr--;
  303. X            if( ! malloc_in_arena(lptr) )
  304. X            {
  305. X                mptr = (CONST struct mlist *) NULL;
  306. X                break;
  307. X            }
  308. X
  309. X            /*
  310. X             * if we found the magic number
  311. X              */
  312. X            if( ((*lptr) & M_MAGIC_BITS) == M_MAGIC )
  313. X            {
  314. X                mptr = (CONST struct mlist *)
  315. X                    (((CONST char *)lptr) - M_FLAGOFF);
  316. X
  317. X                /*
  318. X                 * if the pointer is good.
  319. X                  */
  320. X                if( GoodMlist(mptr) )
  321. X                {
  322. X                    break;
  323. X                }
  324. X            }
  325. X
  326. X        }
  327. X
  328. X        /*
  329. X         * if we didn't find an entry, make sure mptr is null
  330. X         */
  331. X        if( i == MAX_KLUDGE_CHECKS )
  332. X        {
  333. X            mptr = (CONST struct mlist *) NULL;
  334. X        }
  335. X    }
  336. X
  337. X
  338. X    /*
  339. X     * if we still haven't found the right segment, we have to do it the
  340. X     * hard way and search along the malloc list
  341. X     */
  342. X    if( mptr == (CONST struct mlist *) NULL)
  343. X    {
  344. X        /*
  345. X         * Find the malloc block that includes this pointer
  346. X         */
  347. X        mptr = &malloc_start;
  348. X        while( mptr
  349. X            && ! ((((CONST DATATYPE *)mptr) < (CONST DATATYPE *)ptr)
  350. X            &&   (((DATATYPE *)(mptr->data+mptr->s.size)) > ptr) ) )
  351. X        {
  352. X            mptr = mptr->next;
  353. X        }
  354. X    }
  355. X
  356. X    /*
  357. X     * if ptr was not in a malloc block, it must be part of
  358. X     *    some direct sbrk() stuff, so just return.
  359. X     */
  360. X    if( ! mptr )
  361. X    {
  362. X        DEBUG1(10,"ptr (0x%x) not found in malloc search", ptr);
  363. X        return;
  364. X    }
  365. X    
  366. X    /*
  367. X      * Now we have a valid malloc block that contains the indicated
  368. X     * pointer.  We must verify that it is withing the requested block
  369. X     * size (as opposed to the real block size which is rounded up to
  370. X     * allow for correct alignment).
  371. X     */
  372. X
  373. X    DEBUG4(60,"Checking  0x%x-0x%x, 0x%x-0x%x",
  374. X            ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);
  375. X    
  376. X    if(    (ptr < (DATATYPE *)mptr->data)
  377. X          || ((((CONST char *)ptr)+len) > (char *)(mptr->data+mptr->r_size)) )
  378. X    {
  379. X        DEBUG4(0,"pointer not within region 0x%x-0x%x, 0x%x-0x%x",
  380. X            ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);
  381. X
  382. X        malloc_errno = M_CODE_OUTOF_BOUNDS;
  383. X        malloc_warning(func,file,line,mptr);
  384. X    }
  385. X    else if ( (mptr->flag&M_INUSE) == 0 )
  386. X    {
  387. X        DEBUG4(0,"segment not in use 0x%x-0x%x, 0x%x-0x%x",
  388. X            ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);
  389. X
  390. X        malloc_errno = M_CODE_NOT_INUSE;
  391. X        malloc_warning(func,file,line,mptr);
  392. X    }
  393. X
  394. X    return;
  395. X}
  396. X
  397. X/*
  398. X * GoodMlist() - return true if mptr appears to point to a valid segment
  399. X */
  400. Xint
  401. XGoodMlist(mptr)
  402. X    register CONST struct mlist * mptr;
  403. X{
  404. X
  405. X    /*
  406. X     * return true if
  407. X     *     1. the segment has a valid magic number
  408. X     *     2. the inuse flag is set
  409. X     *    3. the previous linkage is set correctly
  410. X     *    4. the next linkage is set correctly
  411. X     *    5. either next and prev is not null
  412. X     */
  413. X        
  414. X    return(    ((mptr->flag&M_MAGIC_BITS) == M_MAGIC)
  415. X        && ((mptr->flag&M_INUSE) != 0 )
  416. X         && (mptr->prev ?
  417. X            (malloc_in_arena(mptr->prev) && (mptr->prev->next == mptr)) : 1 )
  418. X         && (mptr->next ?
  419. X            (malloc_in_arena(mptr->next) && (mptr->next->prev == mptr)) : 1 )
  420. X            && ((mptr->next == NULL) || (mptr->prev == NULL)) );
  421. X
  422. X} /* GoodMlist(... */
  423. X/*
  424. X * $Log: mcheck.c,v $
  425. X * Revision 1.22  1992/08/22  16:27:13  cpcahil
  426. X * final changes for pl14
  427. X *
  428. X * Revision 1.21  1992/06/27  22:48:48  cpcahil
  429. X * misc fixes per bug reports from first week of reviews
  430. X *
  431. X * Revision 1.20  1992/06/22  23:40:10  cpcahil
  432. X * many fixes for working on small int systems
  433. X *
  434. X * Revision 1.19  1992/05/09  00:16:16  cpcahil
  435. X * port to hpux and lots of fixes
  436. X *
  437. X * Revision 1.18  1992/05/08  02:30:35  cpcahil
  438. X * minor cleanups from minix/atari port
  439. X *
  440. X * Revision 1.17  1992/05/08  01:44:11  cpcahil
  441. X * more performance enhancements
  442. X *
  443. X * Revision 1.16  1992/05/06  04:53:29  cpcahil
  444. X * performance enhancments
  445. X *
  446. X * Revision 1.15  1992/04/13  03:06:33  cpcahil
  447. X * Added Stack support, marking of non-leaks, auto-config, auto-testing
  448. X *
  449. X * Revision 1.14  1992/03/01  12:42:38  cpcahil
  450. X * added support for managing freed areas and fixed doublword bndr problems
  451. X *
  452. X * Revision 1.13  1992/01/30  12:23:06  cpcahil
  453. X * renamed mallocint.h -> mallocin.h
  454. X *
  455. X * Revision 1.12  1992/01/10  17:51:03  cpcahil
  456. X * more void stuff that slipped by
  457. X *
  458. X * Revision 1.11  1992/01/10  17:28:03  cpcahil
  459. X * Added support for overriding void datatype
  460. X *
  461. X * Revision 1.10  1991/12/31  02:23:29  cpcahil
  462. X * fixed verify bug of strncpy when len was exactly same as strlen
  463. X *
  464. X * Revision 1.9  91/12/02  19:10:12  cpcahil
  465. X * changes for patch release 5
  466. X * 
  467. X * Revision 1.8  91/11/25  14:42:01  cpcahil
  468. X * Final changes in preparation for patch 4 release
  469. X * 
  470. X * Revision 1.7  91/11/24  00:49:29  cpcahil
  471. X * first cut at patch 4
  472. X * 
  473. X * Revision 1.6  91/11/20  11:54:11  cpcahil
  474. X * interim checkin
  475. X * 
  476. X * Revision 1.5  90/08/29  22:23:48  cpcahil
  477. X * added new function to check on strings up to a specified length 
  478. X * and used it within several strn* functions.
  479. X * 
  480. X * Revision 1.4  90/05/11  00:13:09  cpcahil
  481. X * added copyright statment
  482. X * 
  483. X * Revision 1.3  90/02/24  21:50:22  cpcahil
  484. X * lots of lint fixes
  485. X * 
  486. X * Revision 1.2  90/02/24  17:29:38  cpcahil
  487. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  488. X * id string
  489. X * 
  490. X * Revision 1.1  90/02/24  14:57:03  cpcahil
  491. X * Initial revision
  492. X * 
  493. X */
  494. END_OF_FILE
  495. if test 11212 -ne `wc -c <'mcheck.c'`; then
  496.     echo shar: \"'mcheck.c'\" unpacked with wrong size!
  497. fi
  498. # end of 'mcheck.c'
  499. fi
  500. if test -f 'memory.c' -a "${1}" != "-c" ; then 
  501.   echo shar: Will not clobber existing file \"'memory.c'\"
  502. else
  503. echo shar: Extracting \"'memory.c'\" \(8847 characters\)
  504. sed "s/^X//" >'memory.c' <<'END_OF_FILE'
  505. X
  506. X/*
  507. X * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  508. X *
  509. X * This software may be distributed freely as long as the following conditions
  510. X * are met:
  511. X *         * the distribution, or any derivative thereof, may not be
  512. X *          included as part of a commercial product
  513. X *        * full source code is provided including this copyright
  514. X *        * there is no charge for the software itself (there may be
  515. X *          a minimal charge for the copying or distribution effort)
  516. X *        * this copyright notice is not modified or removed from any
  517. X *          source file
  518. X */
  519. X
  520. X#ifndef lint
  521. Xstatic
  522. Xchar rcs_hdr[] = "$Id: memory.c,v 1.22 1992/08/22 16:27:13 cpcahil Exp $";
  523. X#endif
  524. X
  525. X#include <stdio.h>
  526. X#include "mallocin.h"
  527. X
  528. X/*
  529. X * memccpy - copy memory region up to specified byte or length
  530. X */
  531. XMEMDATA *
  532. Xmemccpy(ptr1, ptr2, ch, len)
  533. X    MEMDATA            * ptr1;
  534. X    CONST MEMDATA        * ptr2;
  535. X    int              ch;
  536. X    MEMSIZE           len;
  537. X{
  538. X    return( DBmemccpy( (char *) NULL, 0, ptr1, ptr2, ch, len) );
  539. X}
  540. X
  541. XMEMDATA *
  542. XDBmemccpy(file,line,ptr1, ptr2, ch, len)
  543. X    CONST char        * file;
  544. X    int              line;
  545. X    MEMDATA            * ptr1;
  546. X    CONST MEMDATA        * ptr2;
  547. X    int              ch;
  548. X    MEMSIZE           len;
  549. X{
  550. X    register CONST char    * myptr2;
  551. X    register MEMSIZE       i;
  552. X    MEMDATA            * rtn;
  553. X
  554. X
  555. X    myptr2 = (CONST char *) ptr2;
  556. X
  557. X    /*
  558. X     * I know that the assignment could be done in the following, but
  559. X     * I wanted to perform a check before any assignment, so first I 
  560. X     * determine the length, check the pointers and then do the assignment.
  561. X     */
  562. X    for( i=0; (i < len) && (myptr2[i] != ch); i++)
  563. X    {
  564. X    }
  565. X
  566. X    /*
  567. X     * if we found the character...
  568. X      */
  569. X    if( i < len )
  570. X    {
  571. X        rtn = ((char *)ptr1)+i+1;
  572. X        i++;
  573. X    }
  574. X    else
  575. X    {
  576. X        rtn = (char *) 0;
  577. X    }
  578. X
  579. X    /*
  580. X     * make sure we have enough room in both ptr1 and ptr2
  581. X     */
  582. X    malloc_check_data("memccpy", file, line, ptr1, i);
  583. X    malloc_check_data("memccpy", file, line, ptr2, i);
  584. X
  585. X    DataMC(ptr1,ptr2,i);
  586. X    
  587. X    return( rtn );
  588. X}
  589. X
  590. X/*
  591. X * memchr - find a byte in a memory region
  592. X */
  593. XMEMDATA *
  594. Xmemchr(ptr1,ch,len)
  595. X    CONST MEMDATA         * ptr1;
  596. X    register int          ch;
  597. X    MEMSIZE           len;
  598. X{
  599. X    return( DBmemchr( (char *)NULL, 0, ptr1, ch, len) );
  600. X}
  601. X
  602. XMEMDATA  *
  603. XDBmemchr(file,line,ptr1,ch,len)
  604. X    CONST char        * file;
  605. X    int              line;
  606. X    CONST MEMDATA         * ptr1;
  607. X    register int          ch;
  608. X    MEMSIZE           len;
  609. X{
  610. X    register CONST char    * myptr1;
  611. X    MEMSIZE           i;
  612. X
  613. X    malloc_check_data("memchr", file, line, ptr1, len);
  614. X
  615. X    myptr1 = (CONST char *) ptr1;
  616. X
  617. X    for( i=0; (i < len) && (myptr1[i] != (char) ch); i++)
  618. X    {
  619. X    }
  620. X
  621. X    if( i < len )
  622. X    {
  623. X        return( (MEMDATA  *) (myptr1+i) );
  624. X    }
  625. X    else
  626. X    {
  627. X        return( (MEMDATA  *) 0);    
  628. X    }
  629. X}
  630. X
  631. X/*
  632. X * memcpy  - copy one memory area to another
  633. X * memmove - copy one memory area to another
  634. X */
  635. XMEMDATA  * 
  636. Xmemmove(ptr1, ptr2, len)
  637. X    MEMDATA         * ptr1;
  638. X    CONST MEMDATA         * ptr2;
  639. X    register MEMSIZE       len;
  640. X{
  641. X    return( DBmemmove( (char *) NULL, 0,ptr1, ptr2, len) );
  642. X}
  643. X
  644. XMEMDATA  * 
  645. XDBmemmove(file,line,ptr1, ptr2, len)
  646. X    CONST char        * file;
  647. X    int              line;
  648. X    MEMDATA         * ptr1;
  649. X    CONST MEMDATA         * ptr2;
  650. X    register MEMSIZE       len;
  651. X{
  652. X    return( DBFmemcpy( "memmove", file, line, ptr1, ptr2, len) );
  653. X}
  654. X
  655. X
  656. XMEMDATA  *
  657. Xmemcpy(ptr1, ptr2, len)
  658. X    MEMDATA         * ptr1;
  659. X    CONST MEMDATA         * ptr2;
  660. X    register MEMSIZE       len;
  661. X{
  662. X    return( DBmemcpy( (char *) NULL, 0, ptr1, ptr2, len) );
  663. X}
  664. X
  665. XMEMDATA  *
  666. XDBmemcpy(file, line, ptr1, ptr2, len)
  667. X    CONST char        * file;
  668. X    int              line;
  669. X    MEMDATA         * ptr1;
  670. X    CONST MEMDATA         * ptr2;
  671. X    register MEMSIZE       len;
  672. X{
  673. X    return( DBFmemcpy( "memcpy", file, line ,ptr1, ptr2, len) );
  674. X}
  675. X
  676. XMEMDATA  *
  677. XDBFmemcpy(func, file, line,ptr1, ptr2, len)
  678. X    CONST char        * func;
  679. X    CONST char        * file;
  680. X    int              line;
  681. X    MEMDATA         * ptr1;
  682. X    CONST MEMDATA         * ptr2;
  683. X    register MEMSIZE       len;
  684. X{
  685. X    MEMDATA         * rtn = ptr1;
  686. X
  687. X    malloc_check_data(func, file, line, ptr1, len);
  688. X    malloc_check_data(func, file, line, ptr2, len);
  689. X
  690. X    DataMC(ptr1,ptr2,len);
  691. X    
  692. X    return(rtn);
  693. X}
  694. X
  695. X/*
  696. X * memcmp - compare two memory regions
  697. X */
  698. Xint
  699. Xmemcmp(ptr1, ptr2, len)
  700. X    CONST MEMDATA         * ptr1;
  701. X    CONST MEMDATA         * ptr2;
  702. X    register MEMSIZE       len;
  703. X{
  704. X    return( DBmemcmp((char *)NULL,0,ptr1,ptr2,len) );
  705. X}
  706. X
  707. Xint
  708. XDBmemcmp(file,line,ptr1, ptr2, len)
  709. X    CONST char        * file;
  710. X    int              line;
  711. X    CONST MEMDATA         * ptr1;
  712. X    CONST MEMDATA         * ptr2;
  713. X    register MEMSIZE       len;
  714. X{
  715. X    return( DBFmemcmp("memcmp",file,line,ptr1,ptr2,len) );
  716. X}
  717. X
  718. Xint
  719. XDBFmemcmp(func,file,line,ptr1, ptr2, len)
  720. X    CONST char        * func;
  721. X    CONST char        * file;
  722. X    int              line;
  723. X    CONST MEMDATA         * ptr1;
  724. X    CONST MEMDATA         * ptr2;
  725. X    register MEMSIZE       len;
  726. X{
  727. X    register CONST char    * myptr1;
  728. X    register CONST char    * myptr2;
  729. X    
  730. X    malloc_check_data(func,file,line, ptr1, len);
  731. X    malloc_check_data(func,file,line, ptr2, len);
  732. X
  733. X    myptr1 = (CONST char *) ptr1;
  734. X    myptr2 = (CONST char *) ptr2;
  735. X
  736. X    while( len > 0  && (*myptr1 == *myptr2) )
  737. X    {
  738. X        len--;
  739. X        myptr1++;
  740. X        myptr2++;
  741. X    }
  742. X
  743. X    /* 
  744. X     * If stopped by len, return zero
  745. X     */
  746. X    if( len == 0 )
  747. X    {
  748. X        return(0);
  749. X    }
  750. X
  751. X    return( *(CONST MEMCMPTYPE *)myptr1 - *(CONST MEMCMPTYPE *)myptr2 );
  752. X}
  753. X
  754. X/*
  755. X * memset - set all bytes of a memory block to a specified value
  756. X */
  757. XMEMDATA  * 
  758. Xmemset(ptr1, ch, len)
  759. X    MEMDATA         * ptr1;
  760. X    register int          ch;
  761. X    register MEMSIZE       len;
  762. X{
  763. X    return( DBmemset((char *)NULL,0,ptr1,ch,len) );
  764. X}
  765. X
  766. XMEMDATA  * 
  767. XDBmemset(file,line,ptr1, ch, len)
  768. X    CONST char        * file;
  769. X    int              line;
  770. X    MEMDATA         * ptr1;
  771. X    register int          ch;
  772. X    register MEMSIZE       len;
  773. X{
  774. X    return( DBFmemset("memset",file,line,ptr1,ch,len) );
  775. X}
  776. X
  777. XMEMDATA  * 
  778. XDBFmemset(func,file,line,ptr1, ch, len)
  779. X    CONST char        * func;
  780. X    CONST char        * file;
  781. X    int              line;
  782. X    MEMDATA         * ptr1;
  783. X    register int          ch;
  784. X    register MEMSIZE       len;
  785. X{
  786. X    MEMDATA         * rtn = ptr1;
  787. X    malloc_check_data(func, file, line, ptr1, len);
  788. X
  789. X    DataMS(ptr1,ch,len);
  790. X
  791. X    return(rtn);
  792. X}
  793. X
  794. X#ifndef ibm032
  795. X/*
  796. X * bcopy - copy memory block to another area
  797. X */
  798. XMEMDATA  *
  799. Xbcopy(ptr2,ptr1,len)
  800. X    CONST MEMDATA     * ptr2;
  801. X    MEMDATA     * ptr1;
  802. X    MEMSIZE       len;
  803. X{
  804. X    return( DBbcopy((char *)NULL,0,ptr2,ptr1,len) );
  805. X}
  806. X#endif /* ibm032 */
  807. X
  808. XMEMDATA  *
  809. XDBbcopy(file,line,ptr2,ptr1,len)
  810. X    CONST char    * file;
  811. X    int          line;
  812. X    CONST MEMDATA     * ptr2;
  813. X    MEMDATA     * ptr1;
  814. X    MEMSIZE       len;
  815. X{
  816. X    return( DBFmemcpy("bcopy",file,line,ptr1,ptr2,len));
  817. X}
  818. X
  819. X/*
  820. X * bzero - clear block of memory to zeros
  821. X */
  822. XMEMDATA  *
  823. Xbzero(ptr1,len)
  824. X    MEMDATA     * ptr1;
  825. X    MEMSIZE       len;
  826. X{
  827. X    return( DBbzero((char *)NULL,0,ptr1,len) );
  828. X}
  829. X
  830. XMEMDATA  *
  831. XDBbzero(file,line,ptr1,len)
  832. X    CONST char    * file;
  833. X    int          line;
  834. X    MEMDATA     * ptr1;
  835. X    MEMSIZE       len;
  836. X{
  837. X    return( DBFmemset("bzero",file,line,ptr1,'\0',len) );
  838. X}
  839. X
  840. X/*
  841. X * bcmp - compary memory blocks
  842. X */
  843. Xint
  844. Xbcmp(ptr2, ptr1, len)
  845. X    CONST MEMDATA     * ptr1;
  846. X    CONST MEMDATA     * ptr2;
  847. X    MEMSIZE       len;
  848. X{
  849. X    return( DBbcmp((char *)NULL,0,ptr2, ptr1, len) );
  850. X}
  851. X
  852. Xint
  853. XDBbcmp(file, line, ptr2, ptr1, len)
  854. X    CONST char    * file;
  855. X    int          line;
  856. X    CONST MEMDATA     * ptr1;
  857. X    CONST MEMDATA     * ptr2;
  858. X    MEMSIZE        len;
  859. X{
  860. X    return( DBFmemcmp("bcmp",file,line,ptr1,ptr2,len) );
  861. X}
  862. X
  863. X/*
  864. X * $Log: memory.c,v $
  865. X * Revision 1.22  1992/08/22  16:27:13  cpcahil
  866. X * final changes for pl14
  867. X *
  868. X * Revision 1.21  1992/07/12  15:30:58  cpcahil
  869. X * Merged in Jonathan I Kamens' changes
  870. X *
  871. X * Revision 1.20  1992/06/22  23:40:10  cpcahil
  872. X * many fixes for working on small int systems
  873. X *
  874. X * Revision 1.19  1992/05/09  21:27:09  cpcahil
  875. X * final (hopefully) changes for patch 11
  876. X *
  877. X * Revision 1.18  1992/05/09  00:16:16  cpcahil
  878. X * port to hpux and lots of fixes
  879. X *
  880. X * Revision 1.17  1992/05/08  02:30:35  cpcahil
  881. X * minor cleanups from minix/atari port
  882. X *
  883. X * Revision 1.16  1992/05/08  01:44:11  cpcahil
  884. X * more performance enhancements
  885. X *
  886. X * Revision 1.15  1992/04/13  03:06:33  cpcahil
  887. X * Added Stack support, marking of non-leaks, auto-config, auto-testing
  888. X *
  889. X * Revision 1.14  1992/01/30  12:23:06  cpcahil
  890. X * renamed mallocint.h -> mallocin.h
  891. X *
  892. X * Revision 1.13  1992/01/24  04:49:05  cpcahil
  893. X * changed memccpy to only check number of chars it will copy.
  894. X *
  895. X * Revision 1.12  1991/12/31  21:31:26  cpcahil
  896. X * changes for patch 6.  See CHANGES file for more info
  897. X *
  898. X * Revision 1.11  1991/12/02  19:10:13  cpcahil
  899. X * changes for patch release 5
  900. X *
  901. X * Revision 1.10  91/11/25  14:42:03  cpcahil
  902. X * Final changes in preparation for patch 4 release
  903. X * 
  904. X * Revision 1.9  91/11/24  00:49:31  cpcahil
  905. X * first cut at patch 4
  906. X * 
  907. X * Revision 1.8  91/05/21  18:33:47  cpcahil
  908. X * fixed bug in memccpy() which checked an extra byte if the first character
  909. X * after the specified length matched the search character.
  910. X * 
  911. X * Revision 1.7  90/08/29  21:27:58  cpcahil
  912. X * fixed value of check in memccpy when character was not found.
  913. X * 
  914. X * Revision 1.6  90/07/16  20:06:26  cpcahil
  915. X * fixed several minor bugs found with Henry Spencer's string/mem tester 
  916. X * program.
  917. X * 
  918. X * 
  919. X * Revision 1.5  90/05/11  15:39:36  cpcahil
  920. X * fixed bug in memccpy().
  921. X * 
  922. X * Revision 1.4  90/05/11  00:13:10  cpcahil
  923. X * added copyright statment
  924. X * 
  925. X * Revision 1.3  90/02/24  21:50:29  cpcahil
  926. X * lots of lint fixes
  927. X * 
  928. X * Revision 1.2  90/02/24  17:29:41  cpcahil
  929. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  930. X * id string
  931. X * 
  932. X * Revision 1.1  90/02/22  23:17:43  cpcahil
  933. X * Initial revision
  934. X * 
  935. X */
  936. END_OF_FILE
  937. if test 8847 -ne `wc -c <'memory.c'`; then
  938.     echo shar: \"'memory.c'\" unpacked with wrong size!
  939. fi
  940. # end of 'memory.c'
  941. fi
  942. if test -f 'prototypes.h' -a "${1}" != "-c" ; then 
  943.   echo shar: Will not clobber existing file \"'prototypes.h'\"
  944. else
  945. echo shar: Extracting \"'prototypes.h'\" \(13010 characters\)
  946. sed "s/^X//" >'prototypes.h' <<'END_OF_FILE'
  947. X#if defined(__STDC__) || defined(__cplusplus)
  948. X# define __stdcargs(s) s
  949. X#else
  950. X# define __stdcargs(s) ()
  951. X#endif
  952. X
  953. X/* malloc.c */
  954. XDATATYPE *malloc __stdcargs((SIZETYPE size));
  955. XDATATYPE *debug_malloc __stdcargs((CONST char *file, int line, SIZETYPE size));
  956. Xchar *DBFmalloc __stdcargs((CONST char *func, int type, unsigned long call_counter, CONST char *file, int line, SIZETYPE size));
  957. XVOIDTYPE malloc_split __stdcargs((struct mlist *ptr));
  958. XVOIDTYPE malloc_join __stdcargs((struct mlist *ptr, struct mlist *nextptr, int inuse_override, int fill_flag));
  959. XVOIDTYPE malloc_fatal __stdcargs((CONST char *funcname, CONST char *file, int line, CONST struct mlist *mptr));
  960. XVOIDTYPE malloc_warning __stdcargs((CONST char *funcname, CONST char *file, int line, CONST struct mlist *mptr));
  961. XVOIDTYPE malloc_dump_info_block __stdcargs((CONST struct mlist *mptr, int id));
  962. XVOIDTYPE malloc_err_handler __stdcargs((int level));
  963. XCONST char *malloc_int_suffix __stdcargs((unsigned long i));
  964. XVOIDTYPE malloc_freeseg __stdcargs((int op, struct mlist *ptr));
  965. XCONST char *MallocFuncName __stdcargs((CONST struct mlist *mptr));
  966. XCONST char *FreeFuncName __stdcargs((CONST struct mlist *mptr));
  967. Xvoid InitMlist __stdcargs((struct mlist *mptr, int type));
  968. X/* datamc.c */
  969. Xvoid DataMC __stdcargs((MEMDATA *ptr1, CONST MEMDATA *ptr2, MEMSIZE len));
  970. X/* datams.c */
  971. Xvoid DataMS __stdcargs((MEMDATA *ptr1, int ch, register MEMSIZE len));
  972. X/* dgmalloc.c */
  973. XDATATYPE *_malloc __stdcargs((SIZETYPE size));
  974. XDATATYPE *_realloc __stdcargs((DATATYPE *cptr, SIZETYPE size));
  975. XDATATYPE *_calloc __stdcargs((SIZETYPE nelem, SIZETYPE elsize));
  976. Xvoid _free __stdcargs((DATATYPE *cptr));
  977. Xint _mallopt __stdcargs((int cmd, union dbmalloptarg value));
  978. XMEMDATA *_bcopy __stdcargs((CONST MEMDATA *ptr2, MEMDATA *ptr1, MEMSIZE len));
  979. XMEMDATA *_bzero __stdcargs((MEMDATA *ptr1, MEMSIZE len));
  980. Xint _bcmp __stdcargs((CONST MEMDATA *ptr2, CONST MEMDATA *ptr1, MEMSIZE len));
  981. XMEMDATA *__dg_bcopy __stdcargs((CONST MEMDATA *ptr2, MEMDATA *ptr1, MEMSIZE len));
  982. XMEMDATA *__dg_bzero __stdcargs((MEMDATA *ptr1, MEMSIZE len));
  983. Xint __dg_bcmp __stdcargs((CONST MEMDATA *ptr2, CONST MEMDATA *ptr1, MEMSIZE len));
  984. X/* fill.c */
  985. XVOIDTYPE FillInit __stdcargs((void));
  986. Xint FillCheck __stdcargs((CONST char *func, CONST char *file, int line, struct mlist *ptr, int showerrors));
  987. Xint FillCheckData __stdcargs((CONST char *func, CONST char *file, int line, struct mlist *ptr, int checktype, int showerrors));
  988. Xvoid FillData __stdcargs((register struct mlist *ptr, int alloctype, SIZETYPE start, struct mlist *nextptr));
  989. X/* free.c */
  990. XFREETYPE free __stdcargs((DATATYPE *cptr));
  991. XFREETYPE debug_free __stdcargs((CONST char *file, int line, DATATYPE *cptr));
  992. XFREETYPE DBFfree __stdcargs((CONST char *func, int type, unsigned long counter, CONST char *file, int line, DATATYPE *cptr));
  993. X/* realloc.c */
  994. XDATATYPE *realloc __stdcargs((DATATYPE *cptr, SIZETYPE size));
  995. XDATATYPE *debug_realloc __stdcargs((CONST char *file, int line, DATATYPE *cptr, SIZETYPE size));
  996. XDATATYPE *DBFrealloc __stdcargs((CONST char *func, int type, unsigned long call_counter, CONST char *file, int line, DATATYPE *cptr, SIZETYPE size));
  997. X/* calloc.c */
  998. XDATATYPE *calloc __stdcargs((SIZETYPE nelem, SIZETYPE elsize));
  999. XDATATYPE *debug_calloc __stdcargs((CONST char *file, int line, SIZETYPE nelem, SIZETYPE elsize));
  1000. Xchar *DBFcalloc __stdcargs((CONST char *func, int type, unsigned long call_counter, CONST char *file, int line, SIZETYPE nelem, SIZETYPE elsize));
  1001. XFREETYPE cfree __stdcargs((DATATYPE *cptr));
  1002. XFREETYPE debug_cfree __stdcargs((CONST char *file, int line, DATATYPE *cptr));
  1003. X/* string.c */
  1004. Xchar *strcat __stdcargs((char *str1, CONST char *str2));
  1005. Xchar *DBstrcat __stdcargs((CONST char *file, int line, register char *str1, register CONST char *str2));
  1006. Xchar *strdup __stdcargs((CONST char *str1));
  1007. Xchar *DBstrdup __stdcargs((CONST char *file, int line, register CONST char *str1));
  1008. Xchar *strncat __stdcargs((char *str1, CONST char *str2, STRSIZE len));
  1009. Xchar *DBstrncat __stdcargs((CONST char *file, int line, register char *str1, register CONST char *str2, register STRSIZE len));
  1010. Xint strcmp __stdcargs((register CONST char *str1, register CONST char *str2));
  1011. Xint DBstrcmp __stdcargs((CONST char *file, int line, register CONST char *str1, register CONST char *str2));
  1012. Xint strncmp __stdcargs((register CONST char *str1, register CONST char *str2, register STRSIZE len));
  1013. Xint DBstrncmp __stdcargs((CONST char *file, int line, register CONST char *str1, register CONST char *str2, register STRSIZE len));
  1014. Xint stricmp __stdcargs((register CONST char *str1, register CONST char *str2));
  1015. Xint DBstricmp __stdcargs((CONST char *file, int line, register CONST char *str1, register CONST char *str2));
  1016. Xint strincmp __stdcargs((register CONST char *str1, register CONST char *str2, register STRSIZE len));
  1017. Xint DBstrincmp __stdcargs((CONST char *file, int line, register CONST char *str1, register CONST char *str2, register STRSIZE len));
  1018. Xchar *strcpy __stdcargs((register char *str1, register CONST char *str2));
  1019. Xchar *DBstrcpy __stdcargs((CONST char *file, int line, register char *str1, register CONST char *str2));
  1020. Xchar *strncpy __stdcargs((register char *str1, register CONST char *str2, register STRSIZE len));
  1021. Xchar *DBstrncpy __stdcargs((CONST char *file, int line, register char *str1, register CONST char *str2, STRSIZE len));
  1022. XSTRSIZE strlen __stdcargs((CONST char *str1));
  1023. XSTRSIZE DBstrlen __stdcargs((CONST char *file, int line, register CONST char *str1));
  1024. Xchar *strchr __stdcargs((CONST char *str1, int c));
  1025. Xchar *DBstrchr __stdcargs((CONST char *file, int line, CONST char *str1, int c));
  1026. Xchar *DBFstrchr __stdcargs((CONST char *func, CONST char *file, int line, register CONST char *str1, register int c));
  1027. Xchar *strrchr __stdcargs((CONST char *str1, int c));
  1028. Xchar *DBstrrchr __stdcargs((CONST char *file, int line, CONST char *str1, int c));
  1029. Xchar *DBFstrrchr __stdcargs((CONST char *func, CONST char *file, int line, register CONST char *str1, register int c));
  1030. Xchar *index __stdcargs((CONST char *str1, int c));
  1031. Xchar *DBindex __stdcargs((CONST char *file, int line, CONST char *str1, int c));
  1032. Xchar *rindex __stdcargs((CONST char *str1, int c));
  1033. Xchar *DBrindex __stdcargs((CONST char *file, int line, CONST char *str1, int c));
  1034. Xchar *strpbrk __stdcargs((CONST char *str1, CONST char *str2));
  1035. Xchar *DBstrpbrk __stdcargs((CONST char *file, int line, register CONST char *str1, register CONST char *str2));
  1036. XSTRSIZE strspn __stdcargs((CONST char *str1, CONST char *str2));
  1037. XSTRSIZE DBstrspn __stdcargs((CONST char *file, int line, register CONST char *str1, register CONST char *str2));
  1038. XSTRSIZE strcspn __stdcargs((CONST char *str1, CONST char *str2));
  1039. XSTRSIZE DBstrcspn __stdcargs((CONST char *file, int line, register CONST char *str1, register CONST char *str2));
  1040. Xchar *strstr __stdcargs((CONST char *str1, CONST char *str2));
  1041. Xchar *DBstrstr __stdcargs((CONST char *file, int line, CONST char *str1, CONST char *str2));
  1042. Xchar *strtok __stdcargs((char *str1, CONST char *str2));
  1043. Xchar *DBstrtok __stdcargs((CONST char *file, int line, char *str1, CONST char *str2));
  1044. Xchar *strtoken __stdcargs((register char **stringp, register CONST char *delim, int skip));
  1045. X/* mcheck.c */
  1046. XVOIDTYPE malloc_check_str __stdcargs((CONST char *func, CONST char *file, int line, CONST char *str));
  1047. XVOIDTYPE malloc_check_strn __stdcargs((CONST char *func, CONST char *file, int line, CONST char *str, STRSIZE len));
  1048. XVOIDTYPE malloc_check_data __stdcargs((CONST char *func, CONST char *file, int line, CONST DATATYPE *ptr, SIZETYPE len));
  1049. XVOIDTYPE malloc_verify __stdcargs((CONST char *func, CONST char *file, int line, register CONST DATATYPE *ptr, register SIZETYPE len));
  1050. Xint GoodMlist __stdcargs((register CONST struct mlist *mptr));
  1051. X/* mchain.c */
  1052. Xint malloc_chain_check __stdcargs((int todo));
  1053. Xint DBmalloc_chain_check __stdcargs((CONST char *file, int line, int todo));
  1054. Xint DBFmalloc_chain_check __stdcargs((CONST char *func, CONST char *file, int line, int todo));
  1055. X/* memory.c */
  1056. XMEMDATA *memccpy __stdcargs((MEMDATA *ptr1, CONST MEMDATA *ptr2, int ch, MEMSIZE len));
  1057. XMEMDATA *DBmemccpy __stdcargs((CONST char *file, int line, MEMDATA *ptr1, CONST MEMDATA *ptr2, int ch, MEMSIZE len));
  1058. XMEMDATA *memchr __stdcargs((CONST MEMDATA *ptr1, register int ch, MEMSIZE len));
  1059. XMEMDATA *DBmemchr __stdcargs((CONST char *file, int line, CONST MEMDATA *ptr1, register int ch, MEMSIZE len));
  1060. XMEMDATA *memmove __stdcargs((MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  1061. XMEMDATA *DBmemmove __stdcargs((CONST char *file, int line, MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  1062. XMEMDATA *memcpy __stdcargs((MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  1063. XMEMDATA *DBmemcpy __stdcargs((CONST char *file, int line, MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  1064. XMEMDATA *DBFmemcpy __stdcargs((CONST char *func, CONST char *file, int line, MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  1065. Xint memcmp __stdcargs((CONST MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  1066. Xint DBmemcmp __stdcargs((CONST char *file, int line, CONST MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  1067. Xint DBFmemcmp __stdcargs((CONST char *func, CONST char *file, int line, CONST MEMDATA *ptr1, CONST MEMDATA *ptr2, register MEMSIZE len));
  1068. XMEMDATA *memset __stdcargs((MEMDATA *ptr1, register int ch, register MEMSIZE len));
  1069. XMEMDATA *DBmemset __stdcargs((CONST char *file, int line, MEMDATA *ptr1, register int ch, register MEMSIZE len));
  1070. XMEMDATA *DBFmemset __stdcargs((CONST char *func, CONST char *file, int line, MEMDATA *ptr1, register int ch, register MEMSIZE len));
  1071. XMEMDATA *bcopy __stdcargs((CONST MEMDATA *ptr2, MEMDATA *ptr1, MEMSIZE len));
  1072. XMEMDATA *DBbcopy __stdcargs((CONST char *file, int line, CONST MEMDATA *ptr2, MEMDATA *ptr1, MEMSIZE len));
  1073. XMEMDATA *bzero __stdcargs((MEMDATA *ptr1, MEMSIZE len));
  1074. XMEMDATA *DBbzero __stdcargs((CONST char *file, int line, MEMDATA *ptr1, MEMSIZE len));
  1075. Xint bcmp __stdcargs((CONST MEMDATA *ptr2, CONST MEMDATA *ptr1, MEMSIZE len));
  1076. Xint DBbcmp __stdcargs((CONST char *file, int line, CONST MEMDATA *ptr2, CONST MEMDATA *ptr1, MEMSIZE len));
  1077. X/* tostring.c */
  1078. Xint tostring __stdcargs((char *buf, unsigned long val, int len, int base, int fill));
  1079. X/* m_perror.c */
  1080. XVOIDTYPE malloc_perror __stdcargs((CONST char *str));
  1081. X/* m_init.c */
  1082. XVOIDTYPE malloc_init __stdcargs((void));
  1083. X/* mallopt.c */
  1084. Xint dbmallopt __stdcargs((int cmd, union dbmalloptarg *value));
  1085. X/* dump.c */
  1086. XVOIDTYPE malloc_dump __stdcargs((int fd));
  1087. XVOIDTYPE malloc_list __stdcargs((int fd, unsigned long histid1, unsigned long histid2));
  1088. XVOIDTYPE malloc_list_items __stdcargs((int fd, int list_type, unsigned long histid1, unsigned long histid2));
  1089. X/* stack.c */
  1090. Xvoid StackEnter __stdcargs((CONST char *func, CONST char *file, int line));
  1091. Xstruct stack *StackNew __stdcargs((CONST char *func, CONST char *file, int line));
  1092. Xint StackMatch __stdcargs((struct stack *this, CONST char *func, CONST char *file, int line));
  1093. Xvoid StackLeave __stdcargs((CONST char *func, CONST char *file, int line));
  1094. Xstruct stack *StackCurrent __stdcargs((void));
  1095. Xvoid StackDump __stdcargs((int fd, CONST char *msg, struct stack *node));
  1096. X/* xmalloc.c */
  1097. Xvoid _XtAllocError __stdcargs((CONST char *type));
  1098. Xvoid _XtBCopy __stdcargs((char *b1, char *b2, int length));
  1099. Xvoid debug_XtBcopy __stdcargs((char *file, int line, char *b1, char *b2, int length));
  1100. Xchar *XtMalloc __stdcargs((unsigned int size));
  1101. Xchar *debug_XtMalloc __stdcargs((CONST char *file, int line, unsigned int size));
  1102. Xchar *XtRealloc __stdcargs((char *ptr, unsigned int size));
  1103. Xchar *debug_XtRealloc __stdcargs((CONST char *file, int line, char *ptr, unsigned int size));
  1104. Xchar *XtCalloc __stdcargs((unsigned int num, unsigned int size));
  1105. Xchar *debug_XtCalloc __stdcargs((CONST char *file, int line, unsigned int num, unsigned int size));
  1106. Xvoid XtFree __stdcargs((char *ptr));
  1107. Xvoid debug_XtFree __stdcargs((CONST char *file, int line, char *ptr));
  1108. X/* xheap.c */
  1109. Xvoid _XtHeapInit __stdcargs((Heap *heap));
  1110. Xchar *_XtHeapAlloc __stdcargs((Heap *heap, Cardinal bytes));
  1111. Xvoid _XtHeapFree __stdcargs((Heap *heap));
  1112. X/* malign.c */
  1113. XDATATYPE *memalign __stdcargs((SIZETYPE align, SIZETYPE size));
  1114. XDATATYPE *DBmemalign __stdcargs((CONST char *file, int line, SIZETYPE align, SIZETYPE size));
  1115. Xint AlignedFit __stdcargs((struct mlist *mptr, SIZETYPE align, SIZETYPE size));
  1116. Xstruct mlist *AlignedMakeSeg __stdcargs((struct mlist *mptr, SIZETYPE align));
  1117. XSIZETYPE AlignedOffset __stdcargs((struct mlist *mptr, SIZETYPE align));
  1118. X/* size.c */
  1119. XSIZETYPE malloc_size __stdcargs((CONST DATATYPE *cptr));
  1120. XSIZETYPE DBmalloc_size __stdcargs((CONST char *file, int line, CONST DATATYPE *cptr));
  1121. X/* abort.c */
  1122. XVOIDTYPE malloc_abort __stdcargs((void));
  1123. X/* leak.c */
  1124. Xunsigned long malloc_inuse __stdcargs((unsigned long *histptr));
  1125. Xunsigned long DBmalloc_inuse __stdcargs((CONST char *file, int line, unsigned long *histptr));
  1126. XVOIDTYPE malloc_mark __stdcargs((DATATYPE *cptr));
  1127. XVOIDTYPE DBmalloc_mark __stdcargs((CONST char *file, int line, DATATYPE *cptr));
  1128. X
  1129. X#undef __stdcargs
  1130. END_OF_FILE
  1131. if test 13010 -ne `wc -c <'prototypes.h'`; then
  1132.     echo shar: \"'prototypes.h'\" unpacked with wrong size!
  1133. fi
  1134. # end of 'prototypes.h'
  1135. fi
  1136. if test -f 'realloc.c' -a "${1}" != "-c" ; then 
  1137.   echo shar: Will not clobber existing file \"'realloc.c'\"
  1138. else
  1139. echo shar: Extracting \"'realloc.c'\" \(11360 characters\)
  1140. sed "s/^X//" >'realloc.c' <<'END_OF_FILE'
  1141. X/*
  1142. X * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  1143. X *
  1144. X * This software may be distributed freely as long as the following conditions
  1145. X * are met:
  1146. X *         * the distribution, or any derivative thereof, may not be
  1147. X *          included as part of a commercial product
  1148. X *        * full source code is provided including this copyright
  1149. X *        * there is no charge for the software itself (there may be
  1150. X *          a minimal charge for the copying or distribution effort)
  1151. X *        * this copyright notice is not modified or removed from any
  1152. X *          source file
  1153. X */
  1154. X
  1155. X#ifndef lint
  1156. Xstatic
  1157. Xchar rcs_hdr[] = "$Id: realloc.c,v 1.26 1992/09/03 22:24:33 cpcahil Exp $";
  1158. X#endif
  1159. X
  1160. X#include <stdio.h>
  1161. X
  1162. X#include "mallocin.h"
  1163. X
  1164. XDATATYPE *
  1165. Xrealloc(cptr,size)
  1166. X    DATATYPE        * cptr;
  1167. X    SIZETYPE          size;
  1168. X{
  1169. X    return( debug_realloc(NULL,-1,cptr,size) );
  1170. X}
  1171. X
  1172. XDATATYPE *
  1173. Xdebug_realloc(file,line,cptr,size)
  1174. X    CONST char        * file;
  1175. X    int              line;
  1176. X    DATATYPE        * cptr;
  1177. X    SIZETYPE          size;
  1178. X{
  1179. X    static IDTYPE          call_counter;
  1180. X
  1181. X    /*
  1182. X     * increment the call counter
  1183. X     */
  1184. X    call_counter++;
  1185. X
  1186. X    return( DBFrealloc("realloc",M_T_REALLOC,call_counter,
  1187. X              file,line,cptr,size) );
  1188. X
  1189. X}
  1190. X
  1191. X/*
  1192. X * Function:    DBFrealloc()
  1193. X *
  1194. X * Purpose:    to re-allocate a data area.
  1195. X *
  1196. X * Arguments:    cptr    - pointer to area to reallocate
  1197. X *        size    - size to change area to
  1198. X *
  1199. X * Returns:    pointer to new area (may be same area)
  1200. X *
  1201. X * Narrative:    verify pointer is within malloc region
  1202. X *        obtain mlist pointer from cptr
  1203. X *        verify magic number is correct
  1204. X *        verify inuse flag is set
  1205. X *        verify connection to adjoining segments is correct
  1206. X *        save requested size
  1207. X *        round-up size to appropriate boundry
  1208. X *        IF size is bigger than what is in this segment
  1209. X *            try to join next segment to this segment
  1210. X *        IF size is less than what is is this segment
  1211. X *            determine leftover amount of space
  1212. X *        ELSE
  1213. X *            allocate new segment of size bites
  1214. X *            IF allocation failed
  1215. X *                return NULL
  1216. X *            copy previous data to new segment
  1217. X *            free previous segment
  1218. X *            return new pointer
  1219. X *        split of extra space in this segment (if any)
  1220. X *        clear bytes beyound what they had before
  1221. X *        return pointer to data 
  1222. X */
  1223. X
  1224. XDATATYPE *
  1225. XDBFrealloc(func,type,call_counter,file,line,cptr,size)
  1226. X    CONST char        * func;
  1227. X    int              type;
  1228. X    IDTYPE              call_counter;
  1229. X    CONST char        * file;
  1230. X    int              line;
  1231. X    DATATYPE        * cptr;
  1232. X    SIZETYPE          size;
  1233. X{
  1234. X    SIZETYPE          i;
  1235. X    char            * new_cptr;
  1236. X    int              marked;
  1237. X    SIZETYPE          need;
  1238. X    struct mlist        * optr;
  1239. X    struct mlist        * ptr;
  1240. X    SIZETYPE          r_size;
  1241. X    SIZETYPE          start;
  1242. X
  1243. X    MALLOC_INIT();
  1244. X
  1245. X    /*
  1246. X     * IF malloc chain checking is on, go do it.
  1247. X     */
  1248. X    if( malloc_opts & MOPT_CKCHAIN )
  1249. X    {
  1250. X        VOIDCAST DBFmalloc_chain_check(func,file,line,1);
  1251. X    }
  1252. X
  1253. X    /*
  1254. X     * if the user wants to be warned about zero length mallocs, do so
  1255. X     */
  1256. X    if( ((malloc_opts & MOPT_ZERO) != 0) && (size == 0) )
  1257. X    {
  1258. X        malloc_errno = M_CODE_ZERO_ALLOC;
  1259. X        malloc_warning(func,file,line,(struct mlist *)NULL);
  1260. X    }
  1261. X
  1262. X    /*
  1263. X     * if this is an ansi-c compiler and we want to use the realloc(0) 
  1264. X     * paradigm, or if this is a call from xtrealloc, then if the 
  1265. X     * pointer is a null, act as if this is a call to malloc.
  1266. X     */
  1267. X#if defined(ANSI_NULLS) || (__STDC__ && ! defined(NO_ANSI_NULLS))
  1268. X    if( cptr == NULL )
  1269. X#else
  1270. X    if( (cptr == NULL) && (type == M_T_XTREALLOC) )
  1271. X#endif
  1272. X    {
  1273. X        /*
  1274. X         * allocate the new chunk
  1275. X         */
  1276. X        new_cptr = DBFmalloc(func,type,call_counter,file,line,size);
  1277. X
  1278. X        return(new_cptr);
  1279. X    }
  1280. X
  1281. X    /*
  1282. X     * verify that cptr is within the malloc region...
  1283. X     */
  1284. X    if(    (cptr < malloc_data_start)
  1285. X        || (cptr > malloc_data_end) 
  1286. X        || ((((long)cptr) & malloc_round) != 0 ) )
  1287. X    {
  1288. X        malloc_errno = M_CODE_BAD_PTR;
  1289. X        malloc_warning(func,file,line,(struct mlist *)NULL);
  1290. X        return (NULL);
  1291. X    }
  1292. X
  1293. X    /* 
  1294. X     * convert pointer to mlist struct pointer.  To do this we must 
  1295. X     * move the pointer backwards the correct number of bytes...
  1296. X     */
  1297. X    
  1298. X    ptr = (struct mlist *) (((char *)cptr) - M_SIZE);
  1299. X    
  1300. X    if( (ptr->flag&M_MAGIC_BITS) != M_MAGIC )
  1301. X    {
  1302. X        malloc_errno = M_CODE_BAD_MAGIC;
  1303. X        malloc_warning(func,file,line,(struct mlist *)NULL);
  1304. X        return(NULL);
  1305. X    }
  1306. X
  1307. X    if( ! (ptr->flag & M_INUSE) )
  1308. X    {
  1309. X        malloc_errno = M_CODE_NOT_INUSE ;
  1310. X        malloc_warning(func,file,line,ptr);
  1311. X        return(NULL);
  1312. X    }
  1313. X
  1314. X     if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  1315. X        (ptr->next && (ptr->next->prev != ptr) ) ||
  1316. X        ((ptr->next == NULL) && (ptr->prev == NULL)) )
  1317. X    {
  1318. X        malloc_errno = M_CODE_BAD_CONNECT;
  1319. X        malloc_warning(func,file,line,ptr);
  1320. X        return(NULL);
  1321. X    }
  1322. X
  1323. X    /*
  1324. X     * save the marked status
  1325. X     */
  1326. X    marked = ptr->flag & M_MARKED;
  1327. X
  1328. X    /*
  1329. X     * save the requested size;
  1330. X     */
  1331. X    r_size = size;
  1332. X
  1333. X    /*
  1334. X     * make sure we have the full boundary that is needed
  1335. X     */
  1336. X    size += malloc_boundsize;
  1337. X
  1338. X    M_ROUNDUP(size);
  1339. X
  1340. X    if( (size > ptr->s.size) && ((malloc_opts & MOPT_REUSE) != 0) )
  1341. X    {
  1342. X        malloc_join(ptr,ptr->next,INUSEOK,DOFILL);
  1343. X    }
  1344. X
  1345. X    /*
  1346. X     * if we still don't have enough room, and we are at the end of the
  1347. X     * malloc chain and we are up against the current sbrk, we can just
  1348. X      * sbrk more room for our data area.
  1349. X      */    
  1350. X    if(    (size > ptr->s.size)
  1351. X        && (ptr == malloc_end) 
  1352. X        && ((ptr->data+ptr->s.size) == sbrk(0) ) )
  1353. X    {
  1354. X        need = size - ptr->s.size;
  1355. X
  1356. X        /*
  1357. X         * if the need is less than the minimum block size,
  1358. X         *     get the minimum block size
  1359. X          */
  1360. X        if( need < M_BLOCKSIZE )
  1361. X        {
  1362. X            need = M_BLOCKSIZE;
  1363. X        }
  1364. X        /*
  1365. X         * else if the need is not an even multiple of the block size,
  1366. X         *     round it up to an even multiple
  1367. X         */
  1368. X        else if( need & (M_BLOCKSIZE-1) )
  1369. X        {
  1370. X            need &= ~(M_BLOCKSIZE-1);
  1371. X            need += M_BLOCKSIZE;
  1372. X        }
  1373. X
  1374. X        /*
  1375. X         * get the space from the os
  1376. X          */
  1377. X        cptr = sbrk(need);
  1378. X
  1379. X        /*
  1380. X         * if we failed to get the space, tell the user about it
  1381. X         */
  1382. X        if( cptr == (char *) -1 )
  1383. X        {
  1384. X            malloc_errno = M_CODE_NOMORE_MEM;
  1385. X            malloc_fatal(func,file,line, (struct mlist *)NULL);
  1386. X            return(NULL);
  1387. X        }
  1388. X
  1389. X        /*
  1390. X         * adjust our segment size (extra space will be split off later
  1391. X         */
  1392. X        start = ptr->s.size;
  1393. X        ptr->s.size += need;
  1394. X
  1395. X        /*
  1396. X         * we have to act like this was a join of a new segment so
  1397. X         * we need to call the fill routine to get it to fill the
  1398. X         * new data area.
  1399. X         */
  1400. X        FILLDATA(ptr,FILL_JOIN,start,(struct mlist *)NULL);
  1401. X
  1402. X        /*
  1403. X         * mark our end point
  1404. X         */
  1405. X        malloc_data_end = sbrk((int)0);
  1406. X    
  1407. X    }
  1408. X    
  1409. X        
  1410. X    /*
  1411. X     * if the size is still too small and the previous segment is free
  1412. X     * and it would be big enough it if was joined to the current segment
  1413. X     */
  1414. X    if(    ((malloc_opts & MOPT_CKCHAIN) != 0)
  1415. X        && (size > ptr->s.size)
  1416. X        && ((ptr->prev->flag & M_INUSE) == 0)
  1417. X        && (size < (ptr->s.size + ptr->prev->s.size + M_SIZE) ) )
  1418. X    {
  1419. X        /*
  1420. X         * save the old pointer
  1421. X         */
  1422. X        optr = ptr;
  1423. X
  1424. X        /*
  1425. X         * move out pointer to the proper area.
  1426. X         */
  1427. X        ptr = ptr->prev;
  1428. X    
  1429. X        /*
  1430. X          * force this pointer to be inuse
  1431. X         */
  1432. X        ptr->flag |= M_INUSE;
  1433. X        ptr->r_size = ptr->next->r_size;
  1434. X        
  1435. X        /*
  1436. X          * join the two segments
  1437. X         */
  1438. X        malloc_join(ptr, ptr->next, ANY_INUSEOK, DONTFILL);
  1439. X
  1440. X        /*
  1441. X         * remove ptr from the free list
  1442. X         */
  1443. X        malloc_freeseg(M_FREE_REMOVE,ptr);
  1444. X
  1445. X        /*
  1446. X         * copy data from the current space to the new space.  Note
  1447. X          * that the data areas for this copy will likely overlap.
  1448. X         */
  1449. X        DataMC(ptr->data,optr->data,ptr->r_size);
  1450. X
  1451. X        /*
  1452. X         * note that we don't fill in the areas here.  It will be
  1453. X         * filled later
  1454. X         */
  1455. X
  1456. X    }
  1457. X    if( size > ptr->s.size )
  1458. X    {
  1459. X        /*
  1460. X         * else we can't combine it, so lets allocate a new chunk,
  1461. X         * copy the data and free the old chunk...
  1462. X         */
  1463. X        new_cptr = DBFmalloc(func,type,call_counter,file,line,size);
  1464. X
  1465. X        if( new_cptr == (char *) 0)
  1466. X        {
  1467. X            return(new_cptr);
  1468. X        }
  1469. X
  1470. X        if( r_size < ptr->r_size )
  1471. X        {
  1472. X            i = r_size;
  1473. X        }
  1474. X        else
  1475. X        {
  1476. X            i = ptr->r_size;
  1477. X        }
  1478. X        in_malloc_code++;
  1479. X        VOIDCAST memcpy(new_cptr,ptr->data,i);
  1480. X        in_malloc_code--;
  1481. X
  1482. X        /*
  1483. X         * if the old segment was marked, unmark it and mark the new
  1484. X         * segment.
  1485. X         */
  1486. X        if( marked )
  1487. X        {
  1488. X            ptr->flag &= ~M_MARKED;
  1489. X            malloc_mark(new_cptr);
  1490. X        }
  1491. X
  1492. X        /*
  1493. X         * free the old segment since it is no longer needed.
  1494. X         */
  1495. X        DBFfree("realloc:free",F_T_REALLOC,call_counter,file,line,cptr);
  1496. X
  1497. X        
  1498. X        return(new_cptr);
  1499. X
  1500. X    } /* if( size... */
  1501. X
  1502. X    /*
  1503. X     * save amount of real data in new segment (this will be used in the
  1504. X     * memset later) and then save requested size of this segment.
  1505. X     */
  1506. X    if( ptr->r_size < r_size )
  1507. X    {
  1508. X        i = ptr->r_size;
  1509. X    }
  1510. X    else
  1511. X    {
  1512. X        i = r_size;
  1513. X    }
  1514. X
  1515. X    ptr->r_size = r_size;
  1516. X
  1517. X    /*
  1518. X     * split off extra free space at end of this segment, if possible...
  1519. X     */
  1520. X
  1521. X    malloc_split(ptr);
  1522. X
  1523. X    /*
  1524. X     * save the id info.
  1525. X     */    
  1526. X    ptr->file      = file;
  1527. X    ptr->line      = line;
  1528. X    ptr->id        = call_counter;
  1529. X    ptr->hist_id   = malloc_hist_id++;
  1530. X    ptr->stack     = StackCurrent();
  1531. X    ptr->freestack = NULL;
  1532. X    SETTYPE(ptr,type);
  1533. X
  1534. X    /*
  1535. X     * fill data and/or boundary areas
  1536. X     */
  1537. X    FILLDATA(ptr,FILL_REALLOC,i, (struct mlist *) NULL);
  1538. X    
  1539. X    return(ptr->data);
  1540. X
  1541. X} /* DBFrealloc(... */
  1542. X
  1543. X
  1544. X/*
  1545. X * $Log: realloc.c,v $
  1546. X * Revision 1.26  1992/09/03  22:24:33  cpcahil
  1547. X * final changes for PL14
  1548. X *
  1549. X * Revision 1.25  1992/08/22  16:27:13  cpcahil
  1550. X * final changes for pl14
  1551. X *
  1552. X * Revision 1.24  1992/07/03  00:03:25  cpcahil
  1553. X * more fixes for pl13, several suggestons from Rich Salz.
  1554. X *
  1555. X * Revision 1.23  1992/05/14  23:02:27  cpcahil
  1556. X * added support for ANSI NULL behavior even with non-ansi compilers (if
  1557. X * chosen at compile time).
  1558. X *
  1559. X * Revision 1.22  1992/05/08  02:30:35  cpcahil
  1560. X * minor cleanups from minix/atari port
  1561. X *
  1562. X * Revision 1.21  1992/05/06  05:37:44  cpcahil
  1563. X * added overriding of fill characters and boundary size
  1564. X *
  1565. X * Revision 1.20  1992/05/06  04:53:29  cpcahil
  1566. X * performance enhancments
  1567. X *
  1568. X * Revision 1.19  1992/04/22  18:17:32  cpcahil
  1569. X * added support for Xt Alloc functions, linted code
  1570. X *
  1571. X * Revision 1.18  1992/04/13  03:06:33  cpcahil
  1572. X * Added Stack support, marking of non-leaks, auto-config, auto-testing
  1573. X *
  1574. X * Revision 1.17  1992/03/01  12:42:38  cpcahil
  1575. X * added support for managing freed areas and fixed doublword bndr problems
  1576. X *
  1577. X * Revision 1.16  1992/01/30  12:23:06  cpcahil
  1578. X * renamed mallocint.h -> mallocin.h
  1579. X *
  1580. X * Revision 1.15  1992/01/10  17:28:03  cpcahil
  1581. X * Added support for overriding void datatype
  1582. X *
  1583. X * Revision 1.14  1991/12/06  08:54:19  cpcahil
  1584. X * cleanup of __STDC__ usage and addition of CHANGES file
  1585. X *
  1586. X * Revision 1.13  91/12/04  09:23:44  cpcahil
  1587. X * several performance enhancements including addition of free list
  1588. X * 
  1589. X * Revision 1.12  91/12/02  19:10:14  cpcahil
  1590. X * changes for patch release 5
  1591. X * 
  1592. X * Revision 1.11  91/11/25  14:42:05  cpcahil
  1593. X * Final changes in preparation for patch 4 release
  1594. X * 
  1595. X * Revision 1.10  91/11/24  00:49:32  cpcahil
  1596. X * first cut at patch 4
  1597. X * 
  1598. X * Revision 1.9  91/11/20  11:54:11  cpcahil
  1599. X * interim checkin
  1600. X * 
  1601. X * Revision 1.8  90/08/29  21:22:52  cpcahil
  1602. X * miscellaneous lint fixes
  1603. X * 
  1604. X * Revision 1.7  90/05/11  00:13:10  cpcahil
  1605. X * added copyright statment
  1606. X * 
  1607. X * Revision 1.6  90/02/25  11:01:20  cpcahil
  1608. X * added support for malloc chain checking.
  1609. X * 
  1610. X * Revision 1.5  90/02/24  21:50:31  cpcahil
  1611. X * lots of lint fixes
  1612. X * 
  1613. X * Revision 1.4  90/02/24  17:29:39  cpcahil
  1614. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  1615. X * id string
  1616. X * 
  1617. X * Revision 1.3  90/02/24  17:20:00  cpcahil
  1618. X * attempt to get rid of full path in rcs header.
  1619. X * 
  1620. X * Revision 1.2  90/02/24  15:14:20  cpcahil
  1621. X * 1. added function header 
  1622. X * 2. changed calls to malloc_warning to conform to new usage
  1623. X * 3. added setting of malloc_errno
  1624. X *  4. broke up bad pointer determination so that errno's would be more
  1625. X *    descriptive
  1626. X * 
  1627. X * Revision 1.1  90/02/22  23:17:43  cpcahil
  1628. X * Initial revision
  1629. X * 
  1630. X */
  1631. END_OF_FILE
  1632. if test 11360 -ne `wc -c <'realloc.c'`; then
  1633.     echo shar: \"'realloc.c'\" unpacked with wrong size!
  1634. fi
  1635. # end of 'realloc.c'
  1636. fi
  1637. if test -f 'stack.c' -a "${1}" != "-c" ; then 
  1638.   echo shar: Will not clobber existing file \"'stack.c'\"
  1639. else
  1640. echo shar: Extracting \"'stack.c'\" \(7960 characters\)
  1641. sed "s/^X//" >'stack.c' <<'END_OF_FILE'
  1642. X
  1643. X/*
  1644. X * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  1645. X *
  1646. X * This software may be distributed freely as long as the following conditions
  1647. X * are met:
  1648. X *         * the distribution, or any derivative thereof, may not be
  1649. X *          included as part of a commercial product
  1650. X *        * full source code is provided including this copyright
  1651. X *        * there is no charge for the software itself (there may be
  1652. X *          a minimal charge for the copying or distribution effort)
  1653. X *        * this copyright notice is not modified or removed from any
  1654. X *          source file
  1655. X */
  1656. X#include <stdio.h>
  1657. X#include "mallocin.h"
  1658. X#include "tostring.h"
  1659. X
  1660. X#define STACK_ALLOC    100 /* allocate 100 elements at a time */
  1661. X#ifndef lint
  1662. Xstatic
  1663. Xchar rcs_hdr[] = "$Id: stack.c,v 1.7 1992/08/22 16:27:13 cpcahil Exp $";
  1664. X#endif
  1665. X
  1666. X#define OUTBUFSIZE    128
  1667. X#define ERRSTR "I/O error durring stack dump"
  1668. X
  1669. X#define WRITEOUT(fd,str,len)    if( write(fd,str,(WRTSIZE)(len)) != (len) ) \
  1670. X                { \
  1671. X                    VOIDCAST write(2,ERRSTR,\
  1672. X                             (WRTSIZE)strlen(ERRSTR));\
  1673. X                    exit(120); \
  1674. X                }
  1675. X
  1676. X#define COPY(s,t,buf,len)    while( (*(s) = *((t)++) ) \
  1677. X                       && ( (s) < ((buf)+(len)-5) ) ) { (s)++; }
  1678. X
  1679. X
  1680. X/*
  1681. X * stack.c - this file contains info used to maintain the malloc stack
  1682. X *         trees that the user may use to identify where malloc is called
  1683. X *         from.  Ideally, we would like to be able to read the stack
  1684. X *         ourselves, but that is very system dependent.
  1685. X *
  1686. X *     The user interface to the stack routines will be as follows:
  1687. X *
  1688. X *    #define malloc_enter(a)    StackEnter(a,__FILE__,__LINE__)
  1689. X *    #define malloc_leave(a) StackLeave(a,__FILE__,__LINE__)
  1690. X *
  1691. X *    NOTE: These functions depend upon the fact that they are called in a
  1692. X *    symentric manner.  If you skip one of them, you will not have valid
  1693. X *    information maintained in the stack.
  1694. X *
  1695. X *    Rather than keep a segment of the stack in each malloc region, we will
  1696. X *     maintain a stack tree here and the malloc segment will have a pointer 
  1697. X *    to the current leaf of the tree.  This should save considerably on the
  1698. X *    amount of space taken up by maintaining this tree.
  1699. X */
  1700. X
  1701. Xstruct stack    * current;
  1702. Xstruct stack       root_node;
  1703. X
  1704. X/*
  1705. X * local function prototpyes
  1706. X */
  1707. Xstruct stack    * StackNew __STDCARGS(( CONST char * func, CONST char * file,
  1708. X                    int line));
  1709. Xint          StackMatch __STDCARGS((struct stack * this,CONST char * func,
  1710. X                     CONST char * file, int line ));
  1711. X
  1712. X/*
  1713. X * stk_enter() - called when one enters a new function.  This function adds the 
  1714. X *          specified function as a new entry (unless it is already in the
  1715. X *          list, in which case it just sets the current pointer).
  1716. X */
  1717. X
  1718. Xvoid
  1719. XStackEnter( func, file, line )
  1720. X    CONST char    * func;
  1721. X    CONST char    * file;
  1722. X    int          line;
  1723. X{
  1724. X    int          match;
  1725. X    struct stack    * this;
  1726. X
  1727. X    /*
  1728. X     * if there are no current entries yet
  1729. X     */
  1730. X    if( current == NULL )
  1731. X    {
  1732. X        this = &root_node;
  1733. X    }
  1734. X    else
  1735. X    {
  1736. X        this = current;
  1737. X    }
  1738. X
  1739. X    /*
  1740. X     * if there are no entries below this func yet,
  1741. X     */
  1742. X    if( this->below == NULL )
  1743. X    {
  1744. X        this->below = StackNew(func,file,line);
  1745. X        this->below->above = this;
  1746. X        current = this->below;
  1747. X    }
  1748. X    else
  1749. X    {
  1750. X        /*
  1751. X         * drop down to the next level and look around for the
  1752. X         * specified function.
  1753. X         */
  1754. X        this = this->below;
  1755. X
  1756. X        /*
  1757. X         * scan across this level looking for a match
  1758. X         */
  1759. X        while(      ( ! (match=StackMatch(this,func,file,line)) )
  1760. X             && (this->beside != NULL) )
  1761. X        {
  1762. X            this = this->beside;
  1763. X        }
  1764. X
  1765. X        /*
  1766. X         * if we found the entry
  1767. X         */
  1768. X        if( match )
  1769. X        {
  1770. X            current = this;
  1771. X        }
  1772. X        else
  1773. X        {
  1774. X            current = this->beside = StackNew(func,file,line);
  1775. X            this->beside->above = this->above;
  1776. X        }
  1777. X    }
  1778. X
  1779. X} /* StackEnter(... */
  1780. X
  1781. X/*
  1782. X * StackNew() - allocate a new stack structure and fill in the default values.
  1783. X *        This is here as a function so that we can manage a list of
  1784. X *        entries and therefore don't have to call malloc too often.
  1785. X *
  1786. X * NOTE: this function does not link the current function to the tree.  That
  1787. X * must be done by the calling function.
  1788. X */
  1789. Xstruct stack *
  1790. XStackNew(func,file,line)
  1791. X    CONST char        * func;
  1792. X    CONST char        * file;
  1793. X    int              line;
  1794. X{
  1795. X    static SIZETYPE          alloccnt;
  1796. X    static SIZETYPE          cnt;
  1797. X    static struct stack    * data;
  1798. X    struct mlist        * mptr;
  1799. X    static struct stack    * this;
  1800. X    static int          call_counter;
  1801. X
  1802. X    /*
  1803. X     * if it is time to allocate more entries
  1804. X     */
  1805. X    if( cnt == alloccnt )
  1806. X    {
  1807. X        /*
  1808. X         * reset the counters
  1809. X         */
  1810. X        cnt = 0;
  1811. X        alloccnt = STACK_ALLOC;
  1812. X
  1813. X        /*
  1814. X         * go allocate the data
  1815. X         */
  1816. X        data = (struct stack *)malloc(
  1817. X            (SIZETYPE)(alloccnt*sizeof(*data)) );
  1818. X
  1819. X        /*
  1820. X         * if we failed to get the data, tell the user about it
  1821. X         */
  1822. X        if( data == NULL )
  1823. X        {
  1824. X            malloc_errno = M_CODE_NOMORE_MEM;
  1825. X            malloc_fatal("StackNew",file,line,(struct mlist *)NULL);
  1826. X        }
  1827. X
  1828. X        /*
  1829. X         * change the id information put in by malloc so that the 
  1830. X         * record appears as a stack record (and doesn't get confused
  1831. X         * with other memory leaks)
  1832. X         */
  1833. X        mptr = (struct mlist *) (((char *)data) - M_SIZE);
  1834. X        mptr->id = call_counter++;
  1835. X        SETTYPE(mptr,M_T_STACK);
  1836. X        
  1837. X    }
  1838. X
  1839. X    /*
  1840. X     * grab next element off of the list
  1841. X     */
  1842. X    this = data + cnt;
  1843. X
  1844. X    /*
  1845. X     * setup the new structure and attach it to the tree
  1846. X     */
  1847. X    this->above = this->below = this->beside = NULL;
  1848. X    this->func  = func;
  1849. X    this->file  = file;
  1850. X    this->line  = line;
  1851. X    
  1852. X    /*
  1853. X     * increment the count since we used yet another entry
  1854. X     */
  1855. X    cnt++;
  1856. X
  1857. X    /*
  1858. X     * return the pointer to the new entry
  1859. X     */
  1860. X    return(this);
  1861. X
  1862. X} /* StackNew(... */
  1863. X
  1864. X/*
  1865. X * StackMatch() - determine if the specified stack entry matches the specified
  1866. X *           set of func,file, and line.  We have to compare all three in
  1867. X *          order to ensure that we get the correct function even if
  1868. X *          there are two functions with the same name (or the caller
  1869. X *          specified the wrong name on the arguement list)
  1870. X */
  1871. Xint
  1872. XStackMatch(this,func,file,line)
  1873. X    struct stack    * this;
  1874. X    CONST char    * func;
  1875. X    CONST char    * file;
  1876. X    int          line;
  1877. X{
  1878. X
  1879. X    return(    (strcmp(this->func,func) == 0)
  1880. X        && (strcmp(this->file,file) == 0)
  1881. X        && (this->line == line )      ) ;
  1882. X
  1883. X} /* StackMatch(... */
  1884. X    
  1885. X
  1886. X/*
  1887. X * StackLeave() - leave the current stack level (called at the end of a
  1888. X *          function.
  1889. X */
  1890. Xvoid
  1891. XStackLeave( func, file, line )
  1892. X    CONST char    * func;
  1893. X    CONST char    * file;
  1894. X    int          line;
  1895. X{
  1896. X    if( current == NULL )
  1897. X    {
  1898. X        malloc_errno = M_CODE_STK_NOCUR;
  1899. X        malloc_fatal("stk_leave", file, line, (struct mlist *)NULL);
  1900. X    }
  1901. X    else if( strcmp(func,current->func) != 0 )
  1902. X    {
  1903. X        malloc_errno = M_CODE_STK_BADFUNC;
  1904. X        malloc_fatal("stk_leave", file, line, (struct mlist *)NULL);
  1905. X    }
  1906. X    else
  1907. X    {
  1908. X        current = current->above;
  1909. X    }
  1910. X    
  1911. X} /* StackLeave(... */
  1912. X
  1913. X/*
  1914. X * StackCurrent() - get the current stack pointer
  1915. X */
  1916. Xstruct stack *
  1917. XStackCurrent()
  1918. X{
  1919. X
  1920. X    return( current );
  1921. X
  1922. X} /* StackCurrent(... */
  1923. X
  1924. X/*
  1925. X * StackDump() - dump the stack from the specified node 
  1926. X */
  1927. Xvoid
  1928. XStackDump(fd, msg, node )
  1929. X    int          fd;
  1930. X    CONST char    * msg;
  1931. X    struct stack    * node;
  1932. X{
  1933. X    char          outbuf[OUTBUFSIZE];
  1934. X    char        * s;
  1935. X    CONST char    * t;
  1936. X
  1937. X    /*
  1938. X     * if there is nothing to show, just return
  1939. X     */
  1940. X    if( (node == NULL) || (node == &root_node) )
  1941. X    {
  1942. X        return;
  1943. X    }
  1944. X
  1945. X    /*
  1946. X     * if caller specified a message to print out, print it out
  1947. X     */
  1948. X    if( msg )
  1949. X    {
  1950. X        WRITEOUT(fd,msg,strlen(msg));
  1951. X    }
  1952. X
  1953. X    /*
  1954. X     * Ok, we have the info, so lets print it out
  1955. X     */
  1956. X    do
  1957. X    {
  1958. X        WRITEOUT(fd,"         -> ",12);
  1959. X
  1960. X        s = outbuf;    
  1961. X
  1962. X        /*
  1963. X         * perform some simple sanity checking on the node pointer
  1964. X         */
  1965. X        if(    (((DATATYPE *)node) < malloc_data_start)
  1966. X            || (((DATATYPE *)node) > malloc_data_end)
  1967. X            || ((((long)node) & 0x1) != 0) )
  1968. X        {
  1969. X            WRITEOUT(fd,"INVALID/BROKEN STACK CHAIN!!!\n",30);
  1970. X            break;
  1971. X        }
  1972. X        
  1973. X
  1974. X        /*
  1975. X         * build the string for this level
  1976. X         */
  1977. X        s = outbuf;    
  1978. X        t = node->func;
  1979. X        COPY(s,t,outbuf,OUTBUFSIZE);
  1980. X        t = "() in ";
  1981. X        COPY(s,t,outbuf,OUTBUFSIZE);
  1982. X        t = node->file;
  1983. X        COPY(s,t,outbuf,OUTBUFSIZE);
  1984. X        *s++ = '(';
  1985. X        s += tostring(s,(ULONG) node->line,0,10,' ');
  1986. X        *s++ = ')';
  1987. X        *s++ = '\n';
  1988. X
  1989. X        /*
  1990. X         * write out the string
  1991. X         */
  1992. X        WRITEOUT(fd,outbuf,s-outbuf);
  1993. X
  1994. X        /*
  1995. X          * move up one level in the stack
  1996. X         */
  1997. X        node = node->above;
  1998. X
  1999. X        /*
  2000. X         * until we get to the top of the tree
  2001. X         */
  2002. X    } while( (node != NULL) && (node != &root_node) );
  2003. X
  2004. X} /* StackDump(... */
  2005. X
  2006. X
  2007. X
  2008. END_OF_FILE
  2009. if test 7960 -ne `wc -c <'stack.c'`; then
  2010.     echo shar: \"'stack.c'\" unpacked with wrong size!
  2011. fi
  2012. # end of 'stack.c'
  2013. fi
  2014. echo shar: End of archive 8 \(of 10\).
  2015. cp /dev/null ark8isdone
  2016. MISSING=""
  2017. for I in 1 2 3 4 5 6 7 8 9 10 ; do
  2018.     if test ! -f ark${I}isdone ; then
  2019.     MISSING="${MISSING} ${I}"
  2020.     fi
  2021. done
  2022. if test "${MISSING}" = "" ; then
  2023.     echo You have unpacked all 10 archives.
  2024.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2025. else
  2026.     echo You still need to unpack the following archives:
  2027.     echo "        " ${MISSING}
  2028. fi
  2029. ##  End of shell archive.
  2030. exit 0
  2031. *** SENTINEL(tm) The ultimate Debugging Environment - email for more info ***
  2032.  
  2033. Conor P. Cahill              (703)430-9247            cpcahil@virtech.vti.com
  2034. Virtual Technologies, Inc.  46030 Manekin Plaza          Dulles, VA 21066 
  2035.  
  2036. exit 0 # Just in case...
  2037.