]> git.mdlowis.com Git - proto/labwc.git/commitdiff
array: use die_if_null() from common/mem.c
authorJohn Lindgren <john@jlindgren.net>
Wed, 3 Sep 2025 04:00:01 +0000 (00:00 -0400)
committerHiroaki Yamamoto <hrak1529@gmail.com>
Sun, 7 Sep 2025 10:17:18 +0000 (19:17 +0900)
include/common/array.h
include/common/mem.h
src/common/mem.c

index a1ea7499005fbcafecfe1038bb6a3e24abd5ee97..b60bafd641f635d0f0c78e7d5e38e19e480406fb 100644 (file)
@@ -1,9 +1,9 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 #ifndef LABWC_ARRAY_H
 #define LABWC_ARRAY_H
-#include <stdio.h>
-#include <stdlib.h>
+
 #include <wayland-server-core.h>
+#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)
 
index d0d5fdc59af9bf0ae2bfbe89ab676a089cc50e3c..ba19c1d680585c5f95163fd5ae583c1e8e44df68 100644 (file)
@@ -4,6 +4,12 @@
 
 #include <stdlib.h>
 
+/*
+ * 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.
index a7289ea05d10add53c2eef28827590461a724fee..72ba33246647084037bfe86fc8d8196b03e2df90 100644 (file)
@@ -5,7 +5,7 @@
 #include <stdio.h>
 #include <string.h>
 
-static void
+void
 die_if_null(void *ptr)
 {
        if (!ptr) {