diff --git a/include/common/array.h b/include/common/array.h index a1ea7499..b60bafd6 100644 --- a/include/common/array.h +++ b/include/common/array.h @@ -1,9 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #ifndef LABWC_ARRAY_H #define LABWC_ARRAY_H -#include -#include + #include +#include "common/mem.h" /* * Wayland's wl_array API is a bit sparse consisting only of @@ -66,10 +66,7 @@ wl_array_len(struct wl_array *array) #define array_add(_arr, _val) do { \ __typeof__(_val) *_entry = wl_array_add( \ (_arr), sizeof(__typeof__(_val))); \ - if (!_entry) { \ - perror("Failed to allocate memory"); \ - exit(EXIT_FAILURE); \ - } \ + die_if_null(_entry); \ *_entry = (_val); \ } while (0) diff --git a/include/common/mem.h b/include/common/mem.h index d0d5fdc5..ba19c1d6 100644 --- a/include/common/mem.h +++ b/include/common/mem.h @@ -4,6 +4,12 @@ #include +/* + * If ptr is NULL, prints an error (based on errno) and exits. + * Suitable for checking the return value of malloc() etc. + */ +void die_if_null(void *ptr); + /* * As defined in busybox, weston, etc. * Allocates zero-filled memory; calls exit() on error. diff --git a/src/common/mem.c b/src/common/mem.c index a7289ea0..72ba3324 100644 --- a/src/common/mem.c +++ b/src/common/mem.c @@ -5,7 +5,7 @@ #include #include -static void +void die_if_null(void *ptr) { if (!ptr) {