home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / reviewed / volume02 / malloclb / part04 < prev    next >
Encoding:
Internet Message Format  |  1992-03-31  |  55.5 KB

  1. From: Conor P. Cahill <cpcahil%virtech@uunet.UU.NET>
  2. Subject: v02i005: malloclib - Malloc Debugging Library, Part04/05
  3. Newsgroups: comp.sources.reviewed
  4. Approved: csr@calvin.dgbt.doc.ca
  5.  
  6. Submitted-by: Conor P. Cahill <cpcahil%virtech@uunet.UU.NET>
  7. Posting-number: Volume 2, Issue 5
  8. Archive-name: malloclib/part04
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 4 (of 5)."
  17. # Contents:  malloc.man memory.c prototypes.h realloc.c
  18. # Wrapped by cpcahil@virtech on Tue Jan 28 16:46:34 1992
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'malloc.man' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'malloc.man'\"
  22. else
  23. echo shar: Extracting \"'malloc.man'\" \(28631 characters\)
  24. sed "s/^X//" >'malloc.man' <<'END_OF_FILE'
  25. X
  26. X
  27. X
  28. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  29. X
  30. X
  31. X
  32. X     NAME
  33. X      malloc - debugging malloc library
  34. X
  35. X     SYNOPSIS
  36. X      #include <malloc.h>
  37. X
  38. X      char * calloc(nelem,elsize);
  39. X      unsigned nelem, elsize;
  40. X
  41. X      void cfree(ptr)
  42. X      char * ptr;
  43. X
  44. X      void free(ptr);
  45. X      char * ptr;
  46. X
  47. X      char * malloc(size);
  48. X      unsigned size;
  49. X
  50. X      int malloc_chain_check(flag);
  51. X      int flag;
  52. X
  53. X      void malloc_dump(fd);
  54. X      int fd;
  55. X
  56. X      void malloc_list(fd,histid1,histid2);
  57. X      int fd;
  58. X      unsigned long    histid1, histid2;
  59. X
  60. X      unsigned long    malloc_size(histidptr);
  61. X      unsigned long    * histidptr;
  62. X
  63. X      int mallopt(cmd,val);
  64. X      int cmd;
  65. X      union    malloptarg val;
  66. X
  67. X      char * realloc(ptr,size);
  68. X      char * ptr;
  69. X      unsigned size;
  70. X
  71. X     DESCRIPTION
  72. X      This malloc library is a replacement for the standard
  73. X      library to be    used during software development/debugging.
  74. X      See the standard malloc(3) pages for more information    on the
  75. X      use of the following functions:
  76. X
  77. X           calloc(), cfree(), free(), malloc(), realloc()
  78. X
  79. X      This library differs from the    standard malloc    library    in the
  80. X      following ways:
  81. X
  82. X      1. Each malloc segment contains a magic number so that free
  83. X      can verify that the pointer passed points to a valid malloc
  84. X
  85. X
  86. X
  87. X     Page 1                1.4
  88. X
  89. X
  90. X
  91. X
  92. X
  93. X
  94. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  95. X
  96. X
  97. X
  98. X      segment.
  99. X
  100. X      2. Each malloc segment is filled with    a non-zero pattern so
  101. X      that code that depends upon malloc segments being null will
  102. X      fail.
  103. X
  104. X      3. The size of each segment will be at least 1 byte larger
  105. X      than requested and the extra bytes will be filled with a
  106. X      non-zero pattern.  When free is called, it will verify that
  107. X      you did not go beyond    the number of bytes you    asked for.
  108. X
  109. X      4. When a segment is freed, it will be filled    with a
  110. X      different non-zero pattern to    ensure that the    program
  111. X      doesn't depend upon the use of already freed data.
  112. X
  113. X      5. Whenever any of the string    or memory functions (str*, b*,
  114. X      mem*)    are called with    a pointer that is within the malloc
  115. X      arena,  the operation    is checked to verify that it does not
  116. X      overrun the malloced segment.     A failure of this check is
  117. X      considered a "warning    level error" (described    later) and is
  118. X      handled accordingly.
  119. X
  120. X      6. Run time checking can include verification    of the malloc
  121. X      chain    at each    and every call to one of the malloc functions
  122. X      or manually by calling the malloc_chain_check    function.
  123. X
  124. X      When a problem is found, the following error message is
  125. X      displayed:
  126. X
  127. X           MALLOC Warning from funcname() (called from filename.c line ###):
  128. X           Warning message goes here
  129. X
  130. X      funcname is the name of the function that has    found the
  131. X      problem and will usually be an entry point into the library.
  132. X      The information that identifies where    the function is    called
  133. X      from will only be available if the source module was
  134. X      compiled with    the malloc.h file included.
  135. X
  136. X      If the error is caused by a problem in the malloc chain and
  137. X      the offending    chain element can be identified, the following
  138. X      information is also displayed    (NOTE: this is just a guess by
  139. X      the software,    it may not be the actual culprit):
  140. X
  141. X           This error is *probably*    associated with    the following allocation:
  142. X
  143. X          A call to malloc for 33 bytes    in program.c on    line 834.
  144. X          This was the 172nd call to malloc.
  145. X
  146. X
  147. X
  148. X
  149. X
  150. X
  151. X
  152. X
  153. X     Page 2                1.4
  154. X
  155. X
  156. X
  157. X
  158. X
  159. X
  160. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  161. X
  162. X
  163. X
  164. X      This example assumes that program.c included the debugging
  165. X      library malloc.h file.  If not, the identification
  166. X      information will be as follows:
  167. X
  168. X           This error is *probably*    associated with    the following allocation:
  169. X
  170. X          A call to malloc for 33 bytes    in an unknown file.
  171. X          This was the 172nd call to malloc.
  172. X
  173. X      The identification of    which call to malloc is    associated
  174. X      with the problem is helpful in that it gives you the
  175. X      information necessary    to set the breakpoint on the
  176. X      allocation function for that particular invocation
  177. X      (breakpoints usually can have    counters associated with
  178. X      them).  The counters for the three primary allocation    entry
  179. X      points (malloc, calloc, and realloc) are managed separately.
  180. X
  181. X      NOTE 1: if you want to set a breakpoint to capture this
  182. X      invocation of    malloc,    the actual function that is being
  183. X      called is debug_malloc (or debug_realloc for realloc and
  184. X      debug_calloc for calloc) and that is where the breakpoint
  185. X      should be set.
  186. X
  187. X      NOTE 2: Since    the software is    guessing at the    offending
  188. X      malloc chain segment,    it is possible that one    of the nearby
  189. X      segments is actually the culprit.  If    the environment
  190. X      variable MALLOC_SHOW_LINKS is    set, both the segment
  191. X      preceding and    the segment following the accused segment will
  192. X      also be identified.  The following is    a sample output:
  193. X
  194. X           This error is *probably*    associated with    the following allocation:
  195. X
  196. X           A call to malloc for    33 bytes in an unknown file.
  197. X           This    was the    172nd call to malloc.
  198. X
  199. X           The malloc chain element prior to the suspect allocation is from:
  200. X
  201. X           A call to calloc for    512 bytes in main.c line 16.
  202. X           This    was the    4th call to calloc.  This block    has been freed.
  203. X
  204. X           The malloc chain element following the suspect allocation is    from:
  205. X
  206. X           A call to realloc for 4096 bytes in func.c line 376.
  207. X           This    was the    1st call to realloc.
  208. X
  209. X
  210. X
  211. X
  212. X
  213. X
  214. X
  215. X
  216. X
  217. X
  218. X
  219. X     Page 3                1.4
  220. X
  221. X
  222. X
  223. X
  224. X
  225. X
  226. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  227. X
  228. X
  229. X
  230. X      Once the error message has been displayed, the software will
  231. X      then determine how to    handle the error.  This    handling will
  232. X      be based upon    the type of error level    (warning or fatal) and
  233. X      the error handling in    effect for that    error level (as
  234. X      specified by calls to    mallopt    or via environment variables).
  235. X      The coding for the error handling is as follows:
  236. X
  237. X        0    continue operations
  238. X        1    drop core and exit
  239. X        2    just exit
  240. X        3    drop core, but continue    executing.  Core files will be
  241. X        placed into core.[PID].[counter] i.e: core.00123.001
  242. X      128    dump malloc chain and continue
  243. X      129    dump malloc chain, dump    core, and exit
  244. X      130    dump malloc chain, exit
  245. X      131    dump malloc chain, dump    core, continue processing
  246. X
  247. X      mallopt() is used to set the malloc debugging    options. The
  248. X      following options can    be set:
  249. X
  250. X      MALLOC_WARN(100)    set the error handling for warning level
  251. X                  errors.  val.i is    an integer that    can
  252. X                  contain any one of the following values:
  253. X
  254. X                  M_HANDLE_IGNORE(0)  ignore error
  255. X                  M_HANDLE_ABORT(1)      drop core and    exit
  256. X                  M_HANDLE_EXIT(2)      just exit (no    core
  257. X                          drop)
  258. X                  M_HANDLE_CORE(3)      drop core, but keep
  259. X                          on going
  260. X
  261. X                  In addition, M_HANDLE_DUMP(128) may be
  262. X                  or'd in to cause a dump of the current
  263. X                  malloc chain.
  264. X
  265. X      MALLOC_FATAL(101)   set the error handling for fatal level
  266. X                  errors.  val.i is    equivalent to val.i
  267. X                  for MALLOC_WARN.
  268. X
  269. X      MALLOC_ERRFILE(102) set the destination for malloc error
  270. X                  messages.     val.str is a pointer to a
  271. X                  character    string containing the name of
  272. X                  the file to be used for error messages.
  273. X
  274. X      MALLOC_CKCHAIN(103) set the malloc chain checking flag.  If
  275. X                  val.i is non-zero, chain checking    at
  276. X                  every call to malloc is turned on.
  277. X
  278. X      MALLOC_FILLAREA(104)set the malloc fill area flag.  If val.i
  279. X                  is zero, malloc and free will not    fill
  280. X                  allocated/freed memory regions with
  281. X                  special fill patterns.  Utilizing    this
  282. X
  283. X
  284. X
  285. X     Page 4                1.4
  286. X
  287. X
  288. X
  289. X
  290. X
  291. X
  292. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  293. X
  294. X
  295. X
  296. X                  feature increases    the performance    of the
  297. X                  library while decreasing the
  298. X                  effectiveness of tracking    down overrun
  299. X                  problems.     It is strongly    recommended
  300. X                  that this    option not be used.
  301. X
  302. X      MALLOC_LOWFRAG(105) set the malloc allocation    fragmentation
  303. X                  handling level.  By default, malloc uses
  304. X                  a    first fit algorithm for    new
  305. X                  allocations.  Under certain allocation
  306. X                  scenarios, this can lead to significant
  307. X                  memory fragmentation because of the fact
  308. X                  that little allocations can break    big
  309. X                  blocks up.
  310. X
  311. X                  If val.i is non-zero, malloc uses    a best
  312. X                  fit algorithm which will reduce
  313. X                  fragmentation.  This mechanism, while
  314. X                  using less memory, is slower because the
  315. X                  entire free list is checked instead of
  316. X                  just checking until we find a segment
  317. X                  that is at least big enough.  Normally
  318. X                  you will not need    to set this variable.
  319. X
  320. X      For example, to set up the session to    generate a core    file
  321. X      for every malloc warning, to drop core and exit on a malloc
  322. X      fatal, and to    log all    messages to the    file "malloc_log" do
  323. X      the following:
  324. X
  325. X           #include    <malloc.h>
  326. X           union malloptarg     m;
  327. X
  328. X           m.i = 131;
  329. X           mallopt(MALLOC_WARN,m);
  330. X
  331. X           m.i = 1;
  332. X           mallopt(MALLOC_FATAL,m);
  333. X
  334. X           m.str = "malloc_log";
  335. X           mallopt(MALLOC_ERRFILE,m);
  336. X
  337. X      mallopt() can    be used    to set/alter the debugging options at
  338. X      any time (i.e. you may want to turn on chain-checking    after
  339. X      the program startup if the program startup does a lot    of
  340. X      allocations which are    known to be OK).
  341. X
  342. X      malloc_chain_check() will check the status of    the malloc
  343. X      arena.  If flag is non-zero, an error    found in the chain
  344. X      will cause a fatal error.  malloc_chain_check() returns zero
  345. X      when there are no problems found in the malloc chain,    non-
  346. X      zero otherwise.
  347. X
  348. X
  349. X
  350. X
  351. X     Page 5                1.4
  352. X
  353. X
  354. X
  355. X
  356. X
  357. X
  358. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  359. X
  360. X
  361. X
  362. X      malloc_dump()    will dump a list of all    in-use malloc segments
  363. X      and the first    few bytes of each segment.  If the environment
  364. X      variable MALLOC_DETAIL is set, all segments (including those
  365. X      that have been freed)    are listed and additional internal
  366. X      information is displayed.  fd    is the file descriptor to
  367. X      write    the data to.
  368. X
  369. X      malloc_list()    will dump a list in the    same format as
  370. X      malloc_dump but only the items that are still    in use and
  371. X      which    have been allocated within the malloc history id range
  372. X      specified by histid1 and histid2, inclusive.    The histids
  373. X      are obtained from calls to malloc_size(). This is especially
  374. X      useful in tracking down memory leaks.     fd is the file
  375. X      descriptor to    write the data to.
  376. X
  377. X      malloc_size()    returns    the amount of malloc data that is
  378. X      currently in use (in bytes).    If histidptr is    not NULL, it
  379. X      is taken to be a pointer to a    place to store the current
  380. X      malloc history id which can be used later when malloc_list
  381. X      is called to list items that are still in use.
  382. X
  383. X      The following    example    shows the typical use of the
  384. X      malloc_size and malloc_list functions    in tracking down
  385. X      memory leaks:
  386. X
  387. X           unsigned    long  histid1, histid2,    orig_size, current_size;
  388. X
  389. X           orig_size = malloc_size(&histid1);
  390. X
  391. X           /* .....    go do lots of stuff ...... */
  392. X
  393. X           current_size = malloc_size(&histid2);
  394. X
  395. X           if( current_size    != orig_size )
  396. X           {
  397. X            malloc_list(2,histid1,histid2);
  398. X           }
  399. X
  400. X
  401. X
  402. X
  403. X
  404. X
  405. X
  406. X
  407. X
  408. X
  409. X
  410. X
  411. X
  412. X
  413. X
  414. X
  415. X
  416. X
  417. X     Page 6                1.4
  418. X
  419. X
  420. X
  421. X
  422. X
  423. X
  424. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  425. X
  426. X
  427. X
  428. X     USAGE
  429. X      The library can be used in several modes, each increasingly
  430. X      intrusive (i.e. requiring changes to be made to the build
  431. X      process and/or source    code).    However, the extra cost    of a
  432. X      little intrusiveness is repaid in much better    problem
  433. X      identification.  Each    mode is    built upon the previous    modes
  434. X      and therefore    requires the changes and/or commands specified
  435. X      in the lower modes.
  436. X
  437. X      MODE 1 - library substitution
  438. X
  439. X      The simplest use is to just link the object module with the
  440. X      libdbmalloc.a.  Be sure to have this library before the C
  441. X      library (libc.a) on the link command (this is    automatic if
  442. X      you use cc to    link and specify the debug library without
  443. X      specifying the C library).
  444. X
  445. X      This mode links in all of the    debug versions of the library
  446. X      modules and will trap    as many    errors as it can (yes, there
  447. X      are errors that the malloc library cannot catch).
  448. X      Environment variables    can be used to control the behavior of
  449. X      the library.
  450. X
  451. X      MODE 2 - malloc.h inclusion
  452. X
  453. X      This mode involves including the malloc.h file included with
  454. X      the debugging    library.  The malloc.h file includes macros
  455. X      that will identify the source    line and file name for each
  456. X      debugging function called.  This is how the library is able
  457. X      to tell you that it was the call to malloc on    line 55    in
  458. X      file junk.c.
  459. X
  460. X      Typically you    should always include malloc.h in your source
  461. X      files    and just use the -I INCLUDEDIR directive for the
  462. X      compiler to point the    compiler to the    debugging version of
  463. X      the header file instead of the normal    file.  That way    you
  464. X      don't    have to    change the source files    when you want to turn
  465. X      off the debugging library.
  466. X
  467. X      NOTE:    Once you compile code in this mode, you    must recompile
  468. X      the code without the debugging malloc.h include file in
  469. X      order    to get the software to use the non-debugging
  470. X      functions.
  471. X
  472. X
  473. X
  474. X
  475. X
  476. X
  477. X
  478. X
  479. X
  480. X
  481. X
  482. X
  483. X     Page 7                1.4
  484. X
  485. X
  486. X
  487. X
  488. X
  489. X
  490. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  491. X
  492. X
  493. X
  494. X      MODE 3 - run-time specification of options
  495. X
  496. X      Environment variables    can be used to control the behavior of
  497. X      the debugging    library    to some    extent.     However, this control
  498. X      is very coarse in that you only have one setting available
  499. X      for the entire running of the    program.
  500. X
  501. X      This can be a    problem    if you want to turn on malloc chain
  502. X      checking, but    know that the problem occurs between a
  503. X      relatively narrow portion of the code    and don't want to take
  504. X      the hit of having chain checking on for the entire program
  505. X      execution.
  506. X
  507. X      The solution to this problem is to include calls to
  508. X      mallopt() with the debugging options which set the
  509. X      appropriate modes when you want them set.  Since you don't
  510. X      want to have to change the code to remove and    add these
  511. X      functions every time you decide to include malloc debugging
  512. X      or not, the malloc.h file defines the    preprocessor symbol
  513. X      _DEBUG_MALLOC_INC which can be used in your code as follows:
  514. X
  515. X           #ifdef _DEBUG_MALLOC_INC
  516. X            mallopt(.... );
  517. X           #endif
  518. X
  519. X      In addition to setting behavior options, you might want to
  520. X      make use of the memory leak detection    routines in this mode.
  521. X      These    calls should also be surrounded    by #ifdefs for the
  522. X      debug    malloc symbol so that you can leave them in the    code
  523. X      and automatically get    the increased functionality whenever
  524. X      you compile with the debugging library.
  525. X
  526. X     ENVIRONMENT VARIABLES
  527. X      Environment variables    can be used to control error handling,
  528. X      error    logging    and malloc chain checking at run time.    The
  529. X      following environment    variables are used:
  530. X
  531. X      MALLOC_CKCHAIN      if 1, turns on malloc chain checking at
  532. X                  every call to any    of the malloc
  533. X                  functions.
  534. X      MALLOC_DETAIL          if set, malloc_dump shows    some internal
  535. X                  detail for each entry in the chain.
  536. X                  This info    is probably only of use    if you
  537. X                  are debugging the    malloc library itself.
  538. X      MALLOC_ERRFILE      specifies    the error log file for error
  539. X                  messages.
  540. X      MALLOC_FATAL          specifies    the error handling for fatal
  541. X                  errors
  542. X      MALLOC_FILLAREA     specifies    the fill area flag setting.
  543. X                  If zero, malloc/free area    filling    and
  544. X                  checking is disabled (thereby increasing
  545. X                  performance, while decreasing
  546. X
  547. X
  548. X
  549. X     Page 8                1.4
  550. X
  551. X
  552. X
  553. X
  554. X
  555. X
  556. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  557. X
  558. X
  559. X
  560. X                  effectiveness of the library).
  561. X      MALLOC_LOWFRAG      if 1, turns on best fit allocation
  562. X                  algorithm.  Otherwise, first fit
  563. X                  algorithm    is used    for finding allocation
  564. X                  segments (which can cause    memory
  565. X                  fragmentation).
  566. X      MALLOC_SHOW_LINKS   when an error is found, the suspected
  567. X                  allocation is displayed.    However, since
  568. X                  it is possible that the next or previous
  569. X                  allocation in the    malloc chain was the
  570. X                  actual culprit these links may be    of
  571. X                  interest.     If this variable is set to
  572. X                  non-zero (i.e. 1)    the links will also be
  573. X                  shown.
  574. X      MALLOC_WARN          specifies    the error handling for warning
  575. X                  errors
  576. X
  577. X      As an    example, to set    up the session to generate a core file
  578. X      for every malloc warning, to drop core and exit on a malloc
  579. X      fatal, and to    log all    messages to the    file "malloc_log" do
  580. X      the following:
  581. X
  582. X           MALLOC_WARN=131
  583. X           MALLOC_FATAL=1
  584. X           MALLOC_ERRFILE=malloc_log
  585. X
  586. X           export MALLOC_WARN MALLOC_FATAL MALLOC_ERRFILE
  587. X
  588. X
  589. X     ERROR MESSAGES
  590. X      The following    error messages are reported by the library:
  591. X
  592. X      M_CODE_BAD_CONNECT(5)        Pointers between this segment
  593. X                    and adjoining segments are
  594. X                    invalid.
  595. X
  596. X                    This error indicates that the
  597. X                    malloc chain has been
  598. X                    corrupted.  This is most often
  599. X                    caused by an overwrite of the
  600. X                    malloc header by an access via
  601. X                    the previous malloc segment.
  602. X
  603. X      M_CODE_BAD_MAGIC(4)        Malloc region does not have a
  604. X                    valid magic number in header.
  605. X
  606. X                    This error is caused by
  607. X                    several    mechanisms including
  608. X                    free()ing the same pointer
  609. X                    twice or a pointer that    was
  610. X                    not returned by    malloc(), or
  611. X                    writing    beyond the end of a
  612. X
  613. X
  614. X
  615. X     Page 9                1.4
  616. X
  617. X
  618. X
  619. X
  620. X
  621. X
  622. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  623. X
  624. X
  625. X
  626. X                    segment.
  627. X
  628. X      M_CODE_BAD_PTR(3)        Pointer    is not within malloc
  629. X                    region.
  630. X
  631. X                    The pointer passed to free
  632. X                    orrealloc is not pointer
  633. X                    returned by malloc.  Another
  634. X                    cause is corruption of the
  635. X                    malloc chain by    writing    beyond
  636. X                    the end    of a segment.
  637. X
  638. X      M_CODE_CHAIN_BROKE(1)        Malloc chain is    corrupted,
  639. X                    pointers out of    order.
  640. X
  641. X                    Corruption has been detected
  642. X                    in the malloc chain that is
  643. X                    related    to the relative
  644. X                    positions of the malloc    chain
  645. X                    segments in memory.  This is
  646. X                    an indication that someone has
  647. X                    overwritten beyond the amount
  648. X                    they allocated.
  649. X
  650. X      M_CODE_FREELIST_BAD(11)    Malloc segment in free list is
  651. X                    in-use.
  652. X
  653. X                    A segment that is in the
  654. X                    free-list is flagged as    in-
  655. X                    use.  This is usually caused
  656. X                    by overwriting from the
  657. X                    previous segment in the    malloc
  658. X                    chain.
  659. X
  660. X      M_CODE_NOMORE_MEM(9)        Unable to get additional
  661. X                    memory from the    system.
  662. X
  663. X                    The system call    sbrk failed to
  664. X                    obtain more memory for the
  665. X                    program.
  666. X
  667. X      M_CODE_NOT_INUSE(8)        Data is    not in use (can't be
  668. X                    freed or reallocated).
  669. X
  670. X                    A pointer to a malloc segment
  671. X                    has been passed    to free() or
  672. X                    realloc(), but this segment
  673. X                    has already been freed.
  674. X
  675. X
  676. X
  677. X
  678. X
  679. X
  680. X
  681. X     Page 10                1.4
  682. X
  683. X
  684. X
  685. X
  686. X
  687. X
  688. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  689. X
  690. X
  691. X
  692. X      M_CODE_NO_END(2)        Malloc chain is    corrupted, end
  693. X                    before end pointer.
  694. X
  695. X                    Yet another overwrite problem.
  696. X                    This error means that we got
  697. X                    to what    we believe is the end
  698. X                    of the chain, but it does not
  699. X                    match the recorded end of the
  700. X                    chain.
  701. X
  702. X      M_CODE_OUTOF_BOUNDS(10)    Pointer    within malloc region,
  703. X                    but outside of malloc data
  704. X                    bounds.
  705. X
  706. X                    This is    caused by a call to
  707. X                    one of the string/memory
  708. X                    functions that attempt to
  709. X                    read/write bytes that are not
  710. X                    included in the    allocation
  711. X                    associated with    that memory.
  712. X                    This is    the most typical error
  713. X                    that you will see from the
  714. X                    malloc library.
  715. X
  716. X      M_CODE_OVERRUN(6)        Data has overrun beyond
  717. X                    requested number of bytes.
  718. X
  719. X                    This error is detected by
  720. X                    free() and indicates that the
  721. X                    current    segment    has written
  722. X                    data beyond the    number of
  723. X                    bytes that it requested.  This
  724. X                    only catches overwrites    when
  725. X                    they are within    the extra
  726. X                    space allocated    with each
  727. X                    segment    (this can range    from
  728. X                    one to eight bytes).  If the
  729. X                    overwrite occurs further along
  730. X                    it will    usually    cause some
  731. X                    corruption in the malloc
  732. X                    chain.
  733. X
  734. X      M_CODE_REUSE(7)        Data in    free'd area has    been
  735. X                    modified.
  736. X
  737. X                    Data in    a freed    segment    has
  738. X                    been modified.    This usually
  739. X                    indicates that the program is
  740. X                    using a    pointer    after it
  741. X                    called free, but it may    also
  742. X                    be caused by an    overwrite from
  743. X                    a previous segment in the
  744. X
  745. X
  746. X
  747. X     Page 11                1.4
  748. X
  749. X
  750. X
  751. X
  752. X
  753. X
  754. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  755. X
  756. X
  757. X
  758. X                    chain.
  759. X
  760. X     DUMP OUTPUT
  761. X      Sample dump/list output:
  762. X
  763. X           ************************** Dump of Malloc Chain ****************************
  764. X           POINTER       FILE     WHERE           LINE     ALLOC          DATA     HEX DUMP
  765. X           TO DATA        ALLOCATED          NUMBER     FUNCT         LENGTH  OF    BYTES 1-7
  766. X           --------    -------------------- ------- -------------- ------- --------------
  767. X           0x403DB4    testerr.c          15 malloc(1)         40 01010101010101
  768. X           0x403E0C    testerr.c          16 realloc(1)         20 01010101010101
  769. X
  770. X      The info in each column is as    follows:
  771. X
  772. X      POINTER          the pointer returned by the allocation
  773. X                  function (the pointer the    the allocated
  774. X                  data area).
  775. X
  776. X      FILE              the name of the file where the
  777. X                  allocation function was called.  This
  778. X                  information is only available if the
  779. X                  source file includes the malloc.h    file
  780. X                  from this    library    (as opposed to the
  781. X                  system include file).  If    the source
  782. X                  file did not include this    file, the file
  783. X                  will be listed as    unknown    and the    line
  784. X                  number will be blank.  Note that any
  785. X                  malloc calls from    system libraries will
  786. X                  probably not have    been compiled with the
  787. X                  malloc.h file included and will
  788. X                  therefore    appear as unknown.
  789. X
  790. X      LINE NUM          The line number of the line that called
  791. X                  the allocation function.    This field
  792. X                  will be left blank if the    malloc.h from
  793. X                  this library was not included in the
  794. X                  source file when it was compiled.
  795. X
  796. X      ALLOC    FUNC          The allocation function called: malloc,
  797. X                  realloc, or calloc.  The number in
  798. X                  parenthesis following the    function name
  799. X                  is the call number for that particular
  800. X                  function.     For example: malloc(1)    means
  801. X                  that this    allocation was the 1st call to
  802. X                  malloc.
  803. X
  804. X      DATA LEN          The number of bytes allocated.
  805. X
  806. X
  807. X
  808. X
  809. X
  810. X
  811. X
  812. X
  813. X     Page 12                1.4
  814. X
  815. X
  816. X
  817. X
  818. X
  819. X
  820. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  821. X
  822. X
  823. X
  824. X      HEX DUMP          A    hexidecimal dump of the    first seven
  825. X                  bytes in the allocated data.  This
  826. X                  example shows the    bytes filled with
  827. X                  0x01s which happen to be the fill
  828. X                  pattern used by the malloc library (to
  829. X                  make sure    that one doesn't depend    upon
  830. X                  the fact that some calls to malloc
  831. X                  result in    NULL bytes).  So the
  832. X                  allocations that are shown haven't
  833. X                  stored any data in the area yet.
  834. X
  835. X      If the environment variable MALLOC_DETAIL is defined,    the
  836. X      following additional information will    be included:
  837. X
  838. X           ************************************************************** Du...
  839. X                        FREE     FREE           ACTUAL SIZE      ...
  840. X         PTR      NEXT       PREV        NEXT     PREV      FLAGS       INT      HEX      ...
  841. X           --------    -------- -------- -------- -------- ---------- --------    --------- ...
  842. X           0x403C94    0x403CEC 0x40326C 0x000000 0x000000 0x03156111         48(0x000030) ...
  843. X           0x403CEC    0x403D2C 0x403C94 0x000000 0x000000 0x03156121         24(0x000018) ...
  844. X           0x403D2C    0x403D6C 0x403CEC 0x000000 0x403D6C 0x03156120         24(0x000018) ...
  845. X           0x403D6C    0x000000 0x403D2C 0x403D2C 0x000000 0x03156120         24(0x000018) ...
  846. X
  847. X           Malloc start:      0x40326C
  848. X           Malloc end:      0x403D2C
  849. X           Malloc data start: 0x403C94
  850. X           Malloc data end:      0x405C94
  851. X           Malloc free list:  0x403D6C
  852. X                   -> 0x403D2C
  853. X
  854. X      NOTE that I cut off the example at the point where the
  855. X      normal output    would begin (hence the ...).
  856. X
  857. X      The info in each column is as    follows:
  858. X
  859. X      PTR          The malloc chain pointer for the segment
  860. X              (the address of the segment).
  861. X
  862. X      NEXT          The pointer to the next segment in the
  863. X              chain.
  864. X
  865. X      PREV          The pointer to the previous segment in the
  866. X              chain.
  867. X
  868. X      FREE NEXT      The pointer to the next segment in the free
  869. X              list.
  870. X
  871. X      FREE PREV      The pointer to the previous segment in the
  872. X              free list.
  873. X
  874. X
  875. X
  876. X
  877. X
  878. X
  879. X     Page 13                1.4
  880. X
  881. X
  882. X
  883. X
  884. X
  885. X
  886. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  887. X
  888. X
  889. X
  890. X      FLAGS          The flags associated with this segment.
  891. X              This is a long integer which contains    the
  892. X              following fields
  893. X
  894. X              0xFFFFFF00  the magic    number.     This should
  895. X                      be 0x03156100 (probably
  896. X                      someone's    birthday).
  897. X
  898. X              0x00000070  the type of allocation function.
  899. X                      Malloc (x010), realloc (0x20),
  900. X                      or calloc    (0x30) are the only
  901. X                      valid values for this field).
  902. X
  903. X              0x00000001  the in-use flag.    if this    value
  904. X                      is non-zero, the indicated
  905. X                      segment is currently in use (not
  906. X                      freed).
  907. X
  908. X      ACTUAL SIZE      The actual size reserved for the allocation
  909. X              in both decimal and hex.  This will be at
  910. X              least    one byte more than the requested size,
  911. X              and as much as 8, so that we can check to
  912. X              see if the allocation    has been overrun.
  913. X
  914. X      Malloc start and end are pointers to the first and last
  915. X      malloc chain segment,    respectively.
  916. X
  917. X      Malloc data start and    data end are the lowest    and highest
  918. X      data bytes managed my    the malloc sub-system.    These are only
  919. X      used as a quick check    to see if a pointer is in the malloc
  920. X      region before    we go hunting down the chain trying to
  921. X      identify the segment it belongs to.
  922. X
  923. X      Malloc free list is a    chain of the elements in the free list
  924. X      (so it is easier for the programmer to follow    the free list
  925. X      if they choose to).  The address of each element in the list
  926. X      follows below    the list head.
  927. X
  928. X     PERFORMANCE
  929. X      This malloc library and its associated string    and memory
  930. X      functions are    much less efficient than the standard
  931. X      functions due    in part    to the extra error checking.  You do
  932. X      not want to use this library when generating a production
  933. X      (i.e.    releasable) version of your software.  It should only
  934. X      be used during development and testing.
  935. X
  936. X
  937. X
  938. X
  939. X
  940. X
  941. X
  942. X
  943. X
  944. X
  945. X     Page 14                1.4
  946. X
  947. X
  948. X
  949. X
  950. X
  951. X
  952. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  953. X
  954. X
  955. X
  956. X      The following    environment variable settings will give    you
  957. X      the best performance (at the expense of some additional
  958. X      error    checking):
  959. X
  960. X           MALLOC_CKCHAIN=0
  961. X           MALLOC_FILLAREA=0
  962. X           MALLOC_LOWFRAG=0
  963. X
  964. X      We strongly recommend    against    setting    MALLOC_FILLAREA    to
  965. X      zero because,    while it will increase the performance
  966. X      considerably,    it takes away the capability to    uncover    small
  967. X      malloc overruns which    don't overrite the pointers
  968. X      surrounding the malloc regions.
  969. X
  970. X      Anyway, with these settings, the malloc library runs at
  971. X      about    1/2 the    speed (things only take    twice as long) as the
  972. X      standard library.  If    you program spends most    of its time in
  973. X      malloc, it will still    be slow    (but perhaps this is an
  974. X      indication that you need to consider changing    the way    you
  975. X      are using malloc).
  976. X
  977. X     WARNINGS
  978. X      The include file for this library "malloc.h" should be
  979. X      included after the includes for any system related
  980. X      information.    This is    because    "malloc.h" redefines several
  981. X      system functions including string and    memory routines    and
  982. X      this will usually cause compilation errors if    malloc.h is
  983. X      processed first (of course, the compile errors will talk
  984. X      about    errors in the other system include files like
  985. X      string.h).
  986. X
  987. X      There    is a possibility that the use of sbrk()    by other
  988. X      modules will cause this library to get confused and possibly
  989. X      report some pointers as bad when the really aren't part of
  990. X      the malloc chain itself.  Therefore the direct use of    sbrk()
  991. X      is strongly discouraged.
  992. X
  993. X      This library attempts    to trap    errors and exit/handle them
  994. X      gracefully. However, the nature of the problems may be such
  995. X      that it causes the code in the library itself    to crash.
  996. X      There    is no way to avoid this, but if    it does    occur,    turn
  997. X      on chain checking to narrow the place    where it will occur.
  998. X
  999. X      The functions    in this    library    will often conflict with
  1000. X      duplicate functions in shared    library    versions of libc.a.
  1001. X      This is usually due to the fact that some shared library
  1002. X      modules have explicit    references to shared library versions
  1003. X      of the debug functions.  The only way    around this is to not
  1004. X      use the shared library when linking.
  1005. X
  1006. X      This malloc library, like most malloc    libraries, is not re-
  1007. X      entrant and therefore    should not be called from interrupt
  1008. X
  1009. X
  1010. X
  1011. X     Page 15                1.4
  1012. X
  1013. X
  1014. X
  1015. X
  1016. X
  1017. X
  1018. X     DEBUG_MALLOC(3)           (VTI)           DEBUG_MALLOC(3)
  1019. X
  1020. X
  1021. X
  1022. X      handlers because of the potential for    receiving an interrupt
  1023. X      in the middle    of a call to malloc which would    really mess
  1024. X      things up.
  1025. X
  1026. X     SEE ALSO
  1027. X      malloc(3)
  1028. X
  1029. X     AUTHOR
  1030. X      Conor    P. Cahill
  1031. X      Virtual Technologies Incorporated
  1032. X      46030    Manekin    Plaza, Suite 160
  1033. X      Sterling VA 22170
  1034. X      703-430-9247
  1035. X
  1036. X      uunet!virtech!cpcahil
  1037. X
  1038. X
  1039. X
  1040. X
  1041. X
  1042. X
  1043. X
  1044. X
  1045. X
  1046. X
  1047. X
  1048. X
  1049. X
  1050. X
  1051. X
  1052. X
  1053. X
  1054. X
  1055. X
  1056. X
  1057. X
  1058. X
  1059. X
  1060. X
  1061. X
  1062. X
  1063. X
  1064. X
  1065. X
  1066. X
  1067. X
  1068. X
  1069. X
  1070. X
  1071. X
  1072. X
  1073. X
  1074. X
  1075. X
  1076. X
  1077. X     Page 16                1.4
  1078. X
  1079. X
  1080. X
  1081. END_OF_FILE
  1082. if test 28631 -ne `wc -c <'malloc.man'`; then
  1083.     echo shar: \"'malloc.man'\" unpacked with wrong size!
  1084. fi
  1085. # end of 'malloc.man'
  1086. fi
  1087. if test -f 'memory.c' -a "${1}" != "-c" ; then 
  1088.   echo shar: Will not clobber existing file \"'memory.c'\"
  1089. else
  1090. echo shar: Extracting \"'memory.c'\" \(8088 characters\)
  1091. sed "s/^X//" >'memory.c' <<'END_OF_FILE'
  1092. X/*
  1093. X * (c) Copyright 1990, 1991 Conor P. Cahill (uunet!virtech!cpcahil).  
  1094. X * You may copy, distribute, and use this software as long as this
  1095. X * copyright statement is not removed.
  1096. X */
  1097. X
  1098. X#ifndef lint
  1099. Xstatic
  1100. Xchar rcs_hdr[] = "$Id: memory.c,v 1.13 1992/01/24 04:49:05 cpcahil Exp $";
  1101. X#endif
  1102. X
  1103. X#include <stdio.h>
  1104. X#include "mallocint.h"
  1105. X
  1106. X/*
  1107. X * memccpy - copy memory region up to specified byte or length
  1108. X */
  1109. XDATATYPE *
  1110. Xmemccpy(ptr1, ptr2, ch, len)
  1111. X    DATATYPE        * ptr1;
  1112. X    const DATATYPE        * ptr2;
  1113. X    int              ch;
  1114. X    SIZETYPE          len;
  1115. X{
  1116. X    return( DBmemccpy( (char *) NULL, 0, ptr1, ptr2, ch, len) );
  1117. X}
  1118. X
  1119. XDATATYPE *
  1120. XDBmemccpy(file,line,ptr1, ptr2, ch, len)
  1121. X    const char        * file;
  1122. X    int              line;
  1123. X    DATATYPE        * ptr1;
  1124. X    const DATATYPE        * ptr2;
  1125. X    int              ch;
  1126. X    SIZETYPE          len;
  1127. X{
  1128. X    register char        * myptr1;
  1129. X    register const char    * myptr2;
  1130. X    register SIZETYPE      i;
  1131. X    DATATYPE        * rtn;
  1132. X
  1133. X
  1134. X    myptr1 = (char *) ptr1;
  1135. X    myptr2 = (char *) ptr2;
  1136. X
  1137. X    /*
  1138. X     * I know that the assignment could be done in the following, but
  1139. X     * I wanted to perform a check before any assignment, so first I 
  1140. X     * determine the length, check the pointers and then do the assignment.
  1141. X     */
  1142. X    for( i=0; (i < len) && (myptr2[i] != ch); i++)
  1143. X    {
  1144. X    }
  1145. X
  1146. X    /*
  1147. X     * if we found the character...
  1148. X      */
  1149. X    if( i < len )
  1150. X    {
  1151. X        rtn = myptr1+i+1;
  1152. X        i++;
  1153. X    }
  1154. X    else
  1155. X    {
  1156. X        rtn = (char *) 0;
  1157. X    }
  1158. X
  1159. X    /*
  1160. X     * make sure we have enough room in both ptr1 and ptr2
  1161. X     */
  1162. X    malloc_check_data("memccpy", file, line, ptr1, i);
  1163. X    malloc_check_data("memccpy", file, line, ptr2, i);
  1164. X
  1165. X     while( i-- > 0 )
  1166. X    {
  1167. X        *(myptr1++) = *(myptr2++);
  1168. X    }
  1169. X    
  1170. X    return( rtn );
  1171. X}
  1172. X
  1173. X/*
  1174. X * memchr - find a byte in a memory region
  1175. X */
  1176. XDATATYPE *
  1177. Xmemchr(ptr1,ch,len)
  1178. X    const DATATYPE        * ptr1;
  1179. X    register int          ch;
  1180. X    SIZETYPE          len;
  1181. X{
  1182. X    return( DBmemchr( (char *)NULL, 0, ptr1, ch, len) );
  1183. X}
  1184. X
  1185. XDATATYPE *
  1186. XDBmemchr(file,line,ptr1,ch,len)
  1187. X    const char        * file;
  1188. X    int              line;
  1189. X    const DATATYPE        * ptr1;
  1190. X    register int          ch;
  1191. X    SIZETYPE          len;
  1192. X{
  1193. X    register const char    * myptr1;
  1194. X    SIZETYPE          i;
  1195. X
  1196. X    malloc_check_data("memchr", file, line, ptr1, len);
  1197. X
  1198. X    myptr1 = (char *) ptr1;
  1199. X
  1200. X    for( i=0; (i < len) && (myptr1[i] != (char) ch); i++)
  1201. X    {
  1202. X    }
  1203. X
  1204. X    if( i < len )
  1205. X    {
  1206. X        return( (DATATYPE *) (myptr1+i) );
  1207. X    }
  1208. X    else
  1209. X    {
  1210. X        return( (DATATYPE *) 0);    
  1211. X    }
  1212. X}
  1213. X
  1214. X/*
  1215. X * memcpy  - copy one memory area to another
  1216. X * memmove - copy one memory area to another
  1217. X */
  1218. XDATATYPE * 
  1219. Xmemmove(ptr1, ptr2, len)
  1220. X    DATATYPE        * ptr1;
  1221. X    const DATATYPE        * ptr2;
  1222. X    register SIZETYPE      len;
  1223. X{
  1224. X    return( DBmemmove( (char *) NULL, 0,ptr1, ptr2, len) );
  1225. X}
  1226. X
  1227. XDATATYPE * 
  1228. XDBmemmove(file,line,ptr1, ptr2, len)
  1229. X    const char        * file;
  1230. X    int              line;
  1231. X    DATATYPE        * ptr1;
  1232. X    const DATATYPE        * ptr2;
  1233. X    register SIZETYPE      len;
  1234. X{
  1235. X    return( DBFmemcpy( "memmove", file, line, ptr1, ptr2, len) );
  1236. X}
  1237. X
  1238. X
  1239. XDATATYPE *
  1240. Xmemcpy(ptr1, ptr2, len)
  1241. X    DATATYPE        * ptr1;
  1242. X    const DATATYPE        * ptr2;
  1243. X    register SIZETYPE      len;
  1244. X{
  1245. X    return( DBmemcpy( (char *) NULL, 0, ptr1, ptr2, len) );
  1246. X}
  1247. X
  1248. XDATATYPE *
  1249. XDBmemcpy(file, line, ptr1, ptr2, len)
  1250. X    const char        * file;
  1251. X    int              line;
  1252. X    DATATYPE        * ptr1;
  1253. X    const DATATYPE        * ptr2;
  1254. X    register SIZETYPE      len;
  1255. X{
  1256. X    return( DBFmemcpy( "memcpy", file, line ,ptr1, ptr2, len) );
  1257. X}
  1258. X
  1259. XDATATYPE *
  1260. XDBFmemcpy(func, file, line,ptr1, ptr2, len)
  1261. X    const char        * func;
  1262. X    const char        * file;
  1263. X    int              line;
  1264. X    DATATYPE        * ptr1;
  1265. X    const DATATYPE        * ptr2;
  1266. X    register SIZETYPE      len;
  1267. X{
  1268. X    register char        * myptr1;
  1269. X    register const char    * myptr2;
  1270. X    DATATYPE        * rtn = ptr1;
  1271. X
  1272. X    malloc_check_data(func, file, line, ptr1, len);
  1273. X    malloc_check_data(func, file, line, ptr2, len);
  1274. X
  1275. X    myptr1 = ptr1;
  1276. X    myptr2 = ptr2;
  1277. X
  1278. X    /*
  1279. X     * while the normal memcpy does not guarrantee that it will 
  1280. X     * handle overlapping memory correctly, we will try...
  1281. X     */
  1282. X    if( myptr1 > myptr2  && myptr1 < (myptr2+len))
  1283. X    {
  1284. X        myptr1 += (len-1);
  1285. X        myptr2 += (len-1);
  1286. X        while( len-- > 0 )
  1287. X        {
  1288. X            *(myptr1--) = *(myptr2--);
  1289. X        }
  1290. X    }
  1291. X    else
  1292. X    {
  1293. X        while( len-- > 0 )
  1294. X        {
  1295. X            *(myptr1++) = *(myptr2++);
  1296. X        }
  1297. X    }
  1298. X    
  1299. X    return(rtn);
  1300. X}
  1301. X
  1302. X/*
  1303. X * memcmp - compare two memory regions
  1304. X */
  1305. Xint
  1306. Xmemcmp(ptr1, ptr2, len)
  1307. X    const DATATYPE        * ptr1;
  1308. X    const DATATYPE        * ptr2;
  1309. X    register SIZETYPE      len;
  1310. X{
  1311. X    return( DBmemcmp((char *)NULL,0,ptr1,ptr2,len) );
  1312. X}
  1313. X
  1314. Xint
  1315. XDBmemcmp(file,line,ptr1, ptr2, len)
  1316. X    const char        * file;
  1317. X    int              line;
  1318. X    const DATATYPE        * ptr1;
  1319. X    const DATATYPE        * ptr2;
  1320. X    register SIZETYPE      len;
  1321. X{
  1322. X    return( DBFmemcmp("memcmp",file,line,ptr1,ptr2,len) );
  1323. X}
  1324. X
  1325. Xint
  1326. XDBFmemcmp(func,file,line,ptr1, ptr2, len)
  1327. X    const char        * func;
  1328. X    const char        * file;
  1329. X    int              line;
  1330. X    const DATATYPE        * ptr1;
  1331. X    const DATATYPE        * ptr2;
  1332. X    register SIZETYPE      len;
  1333. X{
  1334. X    register const char    * myptr1;
  1335. X    register const char    * myptr2;
  1336. X    
  1337. X    malloc_check_data(func,file,line, ptr1, len);
  1338. X    malloc_check_data(func,file,line, ptr2, len);
  1339. X
  1340. X    myptr1 = ptr1;
  1341. X    myptr2 = ptr2;
  1342. X
  1343. X    while( len > 0  && (*myptr1 == *myptr2) )
  1344. X    {
  1345. X        len--;
  1346. X        myptr1++;
  1347. X        myptr2++;
  1348. X    }
  1349. X
  1350. X    /* 
  1351. X     * If stopped by len, return zero
  1352. X     */
  1353. X    if( len == 0 )
  1354. X    {
  1355. X        return(0);
  1356. X    }
  1357. X
  1358. X    return( *myptr1 - *myptr2 );
  1359. X}
  1360. X
  1361. X/*
  1362. X * memset - set all bytes of a memory block to a specified value
  1363. X */
  1364. XDATATYPE * 
  1365. Xmemset(ptr1, ch, len)
  1366. X    DATATYPE        * ptr1;
  1367. X    register int          ch;
  1368. X    register SIZETYPE      len;
  1369. X{
  1370. X    return( DBmemset((char *)NULL,0,ptr1,ch,len) );
  1371. X}
  1372. X
  1373. XDATATYPE * 
  1374. XDBmemset(file,line,ptr1, ch, len)
  1375. X    const char        * file;
  1376. X    int              line;
  1377. X    DATATYPE        * ptr1;
  1378. X    register int          ch;
  1379. X    register SIZETYPE      len;
  1380. X{
  1381. X    return( DBFmemset("memset",file,line,ptr1,ch,len) );
  1382. X}
  1383. X
  1384. XDATATYPE * 
  1385. XDBFmemset(func,file,line,ptr1, ch, len)
  1386. X    const char        * func;
  1387. X    const char        * file;
  1388. X    int              line;
  1389. X    DATATYPE        * ptr1;
  1390. X    register int          ch;
  1391. X    register SIZETYPE      len;
  1392. X{
  1393. X    register char        * myptr1;
  1394. X    char            * rtn = ptr1;
  1395. X
  1396. X    malloc_check_data(func, file, line, ptr1, len);
  1397. X
  1398. X    myptr1 = ptr1;
  1399. X
  1400. X    while( len-- )
  1401. X    {
  1402. X        *(myptr1++) = ch;
  1403. X    }
  1404. X
  1405. X    return(rtn);
  1406. X}
  1407. X
  1408. X/*
  1409. X * bcopy - copy memory block to another area
  1410. X */
  1411. XDATATYPE *
  1412. Xbcopy(ptr2,ptr1,len)
  1413. X    const DATATYPE    * ptr2;
  1414. X    DATATYPE    * ptr1;
  1415. X    SIZETYPE      len;
  1416. X{
  1417. X    return( DBbcopy((char *)NULL,0,ptr2,ptr1,len) );
  1418. X}
  1419. X
  1420. XDATATYPE *
  1421. XDBbcopy(file,line,ptr2,ptr1,len)
  1422. X    const char    * file;
  1423. X    int          line;
  1424. X    const DATATYPE    * ptr2;
  1425. X    DATATYPE    * ptr1;
  1426. X    SIZETYPE      len;
  1427. X{
  1428. X    return( DBFmemcpy("bcopy",file,line,ptr1,ptr2,len));
  1429. X}
  1430. X
  1431. X/*
  1432. X * bzero - clear block of memory to zeros
  1433. X */
  1434. XDATATYPE *
  1435. Xbzero(ptr1,len)
  1436. X    DATATYPE    * ptr1;
  1437. X    SIZETYPE      len;
  1438. X{
  1439. X    return( DBbzero((char *)NULL,0,ptr1,len) );
  1440. X}
  1441. X
  1442. XDATATYPE *
  1443. XDBbzero(file,line,ptr1,len)
  1444. X    const char    * file;
  1445. X    int          line;
  1446. X    DATATYPE    * ptr1;
  1447. X    SIZETYPE      len;
  1448. X{
  1449. X    return( DBFmemset("bzero",file,line,ptr1,'\0',len) );
  1450. X}
  1451. X
  1452. X/*
  1453. X * bcmp - compary memory blocks
  1454. X */
  1455. Xint
  1456. Xbcmp(ptr2, ptr1, len)
  1457. X    const DATATYPE    * ptr1;
  1458. X    const DATATYPE    * ptr2;
  1459. X    SIZETYPE      len;
  1460. X{
  1461. X    return( DBbcmp((char *)NULL,0,ptr2, ptr1, len) );
  1462. X}
  1463. X
  1464. Xint
  1465. XDBbcmp(file, line, ptr2, ptr1, len)
  1466. X    const char    * file;
  1467. X    int          line;
  1468. X    const DATATYPE    * ptr1;
  1469. X    const DATATYPE    * ptr2;
  1470. X    SIZETYPE      len;
  1471. X{
  1472. X    return( DBFmemcmp("bcmp",file,line,ptr1,ptr2,len) );
  1473. X}
  1474. X
  1475. X/*
  1476. X * $Log: memory.c,v $
  1477. X * Revision 1.13  1992/01/24  04:49:05  cpcahil
  1478. X * changed memccpy to only check number of chars it will copy.
  1479. X *
  1480. X * Revision 1.12  1991/12/31  21:31:26  cpcahil
  1481. X * changes for patch 6.  See CHANGES file for more info
  1482. X *
  1483. X * Revision 1.11  1991/12/02  19:10:13  cpcahil
  1484. X * changes for patch release 5
  1485. X *
  1486. X * Revision 1.10  91/11/25  14:42:03  cpcahil
  1487. X * Final changes in preparation for patch 4 release
  1488. X * 
  1489. X * Revision 1.9  91/11/24  00:49:31  cpcahil
  1490. X * first cut at patch 4
  1491. X * 
  1492. X * Revision 1.8  91/05/21  18:33:47  cpcahil
  1493. X * fixed bug in memccpy() which checked an extra byte if the first character
  1494. X * after the specified length matched the search character.
  1495. X * 
  1496. X * Revision 1.7  90/08/29  21:27:58  cpcahil
  1497. X * fixed value of check in memccpy when character was not found.
  1498. X * 
  1499. X * Revision 1.6  90/07/16  20:06:26  cpcahil
  1500. X * fixed several minor bugs found with Henry Spencer's string/mem tester 
  1501. X * program.
  1502. X * 
  1503. X * 
  1504. X * Revision 1.5  90/05/11  15:39:36  cpcahil
  1505. X * fixed bug in memccpy().
  1506. X * 
  1507. X * Revision 1.4  90/05/11  00:13:10  cpcahil
  1508. X * added copyright statment
  1509. X * 
  1510. X * Revision 1.3  90/02/24  21:50:29  cpcahil
  1511. X * lots of lint fixes
  1512. X * 
  1513. X * Revision 1.2  90/02/24  17:29:41  cpcahil
  1514. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  1515. X * id string
  1516. X * 
  1517. X * Revision 1.1  90/02/22  23:17:43  cpcahil
  1518. X * Initial revision
  1519. X * 
  1520. X */
  1521. END_OF_FILE
  1522. if test 8088 -ne `wc -c <'memory.c'`; then
  1523.     echo shar: \"'memory.c'\" unpacked with wrong size!
  1524. fi
  1525. # end of 'memory.c'
  1526. fi
  1527. if test -f 'prototypes.h' -a "${1}" != "-c" ; then 
  1528.   echo shar: Will not clobber existing file \"'prototypes.h'\"
  1529. else
  1530. echo shar: Extracting \"'prototypes.h'\" \(8674 characters\)
  1531. sed "s/^X//" >'prototypes.h' <<'END_OF_FILE'
  1532. X#if defined(__STDC__) || defined(__cplusplus)
  1533. X# define __stdcargs(s) s
  1534. X#else
  1535. X# define __stdcargs(s) ()
  1536. X#endif
  1537. X
  1538. X/* malloc.c */
  1539. XDATATYPE *malloc __stdcargs((SIZETYPE size));
  1540. XDATATYPE *debug_malloc __stdcargs((const char *file, int line, SIZETYPE size));
  1541. XVOIDTYPE malloc_split __stdcargs((struct mlist *ptr));
  1542. XVOIDTYPE malloc_join __stdcargs((struct mlist *ptr, struct mlist *nextptr, int inuse_override, int fill_flag));
  1543. XVOIDTYPE malloc_aligned_memset __stdcargs((DATATYPE *ptr, int byte, register int len));
  1544. XVOIDTYPE malloc_safe_memset __stdcargs((DATATYPE *ptr, int byte, register int len));
  1545. XVOIDTYPE malloc_fatal __stdcargs((const char *funcname, const char *file, int line, const struct mlist *mptr));
  1546. XVOIDTYPE malloc_warning __stdcargs((const char *funcname, const char *file, int line, const struct mlist *mptr));
  1547. XVOIDTYPE malloc_dump_info_block __stdcargs((const struct mlist *mptr, int id));
  1548. XVOIDTYPE malloc_err_handler __stdcargs((int level));
  1549. Xchar *malloc_int_suffix __stdcargs((long i));
  1550. XVOIDTYPE malloc_freeseg __stdcargs((int op, struct mlist *ptr));
  1551. X/* free.c */
  1552. XVOIDTYPE free __stdcargs((DATATYPE *cptr));
  1553. XVOIDTYPE debug_free __stdcargs((const char *file, int line, DATATYPE *cptr));
  1554. XVOIDTYPE DBFfree __stdcargs((const char *func, const char *file, int line, DATATYPE *cptr));
  1555. X/* realloc.c */
  1556. XDATATYPE *realloc __stdcargs((DATATYPE *cptr, SIZETYPE size));
  1557. XDATATYPE *debug_realloc __stdcargs((const char *file, int line, DATATYPE *cptr, SIZETYPE size));
  1558. X/* calloc.c */
  1559. XDATATYPE *calloc __stdcargs((SIZETYPE nelem, SIZETYPE elsize));
  1560. XDATATYPE *debug_calloc __stdcargs((const char *file, int line, SIZETYPE nelem, SIZETYPE elsize));
  1561. XVOIDTYPE cfree __stdcargs((DATATYPE *cptr));
  1562. XVOIDTYPE debug_cfree __stdcargs((const char *file, int line, DATATYPE *cptr));
  1563. X/* string.c */
  1564. Xchar *strcat __stdcargs((char *str1, const char *str2));
  1565. Xchar *DBstrcat __stdcargs((const char *file, int line, register char *str1, register const char *str2));
  1566. Xchar *strdup __stdcargs((const char *str1));
  1567. Xchar *DBstrdup __stdcargs((const char *file, int line, register const char *str1));
  1568. Xchar *strncat __stdcargs((char *str1, const char *str2, SIZETYPE len));
  1569. Xchar *DBstrncat __stdcargs((const char *file, int line, register char *str1, register const char *str2, register SIZETYPE len));
  1570. Xint strcmp __stdcargs((register const char *str1, register const char *str2));
  1571. Xint DBstrcmp __stdcargs((const char *file, int line, register const char *str1, register const char *str2));
  1572. Xint strncmp __stdcargs((register const char *str1, register const char *str2, register SIZETYPE len));
  1573. Xint DBstrncmp __stdcargs((const char *file, int line, register const char *str1, register const char *str2, register SIZETYPE len));
  1574. Xchar *strcpy __stdcargs((register char *str1, register const char *str2));
  1575. Xchar *DBstrcpy __stdcargs((const char *file, int line, register char *str1, register const char *str2));
  1576. Xchar *strncpy __stdcargs((register char *str1, register const char *str2, register SIZETYPE len));
  1577. Xchar *DBstrncpy __stdcargs((const char *file, int line, register char *str1, register const char *str2, register SIZETYPE len));
  1578. XSIZETYPE strlen __stdcargs((const char *str1));
  1579. XSIZETYPE DBstrlen __stdcargs((const char *file, int line, register const char *str1));
  1580. Xchar *strchr __stdcargs((const char *str1, int c));
  1581. Xchar *DBstrchr __stdcargs((const char *file, int line, const char *str1, int c));
  1582. Xchar *DBFstrchr __stdcargs((const char *func, const char *file, int line, register const char *str1, register int c));
  1583. Xchar *strrchr __stdcargs((const char *str1, int c));
  1584. Xchar *DBstrrchr __stdcargs((const char *file, int line, const char *str1, int c));
  1585. Xchar *DBFstrrchr __stdcargs((const char *func, const char *file, int line, register const char *str1, register int c));
  1586. Xchar *index __stdcargs((const char *str1, int c));
  1587. Xchar *DBindex __stdcargs((const char *file, int line, const char *str1, int c));
  1588. Xchar *rindex __stdcargs((const char *str1, int c));
  1589. Xchar *DBrindex __stdcargs((const char *file, int line, const char *str1, int c));
  1590. Xchar *strpbrk __stdcargs((const char *str1, const char *str2));
  1591. Xchar *DBstrpbrk __stdcargs((const char *file, int line, register const char *str1, register const char *str2));
  1592. XSIZETYPE strspn __stdcargs((const char *str1, const char *str2));
  1593. XSIZETYPE DBstrspn __stdcargs((const char *file, int line, register const char *str1, register const char *str2));
  1594. XSIZETYPE strcspn __stdcargs((const char *str1, const char *str2));
  1595. XSIZETYPE DBstrcspn __stdcargs((const char *file, int line, register const char *str1, register const char *str2));
  1596. Xchar *strstr __stdcargs((const char *str1, const char *str2));
  1597. Xchar *DBstrstr __stdcargs((const char *file, int line, const char *str1, const char *str2));
  1598. Xchar *strtok __stdcargs((char *str1, const char *str2));
  1599. Xchar *DBstrtok __stdcargs((const char *file, int line, char *str1, const char *str2));
  1600. Xchar *strtoken __stdcargs((register char **stringp, register const char *delim, int skip));
  1601. X/* malloc_chk.c */
  1602. Xint malloc_in_arena __stdcargs((const DATATYPE *ptr));
  1603. XVOIDTYPE malloc_check_str __stdcargs((const char *func, const char *file, int line, const char *str));
  1604. XVOIDTYPE malloc_check_strn __stdcargs((const char *func, const char *file, int line, const char *str, int len));
  1605. XVOIDTYPE malloc_check_data __stdcargs((const char *func, const char *file, int line, const DATATYPE *ptr, SIZETYPE len));
  1606. XVOIDTYPE malloc_verify __stdcargs((const char *func, const char *file, int line, const DATATYPE *ptr, int len));
  1607. X/* malloc_chn.c */
  1608. Xint malloc_chain_check __stdcargs((int todo));
  1609. Xint DBmalloc_chain_check __stdcargs((const char *file, int line, int todo));
  1610. Xint DBFmalloc_chain_check __stdcargs((const char *func, const char *file, int line, int todo));
  1611. X/* memory.c */
  1612. XDATATYPE *memccpy __stdcargs((DATATYPE *ptr1, const DATATYPE *ptr2, int ch, SIZETYPE len));
  1613. XDATATYPE *DBmemccpy __stdcargs((const char *file, int line, DATATYPE *ptr1, const DATATYPE *ptr2, int ch, SIZETYPE len));
  1614. XDATATYPE *memchr __stdcargs((const DATATYPE *ptr1, register int ch, SIZETYPE len));
  1615. XDATATYPE *DBmemchr __stdcargs((const char *file, int line, const DATATYPE *ptr1, register int ch, SIZETYPE len));
  1616. XDATATYPE *memmove __stdcargs((DATATYPE *ptr1, const DATATYPE *ptr2, register SIZETYPE len));
  1617. XDATATYPE *DBmemmove __stdcargs((const char *file, int line, DATATYPE *ptr1, const DATATYPE *ptr2, register SIZETYPE len));
  1618. XDATATYPE *memcpy __stdcargs((DATATYPE *ptr1, const DATATYPE *ptr2, register SIZETYPE len));
  1619. XDATATYPE *DBmemcpy __stdcargs((const char *file, int line, DATATYPE *ptr1, const DATATYPE *ptr2, register SIZETYPE len));
  1620. XDATATYPE *DBFmemcpy __stdcargs((const char *func, const char *file, int line, DATATYPE *ptr1, const DATATYPE *ptr2, register SIZETYPE len));
  1621. Xint memcmp __stdcargs((const DATATYPE *ptr1, const DATATYPE *ptr2, register SIZETYPE len));
  1622. Xint DBmemcmp __stdcargs((const char *file, int line, const DATATYPE *ptr1, const DATATYPE *ptr2, register SIZETYPE len));
  1623. Xint DBFmemcmp __stdcargs((const char *func, const char *file, int line, const DATATYPE *ptr1, const DATATYPE *ptr2, register SIZETYPE len));
  1624. XDATATYPE *memset __stdcargs((DATATYPE *ptr1, register int ch, register SIZETYPE len));
  1625. XDATATYPE *DBmemset __stdcargs((const char *file, int line, DATATYPE *ptr1, register int ch, register SIZETYPE len));
  1626. XDATATYPE *DBFmemset __stdcargs((const char *func, const char *file, int line, DATATYPE *ptr1, register int ch, register SIZETYPE len));
  1627. XDATATYPE *bcopy __stdcargs((const DATATYPE *ptr2, DATATYPE *ptr1, SIZETYPE len));
  1628. XDATATYPE *DBbcopy __stdcargs((const char *file, int line, const DATATYPE *ptr2, DATATYPE *ptr1, SIZETYPE len));
  1629. XDATATYPE *bzero __stdcargs((DATATYPE *ptr1, SIZETYPE len));
  1630. XDATATYPE *DBbzero __stdcargs((const char *file, int line, DATATYPE *ptr1, SIZETYPE len));
  1631. Xint bcmp __stdcargs((const DATATYPE *ptr2, const DATATYPE *ptr1, SIZETYPE len));
  1632. Xint DBbcmp __stdcargs((const char *file, int line, const DATATYPE *ptr2, const DATATYPE *ptr1, SIZETYPE len));
  1633. X/* tostring.c */
  1634. Xint tostring __stdcargs((char *buf, long val, int len, int base, int fill));
  1635. X/* m_perror.c */
  1636. XVOIDTYPE malloc_perror __stdcargs((char *str));
  1637. X/* m_init.c */
  1638. XVOIDTYPE malloc_init __stdcargs((void));
  1639. X/* mallopt.c */
  1640. Xint mallopt __stdcargs((int cmd, union malloptarg value));
  1641. X/* dump.c */
  1642. XVOIDTYPE malloc_dump __stdcargs((int fd));
  1643. XVOIDTYPE malloc_list __stdcargs((int fd, unsigned long histid1, unsigned long histid2));
  1644. XVOIDTYPE malloc_list_items __stdcargs((int fd, int list_type, unsigned long histid1, unsigned long histid2));
  1645. X/* leak.c */
  1646. Xunsigned long malloc_size __stdcargs((unsigned long *histptr));
  1647. Xunsigned long DBmalloc_size __stdcargs((const char *file, int line, unsigned long *histptr));
  1648. X
  1649. X#undef __stdcargs
  1650. END_OF_FILE
  1651. if test 8674 -ne `wc -c <'prototypes.h'`; then
  1652.     echo shar: \"'prototypes.h'\" unpacked with wrong size!
  1653. fi
  1654. # end of 'prototypes.h'
  1655. fi
  1656. if test -f 'realloc.c' -a "${1}" != "-c" ; then 
  1657.   echo shar: Will not clobber existing file \"'realloc.c'\"
  1658. else
  1659. echo shar: Extracting \"'realloc.c'\" \(6787 characters\)
  1660. sed "s/^X//" >'realloc.c' <<'END_OF_FILE'
  1661. X/*
  1662. X * (c) Copyright 1990, 1991 Conor P. Cahill (uunet!virtech!cpcahil).  
  1663. X * You may copy, distribute, and use this software as long as this
  1664. X * copyright statement is not removed.
  1665. X */
  1666. X
  1667. X#ifndef lint
  1668. Xstatic
  1669. Xchar rcs_hdr[] = "$Id: realloc.c,v 1.15 1992/01/10 17:28:03 cpcahil Exp $";
  1670. X#endif
  1671. X
  1672. X#include <stdio.h>
  1673. X
  1674. X#include "mallocint.h"
  1675. X
  1676. XDATATYPE *
  1677. Xrealloc(cptr,size)
  1678. X    DATATYPE        * cptr;
  1679. X    SIZETYPE          size;
  1680. X{
  1681. X    return( debug_realloc(NULL,-1,cptr,size) );
  1682. X}
  1683. X
  1684. X/*
  1685. X * Function:    debug_realloc()
  1686. X *
  1687. X * Purpose:    to re-allocate a data area.
  1688. X *
  1689. X * Arguments:    cptr    - pointer to area to reallocate
  1690. X *        size    - size to change area to
  1691. X *
  1692. X * Returns:    pointer to new area (may be same area)
  1693. X *
  1694. X * Narrative:    verify pointer is within malloc region
  1695. X *        obtain mlist pointer from cptr
  1696. X *        verify magic number is correct
  1697. X *        verify inuse flag is set
  1698. X *        verify connection to adjoining segments is correct
  1699. X *        save requested size
  1700. X *        round-up size to appropriate boundry
  1701. X *        IF size is bigger than what is in this segment
  1702. X *            try to join next segment to this segment
  1703. X *        IF size is less than what is is this segment
  1704. X *            determine leftover amount of space
  1705. X *        ELSE
  1706. X *            allocate new segment of size bites
  1707. X *            IF allocation failed
  1708. X *                return NULL
  1709. X *            copy previous data to new segment
  1710. X *            free previous segment
  1711. X *            return new pointer
  1712. X *        split of extra space in this segment (if any)
  1713. X *        clear bytes beyound what they had before
  1714. X *        return pointer to data 
  1715. X */
  1716. X
  1717. XDATATYPE *
  1718. Xdebug_realloc(file,line,cptr,size)
  1719. X    const char        * file;
  1720. X    int              line;
  1721. X    DATATYPE        * cptr;
  1722. X    SIZETYPE          size;
  1723. X{
  1724. X    static int          call_counter;
  1725. X    char            * func = "realloc";
  1726. X    int              i;
  1727. X    char            * new_cptr;
  1728. X    struct mlist        * ptr;
  1729. X    int              r_size;
  1730. X
  1731. X    MALLOC_INIT();
  1732. X
  1733. X    /*
  1734. X     * increment the call counter
  1735. X     */
  1736. X    call_counter++;
  1737. X
  1738. X    /*
  1739. X     * IF malloc chain checking is on, go do it.
  1740. X     */
  1741. X    if( malloc_checking )
  1742. X    {
  1743. X        VOIDCAST DBFmalloc_chain_check(func,file,line,1);
  1744. X    }
  1745. X
  1746. X#if __STDC__ && ! defined(NO_ANSI_NULLS)
  1747. X    if( cptr == NULL )
  1748. X    {
  1749. X        /*
  1750. X         * else we can't combine it, so lets allocate a new chunk,
  1751. X         * copy the data and free the old chunk...
  1752. X         */
  1753. X        new_cptr = (char *) debug_malloc(file,line,size);
  1754. X
  1755. X        if( new_cptr == (char *) 0)
  1756. X        {
  1757. X            return(new_cptr);
  1758. X        }
  1759. X
  1760. X        /*
  1761. X         * get the malloc internal struct for the new area.  This
  1762. X         * is needed because malloc has identifed the record as a
  1763. X         * malloc call when it is actually a realloc call.  Therefore
  1764. X         * we have to change the identification info.
  1765. X         */
  1766. X        ptr = (struct mlist *) (new_cptr - M_SIZE);
  1767. X        ptr->id = call_counter;
  1768. X        SETTYPE(ptr,M_T_REALLOC);
  1769. X
  1770. X        return(new_cptr);
  1771. X    }
  1772. X
  1773. X#endif /* __STDC__ */
  1774. X
  1775. X    /*
  1776. X     * verify that cptr is within the malloc region...
  1777. X     */
  1778. X    if( cptr < malloc_data_start || cptr > malloc_data_end )
  1779. X    {
  1780. X        malloc_errno = M_CODE_BAD_PTR;
  1781. X        malloc_warning(func,file,line,(struct mlist *)NULL);
  1782. X        return (NULL);
  1783. X    }
  1784. X
  1785. X    /* 
  1786. X     * convert pointer to mlist struct pointer.  To do this we must 
  1787. X     * move the pointer backwards the correct number of bytes...
  1788. X     */
  1789. X    
  1790. X    ptr = (struct mlist *) (((char *)cptr) - M_SIZE);
  1791. X    
  1792. X    if( (ptr->flag&M_MAGIC_BITS) != M_MAGIC )
  1793. X    {
  1794. X        malloc_errno = M_CODE_BAD_MAGIC;
  1795. X        malloc_warning(func,file,line,(struct mlist *)NULL);
  1796. X        return(NULL);
  1797. X    }
  1798. X
  1799. X    if( ! (ptr->flag & M_INUSE) )
  1800. X    {
  1801. X        malloc_errno = M_CODE_NOT_INUSE ;
  1802. X        malloc_warning(func,file,line,ptr);
  1803. X        return(NULL);
  1804. X    }
  1805. X
  1806. X     if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  1807. X        (ptr->next && (ptr->next->prev != ptr) ) ||
  1808. X        ((ptr->next == NULL) && (ptr->prev == NULL)) )
  1809. X    {
  1810. X        malloc_errno = M_CODE_BAD_CONNECT;
  1811. X        malloc_warning(func,file,line,ptr);
  1812. X        return(NULL);
  1813. X    }
  1814. X
  1815. X    r_size = ++size;
  1816. X
  1817. X    M_ROUNDUP(size);
  1818. X
  1819. X    if( size > ptr->s.size )
  1820. X    {
  1821. X        malloc_join(ptr,ptr->next,1,1);
  1822. X    }
  1823. X
  1824. X    if( size > ptr->s.size )
  1825. X    {
  1826. X        /*
  1827. X         * else we can't combine it, so lets allocate a new chunk,
  1828. X         * copy the data and free the old chunk...
  1829. X         */
  1830. X        new_cptr = debug_malloc(file,line,size);
  1831. X
  1832. X        if( new_cptr == (char *) 0)
  1833. X        {
  1834. X            return(new_cptr);
  1835. X        }
  1836. X
  1837. X        if( r_size < ptr->r_size )
  1838. X        {
  1839. X            i = r_size;
  1840. X        }
  1841. X        else
  1842. X        {
  1843. X            i = ptr->r_size;
  1844. X        }
  1845. X        in_malloc_code++;
  1846. X        VOIDCAST memcpy(new_cptr,ptr->data,i);
  1847. X        in_malloc_code--;
  1848. X
  1849. X        free(cptr);
  1850. X
  1851. X        /*
  1852. X         * get the malloc internal struct for the new area.  This
  1853. X         * is needed because malloc has identifed the record as a
  1854. X         * malloc call when it is actually a realloc call.  Therefore
  1855. X         * we have to change the identification info.
  1856. X         */
  1857. X        ptr = (struct mlist *) (new_cptr - M_SIZE);
  1858. X        ptr->id = call_counter;
  1859. X        SETTYPE(ptr,M_T_REALLOC);
  1860. X
  1861. X        return(new_cptr);
  1862. X
  1863. X    } /* else... */
  1864. X
  1865. X    /*
  1866. X     * save amount of real data in new segment (this will be used in the
  1867. X     * memset later) and then save requested size of this segment.
  1868. X     */
  1869. X
  1870. X    if( ptr->r_size < r_size )
  1871. X    {
  1872. X        i = ptr->r_size;
  1873. X    }
  1874. X    else
  1875. X    {
  1876. X        i = r_size;
  1877. X    }
  1878. X
  1879. X    ptr->r_size = r_size;
  1880. X
  1881. X    /*
  1882. X     * split off extra free space at end of this segment, if possible...
  1883. X     */
  1884. X
  1885. X    malloc_split(ptr);
  1886. X
  1887. X    /*
  1888. X     * save the id info.
  1889. X     */    
  1890. X    ptr->file = file;
  1891. X    ptr->line = line;
  1892. X    ptr->id = call_counter;
  1893. X    ptr->hist_id = malloc_hist_id++;
  1894. X    SETTYPE(ptr,M_T_REALLOC);
  1895. X
  1896. X    /*
  1897. X     * if necessary, fill in the last few bytes with the fill character
  1898. X     */
  1899. X    if( malloc_fill_area )
  1900. X    {
  1901. X        malloc_safe_memset( ptr->data+i, M_FILL, (int)(ptr->s.size-i));
  1902. X    }
  1903. X    
  1904. X    return(ptr->data);
  1905. X
  1906. X} /* realloc(... */
  1907. X
  1908. X
  1909. X/*
  1910. X * $Log: realloc.c,v $
  1911. X * Revision 1.15  1992/01/10  17:28:03  cpcahil
  1912. X * Added support for overriding void datatype
  1913. X *
  1914. X * Revision 1.14  1991/12/06  08:54:19  cpcahil
  1915. X * cleanup of __STDC__ usage and addition of CHANGES file
  1916. X *
  1917. X * Revision 1.13  91/12/04  09:23:44  cpcahil
  1918. X * several performance enhancements including addition of free list
  1919. X * 
  1920. X * Revision 1.12  91/12/02  19:10:14  cpcahil
  1921. X * changes for patch release 5
  1922. X * 
  1923. X * Revision 1.11  91/11/25  14:42:05  cpcahil
  1924. X * Final changes in preparation for patch 4 release
  1925. X * 
  1926. X * Revision 1.10  91/11/24  00:49:32  cpcahil
  1927. X * first cut at patch 4
  1928. X * 
  1929. X * Revision 1.9  91/11/20  11:54:11  cpcahil
  1930. X * interim checkin
  1931. X * 
  1932. X * Revision 1.8  90/08/29  21:22:52  cpcahil
  1933. X * miscellaneous lint fixes
  1934. X * 
  1935. X * Revision 1.7  90/05/11  00:13:10  cpcahil
  1936. X * added copyright statment
  1937. X * 
  1938. X * Revision 1.6  90/02/25  11:01:20  cpcahil
  1939. X * added support for malloc chain checking.
  1940. X * 
  1941. X * Revision 1.5  90/02/24  21:50:31  cpcahil
  1942. X * lots of lint fixes
  1943. X * 
  1944. X * Revision 1.4  90/02/24  17:29:39  cpcahil
  1945. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  1946. X * id string
  1947. X * 
  1948. X * Revision 1.3  90/02/24  17:20:00  cpcahil
  1949. X * attempt to get rid of full path in rcs header.
  1950. X * 
  1951. X * Revision 1.2  90/02/24  15:14:20  cpcahil
  1952. X * 1. added function header 
  1953. X * 2. changed calls to malloc_warning to conform to new usage
  1954. X * 3. added setting of malloc_errno
  1955. X *  4. broke up bad pointer determination so that errno's would be more
  1956. X *    descriptive
  1957. X * 
  1958. X * Revision 1.1  90/02/22  23:17:43  cpcahil
  1959. X * Initial revision
  1960. X * 
  1961. X */
  1962. END_OF_FILE
  1963. if test 6787 -ne `wc -c <'realloc.c'`; then
  1964.     echo shar: \"'realloc.c'\" unpacked with wrong size!
  1965. fi
  1966. # end of 'realloc.c'
  1967. fi
  1968. echo shar: End of archive 4 \(of 5\).
  1969. cp /dev/null ark4isdone
  1970. MISSING=""
  1971. for I in 1 2 3 4 5 ; do
  1972.     if test ! -f ark${I}isdone ; then
  1973.     MISSING="${MISSING} ${I}"
  1974.     fi
  1975. done
  1976. if test "${MISSING}" = "" ; then
  1977.     echo You have unpacked all 5 archives.
  1978.     rm -f ark[1-9]isdone
  1979. else
  1980.     echo You still need to unpack the following archives:
  1981.     echo "        " ${MISSING}
  1982. fi
  1983. ##  End of shell archive.
  1984. exit 0
  1985.  
  1986. exit 0 # Just in case...
  1987.