]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common: Add additional memory utilities (xzalloc() etc.)
authorJohn Lindgren <john@jlindgren.net>
Fri, 16 Sep 2022 22:41:02 +0000 (18:41 -0400)
committerJohn Lindgren <john@jlindgren.net>
Sat, 17 Sep 2022 14:57:30 +0000 (10:57 -0400)
35 files changed:
include/common/mem.h [new file with mode: 0644]
include/common/zfree.h [deleted file]
src/action.c
src/buffer.c
src/common/buf.c
src/common/graphic-helpers.c
src/common/mem.c [new file with mode: 0644]
src/common/meson.build
src/common/scaled_font_buffer.c
src/common/scaled_scene_buffer.c
src/common/zfree.c [deleted file]
src/config/keybind.c
src/config/libinput.c
src/config/mousebind.c
src/config/rcxml.c
src/config/session.c
src/cursor.c
src/layers.c
src/main.c
src/menu/menu.c
src/node.c
src/output.c
src/seat.c
src/ssd/ssd_extents.c
src/ssd/ssd_part.c
src/ssd/ssd_titlebar.c
src/theme.c
src/workspaces.c
src/xbm/parse.c
src/xbm/tokenize.c
src/xdg-deco.c
src/xdg-popup.c
src/xdg.c
src/xwayland-unmanaged.c
src/xwayland.c

diff --git a/include/common/mem.h b/include/common/mem.h
new file mode 100644 (file)
index 0000000..b0e9121
--- /dev/null
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LABWC_MEM_H
+#define __LABWC_MEM_H
+
+#include <stdlib.h>
+
+/*
+ * As defined in busybox, weston, etc.
+ * Allocates zero-filled memory; calls exit() on error.
+ * Returns NULL only if (size == 0).
+ */
+void *xzalloc(size_t size);
+
+/*
+ * As defined in FreeBSD.
+ * Like realloc(), but calls exit() on error.
+ * Returns NULL only if (size == 0).
+ * Does NOT zero-fill memory.
+ */
+void *xrealloc(void *ptr, size_t size);
+
+/* malloc() is a specific case of realloc() */
+#define xmalloc(size) xrealloc(NULL, (size))
+
+/*
+ * As defined in FreeBSD.
+ * Allocates a copy of <str>; calls exit() on error.
+ * Requires (str != NULL) and never returns NULL.
+ */
+char *xstrdup(const char *str);
+
+/*
+ * Frees memory pointed to by <ptr> and sets <ptr> to NULL.
+ * Does nothing if <ptr> is already NULL.
+ */
+#define zfree(ptr) do { \
+        free(ptr); (ptr) = NULL; \
+} while (0)
+
+#endif /* __LABWC_MEM_H */
diff --git a/include/common/zfree.h b/include/common/zfree.h
deleted file mode 100644 (file)
index 30479fb..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#ifndef __LABWC_ZFREE_H
-#define __LABWC_ZFREE_H
-
-void __zfree(void **ptr);
-
-#define zfree(ptr) __zfree((void **)&(ptr))
-
-#endif /* __LABWC_ZFREE_H */
index 95871e43b1a22a8358f7c3007f0620de0e3d5d51..233348fa994199739aca852d98cb0426af59903c 100644 (file)
@@ -6,8 +6,8 @@
 #include <strings.h>
 #include <unistd.h>
 #include <wlr/util/log.h>
+#include "common/mem.h"
 #include "common/spawn.h"
-#include "common/zfree.h"
 #include "debug.h"
 #include "labwc.h"
 #include "menu/menu.h"
@@ -105,7 +105,7 @@ action_create(const char *action_name)
                wlr_log(WLR_ERROR, "action name not specified");
                return NULL;
        }
-       struct action *action = calloc(1, sizeof(struct action));
+       struct action *action = xzalloc(sizeof(struct action));
        action->type = action_type_from_str(action_name);
        wl_list_init(&action->args);
        return action;
@@ -372,11 +372,11 @@ void
 action_arg_add_str(struct action *action, char *key, const char *value)
 {
        assert(value && "Tried to add NULL action string argument");
-       struct action_arg_str *arg = calloc(1, sizeof(*arg));
+       struct action_arg_str *arg = xzalloc(sizeof(*arg));
        arg->base.type = LAB_ACTION_ARG_STR;
        if (key) {
-               arg->base.key = strdup(key);
+               arg->base.key = xstrdup(key);
        }
-       arg->value = strdup(value);
+       arg->value = xstrdup(value);
        wl_list_insert(action->args.prev, &arg->base.link);
 }
index 1c64276a38ed1f13462aa4bbfaca54a0625b8726..e9b02b95551b7569d33df9190c74775ac6e28107 100644 (file)
  * SOFTWARE.
  */
 
-#include "config.h"
 #include <assert.h>
 #include <stdlib.h>
 #include <drm_fourcc.h>
 #include <wlr/interfaces/wlr_buffer.h>
 #include "buffer.h"
+#include "common/mem.h"
 
 static const struct wlr_buffer_impl data_buffer_impl;
 
