| 1 |
/* |
| 2 |
* malloc.c: safe wrappers around malloc, realloc, free, strdup |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdarg.h> |
| 7 |
#include <string.h> |
| 8 |
#include "halibut.h" |
| 9 |
|
| 10 |
#ifdef LOGALLOC |
| 11 |
#define LOGPARAMS char *file, int line, |
| 12 |
static FILE *logallocfp = NULL; |
| 13 |
static int logline = 2; /* off by 1: `null pointer is' */ |
| 14 |
static void loginc(void) |
| 15 |
{ |
| 16 |
} |
| 17 |
static void logallocinit(void) |
| 18 |
{ |
| 19 |
if (!logallocfp) |
| 20 |
{ |
| 21 |
logallocfp = fopen("malloc.log", "w"); |
| 22 |
if (!logallocfp) |
| 23 |
{ |
| 24 |
fprintf(stderr, "panic: unable to open malloc.log\n"); |
| 25 |
exit(10); |
| 26 |
} |
| 27 |
setvbuf(logallocfp, NULL, _IOLBF, BUFSIZ); |
| 28 |
fprintf(logallocfp, "null pointer is %p\n", NULL); |
| 29 |
} |
| 30 |
} |
| 31 |
static void logprintf(char *fmt, ...) |
| 32 |
{ |
| 33 |
va_list ap; |
| 34 |
va_start(ap, fmt); |
| 35 |
vfprintf(logallocfp, fmt, ap); |
| 36 |
va_end(ap); |
| 37 |
} |
| 38 |
|
| 39 |
#define LOGPRINT(x) ( logallocinit(), logprintf x ) |
| 40 |
#define LOGINC do { loginc(); logline++; } while (0) |
| 41 |
#else |
| 42 |
#define LOGPARAMS |
| 43 |
#define LOGPRINT(x) |
| 44 |
#define LOGINC ((void)0) |
| 45 |
#endif |
| 46 |
|
| 47 |
/* |
| 48 |
* smalloc should guarantee to return a useful pointer - Halibut |
| 49 |
* can do nothing except die when it's out of memory anyway. |
| 50 |
*/ |
| 51 |
void *(smalloc) (LOGPARAMS int size) { |
| 52 |
void *p; |
| 53 |
LOGINC; |
| 54 |
LOGPRINT(("%s %d malloc(%ld)", file, line, (long) size)); |
| 55 |
p = malloc(size); |
| 56 |
if (!p) |
| 57 |
fatal(err_nomemory); |
| 58 |
LOGPRINT((" returns %p\n", p)); |
| 59 |
return p; |
| 60 |
} |
| 61 |
|
| 62 |
/* |
| 63 |
* sfree should guaranteeably deal gracefully with freeing NULL |
| 64 |
*/ |
| 65 |
void (sfree) (LOGPARAMS void *p) { |
| 66 |
if (p) |
| 67 |
{ |
| 68 |
LOGINC; |
| 69 |
LOGPRINT(("%s %d free(%p)\n", file, line, p)); |
| 70 |
free(p); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* srealloc should guaranteeably be able to realloc NULL |
| 76 |
*/ |
| 77 |
void *(srealloc) (LOGPARAMS void *p, int size) { |
| 78 |
void *q; |
| 79 |
if (p) |
| 80 |
{ |
| 81 |
LOGINC; |
| 82 |
LOGPRINT(("%s %d realloc(%p,%ld)", file, line, p, (long) size)); |
| 83 |
q = realloc(p, size); |
| 84 |
LOGPRINT((" returns %p\n", q)); |
| 85 |
} else |
| 86 |
{ |
| 87 |
LOGINC; |
| 88 |
LOGPRINT(("%s %d malloc(%ld)", file, line, (long) size)); |
| 89 |
q = malloc(size); |
| 90 |
LOGPRINT((" returns %p\n", q)); |
| 91 |
} |
| 92 |
if (!q) |
| 93 |
fatal(err_nomemory); |
| 94 |
return q; |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* dupstr is like strdup, but with the never-return-NULL property |
| 99 |
* of smalloc (and also reliably defined in all environments :-) |
| 100 |
*/ |
| 101 |
char *dupstr(char *s) |
| 102 |
{ |
| 103 |
char *r = smalloc(1 + strlen(s)); |
| 104 |
strcpy(r, s); |
| 105 |
return r; |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
* Duplicate a linked list of words |
| 110 |
*/ |
| 111 |
word *dup_word_list(word * w) |
| 112 |
{ |
| 113 |
word *head, **eptr = &head; |
| 114 |
|
| 115 |
while (w) |
| 116 |
{ |
| 117 |
word *newwd = mknew(word); |
| 118 |
*newwd = *w; /* structure copy */ |
| 119 |
newwd->text = ustrdup(w->text); |
| 120 |
if (w->alt) |
| 121 |
newwd->alt = dup_word_list(w->alt); |
| 122 |
*eptr = newwd; |
| 123 |
newwd->next = NULL; |
| 124 |
eptr = &newwd->next; |
| 125 |
|
| 126 |
w = w->next; |
| 127 |
} |
| 128 |
|
| 129 |
return head; |
| 130 |
} |
| 131 |
|
| 132 |
/* |
| 133 |
* Free a generic linked list |
| 134 |
*/ |
| 135 |
void free_list(void*p) |
| 136 |
{ |
| 137 |
void *s = p, *n; |
| 138 |
for (; p; p = n) |
| 139 |
{ |
| 140 |
n = *(void**) p; /* Assumes next pointer is the first member! */ |
| 141 |
if (n == s) n = NULL; /* Around the ring? */ |
| 142 |
sfree(p); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* Free a linked list of words |
| 148 |
*/ |
| 149 |
void free_word_list(word * w) |
| 150 |
{ |
| 151 |
word *t; |
| 152 |
while (w) |
| 153 |
{ |
| 154 |
t = w; |
| 155 |
w = w->next; |
| 156 |
sfree(t->text); |
| 157 |
if (t->alt) |
| 158 |
free_word_list(t->alt); |
| 159 |
sfree(t); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Free a linked list of paragraphs |
| 165 |
*/ |
| 166 |
void free_para_list(paragraph * p) |
| 167 |
{ |
| 168 |
paragraph *t; |
| 169 |
while (p) |
| 170 |
{ |
| 171 |
t = p; |
| 172 |
p = p->next; |
| 173 |
sfree(t->keyword); |
| 174 |
free_word_list(t->words); |
| 175 |
sfree(t); |
| 176 |
} |
| 177 |
} |