Mercurial > hg > mlmmj
changeset 873:f6764d59010d
Use dynamic allocation instead of static variables.
author | Ben Schmidt |
---|---|
date | Sat, 31 Mar 2012 10:45:32 +1100 |
parents | 01f8f152e989 |
children | 80f082f2319a |
files | src/prepstdreply.c |
diffstat | 1 files changed, 23 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/src/prepstdreply.c Sat Mar 31 10:42:46 2012 +1100 +++ b/src/prepstdreply.c Sat Mar 31 10:45:32 2012 +1100 @@ -149,23 +149,21 @@ memory_lines_state *init_memory_lines(const char *lines) { - /* We use a static variable rather than dynamic allocation as - * there will never be two lists in use simultaneously */ - static memory_lines_state s; + memory_lines_state *s = mymalloc(sizeof(memory_lines_state)); size_t len; /* Ensure there *is* a trailing newline */ - s.pos = NULL; + s->pos = NULL; len = strlen(lines); if (lines[len-1] == '\n') { - s.lines = mystrdup(lines); - return &s; + s->lines = mystrdup(lines); + return s; } - s.lines = mymalloc((len + 2) * sizeof(char)); - strcpy(s.lines, lines); - s.lines[len] = '\n'; - s.lines[len+1] = '\0'; - return &s; + s->lines = mymalloc((len + 2) * sizeof(char)); + strcpy(s->lines, lines); + s->lines[len] = '\n'; + s->lines[len+1] = '\0'; + return s; } @@ -207,27 +205,29 @@ { if (s == NULL) return; myfree(s->lines); + myfree(s); } file_lines_state *init_file_lines(const char *filename, int open_now) { - /* We use a static variable rather than dynamic allocation as - * there will never be two lists in use simultaneously */ - static file_lines_state s; + file_lines_state *s = mymalloc(sizeof(file_lines_state)); if (open_now) { - s.fd = open(filename, O_RDONLY); - s.filename = NULL; - if (s.fd < 0) return NULL; + s->fd = open(filename, O_RDONLY); + s->filename = NULL; + if (s->fd < 0) { + myfree(s); + return NULL; + } } else { - s.filename = mystrdup(filename); - s.fd = -1; + s->filename = mystrdup(filename); + s->fd = -1; } - s.truncate = '\0'; - s.line = NULL; - return &s; + s->truncate = '\0'; + s->line = NULL; + return s; } @@ -292,6 +292,7 @@ if (s->line != NULL) myfree(s->line); if (s->fd >= 0) close(s->fd); if (s->filename != NULL) myfree(s->filename); + myfree(s); }