@@ -88,10 +88,7 @@ struct lab_data_buffer *
 buffer_create_cairo(uint32_t width, uint32_t height, float scale,
        bool free_on_destroy)
 {
-       struct lab_data_buffer *buffer = calloc(1, sizeof(*buffer));
-       if (!buffer) {
-               return NULL;
-       }
+       struct lab_data_buffer *buffer = xzalloc(sizeof(*buffer));
        buffer->unscaled_width = width;
        buffer->unscaled_height = height;
        width *= scale;
@@ -130,10 +127,7 @@ struct lab_data_buffer *
 buffer_create_wrap(void *pixel_data, uint32_t width, uint32_t height,
        uint32_t stride, bool free_on_destroy)
 {
-       struct lab_data_buffer *buffer = calloc(1, sizeof(*buffer));
-       if (!buffer) {
-               return NULL;
-       }
+       struct lab_data_buffer *buffer = xzalloc(sizeof(*buffer));
        wlr_buffer_init(&buffer->base, &data_buffer_impl, width, height);
        buffer->data = pixel_data;
        buffer->format = DRM_FORMAT_ARGB8888;
index f2e3b095212dd1d71a460baa8507194cf6896ef1..740489dd4940eb17c1b02306ce4fed621943c762 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <ctype.h>
 #include "common/buf.h"
+#include "common/mem.h"
 
 static void
 strip_curly_braces(char *s)
@@ -45,7 +46,7 @@ buf_expand_shell_variables(struct buf *s)
                        /* just add one character at a time */
                        if (new.alloc <= new.len + 1) {
                                new.alloc = new.alloc * 3 / 2 + 16;
-                               new.buf = realloc(new.buf, new.alloc);
+                               new.buf = xrealloc(new.buf, new.alloc);
                        }
                        new.buf[new.len++] = s->buf[i];
                        new.buf[new.len] = '\0';
@@ -60,7 +61,7 @@ void
 buf_init(struct buf *s)
 {
        s->alloc = 256;
-       s->buf = malloc(s->alloc);
+       s->buf = xmalloc(s->alloc);
        s->buf[0] = '\0';
        s->len = 0;
 }
@@ -74,7 +75,7 @@ buf_add(struct buf *s, const char *data)
        int len = strlen(data);
        if (s->alloc <= s->len + len + 1) {
                s->alloc = s->alloc + len;
-               s->buf = realloc(s->buf, s->alloc);
+               s->buf = xrealloc(s->buf, s->alloc);
        }
        memcpy(s->buf + s->len, data, len);
        s->len += len;
index eb14789354294824ca98614eca6188197acdf5d5..f7ae2577524aac092477a6e7fb0c3b918c5075c7 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <wlr/types/wlr_scene.h>
 #include "common/graphic-helpers.h"
+#include "common/mem.h"
 
 static void
 multi_rect_destroy_notify(struct wl_listener *listener, void *data)
@@ -16,7 +17,7 @@ multi_rect_destroy_notify(struct wl_listener *listener, void *data)
 struct multi_rect *
 multi_rect_create(struct wlr_scene_tree *parent, float *colors[3], int line_width)
 {
-       struct multi_rect *rect = calloc(1, sizeof(*rect));
+       struct multi_rect *rect = xzalloc(sizeof(*rect));
        rect->line_width = line_width;
        rect->tree = wlr_scene_tree_create(parent);
        rect->destroy.notify = multi_rect_destroy_notify;
diff --git a/src/common/mem.c b/src/common/mem.c
new file mode 100644 (file)
index 0000000..e63987b
--- /dev/null
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#define _POSIX_C_SOURCE 200809L
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include "common/mem.h"
+
+static void
+die_if_null(void *ptr)
+{
+       if (!ptr) {
+               perror("Failed to allocate memory");
+               exit(EXIT_FAILURE);
+       }
+}
+
+void *
+xzalloc(size_t size)
+{
+       if (!size) {
+               return NULL;
+       }
+       void *ptr = calloc(1, size);
+       die_if_null(ptr);
+       return ptr;
+}
+
+void *
+xrealloc(void *ptr, size_t size)
+{
+       if (!size) {
+               free(ptr);
+               return NULL;
+       }
+       ptr = realloc(ptr, size);
+       die_if_null(ptr);
+       return ptr;
+}
+
+char *
+xstrdup(const char *str)
+{
+       assert(str);
+       char *copy = strdup(str);
+       die_if_null(copy);
+       return copy;
+}
index 72a6d2157a5ecb644dde5147a902686d6ec0efb3..e4f507b645ec35aa390acd3cf2701aa3fa9c62d6 100644 (file)
@@ -5,11 +5,11 @@ labwc_sources += files(
   'font.c',
   'grab-file.c',
   'graphic-helpers.c',
+  'mem.c',
   'nodename.c',
   'scaled_font_buffer.c',
   'scaled_scene_buffer.c',
   'scene-helpers.c',
   'spawn.c',
   'string-helpers.c',
-  'zfree.c',
 )
index 76f8ad751b50a00353027ce00e5631856a5721f8..87ac0cd04541d4f9ce26b8735550275a88bdfcf9 100644 (file)
@@ -7,9 +7,9 @@
 #include <wlr/util/log.h>
 #include "buffer.h"
 #include "common/font.h"
+#include "common/mem.h"
 #include "common/scaled_scene_buffer.h"
 #include "common/scaled_font_buffer.h"
-#include "common/zfree.h"
 
 static struct lab_data_buffer *
 _create_buffer(struct scaled_scene_buffer *scaled_buffer, double scale)
@@ -46,10 +46,7 @@ struct scaled_font_buffer *
 scaled_font_buffer_create(struct wlr_scene_tree *parent)
 {
        assert(parent);
-       struct scaled_font_buffer *self = calloc(1, sizeof(*self));
-       if (!self) {
-               return NULL;
-       }
+       struct scaled_font_buffer *self = xzalloc(sizeof(*self));
 
        struct scaled_scene_buffer *scaled_buffer
                = scaled_scene_buffer_create(parent, &impl);
@@ -80,16 +77,16 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
        zfree(self->arrow);
 
        /* Update internal state */
-       self->text = strdup(text);
+       self->text = xstrdup(text);
        self->max_width = max_width;
        if (font->name) {
-               self->font.name = strdup(font->name);
+               self->font.name = xstrdup(font->name);
        }
        self->font.size = font->size;
        self->font.slant = font->slant;
        self->font.weight = font->weight;
        memcpy(self->color, color, sizeof(self->color));
-       self->arrow = arrow ? strdup(arrow) : NULL;
+       self->arrow = arrow ? xstrdup(arrow) : NULL;
 
        /* Invalidate cache and force a new render */
        scaled_scene_buffer_invalidate_cache(self->scaled_buffer);
index 95e2673df13b841c7ca0e65cfc2ae21ec0e4c95a..cc543ca037a24ef223c082b270b856e50a58ff27 100644 (file)
@@ -7,6 +7,7 @@
 #include <wlr/types/wlr_scene.h>
 #include <wlr/util/log.h>
 #include "buffer.h"
+#include "common/mem.h"
 #include "common/scaled_scene_buffer.h"
 
 /**
@@ -67,7 +68,7 @@ _update_buffer(struct scaled_scene_buffer *self, double scale)
 
        /* Create or reuse cache entry */
        if (wl_list_length(&self->cache) < LAB_SCALED_BUFFER_MAX_CACHE) {
-               cache_entry = calloc(1, sizeof(*cache_entry));
+               cache_entry = xzalloc(sizeof(*cache_entry));
        } else {
                cache_entry = wl_container_of(self->cache.prev, cache_entry, link);
                if (cache_entry->buffer) {
@@ -150,10 +151,7 @@ scaled_scene_buffer_create(struct wlr_scene_tree *parent,
        assert(impl);
        assert(impl->create_buffer);
 
-       struct scaled_scene_buffer *self = calloc(1, sizeof(*self));
-       if (!self) {
-               return NULL;
-       }
+       struct scaled_scene_buffer *self = xzalloc(sizeof(*self));
 
        self->scene_buffer = wlr_scene_buffer_create(parent, NULL);
        if (!self->scene_buffer) {
diff --git a/src/common/zfree.c b/src/common/zfree.c
deleted file mode 100644 (file)
index 3eb399a..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-#include <stdlib.h>
-#include "common/zfree.h"
-
-void __zfree(void **ptr)
-{
-       if (!ptr || !*ptr) {
-               return;
-       }
-       free(*ptr);
-       *ptr = NULL;
-}
index f90bea089ea897cf3aff7a9f7f883e8f8545f098..10be087576b4beefd0cb678ee1c5defdda3d8e6a 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <wlr/util/log.h>
+#include "common/mem.h"
 #include "config/keybind.h"
 #include "config/rcxml.h"
 
@@ -27,7 +28,7 @@ parse_modifier(const char *symname)
 struct keybind *
 keybind_create(const char *keybind)
 {
-       struct keybind *k = calloc(1, sizeof(struct keybind));
+       struct keybind *k = xzalloc(sizeof(struct keybind));
        xkb_keysym_t keysyms[MAX_KEYSYMS];
        gchar **symnames = g_strsplit(keybind, "-", -1);
        for (int i = 0; symnames[i]; i++) {
@@ -60,7 +61,7 @@ keybind_create(const char *keybind)
                return NULL;
        }
        wl_list_insert(rc.keybinds.prev, &k->link);
-       k->keysyms = malloc(k->keysyms_len * sizeof(xkb_keysym_t));
+       k->keysyms = xmalloc(k->keysyms_len * sizeof(xkb_keysym_t));
        memcpy(k->keysyms, keysyms, k->keysyms_len * sizeof(xkb_keysym_t));
        wl_list_init(&k->actions);
        return k;
index 9efa343742aa5df5c0287ff6f407514b0409c71a..581987cf2e38f9e500f44e67069e004683617a8d 100644 (file)
@@ -2,8 +2,10 @@
 #include <string.h>
 #include <strings.h>
 
+#include "common/mem.h"
 #include "config/libinput.h"
 #include "config/rcxml.h"
+
 static void
 libinput_category_init(struct libinput_category *l)
 {
@@ -36,10 +38,7 @@ get_device_type(const char *s)
 struct libinput_category *
 libinput_category_create(void)
 {
-       struct libinput_category *l = calloc(1, sizeof(struct libinput_category));
-       if (!l) {
-               return NULL;
-       }
+       struct libinput_category *l = xzalloc(sizeof(struct libinput_category));
        libinput_category_init(l);
        wl_list_insert(&rc.libinput_categories, &l->link);
        return l;
index 075d89aa7e1dba9622623538ac67451f45d6648d..45029c93d02067c6933ad62f61a052cc3f8587be 100644 (file)
@@ -5,6 +5,7 @@
 #include <strings.h>
 #include <unistd.h>
 #include <wlr/util/log.h>
+#include "common/mem.h"
 #include "config/mousebind.h"
 #include "config/rcxml.h"
 
@@ -108,7 +109,7 @@ mousebind_create(const char *context)
                wlr_log(WLR_ERROR, "mousebind context not specified");
                return NULL;
        }
-       struct mousebind *m = calloc(1, sizeof(struct mousebind));
+       struct mousebind *m = xzalloc(sizeof(struct mousebind));
        m->context = context_from_str(context);
        if (m->context != LAB_SSD_NONE) {
                wl_list_insert(rc.mousebinds.prev, &m->link);
index 886f29b551f20dd4d2ca531da05fa644cc6630a1..f9f166ff8c862cfa13767e51a8b94c0ca4718ff8 100644 (file)
@@ -14,9 +14,9 @@
 #include <wayland-server-core.h>
 #include <wlr/util/log.h>
 #include "action.h"
+#include "common/mem.h"
 #include "common/nodename.h"
 #include "common/string-helpers.h"
-#include "common/zfree.h"
 #include "config/keybind.h"
 #include "config/libinput.h"
 #include "config/mousebind.h"
@@ -194,7 +194,7 @@ fill_libinput_category(char *nodename, char *content)
                                || !strcmp(content, "default")) {
                        current_libinput_category->type = get_device_type(content);
                } else {
-                       current_libinput_category->name = strdup(content);
+                       current_libinput_category->name = xstrdup(content);
                }
        } else if (!strcasecmp(nodename, "naturalScroll")) {
                current_libinput_category->natural_scroll =
@@ -241,7 +241,7 @@ set_font_attr(struct font *font, const char *nodename, const char *content)
 {
        if (!strcmp(nodename, "name")) {
                zfree(font->name);
-               font->name = strdup(content);
+               font->name = xstrdup(content);
        } else if (!strcmp(nodename, "size")) {
                font->size = atoi(content);
        } else if (!strcmp(nodename, "slant")) {
@@ -361,7 +361,7 @@ entry(xmlNode *node, char *nodename, char *content)
        } else if (!strcasecmp(nodename, "adaptiveSync.core")) {
                rc.adaptive_sync = get_bool(content);
        } else if (!strcmp(nodename, "name.theme")) {
-               rc.theme_name = strdup(content);
+               rc.theme_name = xstrdup(content);
        } else if (!strcmp(nodename, "cornerradius.theme")) {
                rc.corner_radius = atoi(content);
        } else if (!strcmp(nodename, "name.font.theme")) {
@@ -401,8 +401,8 @@ entry(xmlNode *node, char *nodename, char *content)
        } else if (!strcasecmp(nodename, "cycleViewOutlines.core")) {
                rc.cycle_preview_outlines = get_bool(content);
        } else if (!strcasecmp(nodename, "name.names.desktops")) {
-               struct workspace *workspace = calloc(1, sizeof(struct workspace));
-               workspace->name = strdup(content);
+               struct workspace *workspace = xzalloc(sizeof(struct workspace));
+               workspace->name = xstrdup(content);
                wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link);
        } else if (!strcasecmp(nodename, "popupTime.desktops")) {
                rc.workspace_config.popuptime = atoi(content);
@@ -671,13 +671,13 @@ post_processing(void)
        merge_mouse_bindings();
 
        if (!rc.font_activewindow.name) {
-               rc.font_activewindow.name = strdup("sans");
+               rc.font_activewindow.name = xstrdup("sans");
        }
        if (!rc.font_menuitem.name) {
-               rc.font_menuitem.name = strdup("sans");
+               rc.font_menuitem.name = xstrdup("sans");
        }
        if (!rc.font_osd.name) {
-               rc.font_osd.name = strdup("sans");
+               rc.font_osd.name = xstrdup("sans");
        }
        if (!wl_list_length(&rc.libinput_categories)) {
                /* So we still allow tap to click by default */
@@ -685,8 +685,8 @@ post_processing(void)
                l->type = DEFAULT_DEVICE;
        }
        if (!wl_list_length(&rc.workspace_config.workspaces)) {
-               struct workspace *workspace = calloc(1, sizeof(struct workspace));
-               workspace->name = strdup("Default");
+               struct workspace *workspace = xzalloc(sizeof(struct workspace));
+               workspace->name = xstrdup("Default");
                wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link);
        }
        if (rc.workspace_config.popuptime == INT_MIN) {
index f861e78501d319341dfcd8b057aa4e81497c7e89..1524edd0cc056b15439af0da0c4a35c625b0b6f3 100644 (file)
@@ -8,6 +8,7 @@
 #include <sys/stat.h>
 #include <wlr/util/log.h>
 #include "common/buf.h"
+#include "common/mem.h"
 #include "common/spawn.h"
 #include "common/string-helpers.h"
 
@@ -78,10 +79,7 @@ build_path(const char *dir, const char *filename)
                return NULL;
        }
        int len = strlen(dir) + strlen(filename) + 2;
-       char *buffer = calloc(1, len);
-       if (!buffer) {
-               return NULL;
-       }
+       char *buffer = xzalloc(len);
        strcat(buffer, dir);
        strcat(buffer, "/");
        strcat(buffer, filename);
@@ -103,25 +101,17 @@ update_activation_env(const char *env_keys)
        const char *dbus = "dbus-update-activation-environment ";
        const char *systemd = "systemctl --user import-environment ";
 
-       cmd = calloc(1, strlen(dbus) + strlen(env_keys) + 1);
-       if (!cmd) {
-               wlr_log(WLR_ERROR, "Failed to allocate memory for dbus env update");
-       } else {
-               strcat(cmd, dbus);
-               strcat(cmd, env_keys);
-               spawn_async_no_shell(cmd);
-               free(cmd);
-       }
+       cmd = xzalloc(strlen(dbus) + strlen(env_keys) + 1);
+       strcat(cmd, dbus);
+       strcat(cmd, env_keys);
+       spawn_async_no_shell(cmd);
+       free(cmd);
 
-       cmd = calloc(1, strlen(systemd) + strlen(env_keys) + 1);
-       if (!cmd) {
-               wlr_log(WLR_ERROR, "Failed to allocate memory for systemd env update");
-       } else {
-               strcat(cmd, systemd);
-               strcat(cmd, env_keys);
-               spawn_async_no_shell(cmd);
-               free(cmd);
-       }
+       cmd = xzalloc(strlen(systemd) + strlen(env_keys) + 1);
+       strcat(cmd, systemd);
+       strcat(cmd, env_keys);
+       spawn_async_no_shell(cmd);
+       free(cmd);
 }
 
 void
@@ -158,11 +148,7 @@ session_autostart_init(const char *dir)
        }
        wlr_log(WLR_INFO, "run autostart file %s", autostart);
        int len = strlen(autostart) + 4;
-       char *cmd = calloc(1, len);
-       if (!cmd) {
-               wlr_log(WLR_ERROR, "Failed to allocate memory for autostart command");
-               goto out;
-       }
+       char *cmd = xzalloc(len);
        strcat(cmd, "sh ");
        strcat(cmd, autostart);
        spawn_async_no_shell(cmd);
index 9753654a61b6de19d0c22ba47c60950a0424f1e3..ff9dc0b11167b27e89186a6c7166dd035731c382 100644 (file)
@@ -6,12 +6,13 @@
 #include <time.h>
 #include <wlr/types/wlr_primary_selection.h>
 #include "action.h"
+#include "common/mem.h"
+#include "common/scene-helpers.h"
+#include "config/mousebind.h"
 #include "labwc.h"
 #include "menu/menu.h"
 #include "resistance.h"
 #include "ssd.h"
-#include "config/mousebind.h"
-#include "common/scene-helpers.h"
 
 static const char **cursor_names = NULL;
 
@@ -531,15 +532,14 @@ create_constraint(struct wl_listener *listener, void *data)
        struct wlr_pointer_constraint_v1 *wlr_constraint = data;
        struct server *server = wl_container_of(listener, server,
                new_constraint);
-       struct view *view;
-       struct constraint *constraint = calloc(1, sizeof(struct constraint));
+       struct constraint *constraint = xzalloc(sizeof(struct constraint));
 
        constraint->constraint = wlr_constraint;
        constraint->seat = &server->seat;
        constraint->destroy.notify = destroy_constraint;
        wl_signal_add(&wlr_constraint->events.destroy, &constraint->destroy);
 
-       view = desktop_focused_view(server);
+       struct view *view = desktop_focused_view(server);
        if (view && view->surface == wlr_constraint->surface) {
                constrain_cursor(server, wlr_constraint);
        }
index 674deaa941f10cac51d7361138a8cb8bf5b24648..f03f2eba2f3ed015e5b7781a16dd937269a71612 100644 (file)
@@ -14,6 +14,7 @@
 #include <wayland-server.h>
 #include <wlr/types/wlr_layer_shell_v1.h>
 #include <wlr/util/log.h>
+#include "common/mem.h"
 #include "layers.h"
 #include "labwc.h"
 #include "node.h"
@@ -202,10 +203,7 @@ create_popup(struct wlr_xdg_popup *wlr_popup, struct wlr_scene_tree *parent,
                struct wlr_box *output_toplevel_sx_box)
 {
        struct lab_layer_popup *popup =
-               calloc(1, sizeof(struct lab_layer_popup));
-       if (!popup) {
-               return NULL;
-       }
+               xzalloc(sizeof(struct lab_layer_popup));
 
        popup->wlr_popup = wlr_popup;
        popup->scene_tree =
@@ -325,10 +323,7 @@ new_layer_surface_notify(struct wl_listener *listener, void *data)
        }
 
        struct lab_layer_surface *surface =
-               calloc(1, sizeof(struct lab_layer_surface));
-       if (!surface) {
-               return;
-       }
+               xzalloc(sizeof(struct lab_layer_surface));
 
        surface->surface_commit.notify = surface_commit_notify;
        wl_signal_add(&layer_surface->surface->events.commit,
index 0cb521b5a9e9ef0fe63ee5e17e4c8b732181de3e..8ca6fe09a35f2d46a165fc46f0fc3f506c10325a 100644 (file)
@@ -2,14 +2,14 @@
 #define _POSIX_C_SOURCE 200809L
 #include <string.h>
 #include "common/dir.h"
+#include "common/fd_util.h"
 #include "common/font.h"
+#include "common/mem.h"
 #include "common/spawn.h"
 #include "config/session.h"
 #include "labwc.h"
 #include "theme.h"
-#include "xbm/xbm.h"
 #include "menu/menu.h"
-#include "common/fd_util.h"
 
 struct rcxml rc = { 0 };
 
@@ -49,7 +49,7 @@ main(int argc, char *argv[])
                        config_file = optarg;
                        break;
                case 'C':
-                       rc.config_dir = strdup(optarg);
+                       rc.config_dir = xstrdup(optarg);
                        break;
                case 'd':
                        verbosity = WLR_DEBUG;
index bb72cd1b91b12dd491a7d10858521b6ae8ee155a..c839c1133334ac98f915dc1b8f33c578617d0196 100644 (file)
 #include <wlr/util/log.h>
 #include "common/buf.h"
 #include "common/font.h"
+#include "common/mem.h"
 #include "common/nodename.h"
 #include "common/scaled_font_buffer.h"
 #include "common/string-helpers.h"
-#include "common/zfree.h"
 #include "labwc.h"
 #include "menu/menu.h"
 #include "theme.h"
 #include "action.h"
-#include "buffer.h"
 #include "node.h"
 
 #define MENUWIDTH (110)
@@ -44,14 +43,14 @@ menu_create(struct server *server, const char *id, const char *label)
 {
        if (nr_menus == alloc_menus) {
                alloc_menus = (alloc_menus + 16) * 2;
-               menus = realloc(menus, alloc_menus * sizeof(struct menu));
+               menus = xrealloc(menus, alloc_menus * sizeof(struct menu));
        }
        struct menu *menu = menus + nr_menus;
        memset(menu, 0, sizeof(*menu));
        nr_menus++;
        wl_list_init(&menu->menuitems);
-       menu->id = strdup(id);
-       menu->label = label ? strdup(label) : strdup(id);
+       menu->id = xstrdup(id);
+       menu->label = xstrdup(label ? label : id);
        menu->parent = current_menu;
        menu->server = server;
        menu->size.width = MENUWIDTH;
@@ -80,10 +79,7 @@ menu_get_by_id(const char *id)
 static struct menuitem *
 item_create(struct menu *menu, const char *text, bool show_arrow)
 {
-       struct menuitem *menuitem = calloc(1, sizeof(struct menuitem));
-       if (!menuitem) {
-               return NULL;
-       }
+       struct menuitem *menuitem = xzalloc(sizeof(struct menuitem));
        menuitem->parent = menu;
        menuitem->selectable = true;
        struct server *server = menu->server;
@@ -164,10 +160,7 @@ item_create(struct menu *menu, const char *text, bool show_arrow)
 static struct menuitem *
 separator_create(struct menu *menu, const char *label)
 {
-       struct menuitem *menuitem = calloc(1, sizeof(struct menuitem));
-       if (!menuitem) {
-               return NULL;
-       }
+       struct menuitem *menuitem = xzalloc(sizeof(struct menuitem));
        menuitem->parent = menu;
        menuitem->selectable = false;
        struct server *server = menu->server;
index 67c01a153f94e34a7af86d4dc5b2a63ed362cd1e..53a87bcad7c99dda4023398bdff42d8f49ea45a0 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <assert.h>
 #include <stdlib.h>
+#include "common/mem.h"
 #include "node.h"
 
 static void
@@ -26,10 +27,7 @@ node_descriptor_create(struct wlr_scene_node *scene_node,
                enum node_descriptor_type type, void *data)
 {
        struct node_descriptor *node_descriptor =
-               calloc(1, sizeof(struct node_descriptor));
-       if (!node_descriptor) {
-               return;
-       }
+               xzalloc(sizeof(struct node_descriptor));
        node_descriptor->type = type;
        node_descriptor->data = data;
        node_descriptor->destroy.notify = destroy_notify;
index d441e7346ab9cee235e66fc37f494a5dd9caebb6..f350009e24408dd424a757258aee4067ce84be84 100644 (file)
@@ -7,7 +7,6 @@
  */
 
 #define _POSIX_C_SOURCE 200809L
-#include "config.h"
 #include <assert.h>
 #include <wlr/types/wlr_buffer.h>
 #include <wlr/types/wlr_drm_lease_v1.h>
 #include <wlr/types/wlr_scene.h>
 #include <wlr/util/region.h>
 #include <wlr/util/log.h>
-#include "buffer.h"
+#include "common/mem.h"
 #include "labwc.h"
 #include "layers.h"
-#include "menu/menu.h"
 #include "node.h"
-#include "ssd.h"
-#include "theme.h"
 
 static void
 output_frame_notify(struct wl_listener *listener, void *data)
@@ -129,7 +125,7 @@ new_output_notify(struct wl_listener *listener, void *data)
 
        wlr_output_commit(wlr_output);
 
-       struct output *output = calloc(1, sizeof(struct output));
+       struct output *output = xzalloc(sizeof(struct output));
        output->wlr_output = wlr_output;
        wlr_output->data = output;
        output->server = server;
index e19c663f39ef5695b3ac7e293f53532d8d8e44ec..70292259189a59f7a34a250ceeea0bb876b3848e 100644 (file)
@@ -8,6 +8,7 @@
 #include <wlr/types/wlr_pointer.h>
 #include <wlr/types/wlr_touch.h>
 #include <wlr/util/log.h>
+#include "common/mem.h"
 #include "labwc.h"
 
 static void
@@ -201,7 +202,7 @@ new_input_notify(struct wl_listener *listener, void *data)
 {
        struct seat *seat = wl_container_of(listener, seat, new_input);
        struct wlr_input_device *device = data;
-       struct input *input = calloc(1, sizeof(struct input));
+       struct input *input = xzalloc(sizeof(struct input));
        input->wlr_input_device = device;
        input->seat = seat;
 
@@ -262,11 +263,8 @@ new_idle_inhibitor(struct wl_listener *listener, void *data)
        struct seat *seat = wl_container_of(listener, seat,
                idle_inhibitor_create);
 
-       struct idle_inhibitor *inhibitor = calloc(1,
-               sizeof(struct idle_inhibitor));
-       if (!inhibitor) {
-               return;
-       }
+       struct idle_inhibitor *inhibitor =
+               xzalloc(sizeof(struct idle_inhibitor));
 
        inhibitor->seat = seat;
        inhibitor->wlr_inhibitor = wlr_inhibitor;
index 31e0b337763657458d87badb1dfe1b83084cdcb5..28a091627bbb7c1b3fe0a23af99819aeff007b33 100644 (file)
@@ -3,6 +3,7 @@
 #include "labwc.h"
 #include "ssd.h"
 #include "theme.h"
+#include "common/mem.h"
 #include "common/scene-helpers.h"
 
 static struct ssd_part *
@@ -18,7 +19,7 @@ add_extent(struct wl_list *part_list, enum ssd_part_type type,
         * part->geometry will get free'd automatically in ssd_destroy_parts().
         */
        part->node = &wlr_scene_rect_create(parent, 0, 0, invisible)->node;
-       part->geometry = calloc(1, sizeof(struct wlr_box));
+       part->geometry = xzalloc(sizeof(struct wlr_box));
        return part;
 }
 
index f08af948a2c1796ca4afd013686d4ebf79151542..9a4db0ced932c5a1e78d30c164642a2044ce0a55 100644 (file)
@@ -1,10 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include <assert.h>
+#include "common/mem.h"
 #include "labwc.h"
 #include "ssd.h"
 #include "node.h"
-#include "common/font.h"
 
 /* Internal helpers */
 static void
@@ -24,7 +24,7 @@ static struct ssd_button *
 ssd_button_descriptor_create(struct wlr_scene_node *node)
 {
        /* Create new ssd_button */
-       struct ssd_button *button = calloc(1, sizeof(struct ssd_button));
+       struct ssd_button *button = xzalloc(sizeof(struct ssd_button));
 
        /* Let it destroy automatically when the scene node destroys */
        button->destroy.notify = ssd_button_destroy_notify;
@@ -39,7 +39,7 @@ ssd_button_descriptor_create(struct wlr_scene_node *node)
 struct ssd_part *
 add_scene_part(struct wl_list *part_list, enum ssd_part_type type)
 {
-       struct ssd_part *part = calloc(1, sizeof(struct ssd_part));
+       struct ssd_part *part = xzalloc(sizeof(struct ssd_part));
        part->type = type;
        wl_list_insert(part_list->prev, &part->link);
        return part;
index b2cfe0ef65100f14cd524c7ee8ac1353b3c966c0..945c2844011a6f9761fe65415d3da3e7a6baffa4 100644 (file)
@@ -7,6 +7,7 @@
 #include "ssd.h"
 #include "theme.h"
 #include "common/font.h"
+#include "common/mem.h"
 #include "common/scaled_font_buffer.h"
 #include "common/scene-helpers.h"
 #include "node.h"
@@ -271,7 +272,7 @@ ssd_update_title(struct view *view)
                if (state->text) {
                        free(state->text);
                }
-               state->text = strdup(title);
+               state->text = xstrdup(title);
        }
        ssd_update_title_positions(view);
 }
index 45af59f80546b75ff1012dbe19d3fd31070757e0..29d4fe18b3aaf24ec8fba3aed82ab6df04e4ccc1 100644 (file)
@@ -22,7 +22,6 @@
 #include "common/font.h"
 #include "common/graphic-helpers.h"
 #include "common/string-helpers.h"
-#include "common/zfree.h"
 #include "config/rcxml.h"
 #include "theme.h"
 #include "xbm/xbm.h"
index b6a9f66437ece7b46842abe83ade0ec7b2745f25..38cf81da61297703a06b624a102394b2c63eed9b 100644 (file)
@@ -10,7 +10,7 @@
 #include "labwc.h"
 #include "common/font.h"
 #include "common/graphic-helpers.h"
-#include "common/zfree.h"
+#include "common/mem.h"
 #include "workspaces.h"
 
 /* Internal helpers */
@@ -154,9 +154,9 @@ _osd_update(struct server *server)
 static void
 add_workspace(struct server *server, const char *name)
 {
-       struct workspace *workspace = calloc(1, sizeof(struct workspace));
+       struct workspace *workspace = xzalloc(sizeof(struct workspace));
        workspace->server = server;
-       workspace->name = strdup(name);
+       workspace->name = xstrdup(name);
        workspace->tree = wlr_scene_tree_create(server->view_tree);
        wl_list_insert(server->workspaces.prev, &workspace->link);
        if (!server->workspace_current) {
index fdb5b07f7acc6d1a25740c71ce4e360c9ff0fcb2..1eb722cc36c3033a20cff0a52724031f9c7f5d20 100644 (file)
@@ -13,6 +13,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "common/mem.h"
 #include "xbm/parse.h"
 
 static uint32_t color;
@@ -37,8 +38,8 @@ parse_set_color(float *rgba)
 static void
 process_bytes(struct pixmap *pixmap, struct token *tokens)
 {
-       pixmap->data = (uint32_t *)calloc(pixmap->width * pixmap->height,
-                                         sizeof(uint32_t));
+       pixmap->data = (uint32_t *)xzalloc(
+               pixmap->width * pixmap->height * sizeof(uint32_t));
        struct token *t = tokens;
        for (int row = 0; row < pixmap->height; row++) {
                int byte = 1;
index ff6bc73b6f5ea6c9e10e7ac66148274e3b9c6641..218d0bd221f3aedaf72dd765e04bacbbb53f4935 100644 (file)
@@ -9,6 +9,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "common/mem.h"
 #include "xbm/tokenize.h"
 
 static char *current_buffer_position;
@@ -20,7 +21,7 @@ add_token(enum token_type token_type)
 {
        if (nr_tokens == alloc_tokens) {
                alloc_tokens = (alloc_tokens + 16) * 2;
-               tokens = realloc(tokens, alloc_tokens * sizeof(struct token));
+               tokens = xrealloc(tokens, alloc_tokens * sizeof(struct token));
        }
        struct token *token = tokens + nr_tokens;
        memset(token, 0, sizeof(*token));
index f0cdd8739ae48e09502f49fbf2550ba292dfa3ec..0d4eece2bc587d236cd44cd7f04535552124aa0e 100644 (file)
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0-only
+#include "common/mem.h"
 #include "labwc.h"
 
 struct xdg_deco {
@@ -43,10 +44,7 @@ xdg_toplevel_decoration(struct wl_listener *listener, void *data)
        struct server *server =
                wl_container_of(listener, server, xdg_toplevel_decoration);
        struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration = data;
-       struct xdg_deco *xdg_deco = calloc(1, sizeof(struct xdg_deco));
-       if (!xdg_deco) {
-               return;
-       }
+       struct xdg_deco *xdg_deco = xzalloc(sizeof(struct xdg_deco));
        xdg_deco->wlr_decoration = wlr_decoration;
        xdg_deco->server = server;
        xdg_deco->view = wlr_decoration->surface->data;
index 36873b6c3f2a01a31771952f47b06ca67727a94c..3edff40480399df3d07ec765e891c6f346977beb 100644 (file)
@@ -7,6 +7,7 @@
  *     - keeping non-layer-shell xdg-popups outside the layers.c code
  */
 
+#include "common/mem.h"
 #include "labwc.h"
 #include "node.h"
 
@@ -67,10 +68,7 @@ xdg_popup_create(struct view *view, struct wlr_xdg_popup *wlr_popup)
                return;
        }
 
-       struct xdg_popup *popup = calloc(1, sizeof(struct xdg_popup));
-       if (!popup) {
-               return;
-       }
+       struct xdg_popup *popup = xzalloc(sizeof(struct xdg_popup));
 
        popup->parent_view = view;
        popup->wlr_popup = wlr_popup;
index 2456f7f8a53f0f27ff1a40f29e90569695c60430..835cdb096615840bfc9a283e194e384ac01dcd51 100644 (file)
--- a/src/xdg.c
+++ b/src/xdg.c
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <assert.h>
+#include "common/mem.h"
 #include "labwc.h"
 #include "node.h"
 #include "ssd.h"
@@ -374,7 +375,7 @@ xdg_surface_new(struct wl_listener *listener, void *data)
 
        wlr_xdg_surface_ping(xdg_surface);
 
-       struct view *view = calloc(1, sizeof(struct view));
+       struct view *view = xzalloc(sizeof(struct view));
        view->server = server;
        view->type = LAB_XDG_SHELL_VIEW;
        view->impl = &xdg_toplevel_view_impl;
index 44608485ed3a71549ec46c12cad58bcee955e440..b5fb58e4c7f9b85b0d0cf6faaca31fb33a0f8ed5 100644 (file)
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0-only
+#include "common/mem.h"
 #include "labwc.h"
 
 static void
@@ -139,8 +140,8 @@ struct xwayland_unmanaged *
 xwayland_unmanaged_create(struct server *server,
                          struct wlr_xwayland_surface *xsurface)
 {
-       struct xwayland_unmanaged *unmanaged;
-       unmanaged = calloc(1, sizeof(struct xwayland_unmanaged));
+       struct xwayland_unmanaged *unmanaged =
+               xzalloc(sizeof(struct xwayland_unmanaged));
        unmanaged->server = server;
        unmanaged->xwayland_surface = xsurface;
 
index 1b51cd37b964bfd258d999bdf337362a7ef53177..5b071e60bb0dbad092745ed1ae96d66482c36118 100644 (file)
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <assert.h>
+#include "common/mem.h"
 #include "labwc.h"
 #include "node.h"
 #include "ssd.h"
@@ -483,7 +484,7 @@ xwayland_surface_new(struct wl_listener *listener, void *data)
                return;
        }
 
-       struct view *view = calloc(1, sizeof(struct view));
+       struct view *view = xzalloc(sizeof(struct view));
        view->server = server;
        view->type = LAB_XWAYLAND_VIEW;
        view->impl = &xwl_view_impl;