From: Michael Stapelberg Date: Sun, 2 Oct 2011 17:08:49 +0000 (+0100) Subject: move sstrdup, scalloc, smalloc, srealloc to libi3, improve error messages X-Git-Tag: 4.1~126^2~4 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=501dc36b98c7e02cb799e0299216f8f95d45a18c;p=i3%2Fi3 move sstrdup, scalloc, smalloc, srealloc to libi3, improve error messages --- diff --git a/include/libi3.h b/include/libi3.h index 3883ba82..a675d11e 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -14,4 +14,32 @@ */ char *socket_path_from_x11(); +/** + * Safe-wrapper around malloc which exits if malloc returns NULL (meaning that + * there is no more memory available) + * + */ +void *smalloc(size_t size); + +/** + * Safe-wrapper around calloc which exits if malloc returns NULL (meaning that + * there is no more memory available) + * + */ +void *scalloc(size_t size); + +/** + * Safe-wrapper around realloc which exits if realloc returns NULL (meaning + * that there is no more memory available). + * + */ +void *srealloc(void *ptr, size_t size); + +/** + * Safe-wrapper around strdup which exits if malloc returns NULL (meaning that + * there is no more memory available) + * + */ +char *sstrdup(const char *str); + #endif diff --git a/include/util.h b/include/util.h index edc51d81..7c7b819a 100644 --- a/include/util.h +++ b/include/util.h @@ -66,34 +66,6 @@ Rect rect_add(Rect a, Rect b); */ bool update_if_necessary(uint32_t *destination, const uint32_t new_value); -/** - * Safe-wrapper around malloc which exits if malloc returns NULL (meaning that - * there is no more memory available) - * - */ -void *smalloc(size_t size); - -/** - * Safe-wrapper around calloc which exits if malloc returns NULL (meaning that - * there is no more memory available) - * - */ -void *scalloc(size_t size); - -/** - * Safe-wrapper around realloc which exits if realloc returns NULL (meaning - * that there is no more memory available). - * - */ -void *srealloc(void *ptr, size_t size); - -/** - * Safe-wrapper around strdup which exits if malloc returns NULL (meaning that - * there is no more memory available) - * - */ -char *sstrdup(const char *str); - /** * Starts the given application by passing it through a shell. We use double * fork to avoid zombie processes. As the started application’s parent exits diff --git a/libi3/safewrappers.c b/libi3/safewrappers.c new file mode 100644 index 00000000..82311fe3 --- /dev/null +++ b/libi3/safewrappers.c @@ -0,0 +1,47 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * + * © 2009-2011 Michael Stapelberg and contributors + * + * See file LICENSE for license information. + * + */ +#include +#include +#include + + +/* + * The s* functions (safe) are wrappers around malloc, strdup, …, which exits if one of + * the called functions returns NULL, meaning that there is no more memory available + * + */ +void *smalloc(size_t size) { + void *result = malloc(size); + if (result == NULL) + err(EXIT_FAILURE, "malloc(%zd)", size); + return result; +} + +void *scalloc(size_t size) { + void *result = calloc(size, 1); + if (result == NULL) + err(EXIT_FAILURE, "calloc(%zd)", size); + return result; +} + +void *srealloc(void *ptr, size_t size) { + void *result = realloc(ptr, size); + if (result == NULL && size > 0) + err(EXIT_FAILURE, "realloc(%zd)", size); + return result; +} + +char *sstrdup(const char *str) { + char *result = strdup(str); + if (result == NULL) + err(EXIT_FAILURE, "strdup()"); + return result; +} diff --git a/src/cfgparse.l b/src/cfgparse.l index dad5a915..49714401 100644 --- a/src/cfgparse.l +++ b/src/cfgparse.l @@ -17,6 +17,7 @@ #include "config.h" #include "log.h" #include "util.h" +#include "libi3.h" #include "cfgparse.tab.h" diff --git a/src/cmdparse.l b/src/cmdparse.l index 968b7e52..f6b132ca 100644 --- a/src/cmdparse.l +++ b/src/cmdparse.l @@ -19,6 +19,7 @@ #include "config.h" #include "util.h" +#include "libi3.h" int cmdyycolumn = 1; diff --git a/src/util.c b/src/util.c index 2d6c3e14..30371bcd 100644 --- a/src/util.c +++ b/src/util.c @@ -58,36 +58,6 @@ bool update_if_necessary(uint32_t *destination, const uint32_t new_value) { return ((*destination = new_value) != old_value); } -/* - * The s* functions (safe) are wrappers around malloc, strdup, …, which exits if one of - * the called functions returns NULL, meaning that there is no more memory available - * - */ -void *smalloc(size_t size) { - void *result = malloc(size); - exit_if_null(result, "Error: out of memory (malloc(%zd))\n", size); - return result; -} - -void *scalloc(size_t size) { - void *result = calloc(size, 1); - exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size); - return result; -} - -void *srealloc(void *ptr, size_t size) { - void *result = realloc(ptr, size); - if (result == NULL && size > 0) - die("Error: out memory (realloc(%zd))\n", size); - return result; -} - -char *sstrdup(const char *str) { - char *result = strdup(str); - exit_if_null(result, "Error: out of memory (strdup())\n"); - return result; -} - /* * Starts the given application by passing it through a shell. We use double fork * to avoid zombie processes. As the started application’s parent exits (immediately),