2 * malloc.c: safe wrappers around malloc, realloc, free, strdup
10 #define LOGPARAMS char *file, int line,
11 static FILE *logallocfp
= NULL
;
12 static int logline
= 2; /* off by 1: `null pointer is' */
13 static void loginc(void) { }
14 static void logallocinit(void) {
16 logallocfp
= fopen("malloc.log", "w");
18 fprintf(stderr
, "panic: unable to open malloc.log\n");
21 setvbuf (logallocfp
, NULL
, _IOLBF
, BUFSIZ
);
22 fprintf(logallocfp
, "null pointer is %p\n", NULL
);
25 static void logprintf(char *fmt
, ...) {
28 vfprintf(logallocfp
, fmt
, ap
);
31 #define LOGPRINT(x) ( logallocinit(), logprintf x )
32 #define LOGINC do { loginc(); logline++; } while (0)
36 #define LOGINC ((void)0)
40 * smalloc should guarantee to return a useful pointer - Halibut
41 * can do nothing except die when it's out of memory anyway.
43 void *(smalloc
)(LOGPARAMS
int size
) {
46 LOGPRINT(("%s %d malloc(%ld)",
47 file
, line
, (long)size
));
51 LOGPRINT((" returns %p\n", p
));
56 * sfree should guaranteeably deal gracefully with freeing NULL
58 void (sfree
)(LOGPARAMS
void *p
) {
61 LOGPRINT(("%s %d free(%p)\n",
68 * srealloc should guaranteeably be able to realloc NULL
70 void *(srealloc
)(LOGPARAMS
void *p
, int size
) {
74 LOGPRINT(("%s %d realloc(%p,%ld)",
75 file
, line
, p
, (long)size
));
77 LOGPRINT((" returns %p\n", q
));
80 LOGPRINT(("%s %d malloc(%ld)",
81 file
, line
, (long)size
));
83 LOGPRINT((" returns %p\n", q
));
91 * dupstr is like strdup, but with the never-return-NULL property
92 * of smalloc (and also reliably defined in all environments :-)
94 char *dupstr(char *s
) {
95 char *r
= smalloc(1+strlen(s
));
101 * Duplicate a linked list of words
103 word
*dup_word_list(word
*w
) {
104 word
*head
, **eptr
= &head
;
107 word
*newwd
= mknew(word
);
108 *newwd
= *w
; /* structure copy */
109 newwd
->text
= ustrdup(w
->text
);
111 newwd
->alt
= dup_word_list(w
->alt
);
123 * Free a linked list of words
125 void free_word_list(word
*w
) {
132 free_word_list(t
->alt
);
138 * Free a linked list of paragraphs
140 void free_para_list(paragraph
*p
) {
146 free_word_list(t
->words
);