home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume32 / dbmalloc / part08 < prev    next >
Encoding:
Text File  |  1992-09-03  |  56.4 KB  |  2,031 lines

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