diff options
| author | Simon Tatham <anakin@pobox.com> | 1999-08-09 10:02:07 +0000 |
|---|---|---|
| committer | Simon Tatham <anakin@pobox.com> | 1999-08-09 10:02:07 +0000 |
| commit | 12a5351076409e50f10dfa8274da3768b364ff7f (patch) | |
| tree | 78dfa1386fd20001fe2f68a7ab31a73420f6eaf2 /malloc.c | |
| parent | 0d14833a9c76c51cc7417d8fd60bec9d92714b8e (diff) | |
| download | halibut-12a5351076409e50f10dfa8274da3768b364ff7f.zip halibut-12a5351076409e50f10dfa8274da3768b364ff7f.tar.gz halibut-12a5351076409e50f10dfa8274da3768b364ff7f.tar.bz2 halibut-12a5351076409e50f10dfa8274da3768b364ff7f.tar.xz | |
More development; not nearly finished yet
[originally from svn r193]
Diffstat (limited to 'malloc.c')
| -rw-r--r-- | malloc.c | 76 |
1 files changed, 70 insertions, 6 deletions
@@ -3,42 +3,101 @@ */ #include <stdlib.h> +#include <stdarg.h> #include "buttress.h" +#ifdef LOGALLOC +#define LOGPARAMS char *file, int line, +static FILE *logallocfp = NULL; +static void logallocinit(void) { + if (!logallocfp) { + logallocfp = fopen("malloc.log", "w"); + if (!logallocfp) { + fprintf(stderr, "panic: unable to open malloc.log\n"); + exit(10); + } + setvbuf (logallocfp, NULL, _IOLBF, BUFSIZ); + fprintf(logallocfp, "null pointer is %p\n", NULL); + } +} +static void logprintf(char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vfprintf(logallocfp, fmt, ap); + va_end(ap); +} +#define LOGPRINT(x) ( logallocinit(), logprintf x ) +#else +#define LOGPARAMS +#define LOGPRINT(x) +#endif + /* * smalloc should guarantee to return a useful pointer - buttress * can do nothing except die when it's out of memory anyway */ -void *smalloc(int size) { +void *(smalloc)(LOGPARAMS int size) { void *p = malloc(size); if (!p) fatal(err_nomemory); + LOGPRINT(("%s %d malloc(%ld) returns %p\n", + file, line, (long)size, p)); return p; } /* * sfree should guaranteeably deal gracefully with freeing NULL */ -void sfree(void *p) { - if (p) +void (sfree)(LOGPARAMS void *p) { + if (p) { free(p); + LOGPRINT(("%s %d free(%p)\n", + file, line, p)); + } } /* * srealloc should guaranteeably be able to realloc NULL */ -void *srealloc(void *p, int size) { +void *(srealloc)(LOGPARAMS void *p, int size) { void *q; - if (p) + if (p) { q = realloc(p, size); - else + LOGPRINT(("%s %d realloc(%p,%ld) returns %p\n", + file, line, p, (long)size, q)); + } else { q = malloc(size); + LOGPRINT(("%s %d malloc(%ld) returns %p\n", + file, line, (long)size, q)); + } if (!q) fatal(err_nomemory); return q; } /* + * Duplicate a linked list of words + */ +word *dup_word_list(word *w) { + word *head, **eptr = &head; + + while (w) { + word *newwd = smalloc(sizeof(word)); + *newwd = *w; /* structure copy */ + newwd->text = ustrdup(w->text); + if (w->alt) + newwd->alt = dup_word_list(w->alt); + *eptr = newwd; + newwd->next = NULL; + eptr = &newwd->next; + + w = w->next; + } + + return head; +} + +/* * Free a linked list of words */ void free_word_list(word *w) { @@ -46,6 +105,9 @@ void free_word_list(word *w) { while (w) { t = w; w = w->next; + sfree(t->text); + if (t->alt) + free_word_list(t->alt); sfree(t); } } @@ -58,6 +120,8 @@ void free_para_list(paragraph *p) { while (p) { t = p; p = p->next; + sfree(t->keyword); + free_word_list(t->words); sfree(t); } } |