diff --git a/include/common/mem.h b/include/common/mem.h new file mode 100644 index 00000000..b0e91217 --- /dev/null +++ b/include/common/mem.h @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LABWC_MEM_H +#define __LABWC_MEM_H + +#include + +/* + * 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 ; calls exit() on error. + * Requires (str != NULL) and never returns NULL. + */ +char *xstrdup(const char *str); + +/* + * Frees memory pointed to by and sets to NULL. + * Does nothing if 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 index 30479fb4..00000000 --- a/include/common/zfree.h +++ /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 */ diff --git a/src/action.c b/src/action.c index 95871e43..233348fa 100644 --- a/src/action.c +++ b/src/action.c @@ -6,8 +6,8 @@ #include #include #include +#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); } diff --git a/src/buffer.c b/src/buffer.c index 1c64276a..e9b02b95 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -24,12 +24,12 @@ * SOFTWARE. */ -#include "config.h" #include #include #include #include #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; diff --git a/src/common/buf.c b/src/common/buf.c index f2e3b095..740489dd 100644 --- a/src/common/buf.c +++ b/src/common/buf.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only #include #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; diff --git a/src/common/graphic-helpers.c b/src/common/graphic-helpers.c index eb147893..f7ae2577 100644 --- a/src/common/graphic-helpers.c +++ b/src/common/graphic-helpers.c @@ -5,6 +5,7 @@ #include #include #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 index 00000000..e63987b5 --- /dev/null +++ b/src/common/mem.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0-only +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#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; +} diff --git a/src/common/meson.build b/src/common/meson.build index 72a6d215..e4f507b6 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -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', ) diff --git a/src/common/scaled_font_buffer.c b/src/common/scaled_font_buffer.c index 76f8ad75..87ac0cd0 100644 --- a/src/common/scaled_font_buffer.c +++ b/src/common/scaled_font_buffer.c @@ -7,9 +7,9 @@ #include #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); diff --git a/src/common/scaled_scene_buffer.c b/src/common/scaled_scene_buffer.c index 95e2673d..cc543ca0 100644 --- a/src/common/scaled_scene_buffer.c +++ b/src/common/scaled_scene_buffer.c @@ -7,6 +7,7 @@ #include #include #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 index 3eb399a1..00000000 --- a/src/common/zfree.c +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -#include -#include "common/zfree.h" - -void __zfree(void **ptr) -{ - if (!ptr || !*ptr) { - return; - } - free(*ptr); - *ptr = NULL; -} diff --git a/src/config/keybind.c b/src/config/keybind.c index f90bea08..10be0875 100644 --- a/src/config/keybind.c +++ b/src/config/keybind.c @@ -5,6 +5,7 @@ #include #include #include +#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; diff --git a/src/config/libinput.c b/src/config/libinput.c index 9efa3437..581987cf 100644 --- a/src/config/libinput.c +++ b/src/config/libinput.c @@ -2,8 +2,10 @@ #include #include +#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; diff --git a/src/config/mousebind.c b/src/config/mousebind.c index 075d89aa..45029c93 100644 --- a/src/config/mousebind.c +++ b/src/config/mousebind.c @@ -5,6 +5,7 @@ #include #include #include +#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); diff --git a/src/config/rcxml.c b/src/config/rcxml.c index 886f29b5..f9f166ff 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -14,9 +14,9 @@ #include #include #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) { diff --git a/src/config/session.c b/src/config/session.c index f861e785..1524edd0 100644 --- a/src/config/session.c +++ b/src/config/session.c @@ -8,6 +8,7 @@ #include #include #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); diff --git a/src/cursor.c b/src/cursor.c index 9753654a..ff9dc0b1 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -6,12 +6,13 @@ #include #include #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); } diff --git a/src/layers.c b/src/layers.c index 674deaa9..f03f2eba 100644 --- a/src/layers.c +++ b/src/layers.c @@ -14,6 +14,7 @@ #include #include #include +#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, diff --git a/src/main.c b/src/main.c index 0cb521b5..8ca6fe09 100644 --- a/src/main.c +++ b/src/main.c @@ -2,14 +2,14 @@ #define _POSIX_C_SOURCE 200809L #include #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; diff --git a/src/menu/menu.c b/src/menu/menu.c index bb72cd1b..c839c113 100644 --- a/src/menu/menu.c +++ b/src/menu/menu.c @@ -12,15 +12,14 @@ #include #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; diff --git a/src/node.c b/src/node.c index 67c01a15..53a87bca 100644 --- a/src/node.c +++ b/src/node.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only #include #include +#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; diff --git a/src/output.c b/src/output.c index d441e734..f350009e 100644 --- a/src/output.c +++ b/src/output.c @@ -7,7 +7,6 @@ */ #define _POSIX_C_SOURCE 200809L -#include "config.h" #include #include #include @@ -16,13 +15,10 @@ #include #include #include -#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; diff --git a/src/seat.c b/src/seat.c index e19c663f..70292259 100644 --- a/src/seat.c +++ b/src/seat.c @@ -8,6 +8,7 @@ #include #include #include +#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; diff --git a/src/ssd/ssd_extents.c b/src/ssd/ssd_extents.c index 31e0b337..28a09162 100644 --- a/src/ssd/ssd_extents.c +++ b/src/ssd/ssd_extents.c @@ -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; } diff --git a/src/ssd/ssd_part.c b/src/ssd/ssd_part.c index f08af948..9a4db0ce 100644 --- a/src/ssd/ssd_part.c +++ b/src/ssd/ssd_part.c @@ -1,10 +1,10 @@ // SPDX-License-Identifier: GPL-2.0-only #include +#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; diff --git a/src/ssd/ssd_titlebar.c b/src/ssd/ssd_titlebar.c index b2cfe0ef..945c2844 100644 --- a/src/ssd/ssd_titlebar.c +++ b/src/ssd/ssd_titlebar.c @@ -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); } diff --git a/src/theme.c b/src/theme.c index 45af59f8..29d4fe18 100644 --- a/src/theme.c +++ b/src/theme.c @@ -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" diff --git a/src/workspaces.c b/src/workspaces.c index b6a9f664..38cf81da 100644 --- a/src/workspaces.c +++ b/src/workspaces.c @@ -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) { diff --git a/src/xbm/parse.c b/src/xbm/parse.c index fdb5b07f..1eb722cc 100644 --- a/src/xbm/parse.c +++ b/src/xbm/parse.c @@ -13,6 +13,7 @@ #include #include +#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; diff --git a/src/xbm/tokenize.c b/src/xbm/tokenize.c index ff6bc73b..218d0bd2 100644 --- a/src/xbm/tokenize.c +++ b/src/xbm/tokenize.c @@ -9,6 +9,7 @@ #include #include +#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)); diff --git a/src/xdg-deco.c b/src/xdg-deco.c index f0cdd873..0d4eece2 100644 --- a/src/xdg-deco.c +++ b/src/xdg-deco.c @@ -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; diff --git a/src/xdg-popup.c b/src/xdg-popup.c index 36873b6c..3edff404 100644 --- a/src/xdg-popup.c +++ b/src/xdg-popup.c @@ -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; diff --git a/src/xdg.c b/src/xdg.c index 2456f7f8..835cdb09 100644 --- a/src/xdg.c +++ b/src/xdg.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only #include +#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; diff --git a/src/xwayland-unmanaged.c b/src/xwayland-unmanaged.c index 44608485..b5fb58e4 100644 --- a/src/xwayland-unmanaged.c +++ b/src/xwayland-unmanaged.c @@ -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; diff --git a/src/xwayland.c b/src/xwayland.c index 1b51cd37..5b071e60 100644 --- a/src/xwayland.c +++ b/src/xwayland.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only #include +#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;