home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / mallc120 / part04 < prev    next >
Encoding:
Text File  |  1993-04-05  |  75.1 KB  |  2,216 lines

  1. Newsgroups: comp.sources.unix
  2. From: gray@antaire.com (Gray Watson)
  3. Subject: v26i101: malloc - debugging version of malloc, V1.2.0, Part04/05
  4. Sender: unix-sources-moderator@vix.com
  5. Approved: paul@vix.com
  6.  
  7. Submitted-By: gray@antaire.com (Gray Watson)
  8. Posting-Number: Volume 26, Issue 101
  9. Archive-Name: malloc-1.2.0/part04
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 4 (of 5)."
  18. # Contents:  chunk.h chunk_loc.h compat.c compat.h conf.h.in
  19. #   malloc.texi malloc_t.c mallocrc
  20. # Wrapped by gray@toaster.antaire.com on Tue Apr  6 01:24:07 1993
  21. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  22. if test -f 'chunk.h' -a "${1}" != "-c" ; then 
  23.   echo shar: Will not clobber existing file \"'chunk.h'\"
  24. else
  25. echo shar: Extracting \"'chunk.h'\" \(2936 characters\)
  26. sed "s/^X//" >'chunk.h' <<'END_OF_FILE'
  27. X/*
  28. X * defines for low level memory management routines
  29. X *
  30. X * Copyright 1992 by Gray Watson and the Antaire Corporation
  31. X *
  32. X * This file is part of the malloc-debug package.
  33. X *
  34. X * This library is free software; you can redistribute it and/or
  35. X * modify it under the terms of the GNU Library General Public
  36. X * License as published by the Free Software Foundation; either
  37. X * version 2 of the License, or (at your option) any later version.
  38. X *
  39. X * This library is distributed in the hope that it will be useful,
  40. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  42. X * Library General Public License for more details.
  43. X *
  44. X * You should have received a copy of the GNU Library General Public
  45. X * License along with this library (see COPYING-LIB); if not, write to the
  46. X * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  47. X *
  48. X * The author of the program may be contacted at gray.watson@antaire.com
  49. X *
  50. X * $Id: chunk.h,v 1.16 1993/04/05 22:29:57 gray Exp $
  51. X */
  52. X
  53. X#ifndef __CHUNK_H__
  54. X#define __CHUNK_H__
  55. X
  56. X#include "malloc.h"            /* for IMPORT ... */
  57. X
  58. X/* some argument defines */
  59. X#define CHUNK_PNT_ANAL    0        /* anal checking on pointer */
  60. X#define CHUNK_PNT_LOOSE    1        /* looser checking */
  61. X
  62. X/*<<<<<<<<<<  The below prototypes are auto-generated by fillproto */
  63. X
  64. XIMPORT    int        _calloc_count;  /* # callocs, done in alloc */
  65. X
  66. X/*
  67. X * startup the low level malloc routines
  68. X */
  69. XIMPORT    int    _chunk_startup(void);
  70. X
  71. X/*
  72. X * run extensive tests on the entire heap depending on TYPE
  73. X */
  74. XIMPORT    int    _chunk_heap_check(void);
  75. X
  76. X/*
  77. X * run extensive tests on PNT from FUNC. test PNT HOW_MUCH of MIN_SIZE
  78. X * (or 0 if unknown).  returns [NO]ERROR
  79. X */
  80. XIMPORT    int    _chunk_pnt_check(const char * func, char * pnt,
  81. X                 const int check, int min_size);
  82. X
  83. X/*
  84. X * return some information associated with PNT, returns [NO]ERROR
  85. X */
  86. XIMPORT    int    _chunk_read_info(char * pnt, unsigned int * size,
  87. X                 char ** file, unsigned int * line);
  88. X
  89. X/*
  90. X * get a SIZE chunk of memory for FILE at LINE
  91. X */
  92. XIMPORT    char    *_chunk_malloc(const char * file, const unsigned int line,
  93. X                   unsigned int size);
  94. X
  95. X/*
  96. X * frees PNT from the heap, returns FREE_ERROR or FREE_NOERROR
  97. X */
  98. XIMPORT    int    _chunk_free(const char * file, const unsigned int line,
  99. X                char * pnt);
  100. X
  101. X/*
  102. X * reallocate a section of memory
  103. X */
  104. XIMPORT    char    *_chunk_realloc(const char * file, const unsigned int line,
  105. X                char * oldp, unsigned int new_size);
  106. X
  107. X/*
  108. X * log present free and used lists
  109. X */
  110. XIMPORT    void    _chunk_list_count(void);
  111. X
  112. X/*
  113. X * log statistics on the heap
  114. X */
  115. XIMPORT    void    _chunk_stats(void);
  116. X
  117. X/*
  118. X * dump the unfreed memory, logs the unfreed information to logger
  119. X */
  120. XIMPORT    void    _chunk_dump_not_freed(void);
  121. X
  122. X/*
  123. X * log the heap structure plus information on the blocks if necessary
  124. X */
  125. XIMPORT    void    _chunk_log_heap_map(void);
  126. X
  127. X/*<<<<<<<<<<   This is end of the auto-generated output from fillproto. */
  128. X
  129. X#endif /* ! __CHUNK_H__ */
  130. END_OF_FILE
  131. if test 2936 -ne `wc -c <'chunk.h'`; then
  132.     echo shar: \"'chunk.h'\" unpacked with wrong size!
  133. fi
  134. # end of 'chunk.h'
  135. fi
  136. if test -f 'chunk_loc.h' -a "${1}" != "-c" ; then 
  137.   echo shar: Will not clobber existing file \"'chunk_loc.h'\"
  138. else
  139. echo shar: Extracting \"'chunk_loc.h'\" \(5339 characters\)
  140. sed "s/^X//" >'chunk_loc.h' <<'END_OF_FILE'
  141. X/*
  142. X * local defines for the low level memory routines
  143. X *
  144. X * Copyright 1992 by Gray Watson and the Antaire Corporation
  145. X *
  146. X * This file is part of the malloc-debug package.
  147. X *
  148. X * This library is free software; you can redistribute it and/or
  149. X * modify it under the terms of the GNU Library General Public
  150. X * License as published by the Free Software Foundation; either
  151. X * version 2 of the License, or (at your option) any later version.
  152. X *
  153. X * This library is distributed in the hope that it will be useful,
  154. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  155. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  156. X * Library General Public License for more details.
  157. X *
  158. X * You should have received a copy of the GNU Library General Public
  159. X * License along with this library (see COPYING-LIB); if not, write to the
  160. X * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  161. X *
  162. X * The author of the program may be contacted at gray.watson@antaire.com
  163. X *
  164. X * $Id: chunk_loc.h,v 1.9 1993/04/05 22:29:58 gray Exp $
  165. X */
  166. X
  167. X#ifndef __CHUNK_LOC_H__
  168. X#define __CHUNK_LOC_H__
  169. X
  170. X/* defines for the malloc subsystem */
  171. X#define BASIC_BLOCK        12    /* basic block size in bits */
  172. X#define SMALLEST_BLOCK         4    /* smallest block size in bits */
  173. X#define LARGEST_BLOCK        24    /* allowable allocation in bits */
  174. X#define LARGEST_NORMAL_BLOCK    16    /* largest normal allocation in bits */
  175. X
  176. X#define BLOCK_SIZE        (1 << BASIC_BLOCK)
  177. X
  178. X#define IS_DBLOCK(size)        ((size) < BLOCK_SIZE)
  179. X
  180. X/* number of blocks in the administrative structures */
  181. X#define BB_PER_ADMIN    ((BLOCK_SIZE - \
  182. X              (sizeof(long) + sizeof(int) + sizeof(char *) + \
  183. X               sizeof(long))) / sizeof(bblock_t))
  184. X#define DB_PER_ADMIN    ((BLOCK_SIZE - (sizeof(long) + sizeof(long))) \
  185. X             / sizeof(dblock_t))
  186. X
  187. X#define CHUNK_MAGIC_BASE    0xDEA007    /* base magic number */
  188. X#define CHUNK_MAGIC_TOP        0x976DEAD    /* top magic number */
  189. X
  190. X#define WHAT_BLOCK(pnt)        (((long)(pnt) / BLOCK_SIZE) * BLOCK_SIZE)
  191. X#define CHUNK_TO_USER(pnt)    ((pnt) + pnt_below_adm)
  192. X
  193. X/* bb_flags values */
  194. X#define BBLOCK_ALLOCATED    0x3F        /* block has been allocated */
  195. X#define BBLOCK_START_USER    0x01        /* start of some user space */
  196. X#define BBLOCK_USER        0x02        /* allocated by user space */
  197. X#define BBLOCK_ADMIN        0x04        /* pointing to bblock admin */
  198. X#define BBLOCK_DBLOCK        0x08        /* pointing to divided block */
  199. X#define BBLOCK_DBLOCK_ADMIN    0x10        /* pointing to dblock admin */
  200. X#define BBLOCK_FREE        0x20        /* block is free */
  201. X
  202. X/*
  203. X * single divided-block administrative structure
  204. X */
  205. Xstruct dblock_st {
  206. X  union {
  207. X    struct {
  208. X      unsigned short    nu_size;        /* size of contiguous area */
  209. X      unsigned short    nu_line;        /* line where it was alloced */
  210. X    } in_nums;
  211. X    
  212. X    struct bblock_st    *in_bblock;        /* pointer to the bblock */
  213. X  } db_info;
  214. X  
  215. X  /* to reference union and struct elements as db elements */
  216. X#define db_bblock    db_info.in_bblock    /* F */
  217. X#define db_size        db_info.in_nums.nu_size    /* U */
  218. X#define db_line        db_info.in_nums.nu_line    /* U */
  219. X  
  220. X  union {
  221. X    struct dblock_st    *pn_next;        /* next in the free list */
  222. X    char        *pn_file;        /* .c filename where alloced */
  223. X  } db_pnt;
  224. X  
  225. X  /* to reference union elements as db elements */
  226. X#define db_next        db_pnt.pn_next        /* F */
  227. X#define db_file        db_pnt.pn_file        /* U */
  228. X  
  229. X};
  230. Xtypedef struct dblock_st    dblock_t;
  231. X
  232. X/*
  233. X * single basic-block administrative structure
  234. X */
  235. Xstruct bblock_st {
  236. X  unsigned short    bb_flags;        /* what it is */
  237. X  
  238. X  union {
  239. X    unsigned short    nu_bitc;        /* chunk size */
  240. X    unsigned short    nu_line;        /* line where it was alloced */
  241. X  } bb_num;
  242. X  
  243. X  /* to reference union elements as bb elements */
  244. X#define bb_bitc        bb_num.nu_bitc        /* DF */
  245. X#define bb_line        bb_num.nu_line        /* U */
  246. X  
  247. X  union {
  248. X    unsigned int    in_count;        /* admin count number */
  249. X    dblock_t        *in_dblock;        /* pointer to dblock info */
  250. X    struct bblock_st    *in_next;        /* next in free list */
  251. X    unsigned int    in_size;        /* size of allocation */
  252. X  } bb_info;
  253. X  
  254. X  /* to reference union elements as bb elements */
  255. X#define bb_count    bb_info.in_count    /* A */
  256. X#define    bb_dblock    bb_info.in_dblock    /* D */
  257. X#define    bb_next        bb_info.in_next        /* F */
  258. X#define    bb_size        bb_info.in_size        /* U */
  259. X  
  260. X  union {
  261. X    struct dblock_adm_st    *pn_slotp;    /* pointer to db_admin block */
  262. X    struct bblock_adm_st    *pn_adminp;    /* pointer to bb_admin block */
  263. X    char            *pn_mem;    /* memory associated to it */
  264. X    char            *pn_file;    /* .c filename where alloced */
  265. X  } bb_pnt;
  266. X  
  267. X  /* to reference union elements as bb elements */
  268. X#define    bb_slotp    bb_pnt.pn_slotp        /* a */
  269. X#define    bb_adminp    bb_pnt.pn_adminp    /* A */
  270. X#define    bb_mem        bb_pnt.pn_mem        /* DF */
  271. X#define    bb_file        bb_pnt.pn_file        /* U */
  272. X  
  273. X};
  274. Xtypedef struct bblock_st    bblock_t;
  275. X
  276. X/*
  277. X * collection of bblock admin structures
  278. X */
  279. Xstruct bblock_adm_st {
  280. X  long            ba_magic1;        /* bottom magic number */
  281. X  int            ba_count;        /* position in bblock array */
  282. X  bblock_t        ba_block[BB_PER_ADMIN];    /* bblock admin info */
  283. X  struct bblock_adm_st    *ba_next;        /* next bblock adm struct */
  284. X  long            ba_magic2;        /* top magic number */
  285. X};
  286. Xtypedef struct bblock_adm_st    bblock_adm_t;
  287. X
  288. X/*
  289. X * collection of dblock admin structures
  290. X */
  291. Xstruct dblock_adm_st {
  292. X  long            da_magic1;        /* bottom magic number */
  293. X  dblock_t        da_block[DB_PER_ADMIN];    /* dblock admin info */
  294. X  long            da_magic2;        /* top magic number */
  295. X};
  296. Xtypedef struct dblock_adm_st    dblock_adm_t;
  297. X
  298. X#endif /* ! __CHUNK_LOC_H__ */
  299. END_OF_FILE
  300. if test 5339 -ne `wc -c <'chunk_loc.h'`; then
  301.     echo shar: \"'chunk_loc.h'\" unpacked with wrong size!
  302. fi
  303. # end of 'chunk_loc.h'
  304. fi
  305. if test -f 'compat.c' -a "${1}" != "-c" ; then 
  306.   echo shar: Will not clobber existing file \"'compat.c'\"
  307. else
  308. echo shar: Extracting \"'compat.c'\" \(4789 characters\)
  309. sed "s/^X//" >'compat.c' <<'END_OF_FILE'
  310. X/*
  311. X * compatibility functions for those systems who are missing them.
  312. X *
  313. X * Copyright 1992 by Gray Watson and the Antaire Corporation
  314. X *
  315. X * This file is part of the malloc-debug package.
  316. X *
  317. X * This library is free software; you can redistribute it and/or
  318. X * modify it under the terms of the GNU Library General Public
  319. X * License as published by the Free Software Foundation; either
  320. X * version 2 of the License, or (at your option) any later version.
  321. X *
  322. X * This library is distributed in the hope that it will be useful,
  323. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  324. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  325. X * Library General Public License for more details.
  326. X *
  327. X * You should have received a copy of the GNU Library General Public
  328. X * License along with this library (see COPYING-LIB); if not, write to the
  329. X * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  330. X *
  331. X * The author of the program may be contacted at gray.watson@antaire.com
  332. X */
  333. X
  334. X/*
  335. X * This file holds the compatibility routines necessary for the library if
  336. X * your system does not have them.
  337. X */
  338. X
  339. X#define MALLOC_DEBUG_DISABLE
  340. X
  341. X#include "malloc.h"
  342. X#include "malloc_loc.h"
  343. X
  344. X#include "compat.h"
  345. X#include "conf.h"
  346. X
  347. X#if INCLUDE_RCS_IDS
  348. XLOCAL    char    *rcs_id =
  349. X  "$Id: compat.c,v 1.8 1993/04/05 01:28:54 gray Exp $";
  350. X#endif
  351. X
  352. X#if HAVE_MEMCPY == 0 && HAVE_BCOPY == 0
  353. X/*
  354. X * copy LEN characters from FROM to TO
  355. X */
  356. XEXPORT    char    *memcpy(char * to, const char * from, int len)
  357. X{
  358. X  char    *hold = to;
  359. X  
  360. X  for (; len > 0; len--, to++, from++)
  361. X    *to = *from;
  362. X  
  363. X  return hold;
  364. X}
  365. X#endif /* HAVE_MEMCPY == 0 && HAVE_BCOPY == 0 */
  366. X
  367. X#if HAVE_MEMCMP == 0 && HAVE_BCMP == 0
  368. X/*
  369. X * compare LEN characters, return -1,0,1 if STR1 is <,==,> STR2
  370. X */
  371. XEXPORT    int    memcmp(const char * str1, const char * str2, int len)
  372. X{
  373. X  for (; len > 0; len--, str1++, str2++)
  374. X    if (*str1 != *str2)
  375. X      return *str1 - *str2;
  376. X  
  377. X  return 0;
  378. X}
  379. X#endif /* HAVE_MEMCMP == 0 && HAVE_BCMP == 0 */
  380. X
  381. X#if HAVE_MEMSET == 0
  382. X/*
  383. X * set LEN characters in STR to character CH
  384. X */
  385. XEXPORT    char    *memset(char * str, int ch, int len)
  386. X{
  387. X  char    *hold = str;
  388. X  
  389. X  for (; len > 0; len--, str++)
  390. X    *str = (char)ch;
  391. X  
  392. X  return hold;
  393. X}
  394. X#endif /* HAVE_MEMSET == 0 */
  395. X
  396. X#if HAVE_INDEX == 0
  397. X/*
  398. X * find CH in STR by searching backwards through the string
  399. X */
  400. XEXPORT    char    *index(const char * str, int ch)
  401. X{
  402. X  for (; *str != NULLC; str++)
  403. X    if (*str == (char)ch)
  404. X      return str;
  405. X  
  406. X  return NULL;
  407. X}
  408. X#endif /* HAVE_INDEX == 0 */
  409. X
  410. X#if HAVE_RINDEX == 0
  411. X/*
  412. X * find CH in STR by searching backwards through the string
  413. X */
  414. XEXPORT    char    *rindex(const char * str, int ch)
  415. X{
  416. X  char    *pnt = NULL;
  417. X  
  418. X  for (; *str != NULLC; str++)
  419. X    if (*str == (char)ch)
  420. X      pnt = str;
  421. X  
  422. X  return pnt;
  423. X}
  424. X#endif /* HAVE_RINDEX == 0 */
  425. X
  426. X#if HAVE_STRCAT == 0
  427. X/*
  428. X * concatenate STR2 onto the end of STR1
  429. X */
  430. XEXPORT    char    *strcat(char * str1, const char * str2)
  431. X{
  432. X  char    *hold = str1;
  433. X  
  434. X  for (; *str1 != NULLC; str1++);
  435. X  
  436. X  while (*str2 != NULLC)
  437. X    *str1++ = *str2++;
  438. X  *str1 = NULLC;
  439. X  
  440. X  return hold;
  441. X}
  442. X#endif /* HAVE_STRCAT == 0 */
  443. X
  444. X#if HAVE_STRCMP == 0
  445. X/*
  446. X * returns -1,0,1 on whether STR1 is <,==,> STR2
  447. X */
  448. XEXPORT    int    strcmp(const char * str1, const char * str2)
  449. X{
  450. X  for (; *str1 != NULLC && *str1 == *str2; str1++, str2++);
  451. X  return *str1 - *str2;
  452. X}
  453. X#endif /* HAVE_STRCMP == 0 */
  454. X
  455. X#if HAVE_STRLEN == 0
  456. X/*
  457. X * return the length in characters of STR
  458. X */
  459. XEXPORT    int    strlen(const char * str)
  460. X{
  461. X  int    len;
  462. X  
  463. X  for (len = 0; *str != NULLC; str++, len++);
  464. X  
  465. X  return len;
  466. X}
  467. X#endif /* HAVE_STRLEN == 0 */
  468. X
  469. X#if HAVE_STRTOK == 0
  470. X/*
  471. X * get the next token from STR (pass in NULL on the 2nd, 3rd, etc. calls),
  472. X * tokens are a list of characters deliminated by a character from DELIM.
  473. X * writes null into STR to end token.
  474. X */
  475. XEXPORT    char    *strtok(char * str, char * delim)
  476. X{
  477. X  static char    *last_str = "";
  478. X  char        *start, *delimp;
  479. X  
  480. X  /* no new strings to search? */
  481. X  if (str != NULL)
  482. X    last_str = str;
  483. X  else
  484. X    /* have we reached end of old one? */
  485. X    if (*last_str == NULLC)
  486. X      return NULL;
  487. X  
  488. X  /* parse through starting token deliminators */
  489. X  for (; *last_str != NULLC; last_str++) {
  490. X    for (delimp = delim; *delimp != NULLC; delimp++)
  491. X      if (*last_str == *delimp)
  492. X    break;
  493. X    
  494. X    /* is the character NOT in the delim list? */
  495. X    if (*delimp == NULLC)
  496. X      break;
  497. X  }
  498. X  
  499. X  /* did we reach the end? */
  500. X  if (*last_str == NULLC)
  501. X    return NULL;
  502. X  
  503. X  /* now start parsing through the string, could be NULLC already */
  504. X  for (start = last_str; *last_str != NULLC; last_str++)
  505. X    for (delimp = delim; *delimp != NULLC; delimp++)
  506. X      if (*last_str == *delimp) {
  507. X    /* punch NULL and point last_str past it */
  508. X    *last_str++ = NULLC;
  509. X    return start;
  510. X      }
  511. X  
  512. X  /* reached the end of the string */
  513. X  return start;
  514. X}
  515. X#endif /* HAVE_STRTOK == 0 */
  516. END_OF_FILE
  517. if test 4789 -ne `wc -c <'compat.c'`; then
  518.     echo shar: \"'compat.c'\" unpacked with wrong size!
  519. fi
  520. # end of 'compat.c'
  521. fi
  522. if test -f 'compat.h' -a "${1}" != "-c" ; then 
  523.   echo shar: Will not clobber existing file \"'compat.h'\"
  524. else
  525. echo shar: Extracting \"'compat.h'\" \(2476 characters\)
  526. sed "s/^X//" >'compat.h' <<'END_OF_FILE'
  527. X/*
  528. X * header file for compatibility functions.
  529. X *
  530. X * Copyright 1992 by Gray Watson and the Antaire Corporation
  531. X *
  532. X * This file is part of the malloc-debug package.
  533. X *
  534. X * This library is free software; you can redistribute it and/or
  535. X * modify it under the terms of the GNU Library General Public
  536. X * License as published by the Free Software Foundation; either
  537. X * version 2 of the License, or (at your option) any later version.
  538. X *
  539. X * This library is distributed in the hope that it will be useful,
  540. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  541. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  542. X * Library General Public License for more details.
  543. X *
  544. X * You should have received a copy of the GNU Library General Public
  545. X * License along with this library (see COPYING-LIB); if not, write to the
  546. X * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  547. X *
  548. X * The author of the program may be contacted at gray.watson@antaire.com
  549. X *
  550. X * $Id: compat.h,v 1.7 1993/04/05 01:28:57 gray Exp $
  551. X */
  552. X
  553. X#ifndef __COMPAT_H__
  554. X#define __COMPAT_H__
  555. X
  556. X#include "malloc.h"            /* for IMPORT ... */
  557. X
  558. X/*<<<<<<<<<<  The below prototypes are auto-generated by fillproto */
  559. X
  560. X/*
  561. X * copy LEN characters from FROM to TO
  562. X */
  563. X#if __GNUC__ < 2
  564. XIMPORT    char    *memcpy(char * to, const char * from, int len);
  565. X#endif
  566. X
  567. X/*
  568. X * compare LEN characters, return -1,0,1 if STR1 is <,==,> STR2
  569. X */
  570. X#if __GNUC__ < 2
  571. XIMPORT    int    memcmp(const char * str1, const char * str2, int len);
  572. X#endif
  573. X
  574. X/*
  575. X * set LEN characters in STR to character CH
  576. X */
  577. XIMPORT    char    *memset(char * str, int ch, int len);
  578. X
  579. X/*
  580. X * find CH in STR by searching backwards through the string
  581. X */
  582. XIMPORT    char    *index(const char * str, int ch);
  583. X
  584. X/*
  585. X * find CH in STR by searching backwards through the string
  586. X */
  587. XIMPORT    char    *rindex(const char * str, int ch);
  588. X
  589. X/*
  590. X * concatenate STR2 onto the end of STR1
  591. X */
  592. XIMPORT    char    *strcat(char * str1, const char * str2);
  593. X
  594. X/*
  595. X * returns -1,0,1 on whether STR1 is <,==,> STR2
  596. X */
  597. XIMPORT    int    strcmp(const char * str1, const char * str2);
  598. X
  599. X/*
  600. X * return the length in characters of STR
  601. X */
  602. XIMPORT    int    strlen(const char * str);
  603. X
  604. X/*
  605. X * get the next token from STR (pass in NULL on the 2nd, 3rd, etc. calls),
  606. X * tokens are a list of characters deliminated by a character from DELIM.
  607. X * writes null into STR to end token.
  608. X */
  609. XIMPORT    char    *strtok(char * str, char * delim);
  610. X
  611. X/*<<<<<<<<<<   This is end of the auto-generated output from fillproto. */
  612. X
  613. X#endif /* ! __COMPAT_H__ */
  614. END_OF_FILE
  615. if test 2476 -ne `wc -c <'compat.h'`; then
  616.     echo shar: \"'compat.h'\" unpacked with wrong size!
  617. fi
  618. # end of 'compat.h'
  619. fi
  620. if test -f 'conf.h.in' -a "${1}" != "-c" ; then 
  621.   echo shar: Will not clobber existing file \"'conf.h.in'\"
  622. else
  623. echo shar: Extracting \"'conf.h.in'\" \(4016 characters\)
  624. sed "s/^X//" >'conf.h.in' <<'END_OF_FILE'
  625. X/*
  626. X * machine configuration flags
  627. X *
  628. X * Copyright 1992 by Gray Watson and the Antaire Corporation
  629. X *
  630. X * This file is part of the malloc-debug package.
  631. X *
  632. X * This library is free software; you can redistribute it and/or
  633. X * modify it under the terms of the GNU Library General Public
  634. X * License as published by the Free Software Foundation; either
  635. X * version 2 of the License, or (at your option) any later version.
  636. X *
  637. X * This library is distributed in the hope that it will be useful,
  638. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  639. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  640. X * Library General Public License for more details.
  641. X *
  642. X * You should have received a copy of the GNU Library General Public
  643. X * License along with this library (see COPYING-LIB); if not, write to the
  644. X * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  645. X *
  646. X * The author of the program may be contacted at gray.watson@antaire.com
  647. X *
  648. X * $Id: conf.h.in,v 1.9 1993/04/06 04:24:30 gray Exp $
  649. X */
  650. X
  651. X/*
  652. X ************************************************************************
  653. X * USER SETTINGS:
  654. X ************************************************************************
  655. X */
  656. X
  657. X/*
  658. X * include the RCS ids in the .c files and the installed library
  659. X */
  660. X#define INCLUDE_RCS_IDS 1
  661. X
  662. X/*
  663. X * should we allow zero length allocations or should be registered as errors.
  664. X * WARNING: this has not been tested although some code does exist for it.
  665. X */
  666. X#define ALLOW_ALLOC_ZERO_SIZE 0
  667. X
  668. X/*
  669. X * should we allow realloc of a NULL pointer.  this is useful when you are
  670. X * extending an array in a loop and do not want to allocate it specially the
  671. X * first time.
  672. X */
  673. X#define ALLOW_REALLOC_NULL 1
  674. X
  675. X/*
  676. X ************************************************************************
  677. X * SPECIAL DEFINES:
  678. X ************************************************************************
  679. X */
  680. X
  681. X/*
  682. X * (char *)sbrk(int incr) is the main system memory allocation routine.  this
  683. X * extends the program's data space by INCR bytes.
  684. X */
  685. X#define HAVE_SBRK 0
  686. X
  687. X/*
  688. X * does your heap grow up?  Hopefully it does because there is not too much
  689. X * support for growing-down heaps because I do not have a system to test it on.
  690. X */
  691. X#define HEAP_GROWS_UP 1
  692. X
  693. X/*
  694. X * the alignment value of all allocations in number of bits (i.e. 2 ^ X) for
  695. X * fence-post checking.  it needs to be a base 2 number and 3 (i.e. 2 ^ 3 == 8)
  696. X * should work.  2 bits may also work on i386/i486 systems (for instance) but
  697. X * some RISC boxes (sparc for instance) require certain memory to be base 8
  698. X * (stack frames, code, etc.).
  699. X *
  700. X * NOTE: larger the number the more memory will be wasted when fence-checking
  701. X * NOTE: this is not necessarily the smallest possible allocated memory chunk
  702. X */
  703. X#define ALLOCATION_ALIGNMENT_IN_BITS 3
  704. X
  705. X/*
  706. X ************************************************************************
  707. X * LIBRARY DEFINES:
  708. X ************************************************************************
  709. X */
  710. X
  711. X/*
  712. X * the Malloc-Debug library provides its own versions of the following
  713. X * functions,  or knows how to work around their absence.
  714. X */
  715. X#define HAVE_BCMP 0
  716. X#define HAVE_BCOPY 0
  717. X
  718. X#define HAVE_MEMCMP 0
  719. X#define HAVE_MEMCPY 0
  720. X#define HAVE_MEMSET 0
  721. X
  722. X#define HAVE_INDEX 0
  723. X#define HAVE_RINDEX 0
  724. X
  725. X#define HAVE_STRCAT 0
  726. X#define HAVE_STRCMP 0
  727. X#define HAVE_STRLEN 0
  728. X#define HAVE_STRTOK 0
  729. X
  730. X/*
  731. X * the below functions are here to provide function argument checking only.
  732. X */
  733. X#define HAVE_BZERO 0
  734. X
  735. X#define HAVE_MEMCCPY 0
  736. X#define HAVE_MEMCHR 0
  737. X
  738. X#define HAVE_STRCHR 0
  739. X#define HAVE_STRRCHR 0
  740. X#define HAVE_STRCPY 0
  741. X#define HAVE_STRNCPY 0
  742. X#define HAVE_STRCASECMP 0
  743. X#define HAVE_STRNCASECMP 0
  744. X#define HAVE_STRSPN 0
  745. X#define HAVE_STRCSPN 0
  746. X#define HAVE_STRNCAT 0
  747. X#define HAVE_STRNCMP 0
  748. X#define HAVE_STRPBRK 0
  749. X#define HAVE_STRSTR 0
  750. X
  751. X/*
  752. X * some defines to standardize memory functions
  753. X */
  754. X#if HAVE_BCMP == 0
  755. X#define bcmp(s1, s2, len)    (void)memcmp((char *)(s1), (char *)(s2), (len))
  756. X#endif
  757. X
  758. X#if HAVE_BCOPY == 0
  759. X#define bcopy(from, to, len)    (void)memcpy((char *)(to), (char *)(from), len)
  760. X#endif
  761. END_OF_FILE
  762. if test 4016 -ne `wc -c <'conf.h.in'`; then
  763.     echo shar: \"'conf.h.in'\" unpacked with wrong size!
  764. fi
  765. # end of 'conf.h.in'
  766. fi
  767. if test -f 'malloc.texi' -a "${1}" != "-c" ; then 
  768.   echo shar: Will not clobber existing file \"'malloc.texi'\"
  769. else
  770. echo shar: Extracting \"'malloc.texi'\" \(41525 characters\)
  771. sed "s/^X//" >'malloc.texi' <<'END_OF_FILE'
  772. X\input texinfo @c -*-texinfo-*-
  773. X@c %**start of header
  774. X@setfilename malloc.info
  775. X@settitle Malloc Tutorial
  776. X@c %**end of header
  777. X
  778. X@c ================================================================
  779. X@c This file has the new style title page commands.
  780. X@c Also, it uses `@@include' files instead of `@@input' files.
  781. X
  782. X@c Run using special version of `texinfo.tex'.
  783. X
  784. X@c Also, run `makeinfo' rather than `texinfo-format-buffer'.
  785. X@c ================================================================
  786. X
  787. X@c ================================================================
  788. X@c $Id: malloc.texi,v 1.12 1993/04/06 04:24:36 gray Exp $
  789. X@c ================================================================
  790. X
  791. X@ifinfo
  792. XThis file is an introduction to the Malloc library which handles general
  793. Xmemory heap management.
  794. XCopyright @copyright{} 1992 by Gray Watson and the Antaire Corporation.
  795. X
  796. XPermission is granted to make and distribute verbatim copies of this
  797. Xmanual provided the copyright notice and this permission notice are
  798. Xpreserved on all copies.
  799. X
  800. X@ignore
  801. XPermission is granted to process this file through TeX and print the
  802. Xresults, provided the printed document carries a copying permission
  803. Xnotice identical to this one except for the removal of this paragraph
  804. X(this paragraph not being relevant to the printed manual).
  805. X
  806. X@end ignore
  807. XPermission is granted to copy and distribute modified versions of this
  808. Xmanual under the conditions for verbatim copying, provided also that the
  809. Xsection entitled ``Copying'' are included exactly as in the original,
  810. Xand provided that the entire resulting derived work is distributed under
  811. Xthe terms of a permission notice identical to this one.
  812. X
  813. XPermission is granted to copy and distribute translations of this manual
  814. Xinto another language, under the above conditions for modified versions,
  815. Xexcept that the section entitled ``Copying'' may be included in a
  816. Xtranslation approved by the author instead of in the original English.
  817. X@end ifinfo
  818. X
  819. X@titlepage
  820. X@title Malloc Debug Library
  821. X@author Gray Watson @samp{<gray.watson@@antaire.com>}
  822. X
  823. X@page
  824. XCopyright @copyright{} 1992 by Gray Watson and the Antaire Corporation.
  825. X
  826. XPublished by Gray Watson @samp{<gray.watson@@antaire.com>}
  827. X
  828. XPermission is granted to make and distribute verbatim copies of this
  829. Xmanual provided the copyright notice and this permission notice are
  830. Xpreserved on all copies.
  831. X
  832. XPermission is granted to copy and distribute modified versions of this
  833. Xmanual under the conditions for verbatim copying, provided also that the
  834. Xsection entitled ``Copying'' are included exactly as in the original,
  835. Xand provided that the entire resulting derived work is distributed under
  836. Xthe terms of a permission notice identical to this one.
  837. X
  838. XPermission is granted to copy and distribute translations of this manual
  839. Xinto another language, under the above conditions for modified versions,
  840. Xexcept that the section entitled ``Copying'' may be included in a
  841. Xtranslation approved by the author instead of in the original English.
  842. X@end titlepage
  843. X
  844. X@node Top, Copying, (dir), (dir)
  845. X@top Malloc Debug Library
  846. X
  847. XThis file documents the general-usage and the inner-workings of the
  848. Xmemory allocation or @dfn{malloc} library it accompanies.
  849. X
  850. XThis malloc library has been designed as a drop in replacement for the
  851. Xsystem's malloc, realloc, calloc, free and other memory management
  852. Xroutines.  For more information about their capabilities, do a @code{man
  853. X3 malloc} to read the system's manual pages.
  854. X
  855. XWhat is unique about this library is that it contains a number of
  856. Xpowerful debugging facilities including very comprehensive heap testing
  857. Xand excellent run-time debugging information.  We have found these
  858. Xcapabilities to be superb development tools.
  859. X
  860. XI can be reached at @samp{<gray.watson@@antaire.com>} with any questions
  861. Xor general comments.
  862. X
  863. XGray Watson, Antaire Corporation.
  864. X
  865. X@menu
  866. X* Copying::             Library copying conditions.
  867. X* Allocation Basics::   Basic description of terms and functions.
  868. X* Features::            Description of the benefits of the library.
  869. X* Usage::               How to run programs with the library.
  870. X* Code::                Information on the source and general concerns.
  871. X* Plugs::               A couple soapbox comments.
  872. X@end menu
  873. X
  874. X@node Copying, Allocation Basics, Top, Top
  875. X@chapter Library Copying Conditions.
  876. X
  877. XThis package is covered by the GNU Library Public License.  See the file
  878. X@file{COPYING-LIB} for details.  If you would like to do something with
  879. Xthis package that you feel is reasonable but prohibited by the license,
  880. Xplease contact me to see if we can work it out.
  881. X
  882. X@emph{NOTICE}: this is not the GNU Public License but the @emph{library}
  883. Xpublic license.  This license allows you to do more with the library
  884. Xthan the standard public license distributed with most GNU software.
  885. XPlease read @file{COPYING-LIB} or contact me for more information.
  886. X
  887. XThe rest of this section contains some messages from the Free Software
  888. XFoundation.  If you find this stuff offensive or annoying, remember that
  889. Xyou probably did not spend any money to get this library so feel free to
  890. Xheave it into the bit bucket.
  891. X
  892. XThe licenses for most software are designed to take away your freedom to
  893. Xshare and change it.  By contrast, the GNU General Public Licenses are
  894. Xintended to guarantee your freedom to share and change free software--to
  895. Xmake sure the software is free for all its users.
  896. X
  897. XThis license, the Library General Public License, applies to some
  898. Xspecially designated Free Software Foundation software, and to any other
  899. Xlibraries whose authors decide to use it.  You can use it for your
  900. Xlibraries, too.
  901. X
  902. XWhen we speak of free software, we are referring to freedom, not price.
  903. XOur General Public Licenses are designed to make sure that you have the
  904. Xfreedom to distribute copies of free software (and charge for this
  905. Xservice if you wish), that you receive source code or can get it if you
  906. Xwant it, that you can change the software or use pieces of it in new
  907. Xfree programs; and that you know you can do these things.
  908. X
  909. XTo protect your rights, we need to make restrictions that forbid anyone
  910. Xto deny you these rights or to ask you to surrender the rights.  These
  911. Xrestrictions translate to certain responsibilities for you if you
  912. Xdistribute copies of the library, or if you modify it.
  913. X
  914. XFor example, if you distribute copies of the library, whether gratis or
  915. Xfor a fee, you must give the recipients all the rights that we gave you.
  916. XYou must make sure that they, too, receive or can get the source code.
  917. XIf you link a program with the library, you must provide complete object
  918. Xfiles to the recipients so that they can relink them with the library,
  919. Xafter making changes to the library and recompiling it.  And you must
  920. Xshow them these terms so they know their rights.
  921. X
  922. XOur method of protecting your rights has two steps: (1) copyright the
  923. Xlibrary, and (2) offer you this license which gives you legal permission
  924. Xto copy, distribute and/or modify the library.
  925. X
  926. XAlso, for each distributor's protection, we want to make certain that
  927. Xeveryone understands that there is no warranty for this free library.
  928. XIf the library is modified by someone else and passed on, we want its
  929. Xrecipients to know that what they have is not the original version, so
  930. Xthat any problems introduced by others will not reflect on the original
  931. Xauthors' reputations.
  932. X
  933. X@node Allocation Basics, Features, Copying, Top
  934. X@chapter Basic Description of Terms and Functions.
  935. X
  936. XThis section provides a basic definition of terms used throughout the
  937. Xmanual as well as a brief overview of the basic malloc functions and
  938. Xexamples of their use.  It is quite unnecessary for you to read this
  939. Xsection if you are familiar with using the heap allocation functions.
  940. X
  941. X@menu
  942. X* Basic definitions::   For defining general terms and concepts.
  943. X* Malloc functions::    Functionality supported by all malloc libs.
  944. X@end menu
  945. X
  946. X@node Basic definitions, Malloc functions,  , Allocation Basics
  947. X@section For Defining General Terms and Concepts.
  948. X
  949. XAny program can be divided into 2 logical parts: text and data.  Text is
  950. Xthe actual program code in machine-readable format and data is the
  951. Xinformation that the text operates on when it is executing.  The data,
  952. Xin turn, can be divided into 3 logical parts according to where it is
  953. Xstored: @dfn{static}, @dfn{stack}, and @dfn{heap}.
  954. X
  955. XStatic data is the information whose storage space is compiled into the
  956. Xprogram.
  957. X@example
  958. X        /* global variables are allocated as static data */
  959. X        int numbers[10];
  960. X
  961. X        main()
  962. X        @{
  963. X           @dots{}
  964. X        @}
  965. X@end example
  966. XStack data is data allocated at run-time to hold information used inside
  967. Xof functions.  This data is managed by the system in the space called
  968. Xstack space.
  969. X
  970. X@example
  971. X        void foo()
  972. X        @{
  973. X           /* this local variable is allocated on the stack */
  974. X           float total;
  975. X
  976. X           @dots{}
  977. X        @}
  978. X@end example
  979. XHeap data is also allocated at run-time and provides a programmer with
  980. Xdynamic memory capabilities.
  981. X
  982. X@example
  983. X        main()
  984. X        @{
  985. X           char * string;
  986. X           @dots{}
  987. X        
  988. X           /* allocate a string of 10 bytes */
  989. X           string = (char *)malloc(10);
  990. X           @dots{}
  991. X           /* de-allocate the string now that I'm done with it */
  992. X           (void)free(string);
  993. X
  994. X           @dots{}
  995. X        @}
  996. X@end example
  997. XIt is the heap data that is managed by this library.
  998. X
  999. XAlthough the above is an example of how to use the malloc and free
  1000. Xcommands, it is not a good example of why using the heap for run-time
  1001. Xstorage is useful.
  1002. X
  1003. XConsider this: You write a program that reads a file into memory,
  1004. Xprocesses it, and displays results.  You would like to handle files with
  1005. Xarbitrary size (from 10 bytes to 1.2 megabytes and more).  One problem,
  1006. Xhowever, is that the entire file must be in memory at one time to do the
  1007. Xcalculations.  You don't want to have to allocate 1.2 megabytes when you
  1008. Xmight only be reading in a 10 byte file because it is wasteful of system
  1009. Xresources.  Also, you are worried that your program might have to handle
  1010. Xfiles of more than 1.2 megabytes.
  1011. X
  1012. XA solution: first checkout the file's size and then, using the
  1013. Xheap-allocation routines, get enough storage to read the entire file
  1014. Xinto memory.  The program will only be using the system resources
  1015. Xnecessary for the job and you will be guaranteed that your program can
  1016. Xhandle any sized file.
  1017. X
  1018. X@node Malloc functions,  , Basic definitions, Allocation Basics
  1019. X@section Functionality Supported by all Malloc Libraries.
  1020. X
  1021. XAll malloc libraries support 4 basic memory allocation commands.  These
  1022. Xinclude @dfn{malloc}, @dfn{calloc}, @dfn{realloc}, and @dfn{free}.
  1023. X
  1024. X@table @samp
  1025. X@item malloc
  1026. XUsage: @code{pnt = (type *)malloc(unsigned int size);}
  1027. X
  1028. XThe malloc routine is the basic memory allocation routine.  It allocates
  1029. Xan area of size bytes.  It will return a pointer to the space requested.
  1030. X
  1031. X@item calloc
  1032. XUsage: @code{pnt = (type *)calloc(unsigned int number, unsigned int
  1033. Xsize);}
  1034. X
  1035. XThe calloc routine allocates a certain number of items, each of size
  1036. Xbytes, and returns a pointer to the space.  It is appropriate to pass in
  1037. Xa @code{sizeof(type)} value as the size argument.
  1038. X
  1039. XAlso, calloc nulls the space that it returns, assuring that the memory
  1040. Xis all zeros.
  1041. X
  1042. X@item realloc
  1043. XUsage: @code{new_pnt = (type *)realloc(void * old_pnt, unsigned
  1044. Xint new_size);}
  1045. X
  1046. XThe realloc function expands or shrinks the memory allocation in old_pnt
  1047. Xto new_size number of bytes.  Realloc copies the information in old_pnt
  1048. Xinto the new_pnt space up to new_size bytes or until it copies all of
  1049. Xthe information from old_pnt.
  1050. X
  1051. X@item free
  1052. XUsage: @code{(void)free(void * pnt);}
  1053. X
  1054. XThe free routine releases an allocation returned by malloc, calloc, or
  1055. Xrealloc back to the heap.  This allows other parts of the program to
  1056. Xre-use memory that is not needed anymore.  It also guarantees that the
  1057. Xprocess does not grow too big and swallow a large portion of the system
  1058. Xresources.
  1059. X
  1060. X@end table
  1061. X
  1062. X@emph{NOTE}: the returned address from the memory
  1063. Xallocation/reallocation functions should @emph{always} be cast to the
  1064. Xappropriate pointer type for the variable being assigned.
  1065. X
  1066. X@emph{WARNING}: there is a quite common myth that all of the space that
  1067. Xis returned by malloc libraries has already been cleared.  @emph{Only}
  1068. Xthe calloc routine will zero the memory space it returns.
  1069. X
  1070. X@node Features, Usage, Allocation Basics, Top
  1071. X@chapter Description of the Benefits of the Library.
  1072. X
  1073. X@menu
  1074. X* Overview::                    General debugging concepts.
  1075. X* Environment variables::       The variable names and their features.
  1076. X* malloc_dbg program::          Env variable setting utility.
  1077. X* RC file::                     Format of the run-time configuration file.
  1078. X* Debug tokens::                Description of the debugging token flags.
  1079. X* Argument checking::           Special checking of function arguments.
  1080. X@end menu
  1081. X
  1082. X@node Overview, Environment variables,  , Features
  1083. X@section General Debugging Concepts.
  1084. X
  1085. XThe features of this library are controlled by a number of environmental
  1086. Xvariables.  They enable the memory debugging features at runtime to help
  1087. Xlocate problems, chart memory leaks, provide basic bounds checking, log
  1088. Xstatistics, etc.. @xref{Environment variables}.
  1089. X
  1090. XThe debugging features that are available can be broken down into a
  1091. Xcouple basic classifications:
  1092. X
  1093. X@table @samp
  1094. X@item file and line number information
  1095. XOne of the nice things about a good debugger is its ability to provide
  1096. Xthe file and line number of an offending piece of code.  This library
  1097. Xattempts to give this functionality with the help of @dfn{cpp}, the C
  1098. Xpreprocessor.  If the file @file{malloc.h} is included, the library can
  1099. Xprovide file and line information for the warning messages and errors it
  1100. Xgenerates.
  1101. X
  1102. X@item fence-post (i.e. bounds) checking
  1103. X@dfn{Fence-post} memory is the area immediately above or below memory
  1104. Xallocations.  I have found it all to easy to write code that accesses
  1105. Xabove or below an allocation (especially when dealing with arrays or
  1106. Xstrings).  The library can write special values in the areas around
  1107. Xevery allocation so it will notice when these areas have been
  1108. Xoverwritten.
  1109. X
  1110. X@emph{NOTE}: The library cannot notice when the program reads from these
  1111. Xareas, only when it writes values.  Also, fence-post checking will
  1112. Xincrease the amount of memory the program allocates.
  1113. X
  1114. X@item heap-constancy verification
  1115. XThe administration of the library is reasonably complex.  If any of the
  1116. Xheap-maintenance information is corrupted, the program will either crash
  1117. Xor give unpredictable results.
  1118. X
  1119. XBy enabling heap-consistency checking, the library will run through its
  1120. Xadministrative structures to make sure all is in order.  This will mean
  1121. Xthat problems will be caught faster and diagnosed better.
  1122. X
  1123. XThe drawback of this is, of course, that the library often takes quite a
  1124. Xlong time to do this.  It is suitable to enable this only during
  1125. Xdevelopment and debugging sessions.
  1126. X
  1127. X@emph{NOTE}: the heap checking routines cannot guarantee that the tests
  1128. Xwill not cause a segmentation-fault if the heap administration
  1129. Xstructures are properly (or improperly if you will) overwritten.  In
  1130. Xother words, they will verify that everything is okay but may not inform
  1131. Xthe user of problems in a graceful manner.
  1132. X
  1133. X@item logging statistics
  1134. XOne of the initial reasons why I personally wanted malloc-debug
  1135. Xcapabilities is to track my programs' memory usage; specifically to
  1136. Xlocate memory @dfn{leaks} which are places where allocated memory is
  1137. Xnever getting freed.
  1138. X
  1139. XThe library has a number of logging capabilities that can track run-time
  1140. Xmemory usage, administrative actions, final program statistics, as well
  1141. Xas un-freed memory pointers.  This information is also good at providing
  1142. Xmore general debugging feedback.
  1143. X
  1144. X@item examining unfreed memory
  1145. XAnother common problem with programs is that they free a memory pointer
  1146. Xbut then use go on to use it again by mistake.  This can lead to
  1147. Xmysterious crashes and unexplained problems.
  1148. X
  1149. XTo combat this, the library can write special values into a block of
  1150. Xmemory after it has been freed.  This serves two purposes: it will make
  1151. Xsure that the program will get garbage data if it trying to access the
  1152. Xarea again, and it will allow the library to verify the area later for
  1153. Xsigns of overwriting.
  1154. X@end table
  1155. X
  1156. XIf any of the above debugging features detect an error, the library will
  1157. Xtry to recover.  If logging is enabled then an error will be logged with
  1158. Xas much information as possible.
  1159. X
  1160. XThe error messages that the library displays are designed to give the
  1161. Xmost information for developers.  If the error message is not
  1162. Xunderstood, then it is most likely just trying to indicate that a part
  1163. Xof the heap has been corrupted.  The bug is most likely near the last
  1164. Xcall made to the library so reviewing the code around this area is
  1165. Xrecommended.
  1166. X
  1167. XThe library can be configured to quit immediately when an error is
  1168. Xdetected and to dump a core file or memory-image.  This can be examined
  1169. Xwith a debugger to determine the source of the problem.
  1170. X
  1171. XWhen running our programs in a debugger such as gdb (the
  1172. X@emph{excellent} GNU debugger), I always put a break-point in
  1173. X@code{_malloc_perror()} which is the internal error routine for the
  1174. Xlibrary.  The program will then hit the break-point as soon as a memory
  1175. Xproblem is detected.
  1176. X
  1177. XOther malloc-debug libraries also support the ability to dump core and
  1178. Xthen continue running.  I decided not to support this once it was
  1179. Xdetermined that some versions of @code{fork} make calls to malloc which
  1180. Xwould cause the library to go recursive.
  1181. X
  1182. X@emph{NOTE}: do not be surprised if the library catches problems with
  1183. Xyour system's library routines.  It took me four hours once to finally
  1184. Xcome to the conclusion that the localtime call, included in SunOS
  1185. Xrelease 4.1, was overwriting one of its fence-post markers.
  1186. X
  1187. X@node Environment variables, malloc_dbg program, Overview, Features
  1188. X@section Environment Variables - Their Names and Features.
  1189. X
  1190. X@dfn{Environment variables} are variables that are part of the user's
  1191. Xworking environment and are shared by all the programs.  The below
  1192. Xvariables are used by the malloc library to enable or disable the memory
  1193. Xdebugging features, at runtime.
  1194. X
  1195. XThey can be set either by hand or with the help of the malloc_dbg
  1196. Xprogram.  @xref{malloc_dbg program}.
  1197. X
  1198. XTo set them by hand, C shell (or tcsh) users need to invoke:
  1199. X
  1200. X@example
  1201. X        setenv variable value;
  1202. X@end example
  1203. XBourne shell (or bash, ksh) users should use:
  1204. X
  1205. X@example
  1206. X        variable=value;
  1207. X        export variable;
  1208. X@end example
  1209. X@table @samp
  1210. X@item MALLOC_DEBUG
  1211. XThis env variable should be set to a value in hexadecimal which
  1212. Xcorresponds to a set of functionality tokens.  @xref{Debug tokens}.  For
  1213. Xinstance, if the user wanted to enabled logging of memory transactions
  1214. X(value @samp{0x008}) and wanted to check fence-post memory (value
  1215. X@samp{0x400}) then @samp{MALLOC_DEBUG} should be set to @samp{0x408}.
  1216. X
  1217. XDon't worry about remembering all the hex values of the tokens, the
  1218. Xmalloc_dbg program automates the setting of this variable especially.
  1219. X
  1220. X@item MALLOC_LOGFILE
  1221. XSet this variable to a filename so that if @samp{MALLOC_DEBUG} has
  1222. Xlogging enabled, the library can log transactions, administration
  1223. Xinformation, and/or errors to the file so memory problems and usage can
  1224. Xbe tracked.
  1225. X
  1226. X@item MALLOC_ADDRESS
  1227. XWhen this env variable is set to a hex address (taken from the malloc
  1228. Xlog-file for instance) malloc will abort when it finds itself either
  1229. Xallocating or freeing that address.
  1230. X
  1231. XThe address can also have an @samp{:number} argument.  For instance, if
  1232. Xit was set it to @samp{0x3e45:10}, the library will kill itself the 10th
  1233. Xtime it sees address @samp{0x3e45}.
  1234. X
  1235. XThis makes it easier to track down specific addresses not being freed.
  1236. X
  1237. X@item MALLOC_INTERVAL
  1238. XBy setting this env variable to a number X, malloc will only check the
  1239. Xheap every X times.  This means a number of @samp{MALLOC_DEBUG} features
  1240. Xcan be enabled while still running the program within a finite amount of
  1241. Xtime.
  1242. X
  1243. XI have found that a setting of @samp{100} works well with reasonably
  1244. Xmemory intensive programs.  This of course means that the library will
  1245. Xnot catch errors exactly when they happen but possibly 100 library calls
  1246. Xlater.
  1247. X
  1248. X@item MALLOC_START
  1249. XSet this env variable to a number X and malloc will begin checking the
  1250. Xheap after X times.  This means the intensive debugging can be started
  1251. Xafter a certain point in a program.
  1252. X
  1253. X@samp{MALLOC_START} also has the format file:line.  For instance, if it
  1254. Xis set to @samp{malloc_t.c:126} malloc will start checking the heap
  1255. Xafter it sees a malloc call from the @file{malloc_t.c} file, line number
  1256. X126.  If line number is 0 then malloc will start checking the heap after
  1257. Xit sees a call from anywhere in the @file{malloc_t.c} file.
  1258. X
  1259. XThis allows the intensive debugging to be started after a certain
  1260. Xroutine or file has been reached in the program.
  1261. X@end table
  1262. X
  1263. X@node malloc_dbg program, RC file, Environment variables, Features
  1264. X@section Env Variable Setting Utility.
  1265. X
  1266. XThe malloc_dbg program is designed to assist in the setting of the
  1267. Xenvironmental variables, especially @samp{MALLOC_DEBUG}.
  1268. X@xref{Environment variables}.  It is designed to print the shell
  1269. Xcommands necessary to make the appropriate changes to the environment.
  1270. XUnfortunately, it cannot make the changes on its own so the output from
  1271. Xmalloc_dbg should be sent through the @code{eval} shell command which
  1272. Xwill do the commands.
  1273. X
  1274. XWith shells that have aliasing or macro capabilities: csh, tcsh, bash,
  1275. Xksh, etc., setting up an alias to malloc_dbg to do the eval call is
  1276. Xrecommended.  csh/tcsh users (for example) should put the following in
  1277. Xtheir @file{.cshrc} file:
  1278. X
  1279. X@example
  1280. X        alias malloc 'eval `malloc_dbg \!*`'
  1281. X@end example
  1282. XThis allows the user to execute @code{malloc args}.
  1283. X
  1284. XThe most basic usage for the program is @code{malloc_dbg [-b] tag}.  The
  1285. X-b flag is for generating Bourne-shell type commands (C-shell type are
  1286. Xthe default).  The tag argument should match a line from the user's
  1287. Xrun-time configuration file.  @xref{RC file}.
  1288. X
  1289. XHere is a detailed list of the flags that can passed to malloc_dbg:
  1290. X
  1291. X@table @samp
  1292. X@item -a address
  1293. XSet the @samp{MALLOC_ADDRESS} variable with the string address (or
  1294. Xalternatively address:number).
  1295. X
  1296. X@item -b
  1297. XOutput Bourne-shell type commands.  (C-shell type output is the
  1298. Xdefault).
  1299. X
  1300. X@item -c
  1301. XClear/unset all of the variables not specified with other arguments.
  1302. X
  1303. X@emph{NOTE}: clear will never unset the @samp{MALLOC_DEBUG} variable.
  1304. XUse @samp{-d 0} or a tag to @samp{none} to achieve this.
  1305. X
  1306. X@item -d bitmask
  1307. XSet the MALLOC_DEBUG to the bitmask value which should be in hex.  This
  1308. Xis overridden (and unnecessary) if a tag is specified.
  1309. X
  1310. X@item -e errno
  1311. XPrint the malloc error string that corresponds to errno.
  1312. X
  1313. X@item -f filename
  1314. XUse this configuration file instead of the RC file
  1315. X@file{$HOME/.mallocrc}.
  1316. X
  1317. X@item -i number
  1318. XSet the @samp{MALLOC_INTERVAL} env variable to number.
  1319. X
  1320. X@item -l filename
  1321. XSet the @samp{MALLOC_LOGFILE} env variable to filename.
  1322. X
  1323. X@item -s number
  1324. XSet the @samp{MALLOC_START} env variable to number (alternatively
  1325. Xfile:line).
  1326. X@end table
  1327. X
  1328. XIf no arguments are specified, malloc_dbg dumps out the current settings
  1329. Xthat you have for the malloc environmental variables.  For example:
  1330. X
  1331. X@example
  1332. X        MALLOC_DEBUG == '0x6417' (debug1)
  1333. X        MALLOC_ADDRESS == '0'
  1334. X        MALLOC_INTERVAL not set
  1335. X        MALLOC_LOGFILE == 'malloc'
  1336. X        MALLOC_START not set
  1337. X@end example
  1338. X
  1339. X@node RC file, Debug tokens, malloc_dbg program, Features
  1340. X@section Format of the Run-Time Configuration File.
  1341. X
  1342. XThe name of default @dfn{RC file} (or run-time configuration file) is
  1343. X@file{$HOME/.mallocrc}.  The @samp{$HOME} environmental variable should
  1344. Xbe set by the system to point to your home-directory.
  1345. X
  1346. XThe rc file file should contain lines in the general form of:
  1347. X
  1348. X        @code{tag token1, token2, ...}
  1349. X
  1350. Xtag is to be matched with the tag argument passed to the malloc_dbg
  1351. Xprogram, token1, token2, ... are debug capability tokens.
  1352. X@xref{malloc_dbg program} and @ref{Debug tokens}.
  1353. X
  1354. XA line can be finished with a '\' meaning it continues onto the next
  1355. Xline.  Lines beginning with '#' are treated as comments and are ignored
  1356. Xalong with empty lines.
  1357. X
  1358. XI have the below contents in my @file{.mallocrc} file:
  1359. X
  1360. X@example
  1361. X#
  1362. X# Malloc run-time configuration file for our malloc-debug library
  1363. X#
  1364. X
  1365. X# no debugging
  1366. Xnone    none
  1367. X
  1368. X# basic debugging
  1369. Xdebug1    log-stats, log-non-free, log-perror, log-bad-pnt, check-fence
  1370. X
  1371. X# more logging and some heap checking
  1372. Xdebug2    log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  1373. X    check-fence, check-heap, check-lists, error-abort
  1374. X
  1375. X# good utilities
  1376. Xdebug3    log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  1377. X    log-admin, check-fence, check-heap, check-lists, realloc-copy, \
  1378. X    free-blank, error-abort
  1379. X
  1380. X@dots{}
  1381. X@end example
  1382. XWith the above file, when I say @code{eval `malloc_dbg debug1`}, I
  1383. Xenable the logging of statistics, the logging of non-freed memory,
  1384. Xlogging of errors, logging of bad pointer information, and the checking
  1385. Xof fence-post memory areas.
  1386. X
  1387. XWhen I say @code{eval `malloc_dbg none`}, all memory debugging features
  1388. Xare disabled.
  1389. X
  1390. X@node Debug tokens, Argument Checking, RC file, Features
  1391. X@section Description of the Debugging Token Flags.
  1392. X
  1393. XThe below tokens and their corresponding descriptions are for the
  1394. Xsetting of the @samp{MALLOC_DEBUG} environmental variable
  1395. X@xref{Environment variables}.  They should be specified in the user's
  1396. X@file{.mallocrc} file @xref{RC file}.
  1397. X
  1398. X@table @samp
  1399. X@item none
  1400. Xno debugging functionality
  1401. X
  1402. X@item log-stats
  1403. Xlog general statistics when malloc_shutdown is called
  1404. X
  1405. X@item log-non-free
  1406. Xlog non-freed memory pointers when malloc_shutdown is called
  1407. X
  1408. X@item log-perror
  1409. Xlog internal error-messages
  1410. X
  1411. X@item log-trans
  1412. Xlog general memory transactions
  1413. X
  1414. X@item log-bad-pnt
  1415. Xlog information about bad-pointers
  1416. X
  1417. X@item log-admin
  1418. Xlog full administrative information
  1419. X
  1420. X@item log-blocks
  1421. Xlog detailed block information when malloc_heap_map is called
  1422. X
  1423. X@item log-unknown
  1424. Xlike log-non-free but logs unknown non-freed memory pointers
  1425. X
  1426. X@item check-fence
  1427. Xcheck fence-post memory areas
  1428. X
  1429. X@item check-heap
  1430. Xverify heap administrative structure
  1431. X
  1432. X@item check-lists
  1433. Xexamine internal heap linked-lists
  1434. X
  1435. X@item check-dblock
  1436. Xdo detailed checking on small allocations
  1437. X
  1438. X@item check-dblock-fence
  1439. Xcheck the fence-post areas of small allocations
  1440. X
  1441. X@item check-free
  1442. Xcheck to see if free space has been overwritten
  1443. X
  1444. X@item check-funcs
  1445. Xcheck the arguments of some functions (mostly string operations) looking
  1446. Xfor bad pointers
  1447. X
  1448. X@item realloc-copy
  1449. Xalways copy data to a new pointer when realloc
  1450. X
  1451. X@item free-blank
  1452. Xwrite special values (non-0) into space when it is freed
  1453. X
  1454. X@item error-abort
  1455. Xabort the program (and dump core) on errors
  1456. X
  1457. X@item alloc-blank
  1458. Xwrite special values (non-0) into space when it is alloced
  1459. X
  1460. X@item heap-check-map
  1461. Xlog a heap-map to the logfile every time the heap is checked
  1462. X
  1463. X@item print-perror
  1464. Xlog any errors and messages to the screen via standard-error
  1465. X@end table
  1466. X
  1467. X@node Argument Checking,  , Debug tokens, Features
  1468. X@section Check Certain Function Arguments.
  1469. X
  1470. XOne potential problem with the library and its multitude of checks and
  1471. Xdiagnoses is that they only get performed when a malloc function is
  1472. Xcalled.  One solution this is to include @file{malloc.h} and compile
  1473. Xyour source code with the @code{MALLOC_FUNC_CHECK} flag defined and
  1474. Xenable the @code{check-funcs} token @xref{Debug tokens}.
  1475. X
  1476. X@example
  1477. X        gcc -DMALLOC_FUNC_CHECK file.c
  1478. X@end example
  1479. XOnce you have compiled your source with FUNC_CHECK enabled, you will
  1480. Xhave to recompile with it off to disconnect the library @xref{Disabling
  1481. Xthe Library}.
  1482. X
  1483. XWhen this is defined malloc will override a number of functions and will
  1484. Xinsert a routine which knowns how to check its own arguments and then
  1485. Xcall the real function.  Malloc can check such functions as bcopy,
  1486. Xindex, strcat, and strcasecmp (for the full list see the end of
  1487. X@file{malloc.h}).
  1488. X
  1489. XWhen you call strlen, for instance, malloc will make sure the string
  1490. Xargument's fence-post areas have not been overwritten, its file and line
  1491. Xnumber locations are good, etc.  With bcopy, malloc will make sure that
  1492. Xthe destination string has enough space to store the number of bytes
  1493. Xspecified.
  1494. X
  1495. XFor all of the arguments checked, if the pointer is not in the heap then
  1496. Xit is ignored since malloc does know anything about it.
  1497. X
  1498. X@emph{NOTE}: this is one of the newest parts of the library so problems
  1499. Xmay still be lurking.
  1500. X
  1501. X@node Usage, Code, Features, Top
  1502. X@chapter How to Run Programs With the Library.
  1503. X
  1504. X@menu
  1505. X* Allocation macros::           For providing file and line information.
  1506. X* Extensions::                  Additional non-standard routines.
  1507. X* Disabling the Library::       How to compile/link without the library.
  1508. X@end menu
  1509. X
  1510. X@node Allocation macros, Extensions,  , Usage
  1511. X@section For Providing File/Line Debugging Information.
  1512. X
  1513. XBy including @file{malloc.h} in your C files, your calls to calloc,
  1514. Xfree, malloc, or realloc are replaced with calls to _calloc_leap,
  1515. X_free_leap, _malloc_leap, and _realloc_leap.
  1516. X
  1517. XThese leap macros use the c-preprocessor @code{__FILE__} and
  1518. X@code{__LINE__} macros which get replaced at compilation time with the
  1519. Xcurrent file and line-number of the source code in question.  The leap
  1520. Xroutines take this information and pass it on to the library making it
  1521. Xable to produce verbose reports on memory not freed:
  1522. X
  1523. X@example
  1524. X    not freed: 0x38410 ( 22 bytes) from 'malloc_t.c:92'
  1525. X        not freed: 0x38600 ( 10232 bytes) from 'malloc_t.c:104'
  1526. X@end example
  1527. XThese lines from a log file shows that two allocations were not freed.
  1528. XOne at address 0x38410 of size 22 bytes in source file @file{malloc_t.c}
  1529. Xat line 92 and another at address 0x38600 of size 10232 bytes at line
  1530. X104 of @file{malloc_t.c}.
  1531. X
  1532. XAlong with the above leap macros, @file{malloc.h} also contains the
  1533. Xfollowing macros which I have been using for all our memory allocation
  1534. Xneeds for some time now.  They take care of all the type-casting and
  1535. Xmake the code look much cleaner (IMHO).
  1536. X
  1537. X@table @code
  1538. X@item ALLOC(type, count)
  1539. XUsage: @code{long_pnt = ALLOC(long, 30);}.  This means allocate space
  1540. Xfor 30 longs.
  1541. X
  1542. X@item MALLOC(size)
  1543. XUsage: @code{char_pnt = MALLOC(1000);}.  This is like ALLOC but for
  1544. Xcharacters only.  It means allocate space for 1000 characters.
  1545. X
  1546. X@item CALLOC(type, count)
  1547. XUsage: @code{infp = CALLOC(struct info_st, 100);}.  This means allocate
  1548. Xspace for 100 info_st structures and zero them all.
  1549. X
  1550. X@emph{NOTE}: the arguments for the CALLOC macro are sort of reversed
  1551. Xfrom calloc(unsigned int count, unsigned int size).
  1552. X
  1553. X@item REALLOC(pnt, type, count)
  1554. XUsage: @code{long_pnt = REALLOC(old_pnt, long, 10);}.  This takes
  1555. Xold_pnt and and changes its size to accommodate 10 longs.
  1556. X
  1557. X@item REMALLOC(pnt, size)
  1558. XUsage: @code{char_pnt = REMALLOC(char_pnt, 100);}.  This is like REALLOC
  1559. Xbut for characters only.  It takes char_pnt and changes its size to 100
  1560. Xcharacters.
  1561. X
  1562. X@item FREE(pnt)
  1563. XUsage: @code{(void)FREE(pnt);}.  This frees memory pointers.
  1564. X
  1565. X@item STRDUP(string)
  1566. XUsage: @code{char_pnt = STRDUP("hello");}.  This macro duplicates the
  1567. Xfunctionality of the @samp{strdup} function.  string can be either a
  1568. Xstatic string like "hello" or a character pointer.  Non-gcc users should
  1569. Xuse @code{STRDUP("hello", char_pnt);} where char_pnt is the variable
  1570. Xwhich will be assigned to the pointer to the copy of "hello".
  1571. X
  1572. X@item BDUP(pnt, size)
  1573. XUsage: @code{new_item_pnt = BDUP(&item, sizeof(item));}.  This allocates
  1574. Xspace for size bytes, copies size bytes from pnt into the new allocation
  1575. Xand returns it.  It is like strdup but for non-strings.  Non-gcc users
  1576. Xshould use @code{BDUP(&item, sizeof(item), new_item_pnt);} where
  1577. Xnew_item_pnt is the variable which will be assigned the pointer to the
  1578. Xcopy of item.
  1579. X@end table
  1580. X
  1581. XIn the above macro list, I have also included a STRDUP and a BDUP macro.
  1582. XSTRDUP, for those who are not familiar with the strdup function, takes a
  1583. Xstring, allocates enough information to store the string (along with its
  1584. Xnull character), and then copies the string into the new space.  This
  1585. Xmacro does not actually call strdup but provides the same functionality
  1586. Xand provides file and line memory information to the library.
  1587. X
  1588. XBDUP is a function that I invented.  I use it to duplicate structures or
  1589. Xother elements that are not strings.  A pointer to an element and its
  1590. Xsize are passed in and the macro returns an allocated copy of it.
  1591. X
  1592. Xgcc (GNUs c-compiler) has a neat feature in that it understands
  1593. Xreturn-values from macros.  I have included a gcc form of these 2 macros
  1594. X(which makes them a lot more functional) as well as a non-gcc version.
  1595. X
  1596. X@emph{NOTE}: I would like to strongly recommend the usage of gcc.  It is
  1597. Xa superior compiler and future releases of this library may require its
  1598. Xuse.
  1599. X
  1600. X@node Extensions, Disabling the Library, Allocation macros, Usage
  1601. X@section Additional Non-Standard Routines.
  1602. X
  1603. XThe library has a number of variables and routines that are not a
  1604. Xstandard part of most malloc libraries:
  1605. X
  1606. X@table @code
  1607. X@item char * malloc_logpath;
  1608. XThis variable can be used to set the malloc log filename.  The env
  1609. Xvariable MALLOC_LOGFILE overrides this variable.
  1610. X
  1611. X@item int malloc_errno;
  1612. XThis variable stores the internal malloc library error number like errno
  1613. Xdoes for the system calls.  It can be passed to @code{malloc_strerror()}
  1614. X(see below) to get a string version of the error.  It will have a value
  1615. Xof zero if the library has not detected any problems.
  1616. X
  1617. X@item void malloc_shutdown(void);
  1618. XThis routine shuts the library down and logs the final statistics and
  1619. Xinformation especially the non-freed memory pointers.  It should be run
  1620. Xright before @code{exit()} or as the last function in @code{main()}.
  1621. X
  1622. X@example
  1623. X        main()
  1624. X        @{
  1625. X                @dots{}
  1626. X                malloc_shutdown();
  1627. X                exit(0);
  1628. X        @}
  1629. X@end example
  1630. X@item int malloc_heap_map(void);
  1631. XThis routine will log to the logfile (if it is enabled) a graphical
  1632. Xrepresentation of the current heap space.  It needs some work but should
  1633. Xprovide some good information.
  1634. X
  1635. X@item int malloc_verify(char * pnt);
  1636. XUse @code{malloc_verify} to verify individual memory pointers that are
  1637. Xsuspect of memory problems.  To check the entire heap pass in a NULL or
  1638. X0 pointer.
  1639. X
  1640. X@emph{NOTE}: @samp{malloc_verify} can only check the heap with the
  1641. Xfunctions that have been enabled.  For example, if fence-post checking
  1642. Xis not enabled in the @samp{MALLOC_DEBUG} variable, @samp{malloc_verify}
  1643. Xcannot check the fence-post areas in the heap.
  1644. X
  1645. X@item int malloc_debug(long debug);
  1646. XWith this routine, the value in the @samp{MALLOC_DEBUG} variable can be
  1647. Xoverridden and the library debugging features set explicitly.  For
  1648. Xinstance, if debugging should never be enabled for a program, a call to
  1649. X@code{malloc_debug(0);} as the first call in @code{main()} will disable
  1650. Xall the memory debugging from that point on.
  1651. X
  1652. XOne problem however is that some compilers (gcc for instance) make calls
  1653. Xto memory allocation functions @emph{before} @code{main()} is reached
  1654. Xand @code{malloc_debug()} called meaning some debugging information may
  1655. Xbe generated regardless.
  1656. X
  1657. X@item int malloc_examine(char * pnt, int * size, char ** file, int * line);
  1658. XThis routine provides some very interesting functionality.  It returns
  1659. Xthe size of a pnt's allocation as well as the file and line from which
  1660. Xit was allocated.
  1661. X
  1662. X@emph{NOTE}: This function is @emph{certainly} not portable and is not
  1663. Xprovided by other malloc libraries.
  1664. X
  1665. X@item char * malloc_strerror(int errnum);
  1666. X@code{malloc_strerror} returns the string representation of the error
  1667. Xvalue in errnum (which probably should be malloc_errno).  This allows
  1668. Xthe logging of more verbose memory error messages.
  1669. X
  1670. XYou can also display the string representation of an error value by a
  1671. Xcall to the @file{malloc_dbg} program with a @samp{-e #} option
  1672. X@xref{malloc_dbg program}.
  1673. X@end table
  1674. X
  1675. X@node Disabling the Library,  , Extensions, Usage
  1676. X@section How to Compile/Link Without the Library.
  1677. X
  1678. XWhen you are finished with the development and debugging sessions, you
  1679. Xmay want to disable the malloc-debug library and put in its place either
  1680. Xthe system's memory-allocation routines, gnu-malloc, or maybe your own.
  1681. XI have tried to make this a reasonably painless process.  The ease of
  1682. Xthe extraction depends heavily on how many of the library's features
  1683. Xyour made use of during your coding.
  1684. X
  1685. XI am open to any reasonable suggestions as to how to improve this
  1686. Xprocess while maintaining the effectiveness of the debugging.
  1687. X
  1688. X@itemize @bullet
  1689. X@item
  1690. XIf you compiled any of your source modules with @code{MALLOC_FUNC_CHECK}
  1691. Xdefined then you must first recompile all those modules without the flag
  1692. Xenabled.
  1693. X
  1694. X@item
  1695. XIf you are using any of the special functions provided by the
  1696. Xmalloc-debug library (such as @code{malloc_shutdown()}), then you will
  1697. Xneed to @code{#ifdef}, remove, or comment them out of your code.
  1698. X
  1699. X@item
  1700. XIf you want to @emph{totally} disable the malloc-debug library then you
  1701. Xwill need to recompile all the C files that include @file{malloc.h}
  1702. Xwhile defining @code{MALLOC_DEBUG_DISABLE}.  This will cause the malloc
  1703. Xleap macros to not be applied @xref{Allocation macros}.
  1704. X
  1705. X@example
  1706. X        gcc -O -g -DMALLOC_DEBUG_DISABLE main.c
  1707. X@end example
  1708. X@item
  1709. XNow you are ready to relink with a new library.  If you have not
  1710. Xcompiled all your source with @code{MALLOC_DEBUG_DISABLED} defined then
  1711. Xyou need to include the @file{malloc_lp.o} file on the link line.
  1712. X
  1713. X@example
  1714. X        gcc -O -g main.o malloc_lp.o -L/usr/local/lib -lgmalloc
  1715. X@end example
  1716. XIf you have disabled malloc with the @code{MALLOC_DEBUG_DISABLED} flag
  1717. Xor never included @file{malloc.h} in any of your C files, then you will
  1718. Xnot need to include the @file{malloc_lp.o} file on the link line.
  1719. X
  1720. X@example
  1721. X        gcc -O -g main.o -L/usr/local/lib -lgmalloc
  1722. X@end example
  1723. XIf you get unresolved references like @code{_malloc_leap} or
  1724. X@code{_malloc_bcopy} then something was not disabled as it should have
  1725. Xbeen.
  1726. X@end itemize
  1727. X
  1728. X@node Code, Plugs, Usage, Top
  1729. X@chapter Information on the Source and General Concerns.
  1730. X
  1731. X@menu
  1732. X* Definitions::         Some terms and other information.
  1733. X* Compatibility::       General compatibility concerns.
  1734. X* Portability::         Issues important for porting the library.
  1735. X@end menu
  1736. X
  1737. X@node Definitions, Compatibility,  , Code
  1738. X@section Some Terms and Other Information.
  1739. X
  1740. XHere are a couple definitions and other information for those interested
  1741. Xin "picking the brain" of the library.  The code is a little ugly here
  1742. Xand there and it conforms to the Gray-Watson handbook of coding
  1743. Xstandards only.
  1744. X
  1745. X@table @dfn
  1746. X@item bblock
  1747. Xbasic block containing 2 ^ BASIC_BLOCK bytes of info
  1748. X
  1749. X@item bblock_adm
  1750. Xadministration for a set of basic blocks
  1751. X
  1752. X@item dblock
  1753. Xdivided block containing some base 2 number of blocks smaller than a
  1754. Xbasic block.
  1755. X
  1756. X@item dblock_adm
  1757. Xadministration for a set of divided blocks
  1758. X
  1759. X@item chunk
  1760. Xsome anonymous amount of memory
  1761. X@end table
  1762. X
  1763. X@node Compatibility, Portability, Definitions, Code
  1764. X@section General Compatibility Concerns.
  1765. X
  1766. X@itemize @bullet
  1767. X@item
  1768. XRealloc() backwards compatibility with being able to realloc from the
  1769. Xlast freed block is @emph{not} supported.
  1770. X
  1771. X@item
  1772. XRealloc() of a NULL pointer is supported in which case the library will
  1773. Xjust make a call to malloc().  This is a compilation option in the
  1774. X@file{conf.h} file.
  1775. X
  1776. X@item
  1777. XThe library does @emph{not} provide memalign() nor valloc() support as
  1778. Xof yet, but may in future releases.  I would be interested to know who
  1779. Xis using these functions, which architectures they are supported on, and
  1780. Xfor what reason they are being used.
  1781. X
  1782. X@item
  1783. XAside from possibly being slower than the system's memory allocation
  1784. Xfunctions, the library should be fully compatible with the standard
  1785. Xmemory routines.  If this is @emph{not} the case please bring this to my
  1786. Xattention.
  1787. X@end itemize
  1788. X
  1789. X@node Portability,  , Compatibility, Code
  1790. X@section Issues Important for Porting the Library.
  1791. X
  1792. XGeneral compatibility issues center around:
  1793. X
  1794. X@itemize @bullet
  1795. X@item
  1796. Xsbrk or compatible function usages
  1797. X@item
  1798. XWhether the systems's heap grows towards high or low memory.  The
  1799. Xchunk.c code is designed (loosely) around the fact that consecutive
  1800. Xcalls to sbrk should give higher memory addresses.
  1801. X
  1802. XI have not been able to test the library on a system whose heap grows
  1803. Xtowards low memory.  If you are trying to run the library on such a
  1804. Xsystem I would be interested in talking with you.
  1805. X@end itemize
  1806. X
  1807. X@node Plugs,  , Code, Top
  1808. X@chapter Soapbox Comments.
  1809. X
  1810. XSince I have your attention I would like to talk for a second about a
  1811. Xcouple of things that I feel strongly about.  If you would like any more
  1812. Xinformation about the below, please mail to the supplied addresses or
  1813. Xdrop me a line with any questions.
  1814. X
  1815. X@table @samp
  1816. X@item The Free Software Foundation <gnu@@prep.ai.mit.edu>
  1817. XAs you should be able to tell by now, I am a FSF supporter.  The FSF's
  1818. Xgoal, as I see and support it, is to encourage the exchange of free
  1819. Xsource code.  The organization and its individuals have volunteered an
  1820. Xamazing amount of time toward this.  If you use emacs, gcc, gdb, patch,
  1821. Xperl, bison, or any of their many programs and libraries then you have
  1822. Xbenefited from the movement.  Please consider supporting it.
  1823. X
  1824. X@item Berkeley Software Design, Inc. <bsdi-info@@bsdi.com>
  1825. XWe at the Antaire Corporation are the proud and enthusiastic owners of
  1826. Xthe BSD/386 operating system.  For $1k you get a @emph{complete}
  1827. XBSD-flavor operating system with @emph{full source} for 386 and 486
  1828. Xsystems (binary licenses are available).  Along with the obvious
  1829. Xbenefits of full source code come excellent customer support/service and
  1830. Xsystem features such as a MS-DOG runtime environment, complete tcp/ip
  1831. Xnetworking facilities including nfs, full software development
  1832. Xutilities, X, etc.
  1833. X
  1834. X@contents
  1835. X@bye
  1836. END_OF_FILE
  1837. if test 41525 -ne `wc -c <'malloc.texi'`; then
  1838.     echo shar: \"'malloc.texi'\" unpacked with wrong size!
  1839. fi
  1840. # end of 'malloc.texi'
  1841. fi
  1842. if test -f 'malloc_t.c' -a "${1}" != "-c" ; then 
  1843.   echo shar: Will not clobber existing file \"'malloc_t.c'\"
  1844. else
  1845. echo shar: Extracting \"'malloc_t.c'\" \(6254 characters\)
  1846. sed "s/^X//" >'malloc_t.c' <<'END_OF_FILE'
  1847. X/*
  1848. X * test program for malloc code
  1849. X *
  1850. X * Copyright 1992 by Gray Watson and the Antaire Corporation
  1851. X *
  1852. X * This file is part of the malloc-debug package.
  1853. X *
  1854. X * This library is free software; you can redistribute it and/or
  1855. X * modify it under the terms of the GNU Library General Public
  1856. X * License as published by the Free Software Foundation; either
  1857. X * version 2 of the License, or (at your option) any later version.
  1858. X *
  1859. X * This library is distributed in the hope that it will be useful,
  1860. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1861. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1862. X * Library General Public License for more details.
  1863. X *
  1864. X * You should have received a copy of the GNU Library General Public
  1865. X * License along with this library (see COPYING-LIB); if not, write to the
  1866. X * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1867. X *
  1868. X * The author of the program may be contacted at gray.watson@antaire.com
  1869. X */
  1870. X
  1871. X/*
  1872. X * Test program for the malloc library.  Current it is interactive although
  1873. X * probably should be script based.
  1874. X */
  1875. X
  1876. X#include <stdio.h>                /* for stdin */
  1877. X
  1878. X#include "malloc.h"
  1879. X
  1880. X#if INCLUDE_RCS_IDS
  1881. XLOCAL    char    *rcs_id =
  1882. X  "$Id: malloc_t.c,v 1.15 1993/04/06 04:24:44 gray Exp $";
  1883. X#endif
  1884. X
  1885. X#define DEFAULT_ITERATIONS    1000
  1886. X
  1887. X/*
  1888. X * hexadecimal STR to integer translation
  1889. X */
  1890. XLOCAL    int    hex_to_int(char * str)
  1891. X{
  1892. X  int        ret;
  1893. X  
  1894. X  /* strip off spaces */
  1895. X  for (; *str == ' ' || *str == '\t'; str++);
  1896. X  
  1897. X  /* skip a leading 0[xX] */
  1898. X  if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X'))
  1899. X    str += 2;
  1900. X  
  1901. X  for (ret = 0;; str++) {
  1902. X    if (*str >= '0' && *str <= '9')
  1903. X      ret = ret * 16 + (*str - '0');
  1904. X    else if (*str >= 'a' && *str <= 'f')
  1905. X      ret = ret * 16 + (*str - 'a' + 10);
  1906. X    else if (*str >= 'A' && *str <= 'F')
  1907. X      ret = ret * 16 + (*str - 'A' + 10);
  1908. X    else
  1909. X      break;
  1910. X  }
  1911. X  
  1912. X  return ret;
  1913. X}
  1914. X
  1915. X/*
  1916. X * read an address from the user
  1917. X */
  1918. XLOCAL    long    get_address(void)
  1919. X{
  1920. X  char    line[80];
  1921. X  long    pnt;
  1922. X  
  1923. X  do {
  1924. X    (void)printf("Enter a hex address: ");
  1925. X    (void)fgets(line, sizeof(line), stdin);
  1926. X  } while (line[0] == '\0');
  1927. X  
  1928. X  pnt = hex_to_int(line);
  1929. X  
  1930. X  return pnt;
  1931. X}
  1932. X
  1933. XEXPORT    int    main(int argc, char ** argv)
  1934. X{
  1935. X  argc--, argv++;
  1936. X  
  1937. X  (void)srand(time(0) ^ 0xDEADBEEF);
  1938. X  
  1939. X  (void)printf("------------------------------------------------------\n");
  1940. X  (void)printf("Malloc test program.  Type 'help' for assistance.\n");
  1941. X  
  1942. X  for (;;) {
  1943. X    int        len;
  1944. X    char    line[128], *linep;
  1945. X    
  1946. X    (void)printf("------------------------------------------------------\n");
  1947. X    (void)printf("prompt> ");
  1948. X    (void)fgets(line, sizeof(line), stdin);
  1949. X    linep = (char *)index(line, '\n');
  1950. X    if (linep != NULL)
  1951. X      *linep = '\0';
  1952. X    
  1953. X    len = strlen(line);
  1954. X    if (len == 0)
  1955. X      continue;
  1956. X    
  1957. X    if (strncmp(line, "?", len) == 0
  1958. X    || strncmp(line, "help", len) == 0) {
  1959. X      (void)printf("------------------------------------------------------\n");
  1960. X      (void)printf("HELP:\n\n");
  1961. X      
  1962. X      (void)printf("help      - print this message\n\n");
  1963. X      
  1964. X      (void)printf("malloc    - allocate memory\n");
  1965. X      (void)printf("free      - deallocate memory\n");
  1966. X      (void)printf("realloc   - reallocate memory\n\n");
  1967. X      
  1968. X      (void)printf("map       - map the heap to the logfile\n");
  1969. X      (void)printf("overwrite - overwrite some memory to test errors\n");
  1970. X      (void)printf("random    - randomly execute a number of malloc/frees\n");
  1971. X      (void)printf("verify    - check out a memory address\n\n");
  1972. X      
  1973. X      (void)printf("quit      - quit this test program\n");
  1974. X      continue;
  1975. X    }
  1976. X    
  1977. X    if (strncmp(line, "quit", len) == 0)
  1978. X      break;
  1979. X    
  1980. X    if (strncmp(line, "malloc", len) == 0) {
  1981. X      int    size;
  1982. X      
  1983. X      (void)printf("How much: ");
  1984. X      (void)fgets(line, sizeof(line), stdin);
  1985. X      size = atoi(line);
  1986. X      (void)printf("malloc(%d) returned: %#lx\n", size, (long)MALLOC(size));
  1987. X      continue;
  1988. X    }
  1989. X    
  1990. X    if (strncmp(line, "free", len) == 0) {
  1991. X      long    pnt;
  1992. X      
  1993. X      pnt = get_address();
  1994. X      (void)printf("free(%#lx) returned: %s\n",
  1995. X           pnt, (FREE(pnt) == FREE_NOERROR ? "success" : "failure"));
  1996. X      continue;
  1997. X    }
  1998. X    
  1999. X    if (strncmp(line, "realloc", len) == 0) {
  2000. X      int    size;
  2001. X      long    pnt;
  2002. X      
  2003. X      pnt = get_address();
  2004. X      
  2005. X      (void)printf("How much: ");
  2006. X      (void)fgets(line, sizeof(line), stdin);
  2007. X      size = atoi(line);
  2008. X      
  2009. X      (void)printf("realloc(%#lx, %d) returned: %#lx\n",
  2010. X           pnt, size, (long)REMALLOC(pnt, size));
  2011. X      
  2012. X      continue;
  2013. X    }
  2014. X    
  2015. X    if (strncmp(line, "map", len) == 0) {
  2016. X      (void)malloc_heap_map();
  2017. X      (void)printf("Done.\n");
  2018. X      continue;
  2019. X    }
  2020. X    
  2021. X    if (strncmp(line, "overwrite", len) == 0) {
  2022. X      long    pnt, magic;
  2023. X      
  2024. X      pnt = get_address();
  2025. X      
  2026. X      magic = 0x12345678;
  2027. X      bcopy(&magic, (char *)pnt, sizeof(magic));
  2028. X      
  2029. X      (void)printf("Done.\n");
  2030. X      continue;
  2031. X    }
  2032. X    
  2033. X    /* do random heap hits */
  2034. X    if (strncmp(line, "random", len) == 0) {
  2035. X      int    count, max;
  2036. X      
  2037. X      (void)printf("How many iterations[%d]: ", DEFAULT_ITERATIONS);
  2038. X      (void)fgets(line, sizeof(line), stdin);
  2039. X      if (line[0] == '\0' || line[0] == '\n')
  2040. X    max = DEFAULT_ITERATIONS;
  2041. X      else
  2042. X    max = atoi(line);
  2043. X      
  2044. X      for (count = 1; count < max; count += 10) {
  2045. X    int    amount;
  2046. X    char    *data;
  2047. X    
  2048. X    amount = rand() % (count * 10) + 1;
  2049. X    data = MALLOC(amount);
  2050. X    (void)FREE(data);
  2051. X      }
  2052. X      
  2053. X      (void)printf("Done.\n");
  2054. X      continue;
  2055. X    }
  2056. X    
  2057. X    if (strncmp(line, "verify", len) == 0) {
  2058. X      long    pnt;
  2059. X      int    ret;
  2060. X      
  2061. X      pnt = get_address();
  2062. X      
  2063. X      ret = malloc_verify((char *)pnt);
  2064. X      (void)printf("malloc_verify(%#lx) returned: %s\n",
  2065. X           pnt,
  2066. X           (ret == MALLOC_VERIFY_NOERROR ? "success" : "failure"));
  2067. X      continue;
  2068. X    }
  2069. X    
  2070. X    (void)printf("Unknown command '%s'.\n", line);
  2071. X    (void)printf("Type 'help' for assistance.\n");
  2072. X  }
  2073. X  
  2074. X  /* shutdown the alloc routines */
  2075. X  malloc_shutdown();
  2076. X  
  2077. X  (void)printf("------------------------------------------------------\n");
  2078. X  (void)printf("final malloc_verify returned: %s\n",
  2079. X           (malloc_verify(NULL) == MALLOC_VERIFY_NOERROR ? "success" :
  2080. X        "failure"));
  2081. X  (void)printf("------------------------------------------------------\n");
  2082. X  
  2083. X  exit(0);
  2084. X}
  2085. END_OF_FILE
  2086. if test 6254 -ne `wc -c <'malloc_t.c'`; then
  2087.     echo shar: \"'malloc_t.c'\" unpacked with wrong size!
  2088. fi
  2089. # end of 'malloc_t.c'
  2090. fi
  2091. if test -f 'mallocrc' -a "${1}" != "-c" ; then 
  2092.   echo shar: Will not clobber existing file \"'mallocrc'\"
  2093. else
  2094. echo shar: Extracting \"'mallocrc'\" \(3302 characters\)
  2095. sed "s/^X//" >'mallocrc' <<'END_OF_FILE'
  2096. X#
  2097. X# Sample Malloc rc file for the debug malloc library
  2098. X# Rename this to .mallocrc and place in your home directory to activate.
  2099. X# See malloc.info for more information
  2100. X#
  2101. X# $Id: mallocrc,v 1.7 1993/04/05 22:30:25 gray Exp $
  2102. X#
  2103. X
  2104. X#
  2105. X# Lines in this file should have the form:
  2106. X#
  2107. X#    tag    token1, token2, ...
  2108. X#
  2109. X# The above 'tag' is then passed to the 'malloc' alias to set the current
  2110. X# debugging functionality to token1, token2, ...
  2111. X#
  2112. X# Tag lines can be finished with a \ meaning it continues onto the next line.
  2113. X# Lines begining with '#' or empty lines are ignored
  2114. X#
  2115. X
  2116. X#
  2117. X# Token:            Description:
  2118. X###########################################################################
  2119. X# none                no debuging functionality
  2120. X#
  2121. X# log-stats            log general statistics
  2122. X# log-non-free            log non-freed memory pointers on shutdown
  2123. X# log-perror            log error codes and messages
  2124. X# log-trans            log memory transactions
  2125. X# log-bad-pnt            log information about bad-pointers
  2126. X# log-admin            log full administrative information
  2127. X# log-blocks            log detailed block information in heap_map
  2128. X# log-unknown            log unknown non-freed memory pointers too
  2129. X#
  2130. X# check-fence            check fence-post areas
  2131. X# check-heap            do general heap checking
  2132. X# check-lists            verify internal heap linked-lists
  2133. X# check-dblock            do detailed checking on small allocations
  2134. X# check-dblock-fence        check the fence-post areas of small allocations
  2135. X# check-free            check to see if free space has been overwritten
  2136. X# check-funcs            check the arguments of some routines
  2137. X#
  2138. X# realloc-copy            always copy data to a new pointer when realloc
  2139. X# free-blank            overwrite space that is freed
  2140. X# error-abort            abort the program (and dump core) on errors
  2141. X# alloc-blank            overwrite space that is to be alloced
  2142. X# heap-check-map        log a heap-map every time the heap is checked
  2143. X# print-perror            dump errors and messages to STDERR
  2144. X#
  2145. X
  2146. X# no debugging
  2147. Xnone    none
  2148. X
  2149. X# basic production
  2150. Xproduction    log-stats, log-non-free, log-perror, log-bad-pnt
  2151. X
  2152. X# basic runtime
  2153. Xruntime        log-stats, log-non-free, log-perror, log-bad-pnt, \
  2154. X        check-fence
  2155. X
  2156. X# more logging and some heap checking
  2157. X2        log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  2158. X        check-fence, check-heap, check-lists, check-dblock-fence, \
  2159. X        error-abort
  2160. X
  2161. X# good checking functionality
  2162. X3        log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  2163. X        log-admin, log-blocks, \
  2164. X        check-fence, check-heap, check-lists, check-dblock-fence,
  2165. X        check-funcs, \
  2166. X        realloc-copy, free-blank, alloc-blank, error-abort
  2167. X
  2168. X# extensive checking
  2169. X4        log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  2170. X        log-admin, log-blocks, \
  2171. X        check-fence, check-heap, check-lists, check-dblock, \
  2172. X        check-fence-dblock, check-funcs, \
  2173. X        realloc-copy, free-blank, alloc-blank, error-abort
  2174. X
  2175. X# ultra-extensive checking
  2176. X5        log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  2177. X        log-admin, log-blocks, \
  2178. X        check-fence, check-heap, check-lists, check-dblock, \
  2179. X        check-dblock-fence, check-free, check-funcs, \
  2180. X        realloc-copy, free-blank, alloc-blank, error-abort
  2181. X
  2182. X# full fence-post checking
  2183. Xfence        log-stats, log-non-free, log-perror, log-bad-pnt, \
  2184. X        check-fence, check-dblock-fence
  2185. X
  2186. X# full logging of statistics to logfile and screen
  2187. Xlog        log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  2188. X        log-admin, log-blocks, \
  2189. X        check-fence, \
  2190. X        heap-check-map, print-perror
  2191. END_OF_FILE
  2192. if test 3302 -ne `wc -c <'mallocrc'`; then
  2193.     echo shar: \"'mallocrc'\" unpacked with wrong size!
  2194. fi
  2195. # end of 'mallocrc'
  2196. fi
  2197. echo shar: End of archive 4 \(of 5\).
  2198. cp /dev/null ark4isdone
  2199. MISSING=""
  2200. for I in 1 2 3 4 5 ; do
  2201.     if test ! -f ark${I}isdone ; then
  2202.     MISSING="${MISSING} ${I}"
  2203.     fi
  2204. done
  2205. if test "${MISSING}" = "" ; then
  2206.     echo You have unpacked all 5 archives.
  2207.     echo "Do a 'sh ./configure' to configure the library"
  2208.     rm -f ark[1-9]isdone
  2209. else
  2210.     echo You still need to unpack the following archives:
  2211.     echo "        " ${MISSING}
  2212. fi
  2213. ##  End of shell archive.
  2214. exit 0
  2215.