common: Add znew/znew_n() macros

This commit is contained in:
John Lindgren 2022-09-18 15:22:26 -04:00
parent da57483961
commit a54d378e6c
26 changed files with 47 additions and 47 deletions

View file

@ -11,6 +11,17 @@
*/ */
void *xzalloc(size_t size); 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. * As defined in FreeBSD.
* Like realloc(), but calls exit() on error. * Like realloc(), but calls exit() on error.

View file

@ -105,7 +105,7 @@ action_create(const char *action_name)
wlr_log(WLR_ERROR, "action name not specified"); wlr_log(WLR_ERROR, "action name not specified");
return NULL; return NULL;
} }
struct action *action = xzalloc(sizeof(struct action)); struct action *action = znew(*action);
action->type = action_type_from_str(action_name); action->type = action_type_from_str(action_name);
wl_list_init(&action->args); wl_list_init(&action->args);
return action; return action;
@ -372,7 +372,7 @@ void
action_arg_add_str(struct action *action, char *key, const char *value) action_arg_add_str(struct action *action, char *key, const char *value)
{ {
assert(value && "Tried to add NULL action string argument"); 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; arg->base.type = LAB_ACTION_ARG_STR;
if (key) { if (key) {
arg->base.key = xstrdup(key); arg->base.key = xstrdup(key);

View file

@ -88,7 +88,7 @@ struct lab_data_buffer *
buffer_create_cairo(uint32_t width, uint32_t height, float scale, buffer_create_cairo(uint32_t width, uint32_t height, float scale,
bool free_on_destroy) 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_width = width;
buffer->unscaled_height = height; buffer->unscaled_height = height;
width *= scale; width *= scale;
@ -127,7 +127,7 @@ struct lab_data_buffer *
buffer_create_wrap(void *pixel_data, uint32_t width, uint32_t height, buffer_create_wrap(void *pixel_data, uint32_t width, uint32_t height,
uint32_t stride, bool free_on_destroy) 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); wlr_buffer_init(&buffer->base, &data_buffer_impl, width, height);
buffer->data = pixel_data; buffer->data = pixel_data;
buffer->format = DRM_FORMAT_ARGB8888; buffer->format = DRM_FORMAT_ARGB8888;

View file

@ -17,7 +17,7 @@ multi_rect_destroy_notify(struct wl_listener *listener, void *data)
struct multi_rect * struct multi_rect *
multi_rect_create(struct wlr_scene_tree *parent, float *colors[3], int line_width) 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->line_width = line_width;
rect->tree = wlr_scene_tree_create(parent); rect->tree = wlr_scene_tree_create(parent);
rect->destroy.notify = multi_rect_destroy_notify; rect->destroy.notify = multi_rect_destroy_notify;

View file

@ -46,8 +46,7 @@ struct scaled_font_buffer *
scaled_font_buffer_create(struct wlr_scene_tree *parent) scaled_font_buffer_create(struct wlr_scene_tree *parent)
{ {
assert(parent); assert(parent);
struct scaled_font_buffer *self = xzalloc(sizeof(*self)); struct scaled_font_buffer *self = znew(*self);
struct scaled_scene_buffer *scaled_buffer struct scaled_scene_buffer *scaled_buffer
= scaled_scene_buffer_create(parent, &impl); = scaled_scene_buffer_create(parent, &impl);
if (!scaled_buffer) { if (!scaled_buffer) {

View file

@ -68,7 +68,7 @@ _update_buffer(struct scaled_scene_buffer *self, double scale)
/* Create or reuse cache entry */ /* Create or reuse cache entry */
if (wl_list_length(&self->cache) < LAB_SCALED_BUFFER_MAX_CACHE) { if (wl_list_length(&self->cache) < LAB_SCALED_BUFFER_MAX_CACHE) {
cache_entry = xzalloc(sizeof(*cache_entry)); cache_entry = znew(*cache_entry);
} else { } else {
cache_entry = wl_container_of(self->cache.prev, cache_entry, link); cache_entry = wl_container_of(self->cache.prev, cache_entry, link);
if (cache_entry->buffer) { if (cache_entry->buffer) {
@ -151,8 +151,7 @@ scaled_scene_buffer_create(struct wlr_scene_tree *parent,
assert(impl); assert(impl);
assert(impl->create_buffer); 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); self->scene_buffer = wlr_scene_buffer_create(parent, NULL);
if (!self->scene_buffer) { if (!self->scene_buffer) {
wlr_log(WLR_ERROR, "Failed to create scene buffer"); wlr_log(WLR_ERROR, "Failed to create scene buffer");

View file

@ -28,7 +28,7 @@ parse_modifier(const char *symname)
struct keybind * struct keybind *
keybind_create(const char *keybind) keybind_create(const char *keybind)
{ {
struct keybind *k = xzalloc(sizeof(struct keybind)); struct keybind *k = znew(*k);
xkb_keysym_t keysyms[MAX_KEYSYMS]; xkb_keysym_t keysyms[MAX_KEYSYMS];
gchar **symnames = g_strsplit(keybind, "-", -1); gchar **symnames = g_strsplit(keybind, "-", -1);
for (int i = 0; symnames[i]; i++) { for (int i = 0; symnames[i]; i++) {

View file

@ -38,7 +38,7 @@ get_device_type(const char *s)
struct libinput_category * struct libinput_category *
libinput_category_create(void) libinput_category_create(void)
{ {
struct libinput_category *l = xzalloc(sizeof(struct libinput_category)); struct libinput_category *l = znew(*l);
libinput_category_init(l); libinput_category_init(l);
wl_list_insert(&rc.libinput_categories, &l->link); wl_list_insert(&rc.libinput_categories, &l->link);
return l; return l;

View file

@ -109,7 +109,7 @@ mousebind_create(const char *context)
wlr_log(WLR_ERROR, "mousebind context not specified"); wlr_log(WLR_ERROR, "mousebind context not specified");
return NULL; return NULL;
} }
struct mousebind *m = xzalloc(sizeof(struct mousebind)); struct mousebind *m = znew(*m);
m->context = context_from_str(context); m->context = context_from_str(context);
if (m->context != LAB_SSD_NONE) { if (m->context != LAB_SSD_NONE) {
wl_list_insert(rc.mousebinds.prev, &m->link); wl_list_insert(rc.mousebinds.prev, &m->link);

View file

@ -401,7 +401,7 @@ entry(xmlNode *node, char *nodename, char *content)
} else if (!strcasecmp(nodename, "cycleViewOutlines.core")) { } else if (!strcasecmp(nodename, "cycleViewOutlines.core")) {
rc.cycle_preview_outlines = get_bool(content); rc.cycle_preview_outlines = get_bool(content);
} else if (!strcasecmp(nodename, "name.names.desktops")) { } else if (!strcasecmp(nodename, "name.names.desktops")) {
struct workspace *workspace = xzalloc(sizeof(struct workspace)); struct workspace *workspace = znew(*workspace);
workspace->name = xstrdup(content); workspace->name = xstrdup(content);
wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link); wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link);
} else if (!strcasecmp(nodename, "popupTime.desktops")) { } else if (!strcasecmp(nodename, "popupTime.desktops")) {
@ -685,7 +685,7 @@ post_processing(void)
l->type = DEFAULT_DEVICE; l->type = DEFAULT_DEVICE;
} }
if (!wl_list_length(&rc.workspace_config.workspaces)) { if (!wl_list_length(&rc.workspace_config.workspaces)) {
struct workspace *workspace = xzalloc(sizeof(struct workspace)); struct workspace *workspace = znew(*workspace);
workspace->name = xstrdup("Default"); workspace->name = xstrdup("Default");
wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link); wl_list_insert(rc.workspace_config.workspaces.prev, &workspace->link);
} }

View file

@ -79,7 +79,7 @@ build_path(const char *dir, const char *filename)
return NULL; return NULL;
} }
int len = strlen(dir) + strlen(filename) + 2; int len = strlen(dir) + strlen(filename) + 2;
char *buffer = xzalloc(len); char *buffer = znew_n(char, len);
strcat(buffer, dir); strcat(buffer, dir);
strcat(buffer, "/"); strcat(buffer, "/");
strcat(buffer, filename); strcat(buffer, filename);
@ -101,13 +101,13 @@ update_activation_env(const char *env_keys)
const char *dbus = "dbus-update-activation-environment "; const char *dbus = "dbus-update-activation-environment ";
const char *systemd = "systemctl --user import-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, dbus);
strcat(cmd, env_keys); strcat(cmd, env_keys);
spawn_async_no_shell(cmd); spawn_async_no_shell(cmd);
free(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, systemd);
strcat(cmd, env_keys); strcat(cmd, env_keys);
spawn_async_no_shell(cmd); spawn_async_no_shell(cmd);
@ -148,7 +148,7 @@ session_autostart_init(const char *dir)
} }
wlr_log(WLR_INFO, "run autostart file %s", autostart); wlr_log(WLR_INFO, "run autostart file %s", autostart);
int len = strlen(autostart) + 4; int len = strlen(autostart) + 4;
char *cmd = xzalloc(len); char *cmd = znew_n(char, len);
strcat(cmd, "sh "); strcat(cmd, "sh ");
strcat(cmd, autostart); strcat(cmd, autostart);
spawn_async_no_shell(cmd); spawn_async_no_shell(cmd);

View file

@ -532,7 +532,7 @@ create_constraint(struct wl_listener *listener, void *data)
struct wlr_pointer_constraint_v1 *wlr_constraint = data; struct wlr_pointer_constraint_v1 *wlr_constraint = data;
struct server *server = wl_container_of(listener, server, struct server *server = wl_container_of(listener, server,
new_constraint); new_constraint);
struct constraint *constraint = xzalloc(sizeof(struct constraint)); struct constraint *constraint = znew(*constraint);
constraint->constraint = wlr_constraint; constraint->constraint = wlr_constraint;
constraint->seat = &server->seat; constraint->seat = &server->seat;

View file

@ -202,9 +202,7 @@ static struct lab_layer_popup *
create_popup(struct wlr_xdg_popup *wlr_popup, struct wlr_scene_tree *parent, create_popup(struct wlr_xdg_popup *wlr_popup, struct wlr_scene_tree *parent,
struct wlr_box *output_toplevel_sx_box) struct wlr_box *output_toplevel_sx_box)
{ {
struct lab_layer_popup *popup = struct lab_layer_popup *popup = znew(*popup);
xzalloc(sizeof(struct lab_layer_popup));
popup->wlr_popup = wlr_popup; popup->wlr_popup = wlr_popup;
popup->scene_tree = popup->scene_tree =
wlr_scene_xdg_surface_create(parent, wlr_popup->base); 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; layer_surface->output = output;
} }
struct lab_layer_surface *surface = struct lab_layer_surface *surface = znew(*surface);
xzalloc(sizeof(struct lab_layer_surface));
surface->surface_commit.notify = surface_commit_notify; surface->surface_commit.notify = surface_commit_notify;
wl_signal_add(&layer_surface->surface->events.commit, wl_signal_add(&layer_surface->surface->events.commit,

View file

@ -79,7 +79,7 @@ menu_get_by_id(const char *id)
static struct menuitem * static struct menuitem *
item_create(struct menu *menu, const char *text, bool show_arrow) 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->parent = menu;
menuitem->selectable = true; menuitem->selectable = true;
struct server *server = menu->server; struct server *server = menu->server;
@ -160,7 +160,7 @@ item_create(struct menu *menu, const char *text, bool show_arrow)
static struct menuitem * static struct menuitem *
separator_create(struct menu *menu, const char *label) separator_create(struct menu *menu, const char *label)
{ {
struct menuitem *menuitem = xzalloc(sizeof(struct menuitem)); struct menuitem *menuitem = znew(*menuitem);
menuitem->parent = menu; menuitem->parent = menu;
menuitem->selectable = false; menuitem->selectable = false;
struct server *server = menu->server; struct server *server = menu->server;

View file

@ -26,8 +26,7 @@ void
node_descriptor_create(struct wlr_scene_node *scene_node, node_descriptor_create(struct wlr_scene_node *scene_node,
enum node_descriptor_type type, void *data) enum node_descriptor_type type, void *data)
{ {
struct node_descriptor *node_descriptor = struct node_descriptor *node_descriptor = znew(*node_descriptor);
xzalloc(sizeof(struct node_descriptor));
node_descriptor->type = type; node_descriptor->type = type;
node_descriptor->data = data; node_descriptor->data = data;
node_descriptor->destroy.notify = destroy_notify; node_descriptor->destroy.notify = destroy_notify;

View file

@ -125,7 +125,7 @@ new_output_notify(struct wl_listener *listener, void *data)
wlr_output_commit(wlr_output); wlr_output_commit(wlr_output);
struct output *output = xzalloc(sizeof(struct output)); struct output *output = znew(*output);
output->wlr_output = wlr_output; output->wlr_output = wlr_output;
wlr_output->data = output; wlr_output->data = output;
output->server = server; output->server = server;

View 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 seat *seat = wl_container_of(listener, seat, new_input);
struct wlr_input_device *device = data; struct wlr_input_device *device = data;
struct input *input = xzalloc(sizeof(struct input)); struct input *input = znew(*input);
input->wlr_input_device = device; input->wlr_input_device = device;
input->seat = seat; input->seat = seat;
@ -263,9 +263,7 @@ new_idle_inhibitor(struct wl_listener *listener, void *data)
struct seat *seat = wl_container_of(listener, seat, struct seat *seat = wl_container_of(listener, seat,
idle_inhibitor_create); idle_inhibitor_create);
struct idle_inhibitor *inhibitor = struct idle_inhibitor *inhibitor = znew(*inhibitor);
xzalloc(sizeof(struct idle_inhibitor));
inhibitor->seat = seat; inhibitor->seat = seat;
inhibitor->wlr_inhibitor = wlr_inhibitor; inhibitor->wlr_inhibitor = wlr_inhibitor;
inhibitor->destroy.notify = destroy_idle_inhibitor; inhibitor->destroy.notify = destroy_idle_inhibitor;

View 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->geometry will get free'd automatically in ssd_destroy_parts().
*/ */
part->node = &wlr_scene_rect_create(parent, 0, 0, invisible)->node; 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; return part;
} }

View file

@ -24,7 +24,7 @@ static struct ssd_button *
ssd_button_descriptor_create(struct wlr_scene_node *node) ssd_button_descriptor_create(struct wlr_scene_node *node)
{ {
/* Create new ssd_button */ /* 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 */ /* Let it destroy automatically when the scene node destroys */
button->destroy.notify = ssd_button_destroy_notify; button->destroy.notify = ssd_button_destroy_notify;
@ -39,7 +39,7 @@ ssd_button_descriptor_create(struct wlr_scene_node *node)
struct ssd_part * struct ssd_part *
add_scene_part(struct wl_list *part_list, enum ssd_part_type type) 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; part->type = type;
wl_list_insert(part_list->prev, &part->link); wl_list_insert(part_list->prev, &part->link);
return part; return part;

View file

@ -154,7 +154,7 @@ _osd_update(struct server *server)
static void static void
add_workspace(struct server *server, const char *name) add_workspace(struct server *server, const char *name)
{ {
struct workspace *workspace = xzalloc(sizeof(struct workspace)); struct workspace *workspace = znew(*workspace);
workspace->server = server; workspace->server = server;
workspace->name = xstrdup(name); workspace->name = xstrdup(name);
workspace->tree = wlr_scene_tree_create(server->view_tree); workspace->tree = wlr_scene_tree_create(server->view_tree);

View file

@ -38,8 +38,7 @@ parse_set_color(float *rgba)
static void static void
process_bytes(struct pixmap *pixmap, struct token *tokens) process_bytes(struct pixmap *pixmap, struct token *tokens)
{ {
pixmap->data = (uint32_t *)xzalloc( pixmap->data = znew_n(uint32_t, pixmap->width * pixmap->height);
pixmap->width * pixmap->height * sizeof(uint32_t));
struct token *t = tokens; struct token *t = tokens;
for (int row = 0; row < pixmap->height; row++) { for (int row = 0; row < pixmap->height; row++) {
int byte = 1; int byte = 1;

View file

@ -44,7 +44,7 @@ xdg_toplevel_decoration(struct wl_listener *listener, void *data)
struct server *server = struct server *server =
wl_container_of(listener, server, xdg_toplevel_decoration); wl_container_of(listener, server, xdg_toplevel_decoration);
struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration = data; 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->wlr_decoration = wlr_decoration;
xdg_deco->server = server; xdg_deco->server = server;
xdg_deco->view = wlr_decoration->surface->data; xdg_deco->view = wlr_decoration->surface->data;

View file

@ -68,8 +68,7 @@ xdg_popup_create(struct view *view, struct wlr_xdg_popup *wlr_popup)
return; return;
} }
struct xdg_popup *popup = xzalloc(sizeof(struct xdg_popup)); struct xdg_popup *popup = znew(*popup);
popup->parent_view = view; popup->parent_view = view;
popup->wlr_popup = wlr_popup; popup->wlr_popup = wlr_popup;

View file

@ -375,7 +375,7 @@ xdg_surface_new(struct wl_listener *listener, void *data)
wlr_xdg_surface_ping(xdg_surface); wlr_xdg_surface_ping(xdg_surface);
struct view *view = xzalloc(sizeof(struct view)); struct view *view = znew(*view);
view->server = server; view->server = server;
view->type = LAB_XDG_SHELL_VIEW; view->type = LAB_XDG_SHELL_VIEW;
view->impl = &xdg_toplevel_view_impl; view->impl = &xdg_toplevel_view_impl;

View file

@ -140,8 +140,7 @@ struct xwayland_unmanaged *
xwayland_unmanaged_create(struct server *server, xwayland_unmanaged_create(struct server *server,
struct wlr_xwayland_surface *xsurface) struct wlr_xwayland_surface *xsurface)
{ {
struct xwayland_unmanaged *unmanaged = struct xwayland_unmanaged *unmanaged = znew(*unmanaged);
xzalloc(sizeof(struct xwayland_unmanaged));
unmanaged->server = server; unmanaged->server = server;
unmanaged->xwayland_surface = xsurface; unmanaged->xwayland_surface = xsurface;

View file

@ -484,7 +484,7 @@ xwayland_surface_new(struct wl_listener *listener, void *data)
return; return;
} }
struct view *view = xzalloc(sizeof(struct view)); struct view *view = znew(*view);
view->server = server; view->server = server;
view->type = LAB_XWAYLAND_VIEW; view->type = LAB_XWAYLAND_VIEW;
view->impl = &xwl_view_impl; view->impl = &xwl_view_impl;