diff --git a/include/common/mem.h b/include/common/mem.h index b0e91217..577970d1 100644 --- a/include/common/mem.h +++ b/include/common/mem.h @@ -11,6 +11,17 @@ */ void *xzalloc(size_t size); +/* + * Type-safe macros in the style of C++ new/new[]. + * 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. diff --git a/src/action.c b/src/action.c index 233348fa..2a4aaeb2 100644 --- a/src/action.c +++ b/src/action.c @@ -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); diff --git a/src/buffer.c b/src/buffer.c index e9b02b95..e96c4d88 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -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; diff --git a/src/common/graphic-helpers.c b/src/common/graphic-helpers.c index f7ae2577..65cd4d7b 100644 --- a/src/common/graphic-helpers.c +++ b/src/common/graphic-helpers.c @@ -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; diff --git a/src/common/scaled_font_buffer.c b/src/common/scaled_font_buffer.c index 87ac0cd0..61c98ff2 100644 --- a/src/common/scaled_font_buffer.c +++ b/src/common/scaled_font_buffer.c @@ -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) { diff --git a/src/common/scaled_scene_buffer.c b/src/common/scaled_scene_buffer.c index cc543ca0..85638e6f 100644 --- a/src/common/scaled_scene_buffer.c +++ b/src/common/scaled_scene_buffer.c @@ -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"); diff --git a/src/config/keybind.c b/src/config/keybind.c index 10be0875..08b5e9ba 100644 --- a/src/config/keybind.c +++ b/src/config/keybind.c @@ -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++) { diff --git a/src/config/libinput.c b/src/config/libinput.c index 581987cf..685c6720 100644 --- a/src/config/libinput.c +++ b/src/config/libinput.c @@ -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; diff --git a/src/config/mousebind.c b/src/config/mousebind.c index 45029c93..1b1d5c74 100644 --- a/src/config/mousebind.c +++ b/src/config/mousebind.c @@ -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); diff --git a/src/config/rcxml.c b/src/config/rcxml.c index f9f166ff..b844fb07 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -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); } diff --git a/src/config/session.c b/src/config/session.c index 1524edd0..71855c9e 100644 --- a/src/config/session.c +++ b/src/config/session.c @@ -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); diff --git a/src/cursor.c b/src/cursor.c index ff9dc0b1..7eac1e9b 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -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; diff --git a/src/layers.c b/src/layers.c index f03f2eba..94a2d7ed 100644 --- a/src/layers.c +++ b/src/layers.c @@ -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, diff --git a/src/menu/menu.c b/src/menu/menu.c index c839c113..9c990927 100644 --- a/src/menu/menu.c +++ b/src/menu/menu.c @@ -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; diff --git a/src/node.c b/src/node.c index 53a87bca..84ca3f6c 100644 --- a/src/node.c +++ b/src/node.c @@ -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; diff --git a/src/output.c b/src/output.c index f350009e..1ec1c232 100644 --- a/src/output.c +++ b/src/output.c @@ -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; diff --git a/src/seat.c b/src/seat.c index 70292259..ddc723ed 100644 --- a/src/seat.c +++ b/src/seat.c @@ -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; diff --git a/src/ssd/ssd_extents.c b/src/ssd/ssd_extents.c index 28a09162..a6005638 100644 --- a/src/ssd/ssd_extents.c +++ b/src/ssd/ssd_extents.c @@ -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; } diff --git a/src/ssd/ssd_part.c b/src/ssd/ssd_part.c index 9a4db0ce..561c02df 100644 --- a/src/ssd/ssd_part.c +++ b/src/ssd/ssd_part.c @@ -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; diff --git a/src/workspaces.c b/src/workspaces.c index 38cf81da..87ccc428 100644 --- a/src/workspaces.c +++ b/src/workspaces.c @@ -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); diff --git a/src/xbm/parse.c b/src/xbm/parse.c index 1eb722cc..bb457058 100644 --- a/src/xbm/parse.c +++ b/src/xbm/parse.c @@ -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; diff --git a/src/xdg-deco.c b/src/xdg-deco.c index 0d4eece2..9f4efcb1 100644 --- a/src/xdg-deco.c +++ b/src/xdg-deco.c @@ -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; diff --git a/src/xdg-popup.c b/src/xdg-popup.c index 3edff404..35e49316 100644 --- a/src/xdg-popup.c +++ b/src/xdg-popup.c @@ -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; diff --git a/src/xdg.c b/src/xdg.c index 835cdb09..a055ca9b 100644 --- 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; diff --git a/src/xwayland-unmanaged.c b/src/xwayland-unmanaged.c index b5fb58e4..decc3d65 100644 --- a/src/xwayland-unmanaged.c +++ b/src/xwayland-unmanaged.c @@ -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; diff --git a/src/xwayland.c b/src/xwayland.c index 5b071e60..baa26477 100644 --- a/src/xwayland.c +++ b/src/xwayland.c @@ -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;