*/
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.
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;
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);
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;
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;
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;
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) {
/* 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) {
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");
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++) {
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;
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);
} 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")) {
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);
}
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);
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);
}
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);
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;
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);
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,
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;
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;
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;
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;
{
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;
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;
* 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;
}
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;
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;
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);
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;
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;
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;
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;
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;
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;