From b2756d8a49df1ec8efe12f2e7f50ae930d043560 Mon Sep 17 00:00:00 2001 From: rxi Date: Fri, 8 May 2020 13:41:39 +0100 Subject: [PATCH] Removed xalloc.c/h --- src/api/renderer.c | 1 - src/renderer.c | 31 ++++++++++++++++++++----------- src/xalloc.c | 40 ---------------------------------------- src/xalloc.h | 11 ----------- 4 files changed, 20 insertions(+), 63 deletions(-) delete mode 100644 src/xalloc.c delete mode 100644 src/xalloc.h diff --git a/src/api/renderer.c b/src/api/renderer.c index fa5990b6..6b9a98e9 100644 --- a/src/api/renderer.c +++ b/src/api/renderer.c @@ -1,7 +1,6 @@ #include "api.h" #include "renderer.h" #include "rencache.h" -#include "xalloc.h" static RenColor checkcolor(lua_State *L, int idx, int def) { diff --git a/src/renderer.c b/src/renderer.c index c386724c..cc88cb89 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -2,7 +2,6 @@ #include #include #include "lib/stb/stb_truetype.h" -#include "xalloc.h" #include "renderer.h" #define MAX_GLYPHSET 256 @@ -30,6 +29,15 @@ static SDL_Window *window; static struct { int left, top, right, bottom; } clip; +static void* check_alloc(void *ptr) { + if (!ptr) { + fprintf(stderr, "Fatal error: memory allocation failed\n"); + exit(EXIT_FAILURE); + } + return ptr; +} + + static const char* utf8_to_codepoint(const char *p, unsigned *dst) { unsigned res, n; switch (*p & 0xf0) { @@ -77,7 +85,8 @@ void ren_get_size(int *x, int *y) { RenImage* ren_new_image(int width, int height) { assert(width > 0 && height > 0); - RenImage *image = xmalloc(sizeof(RenImage) + width * height * sizeof(RenColor)); + RenImage *image = malloc(sizeof(RenImage) + width * height * sizeof(RenColor)); + check_alloc(image); image->pixels = (void*) (image + 1); image->width = width; image->height = height; @@ -86,12 +95,12 @@ RenImage* ren_new_image(int width, int height) { void ren_free_image(RenImage *image) { - xfree(image); + free(image); } static GlyphSet* load_glyphset(RenFont *font, int idx) { - GlyphSet *set = xcalloc(1, sizeof(GlyphSet)); + GlyphSet *set = check_alloc(calloc(1, sizeof(GlyphSet))); /* init image */ int width = 128; @@ -149,7 +158,7 @@ RenFont* ren_load_font(const char *filename, float size) { FILE *fp = NULL; /* init font */ - font = xcalloc(1, sizeof(RenFont)); + font = check_alloc(calloc(1, sizeof(RenFont))); font->size = size; /* load font into buffer */ @@ -158,7 +167,7 @@ RenFont* ren_load_font(const char *filename, float size) { /* get size */ fseek(fp, 0, SEEK_END); int buf_size = ftell(fp); fseek(fp, 0, SEEK_SET); /* load */ - font->data = xmalloc(buf_size); + font->data = check_alloc(malloc(buf_size)); int _ = fread(font->data, 1, buf_size, fp); (void) _; fclose(fp); fp = NULL; @@ -182,8 +191,8 @@ RenFont* ren_load_font(const char *filename, float size) { fail: if (fp) { fclose(fp); } - if (font) { xfree(font->data); } - xfree(font); + if (font) { free(font->data); } + free(font); return NULL; } @@ -193,11 +202,11 @@ void ren_free_font(RenFont *font) { GlyphSet *set = font->sets[i]; if (set) { ren_free_image(set->image); - xfree(set); + free(set); } } - xfree(font->data); - xfree(font); + free(font->data); + free(font); } diff --git a/src/xalloc.c b/src/xalloc.c deleted file mode 100644 index 3b4b73a5..00000000 --- a/src/xalloc.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include "xalloc.h" - - -static void panic(void) { - fprintf(stderr, "Fatal error: out of memory\n"); - exit(1); -} - - -void* xmalloc(size_t size) { - void *ptr = malloc(size); - if (!ptr) { - panic(); - } - return ptr; -} - - -void xfree(void *ptr) { - free(ptr); -} - - -void* xcalloc(size_t nmemb, size_t size) { - void *ptr = calloc(nmemb, size); - if (!ptr) { - panic(); - } - return ptr; -} - - -void* xrealloc(void *ptr, size_t size) { - ptr = realloc(ptr, size); - if (!ptr) { - panic(); - } - return ptr; -} diff --git a/src/xalloc.h b/src/xalloc.h deleted file mode 100644 index e64ae7fd..00000000 --- a/src/xalloc.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef XALLOC_H -#define XALLOC_H - -#include - -void* xmalloc(size_t size); -void xfree(void *ptr); -void* xcalloc(size_t nmemb, size_t size); -void* xrealloc(void *ptr, size_t size); - -#endif