]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common: Add znew/znew_n() macros
authorJohn Lindgren <john@jlindgren.net>
Sun, 18 Sep 2022 19:22:26 +0000 (15:22 -0400)
committerJohn Lindgren <john@jlindgren.net>
Sun, 18 Sep 2022 19:25:19 +0000 (15:25 -0400)
26 files changed:
include/common/mem.h
src/action.c
src/buffer.c
src/common/graphic-helpers.c
src/common/scaled_font_buffer.c
src/common/scaled_scene_buffer.c
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/menu/menu.c
src/node.c
src/output.c
src/seat.c
src/ssd/ssd_extents.c
src/ssd/ssd_part.c
src/workspaces.c
src/xbm/parse.c
src/xdg-deco.c
src/xdg-popup.c
src/xdg.c
src/xwayland-unmanaged.c
src/xwayland.c

index b0e912170c0ad45a3f24a5f23e913b61f68a5379..577970d1b1a9bd3ac28a3685710471d3d40f4494 100644 (file)
  */
 void *xzalloc(size_t size);
 
+/*
+ * Type-safe macros in the style of C++ new/new[].
+ * <expr> may be either a type name or value expression.
+ *
+ * Examples:
+ *   struct wlr_box *box = znew(*box);
+ *   char *buf = znew_n(char, 80);
+ */
+#define znew(expr)       ((__typeof__(expr) *)xzalloc(sizeof(expr)))
+#define znew_n(expr, n)  ((__typeof__(expr) *)xzalloc((n) * sizeof(expr)))
+
 /*
  * As defined in FreeBSD.
  * Like realloc(), but calls exit() on error.
index 233348fa994199739aca852d98cb0426af59903c..2a4aaeb2601f697b63bb4caa980a5d4bf0b81714 100644 (file)
@@ -105,7 +105,7 @@ action_create(const char *action_name)
                wlr_log(WLR_ERROR, "action name not specified");
                return NULL;
        }
-       struct action *action = xzalloc(sizeof(struct action));
+       struct action *action = znew(*action);
        action->type = action_type_from_str(action_name);
        wl_list_init(&action->args);
        return action;
@@ -372,7 +372,7 @@ 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 = xzalloc(sizeof(*arg));
+       struct action_arg_str *arg = znew(*arg);
        arg->base.type = LAB_ACTION_ARG_STR;
        if (key) {
                arg->base.key = xstrdup(key);
index e9b02b95551b7569d33df9190c74775ac6e28107..e96c4d883a7beba53ce7a8a13c8f928efb387867 100644 (file)
@@ -88,7 +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 = xzalloc(sizeof(*buffer));
+       struct lab_data_buffer *buffer = znew(*buffer);
        buffer->unscaled_width = width;
        buffer->unscaled_height = height;
        width *= scale;
@@ -127,7 +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 = xzalloc(sizeof(*buffer));
+       struct lab_data_buffer *buffer = znew(*buffer);
        wlr_buffer_init(&buffer->base, &data_buffer_impl, width, height);
        buffer->data = pixel_data;
        buffer->format = DRM_FORMAT_ARGB8888;
index f7ae2577524aac092477a6e7fb0c3b918c5075c7..65cd4d7bbec8835dbbffe4a51d75f87b89adf6f4 100644 (file)
@@ -17,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 = xzalloc(sizeof(*rect));
+       struct multi_rect *rect = znew(*rect);
        rect->line_width = line_width;
        rect->tree = wlr_scene_tree_create(parent);
        rect->destroy.notify = multi_rect_destroy_notify;
index 87ac0cd04541d4f9ce26b8735550275a88bdfcf9..61c98ff267344182ae97b25f9db1d694b0d551ac 100644 (file)
@@ -46,8 +46,7 @@ struct scaled_font_buffer *
 scaled_font_buffer_create(struct wlr_scene_tree *parent)
 {
        assert(parent);
-       struct scaled_font_buffer *self = xzalloc(sizeof(*self));
-
+       struct scaled_font_buffer *self = znew(*self);
        struct scaled_scene_buffer *scaled_buffer
                = scaled_scene_buffer_create(parent, &impl);
        if (!scaled_buffer) {
index cc543ca037a24ef223c082b270b856e50a58ff27..85638e6f551a8a5ffe3b20203e15b8199381d197 100644 (file)
@@ -68,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 = xzalloc(sizeof(*cache_entry));
+               cache_entry = znew(*cache_entry);
        } else {
                cache_entry = wl_container_of(self->cache.prev, cache_entry, link);
                if (cache_entry->buffer) {
@@ -151,8 +151,7 @@ scaled_scene_buffer_create(struct wlr_scene_tree *parent,
        assert(impl);
        assert(impl->create_buffer);
 
-       struct scaled_scene_buffer *self = xzalloc(sizeof(*self));
-
+       struct scaled_scene_buffer *self = znew(*self);
        self->scene_buffer = wlr_scene_buffer_create(parent, NULL);
        if (!self->scene_buffer) {
                wlr_log(WLR_ERROR, "Failed to create scene buffer");
index 10be087576b4beefd0cb678ee1c5defdda3d8e6a..08b5e9ba899775b2ff1d4455f51bc6fb645a6013 100644 (file)
@@ -28,7 +28,7 @@ parse_modifier(const char *symname)
 struct keybind *
 keybind_create(const char *keybind)
 {
-       struct keybind *k = xzalloc(sizeof(struct keybind));
+       struct keybind *k = znew(*k);
        xkb_keysym_t keysyms[MAX_KEYSYMS];
        gchar **symnames = g_strsplit(keybind, "-", -1);
        for (int i = 0; symnames[i]; i++) {
index 581987cf2e38f9e500f44e67069e004683617a8d..685c6720cb1917c7111b943bcb34117f0216a7fd 100644 (file)
@@ -38,7 +38,7 @@ get_device_type(const char *s)
 struct libinput_category *
 libinput_category_create(void)
 {
-       struct libinput_category *l = xzalloc(sizeof(struct libinput_category));
+       struct libinput_category *l = znew(*l);
        libinput_category_init(l);
        wl_list_insert(&rc.libinput_categories, &l->link);
        return l;
index 45029c93d02067c6933ad62f61a052cc3f8587be..1b1d5c74d1f1aeb12dd2a6b8d3ed6348d2f74237 100644 (file)
@@ -109,7 +109,7 @@ mousebind_create(const char *context)
                wlr_log(WLR_ERROR, "mousebind context not specified");
                return NULL;
        }
-       struct mousebind *m = xzalloc(sizeof(struct mousebind));
+       struct mousebind *m = znew(*m);
        m->context = context_from_str(context);
        if (m->context != LAB_SSD_NONE) {
                wl_list_insert(rc.mousebinds.prev, &m->link);
index f9f166ff8c862cfa13767e51a8b94c0ca4718ff8..b844fb07448f947d7665e38c4b57476b36606411 100644 (file)
@@ -401,7 +401,7 @@ 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 = xzalloc(sizeof(struct workspace));
+               struct workspace *workspace = znew(*workspace);
                workspace->name = xstrdup(content);
                wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link);
        } else if (!strcasecmp(nodename, "popupTime.desktops")) {
@@ -685,7 +685,7 @@ post_processing(void)
                l->type = DEFAULT_DEVICE;
        }
        if (!wl_list_length(&rc.workspace_config.workspaces)) {
-               struct workspace *workspace = xzalloc(sizeof(struct workspace));
+               struct workspace *workspace = znew(*workspace);
                workspace->name = xstrdup("Default");
                wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link);
        }
index 1524edd0cc056b15439af0da0c4a35c625b0b6f3..71855c9e7e76ab0eae63f395486f3a7a19fcbfc9 100644 (file)
@@ -79,7 +79,7 @@ build_path(const char *dir, const char *filename)
                return NULL;
        }
        int len = strlen(dir) + strlen(filename) + 2;
-       char *buffer = xzalloc(len);
+       char *buffer = znew_n(char, len);
        strcat(buffer, dir);
        strcat(buffer, "/");
        strcat(buffer, filename);
@@ -101,13 +101,13 @@ update_activation_env(const char *env_keys)
        const char *dbus = "dbus-update-activation-environment ";
        const char *systemd = "systemctl --user import-environment ";
 
-       cmd = xzalloc(strlen(dbus) + strlen(env_keys) + 1);
+       cmd = znew_n(char, strlen(dbus) + strlen(env_keys) + 1);
        strcat(cmd, dbus);
        strcat(cmd, env_keys);
        spawn_async_no_shell(cmd);
        free(cmd);
 
-       cmd = xzalloc(strlen(systemd) + strlen(env_keys) + 1);
+       cmd = znew_n(char, strlen(systemd) + strlen(env_keys) + 1);
        strcat(cmd, systemd);
        strcat(cmd, env_keys);
        spawn_async_no_shell(cmd);
@@ -148,7 +148,7 @@ session_autostart_init(const char *dir)
        }
        wlr_log(WLR_INFO, "run autostart file %s", autostart);
        int len = strlen(autostart) + 4;
-       char *cmd = xzalloc(len);
+       char *cmd = znew_n(char, len);
        strcat(cmd, "sh ");
        strcat(cmd, autostart);
        spawn_async_no_shell(cmd);
index ff9dc0b11167b27e89186a6c7166dd035731c382..7eac1e9b161e0d0739acbc91833f05ce9fdc771b 100644 (file)
@@ -532,7 +532,7 @@ 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 constraint *constraint = xzalloc(sizeof(struct constraint));
+       struct constraint *constraint = znew(*constraint);
 
        constraint->constraint = wlr_constraint;
        constraint->seat = &server->seat;
index f03f2eba2f3ed015e5b7781a16dd937269a71612..94a2d7ed7af6d447f4cb8dab90cb1c828a4fa675 100644 (file)
@@ -202,9 +202,7 @@ static struct lab_layer_popup *
 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 =
-               xzalloc(sizeof(struct lab_layer_popup));
-
+       struct lab_layer_popup *popup = znew(*popup);
        popup->wlr_popup = wlr_popup;
        popup->scene_tree =
                wlr_scene_xdg_surface_create(parent, wlr_popup->base);
@@ -322,8 +320,7 @@ new_layer_surface_notify(struct wl_listener *listener, void *data)
                layer_surface->output = output;
        }
 
-       struct lab_layer_surface *surface =
-               xzalloc(sizeof(struct lab_layer_surface));
+       struct lab_layer_surface *surface = znew(*surface);
 
        surface->surface_commit.notify = surface_commit_notify;
        wl_signal_add(&layer_surface->surface->events.commit,
index c839c1133334ac98f915dc1b8f33c578617d0196..9c990927b7cd4600752f19ed2b73e77c0f5be969 100644 (file)
@@ -79,7 +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 = xzalloc(sizeof(struct menuitem));
+       struct menuitem *menuitem = znew(*menuitem);
        menuitem->parent = menu;
        menuitem->selectable = true;
        struct server *server = menu->server;
@@ -160,7 +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 = xzalloc(sizeof(struct menuitem));
+       struct menuitem *menuitem = znew(*menuitem);
        menuitem->parent = menu;
        menuitem->selectable = false;
        struct server *server = menu->server;
index 53a87bcad7c99dda4023398bdff42d8f49ea45a0..84ca3f6c6320a3f68a42f62698226eec095780f8 100644 (file)
@@ -26,8 +26,7 @@ void
 node_descriptor_create(struct wlr_scene_node *scene_node,
                enum node_descriptor_type type, void *data)
 {
-       struct node_descriptor *node_descriptor =
-               xzalloc(sizeof(struct node_descriptor));
+       struct node_descriptor *node_descriptor = znew(*node_descriptor);
        node_descriptor->type = type;
        node_descriptor->data = data;
        node_descriptor->destroy.notify = destroy_notify;
index f350009e24408dd424a757258aee4067ce84be84..1ec1c232809fd925bfbeefb8444d0c66e9dfec42 100644 (file)
@@ -125,7 +125,7 @@ new_output_notify(struct wl_listener *listener, void *data)
 
        wlr_output_commit(wlr_output);
 
-       struct output *output = xzalloc(sizeof(struct output));
+       struct output *output = znew(*output);
        output->wlr_output = wlr_output;
        wlr_output->data = output;
        output->server = server;
index 70292259189a59f7a34a250ceeea0bb876b3848e..ddc723ed84a41bea3fd9ae782dfadad45ad19ba2 100644 (file)
@@ -202,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 = xzalloc(sizeof(struct input));
+       struct input *input = znew(*input);
        input->wlr_input_device = device;
        input->seat = seat;
 
@@ -263,9 +263,7 @@ new_idle_inhibitor(struct wl_listener *listener, void *data)
        struct seat *seat = wl_container_of(listener, seat,
                idle_inhibitor_create);
 
-       struct idle_inhibitor *inhibitor =
-               xzalloc(sizeof(struct idle_inhibitor));
-
+       struct idle_inhibitor *inhibitor = znew(*inhibitor);
        inhibitor->seat = seat;
        inhibitor->wlr_inhibitor = wlr_inhibitor;
        inhibitor->destroy.notify = destroy_idle_inhibitor;
index 28a091627bbb7c1b3fe0a23af99819aeff007b33..a60056384453c3c39daa6ccc38c8eceebb32189c 100644 (file)
@@ -19,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 = xzalloc(sizeof(struct wlr_box));
+       part->geometry = znew(struct wlr_box);
        return part;
 }
 
index 9a4db0ced932c5a1e78d30c164642a2044ce0a55..561c02dfea037b092ca491b5c4451e5be35a9844 100644 (file)
@@ -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 = xzalloc(sizeof(struct ssd_button));
+       struct ssd_button *button = znew(*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 = xzalloc(sizeof(struct ssd_part));
+       struct ssd_part *part = znew(*part);
        part->type = type;
        wl_list_insert(part_list->prev, &part->link);
        return part;
index 38cf81da61297703a06b624a102394b2c63eed9b..87ccc4286039e30dafe11b2efbd0665e05f80bbd 100644 (file)
@@ -154,7 +154,7 @@ _osd_update(struct server *server)
 static void
 add_workspace(struct server *server, const char *name)
 {
-       struct workspace *workspace = xzalloc(sizeof(struct workspace));
+       struct workspace *workspace = znew(*workspace);
        workspace->server = server;
        workspace->name = xstrdup(name);
        workspace->tree = wlr_scene_tree_create(server->view_tree);
index 1eb722cc36c3033a20cff0a52724031f9c7f5d20..bb45705826db87543d5fc2600573b874edab391e 100644 (file)
@@ -38,8 +38,7 @@ parse_set_color(float *rgba)
 static void
 process_bytes(struct pixmap *pixmap, struct token *tokens)
 {
-       pixmap->data = (uint32_t *)xzalloc(
-               pixmap->width * pixmap->height * sizeof(uint32_t));
+       pixmap->data = znew_n(uint32_t, pixmap->width * pixmap->height);
        struct token *t = tokens;
        for (int row = 0; row < pixmap->height; row++) {
                int byte = 1;
index 0d4eece2bc587d236cd44cd7f04535552124aa0e..9f4efcb1ffe3d8964f4be10a7201cf1e59880464 100644 (file)
@@ -44,7 +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 = xzalloc(sizeof(struct xdg_deco));
+       struct xdg_deco *xdg_deco = znew(*xdg_deco);
        xdg_deco->wlr_decoration = wlr_decoration;
        xdg_deco->server = server;
        xdg_deco->view = wlr_decoration->surface->data;
index 3edff40480399df3d07ec765e891c6f346977beb..35e4931637fc2606831a2f97b70200c76bb68eda 100644 (file)
@@ -68,8 +68,7 @@ xdg_popup_create(struct view *view, struct wlr_xdg_popup *wlr_popup)
                return;
        }
 
-       struct xdg_popup *popup = xzalloc(sizeof(struct xdg_popup));
-
+       struct xdg_popup *popup = znew(*popup);
        popup->parent_view = view;
        popup->wlr_popup = wlr_popup;
 
index 835cdb096615840bfc9a283e194e384ac01dcd51..a055ca9b9200823624f7b1f8d4f0d55d33cf8194 100644 (file)
--- a/src/xdg.c
+++ b/src/xdg.c
@@ -375,7 +375,7 @@ xdg_surface_new(struct wl_listener *listener, void *data)
 
        wlr_xdg_surface_ping(xdg_surface);
 
-       struct view *view = xzalloc(sizeof(struct view));
+       struct view *view = znew(*view);
        view->server = server;
        view->type = LAB_XDG_SHELL_VIEW;
        view->impl = &xdg_toplevel_view_impl;
index b5fb58e4c7f9b85b0d0cf6faaca31fb33a0f8ed5..decc3d65484f20b16acfa50e9074bb1e24bc24d1 100644 (file)
@@ -140,8 +140,7 @@ struct xwayland_unmanaged *
 xwayland_unmanaged_create(struct server *server,
                          struct wlr_xwayland_surface *xsurface)
 {
-       struct xwayland_unmanaged *unmanaged =
-               xzalloc(sizeof(struct xwayland_unmanaged));
+       struct xwayland_unmanaged *unmanaged = znew(*unmanaged);
        unmanaged->server = server;
        unmanaged->xwayland_surface = xsurface;
 
index 5b071e60bb0dbad092745ed1ae96d66482c36118..baa264776e03ab58956662eee6c073c70c6382f7 100644 (file)
@@ -484,7 +484,7 @@ xwayland_surface_new(struct wl_listener *listener, void *data)
                return;
        }
 
-       struct view *view = xzalloc(sizeof(struct view));
+       struct view *view = znew(*view);
        view->server = server;
        view->type = LAB_XWAYLAND_VIEW;
        view->impl = &xwl_view_impl;