hirc

[archived] IRC client
git clone https://hhvn.uk/hirc
git clone git://hhvn.uk/hirc
Log | Files | Refs

mem.c (2199B)


      1 /*
      2  * src/mem.c from hirc
      3  *
      4  * Copyright (c) 2021-2022 hhvn <dev@hhvn.uk>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  *
     18  */
     19 
     20 #include <stdio.h>
     21 #include <wchar.h>
     22 #include <string.h>
     23 #include <stdlib.h>
     24 #include <ncurses.h>
     25 #include "hirc.h"
     26 
     27 /* Free memory and make the original pointer point to NULL.
     28  * This makes it hard to double free or access free'd memory.
     29  * Anything that attempts to dereference free'd memory will
     30  * instead dereference NULL resulting in a segfault, rather
     31  * than producing potential heisenbugs. */
     32 void
     33 pfree_(void **ptr) {
     34 	if (!ptr || !*ptr)
     35 		return;
     36 	free(*ptr);
     37 	*ptr = NULL;
     38 }
     39 
     40 /* Error-checking memory allocation.
     41  * IRC isn't that critical in most cases, better 
     42  * to kill ourself than let oomkiller do it */
     43 void *
     44 emalloc(size_t size) {
     45 	void *mem;
     46 
     47 	assert_warn(size, NULL);
     48 	if ((mem = malloc(size)) == NULL) {
     49 		endwin();
     50 		perror("malloc()");
     51 		exit(EXIT_FAILURE);
     52 	}
     53 
     54 	return mem;
     55 }
     56 
     57 void *
     58 erealloc(void *ptr, size_t size) {
     59 	void *mem;
     60 
     61 	assert_warn(ptr && size, NULL);
     62 	if ((mem = realloc(ptr, size)) == NULL) {
     63 		endwin();
     64 		perror("realloc()");
     65 		exit(EXIT_FAILURE);
     66 	}
     67 
     68 	return mem;
     69 }
     70 
     71 char *
     72 estrdup(const char *str) {
     73 	char *ret;
     74 
     75 	assert_warn(str, NULL);
     76 	if ((ret = strdup(str)) == NULL) {
     77 		endwin();
     78 		perror("strdup()");
     79 		exit(EXIT_FAILURE);
     80 	}
     81 
     82 	return ret;
     83 }
     84 
     85 wchar_t *
     86 ewcsdup(const wchar_t *str) {
     87 	wchar_t *ret;
     88 	assert_warn(str, NULL);
     89 	if ((ret = wcsdup(str)) == NULL) {
     90 		endwin();
     91 		perror("wcsdup()");
     92 		exit(EXIT_FAILURE);
     93 	}
     94 	return ret;
     95 }