hirc

IRC client
Log | Files | Refs

commit 8ab52e9749e6577dbe372eeddfa4a0ca85d12bb7
parent 6e0ddd6e2ef40abd26577c63440306f577065da7
Author: hhvn <dev@hhvn.uk>
Date:   Wed, 12 Jan 2022 20:46:57 +0000

s/main.c s/commands.c s/handle.c s/hirc.h: temporary mem allocation

Diffstat:
Msrc/commands.c | 7++-----
Msrc/handle.c | 3+--
Msrc/hirc.h | 2++
Msrc/main.c | 46++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/src/commands.c b/src/commands.c @@ -784,10 +784,9 @@ command_format(struct Server *server, char *str) { } len = strlen(str) + strlen("format.") + 1; - newstr = malloc(len); + newstr = talloc(len); snprintf(newstr, len, "format.%s", str); command_set(server, newstr); - free(newstr); } static void @@ -1512,7 +1511,7 @@ command_eval(char *str) { char *cmd; char *s; - s = strdup(alias_eval(str)); + s = tstrdup(alias_eval(str)); if (*s != '/' || strncmp(s, "/ /", sizeof("/ /")) == 0) { /* Provide a way to escape commands @@ -1553,6 +1552,4 @@ command_eval(char *str) { ui_error("no such command: '%s'", cmd); } - - free(s); } diff --git a/src/handle.c b/src/handle.c @@ -316,7 +316,7 @@ handle_RPL_ISUPPORT(char *msg, char **params, struct Server *server, time_t time /* skip the last param ".... :are supported by this server" */ for (; *params && *(params+1); params++) { - key = strdup(*params); + key = tstrdup(*params); if ((value = strchr(key, '=')) != NULL) { *value = '\0'; if (*(value+1)) @@ -326,7 +326,6 @@ handle_RPL_ISUPPORT(char *msg, char **params, struct Server *server, time_t time } support_set(server, key, value); - free(key); } } diff --git a/src/hirc.h b/src/hirc.h @@ -37,6 +37,8 @@ size_t strlcpy(char *dst, const char *src, size_t dsize); /* main.c */ void * emalloc(size_t size); char * estrdup(const char *str); +void * talloc(size_t size); +char * tstrdup(const char *str); void cleanup(char *quitmsg); void param_free(char **params); int param_len(char **params); diff --git a/src/main.c b/src/main.c @@ -26,6 +26,7 @@ #include <unistd.h> #include <stdarg.h> #include <signal.h> +#include <assert.h> #include <poll.h> #include "hirc.h" @@ -56,6 +57,48 @@ estrdup(const char *str) { return ret; } +/* Assign memory and store in array for freeing by main loop */ +void * +talloc(size_t size) { + static void **mema = NULL; + static size_t mems = 0; + void *mem = NULL; + size_t i; + + if (size) { + mem = emalloc(size); + if (!mems) + mema = calloc(sizeof(char *), mems + 1); + else + mema = reallocarray(mema, sizeof(char *), mems + 1); + assert(mema != NULL); + + *(mema + mems) = mem; + mems++; + } else if (mema && mems) { + for (i = 0; i < mems; i++) + free(*(mema + i)); + free(mema); + mems = 0; + mema = NULL; + } + + return mem; +} + +/* strdup using talloc */ +char * +tstrdup(const char *str) { + size_t size; + char *ret; + + size = strlen(str) + 1; + ret = talloc(size); + memcpy(ret, str, size); + + return ret; +} + void cleanup(char *quitmsg) { struct Server *sp, *prev; @@ -422,6 +465,9 @@ main(int argc, char *argv[]) { wrefresh(windows[Win_input].window); ui_read(); + + /* free temporary mem */ + talloc(0); } return 0;