hirc

IRC client
Log | Files | Refs

commit fbfba15809e91570657f263cadc455f6d403f819
parent ea708ebb4324d16c9d4d801720214b51e7e6f7e2
Author: hhvn <dev@hhvn.uk>
Date:   Wed, 23 Mar 2022 14:17:42 +0000

Organize memory functions into seperate file

Diffstat:
MMakefile | 6+++---
Msrc/hirc.h | 18++++++++++--------
Msrc/main.c | 113-------------------------------------------------------------------------------
3 files changed, 13 insertions(+), 124 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,9 +4,9 @@ PREFIX = /usr/local BINDIR = $(PREFIX)/bin MANDIR = $(PREFIX)/share/man BIN = hirc -SRC = src/main.c src/handle.c src/hist.c src/nick.c \ - src/chan.c src/serv.c src/ui.c src/commands.c \ - src/config.c +SRC = src/main.c src/mem.c src/handle.c src/hist.c \ + src/nick.c src/chan.c src/serv.c src/ui.c \ + src/commands.c src/config.c OBJ = $(SRC:.c=.o) MAN = hirc.1 COMMIT = $(shell git log HEAD...HEAD~1 --pretty=format:%h) diff --git a/src/hirc.h b/src/hirc.h @@ -40,14 +40,6 @@ size_t wcslcpy(wchar_t *, const wchar_t *, size_t); #endif /* HIST_WCSLCPY */ /* main.c */ -void * emalloc(size_t size); -void * erealloc(void *ptr, size_t size); -char * estrdup(const char *str); -void * talloc(size_t size); -char * tstrdup(const char *str); -wchar_t * ewcsdup(const wchar_t *str); -wchar_t * stowc(char *str); -char * wctos(wchar_t *str); void cleanup(char *quitmsg); void param_free(char **params); int param_len(char **params); @@ -63,6 +55,16 @@ int strisnum(char *str); char * strntok(char *str, char *sep, int n); char * strrdate(time_t secs); +/* mem.c */ +void * emalloc(size_t size); +void * erealloc(void *ptr, size_t size); +char * estrdup(const char *str); +void * talloc(size_t size); +char * tstrdup(const char *str); +wchar_t * ewcsdup(const wchar_t *str); +wchar_t * stowc(char *str); +char * wctos(wchar_t *str); + /* chan.c */ void chan_free(struct Channel *channel); void chan_free_list(struct Channel **head); diff --git a/src/main.c b/src/main.c @@ -34,119 +34,6 @@ struct Server *servers = NULL; struct HistInfo *main_buf; -void * -emalloc(size_t size) { - void *mem; - - if ((mem = malloc(size)) == NULL) { - perror("malloc()"); - endwin(); - exit(EXIT_FAILURE); - } - - return mem; -} - -char * -estrdup(const char *str) { - char *ret; - - if ((ret = strdup(str)) == NULL) { - perror("strdup()"); - endwin(); - exit(EXIT_FAILURE); - } - - return ret; -} - -void * -erealloc(void *ptr, size_t size) { - void *mem; - - if ((mem = realloc(ptr, size)) == NULL) { - perror("realloc()"); - endwin(); - exit(EXIT_FAILURE); - } - - return mem; -} - -/* 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 = emalloc((sizeof(char *)) * (mems + 1)); - else - mema = erealloc(mema, (sizeof(char *)) * (mems + 1)); - - *(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; -} - -wchar_t * -ewcsdup(const wchar_t *str) { - wchar_t *ret; - if ((ret = wcsdup(str)) == NULL) { - endwin(); - perror("wcsdup()"); - exit(EXIT_FAILURE); - } - return ret; -} - -wchar_t * -stowc(char *str) { - wchar_t *ret; - size_t len; - - len = mbstowcs(NULL, str, 0) + 1; - ret = emalloc(len * sizeof(wchar_t)); - mbstowcs(ret, str, len); - return ret; -} - -char * -wctos(wchar_t *str) { - char *ret; - size_t len; - - len = wcstombs(NULL, str, 0) + 1; - ret = emalloc(len); - wcstombs(ret, str, len); - return ret; -} - -/* 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;