Use struct initializers instead of memset()

This is a bit more type-safe.
This commit is contained in:
Simon Ser 2023-07-07 14:34:56 +02:00
parent 4966857f21
commit 7a9f8d8d6b
34 changed files with 134 additions and 113 deletions

View file

@ -197,8 +197,8 @@ Functions that are responsible for constructing objects should take one of the
two following forms:
* `init`: for functions which accept a pointer to a pre-allocated object (e.g.
a member of a struct) and initialize it. Such functions must call `memset()`
to zero out the memory before initializing it to avoid leaving unset fields.
a member of a struct) and initialize it. Such functions must zero out the
memory before initializing it to avoid leaving unset fields.
* `create`: for functions which allocate the memory for an object, initialize
it, and return a pointer. Such functions should allocate the memory with
`calloc()` to avoid leaving unset fields.

View file

@ -40,8 +40,9 @@
void wlr_backend_init(struct wlr_backend *backend,
const struct wlr_backend_impl *impl) {
memset(backend, 0, sizeof(*backend));
backend->impl = impl;
*backend = (struct wlr_backend){
.impl = impl,
};
wl_signal_init(&backend->events.destroy);
wl_signal_init(&backend->events.new_input);
wl_signal_init(&backend->events.new_output);

View file

@ -50,7 +50,7 @@ struct atomic {
};
static void atomic_begin(struct atomic *atom) {
memset(atom, 0, sizeof(*atom));
*atom = (struct atomic){0};
atom->req = drmModeAtomicAlloc();
if (!atom->req) {

View file

@ -445,11 +445,12 @@ static bool drm_crtc_commit(struct wlr_drm_connector *conn,
static void drm_connector_state_init(struct wlr_drm_connector_state *state,
struct wlr_drm_connector *conn,
const struct wlr_output_state *base) {
memset(state, 0, sizeof(*state));
state->base = base;
state->modeset = base->allow_artifacts;
state->active = (base->committed & WLR_OUTPUT_STATE_ENABLED) ?
base->enabled : conn->output.enabled;
*state = (struct wlr_drm_connector_state){
.base = base,
.modeset = base->allow_artifacts,
.active = (base->committed & WLR_OUTPUT_STATE_ENABLED) ?
base->enabled : conn->output.enabled,
};
if (base->committed & WLR_OUTPUT_STATE_MODE) {
switch (base->mode_type) {
@ -1028,7 +1029,7 @@ static void drm_connector_destroy_output(struct wlr_output *output) {
free(mode);
}
memset(&conn->output, 0, sizeof(struct wlr_output));
conn->output = (struct wlr_output){0};
}
static const struct wlr_drm_format_set *drm_connector_get_cursor_formats(

View file

@ -55,7 +55,7 @@ static void finish_drm_surface(struct wlr_drm_surface *surf) {
wlr_swapchain_destroy(surf->swapchain);
memset(surf, 0, sizeof(*surf));
*surf = (struct wlr_drm_surface){0};
}
bool init_drm_surface(struct wlr_drm_surface *surf,

View file

@ -22,9 +22,10 @@
void wlr_allocator_init(struct wlr_allocator *alloc,
const struct wlr_allocator_interface *impl, uint32_t buffer_caps) {
assert(impl && impl->destroy && impl->create_buffer);
memset(alloc, 0, sizeof(*alloc));
alloc->impl = impl;
alloc->buffer_caps = buffer_caps;
*alloc = (struct wlr_allocator){
.impl = impl,
.buffer_caps = buffer_caps,
};
wl_signal_init(&alloc->events.destroy);
}

View file

@ -86,8 +86,9 @@ bool wlr_drm_format_set_add(struct wlr_drm_format_set *set, uint32_t format,
}
void wlr_drm_format_init(struct wlr_drm_format *fmt, uint32_t format) {
memset(fmt, 0, sizeof(*fmt));
fmt->format = format;
*fmt = (struct wlr_drm_format){
.format = format,
};
}
bool wlr_drm_format_has(const struct wlr_drm_format *fmt, uint64_t modifier) {

View file

@ -373,8 +373,9 @@ struct wlr_texture *gles2_texture_from_buffer(struct wlr_renderer *wlr_renderer,
void wlr_gles2_texture_get_attribs(struct wlr_texture *wlr_texture,
struct wlr_gles2_texture_attribs *attribs) {
struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture);
memset(attribs, 0, sizeof(*attribs));
attribs->target = texture->target;
attribs->tex = texture->tex;
attribs->has_alpha = texture->has_alpha;
*attribs = (struct wlr_gles2_texture_attribs){
.target = texture->target,
.tex = texture->tex,
.has_alpha = texture->has_alpha,
};
}

View file

@ -15,8 +15,9 @@ struct wlr_render_pass_legacy {
void wlr_render_pass_init(struct wlr_render_pass *render_pass,
const struct wlr_render_pass_impl *impl) {
assert(impl->submit && impl->add_texture && impl->add_rect);
memset(render_pass, 0, sizeof(*render_pass));
render_pass->impl = impl;
*render_pass = (struct wlr_render_pass){
.impl = impl,
};
}
bool wlr_render_pass_submit(struct wlr_render_pass *render_pass) {

View file

@ -42,7 +42,7 @@ static void slot_reset(struct wlr_swapchain_slot *slot) {
wl_list_remove(&slot->release.link);
}
wlr_buffer_drop(slot->buffer);
memset(slot, 0, sizeof(*slot));
*slot = (struct wlr_swapchain_slot){0};
}
void wlr_swapchain_destroy(struct wlr_swapchain *swapchain) {

View file

@ -44,8 +44,9 @@ void wlr_renderer_init(struct wlr_renderer *renderer,
assert(impl->get_shm_texture_formats);
assert(impl->get_render_buffer_caps);
memset(renderer, 0, sizeof(*renderer));
renderer->impl = impl;
*renderer = (struct wlr_renderer){
.impl = impl,
};
wl_signal_init(&renderer->events.destroy);
wl_signal_init(&renderer->events.lost);

View file

@ -10,11 +10,12 @@ void wlr_texture_init(struct wlr_texture *texture, struct wlr_renderer *renderer
const struct wlr_texture_impl *impl, uint32_t width, uint32_t height) {
assert(renderer);
memset(texture, 0, sizeof(*texture));
texture->renderer = renderer;
texture->impl = impl;
texture->width = width;
texture->height = height;
*texture = (struct wlr_texture){
.renderer = renderer,
.impl = impl,
.width = width,
.height = height,
};
}
void wlr_texture_destroy(struct wlr_texture *texture) {

View file

@ -11,10 +11,11 @@ void wlr_buffer_init(struct wlr_buffer *buffer,
assert(impl->begin_data_ptr_access && impl->end_data_ptr_access);
}
memset(buffer, 0, sizeof(*buffer));
buffer->impl = impl;
buffer->width = width;
buffer->height = height;
*buffer = (struct wlr_buffer){
.impl = impl,
.width = width,
.height = height,
};
wl_signal_init(&buffer->events.destroy);
wl_signal_init(&buffer->events.release);
wlr_addon_set_init(&buffer->addons);

View file

@ -57,7 +57,7 @@ bool dmabuf_buffer_drop(struct wlr_dmabuf_buffer *buffer) {
if (!wlr_dmabuf_attributes_copy(&saved_dmabuf, &buffer->dmabuf)) {
wlr_log(WLR_ERROR, "Failed to save DMA-BUF");
ok = false;
memset(&buffer->dmabuf, 0, sizeof(buffer->dmabuf));
buffer->dmabuf = (struct wlr_dmabuf_attributes){0};
} else {
buffer->dmabuf = saved_dmabuf;
buffer->saved = true;

View file

@ -14,11 +14,12 @@ void wlr_data_source_init(struct wlr_data_source *source,
const struct wlr_data_source_impl *impl) {
assert(impl->send);
memset(source, 0, sizeof(*source));
source->impl = impl;
*source = (struct wlr_data_source){
.impl = impl,
.actions = -1,
};
wl_array_init(&source->mime_types);
wl_signal_init(&source->events.destroy);
source->actions = -1;
}
void wlr_data_source_send(struct wlr_data_source *source, const char *mime_type,

View file

@ -332,15 +332,17 @@ void wlr_output_init(struct wlr_output *output, struct wlr_backend *backend,
assert(impl->set_cursor && impl->move_cursor);
}
memset(output, 0, sizeof(*output));
output->backend = backend;
output->impl = impl;
output->display = display;
*output = (struct wlr_output){
.backend = backend,
.impl = impl,
.display = display,
.render_format = DRM_FORMAT_XRGB8888,
.transform = WL_OUTPUT_TRANSFORM_NORMAL,
.scale = 1,
.commit_seq = 0,
};
wl_list_init(&output->modes);
output->render_format = DRM_FORMAT_XRGB8888;
output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
output->scale = 1;
output->commit_seq = 0;
wl_list_init(&output->cursors);
wl_list_init(&output->layers);
wl_list_init(&output->resources);

View file

@ -57,10 +57,11 @@ struct wlr_scene *scene_node_get_root(struct wlr_scene_node *node) {
static void scene_node_init(struct wlr_scene_node *node,
enum wlr_scene_node_type type, struct wlr_scene_tree *parent) {
memset(node, 0, sizeof(*node));
node->type = type;
node->parent = parent;
node->enabled = true;
*node = (struct wlr_scene_node){
.type = type,
.parent = parent,
.enabled = true,
};
wl_list_init(&node->link);
@ -141,7 +142,7 @@ void wlr_scene_node_destroy(struct wlr_scene_node *node) {
static void scene_tree_init(struct wlr_scene_tree *tree,
struct wlr_scene_tree *parent) {
memset(tree, 0, sizeof(*tree));
*tree = (struct wlr_scene_tree){0};
scene_node_init(&tree->node, WLR_SCENE_NODE_TREE, parent);
wl_list_init(&tree->children);
}
@ -386,8 +387,8 @@ static void update_node_update_outputs(struct wlr_scene_node *node,
}
if (old_primary_output != scene_buffer->primary_output) {
memset(&scene_buffer->prev_feedback_options, 0,
sizeof(scene_buffer->prev_feedback_options));
scene_buffer->prev_feedback_options =
(struct wlr_linux_dmabuf_feedback_v1_init_options){0};
}
uint64_t old_active = scene_buffer->active_outputs;

View file

@ -588,10 +588,10 @@ struct wlr_surface *wlr_surface_from_resource(struct wl_resource *resource) {
}
static void surface_state_init(struct wlr_surface_state *state) {
memset(state, 0, sizeof(*state));
state->scale = 1;
state->transform = WL_OUTPUT_TRANSFORM_NORMAL;
*state = (struct wlr_surface_state){
.scale = 1,
.transform = WL_OUTPUT_TRANSFORM_NORMAL,
};
wl_list_init(&state->subsurfaces_above);
wl_list_init(&state->subsurfaces_below);

View file

@ -302,7 +302,7 @@ static void get_mapping(struct wlr_cursor *cur,
struct wlr_input_device *dev, struct wlr_box *box) {
assert(cur->state->layout);
memset(box, 0, sizeof(*box));
*box = (struct wlr_box){0};
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
if (c_device) {
@ -1146,7 +1146,7 @@ void wlr_cursor_map_input_to_output(struct wlr_cursor *cur,
void wlr_cursor_map_to_region(struct wlr_cursor *cur,
const struct wlr_box *box) {
memset(&cur->state->mapped_box, 0, sizeof(cur->state->mapped_box));
cur->state->mapped_box = (struct wlr_box){0};
if (box) {
if (wlr_box_empty(box)) {
@ -1159,7 +1159,7 @@ void wlr_cursor_map_to_region(struct wlr_cursor *cur,
void wlr_cursor_map_input_to_region(struct wlr_cursor *cur,
struct wlr_input_device *dev, const struct wlr_box *box) {
memset(&cur->state->mapped_box, 0, sizeof(cur->state->mapped_box));
cur->state->mapped_box = (struct wlr_box){0};
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
if (!c_device) {

View file

@ -8,10 +8,10 @@
#define WLR_DAMAGE_RING_MAX_RECTS 20
void wlr_damage_ring_init(struct wlr_damage_ring *ring) {
memset(ring, 0, sizeof(*ring));
ring->width = INT_MAX;
ring->height = INT_MAX;
*ring = (struct wlr_damage_ring){
.width = INT_MAX,
.height = INT_MAX,
};
pixman_region32_init(&ring->current);
for (size_t i = 0; i < WLR_DAMAGE_RING_PREVIOUS_LEN; ++i) {

View file

@ -6,11 +6,10 @@
void wlr_input_device_init(struct wlr_input_device *dev,
enum wlr_input_device_type type, const char *name) {
memset(dev, 0, sizeof(*dev));
dev->type = type;
dev->name = strdup(name);
dev->vendor = 0;
dev->product = 0;
*dev = (struct wlr_input_device){
.type = type,
.name = strdup(name),
};
wl_signal_init(&dev->events.destroy);
}

View file

@ -121,20 +121,20 @@ void wlr_keyboard_notify_key(struct wlr_keyboard *keyboard,
void wlr_keyboard_init(struct wlr_keyboard *kb,
const struct wlr_keyboard_impl *impl, const char *name) {
memset(kb, 0, sizeof(*kb));
*kb = (struct wlr_keyboard){
.impl = impl,
.keymap_fd = -1,
// Sane defaults
.repeat_info.rate = 25,
.repeat_info.delay = 600,
};
wlr_input_device_init(&kb->base, WLR_INPUT_DEVICE_KEYBOARD, name);
kb->impl = impl;
wl_signal_init(&kb->events.key);
wl_signal_init(&kb->events.modifiers);
wl_signal_init(&kb->events.keymap);
wl_signal_init(&kb->events.repeat_info);
kb->keymap_fd = -1;
// Sane defaults
kb->repeat_info.rate = 25;
kb->repeat_info.delay = 600;
}
static void keyboard_unset_keymap(struct wlr_keyboard *kb) {

View file

@ -1025,7 +1025,7 @@ struct wlr_linux_dmabuf_feedback_v1_tranche *wlr_linux_dmabuf_feedback_add_tranc
wlr_log_errno(WLR_ERROR, "Allocation failed");
return NULL;
}
memset(tranche, 0, sizeof(*tranche));
*tranche = (struct wlr_linux_dmabuf_feedback_v1_tranche){0};
return tranche;
}
@ -1053,7 +1053,7 @@ bool wlr_linux_dmabuf_feedback_v1_init_with_options(struct wlr_linux_dmabuf_feed
assert(options->scanout_primary_output == NULL ||
options->output_layer_feedback_event == NULL);
memset(feedback, 0, sizeof(*feedback));
*feedback = (struct wlr_linux_dmabuf_feedback_v1){0};
int renderer_drm_fd = wlr_renderer_get_drm_fd(options->main_renderer);
if (renderer_drm_fd < 0) {

View file

@ -339,7 +339,7 @@ void wlr_output_layout_closest_point(struct wlr_output_layout *layout,
void wlr_output_layout_get_box(struct wlr_output_layout *layout,
struct wlr_output *reference, struct wlr_box *dest_box) {
memset(dest_box, 0, sizeof(*dest_box));
*dest_box = (struct wlr_box){0};
struct wlr_output_layout_output *l_output;
if (reference) {

View file

@ -15,10 +15,11 @@ struct wlr_pointer *wlr_pointer_from_input_device(
void wlr_pointer_init(struct wlr_pointer *pointer,
const struct wlr_pointer_impl *impl, const char *name) {
memset(pointer, 0, sizeof(*pointer));
*pointer = (struct wlr_pointer){
.impl = impl,
};
wlr_input_device_init(&pointer->base, WLR_INPUT_DEVICE_POINTER, name);
pointer->impl = impl;
wl_signal_init(&pointer->events.motion);
wl_signal_init(&pointer->events.motion_absolute);
wl_signal_init(&pointer->events.button);

View file

@ -249,13 +249,14 @@ void wlr_presentation_feedback_destroy(
void wlr_presentation_event_from_output(struct wlr_presentation_event *event,
const struct wlr_output_event_present *output_event) {
memset(event, 0, sizeof(*event));
event->output = output_event->output;
event->tv_sec = (uint64_t)output_event->when->tv_sec;
event->tv_nsec = (uint32_t)output_event->when->tv_nsec;
event->refresh = (uint32_t)output_event->refresh;
event->seq = (uint64_t)output_event->seq;
event->flags = output_event->flags;
*event = (struct wlr_presentation_event){
.output = output_event->output,
.tv_sec = (uint64_t)output_event->when->tv_sec,
.tv_nsec = (uint32_t)output_event->when->tv_nsec,
.refresh = (uint32_t)output_event->refresh,
.seq = (uint64_t)output_event->seq,
.flags = output_event->flags,
};
}
static void feedback_unset_output(struct wlr_presentation_feedback *feedback) {

View file

@ -7,10 +7,11 @@ void wlr_primary_selection_source_init(
struct wlr_primary_selection_source *source,
const struct wlr_primary_selection_source_impl *impl) {
assert(impl->send);
memset(source, 0, sizeof(*source));
*source = (struct wlr_primary_selection_source){
.impl = impl,
};
wl_array_init(&source->mime_types);
wl_signal_init(&source->events.destroy);
source->impl = impl;
}
void wlr_primary_selection_source_destroy(

View file

@ -15,10 +15,11 @@ struct wlr_switch *wlr_switch_from_input_device(
void wlr_switch_init(struct wlr_switch *switch_device,
const struct wlr_switch_impl *impl, const char *name) {
memset(switch_device, 0, sizeof(*switch_device));
*switch_device = (struct wlr_switch){
.impl = impl,
};
wlr_input_device_init(&switch_device->base, WLR_INPUT_DEVICE_SWITCH, name);
switch_device->impl = impl;
wl_signal_init(&switch_device->events.toggle);
}

View file

@ -16,10 +16,11 @@ struct wlr_tablet_pad *wlr_tablet_pad_from_input_device(
void wlr_tablet_pad_init(struct wlr_tablet_pad *pad,
const struct wlr_tablet_pad_impl *impl, const char *name) {
memset(pad, 0, sizeof(*pad));
*pad = (struct wlr_tablet_pad){
.impl = impl,
};
wlr_input_device_init(&pad->base, WLR_INPUT_DEVICE_TABLET_PAD, name);
pad->impl = impl;
wl_signal_init(&pad->events.button);
wl_signal_init(&pad->events.ring);
wl_signal_init(&pad->events.strip);

View file

@ -15,10 +15,11 @@ struct wlr_tablet *wlr_tablet_from_input_device(
void wlr_tablet_init(struct wlr_tablet *tablet,
const struct wlr_tablet_impl *impl, const char *name) {
memset(tablet, 0, sizeof(*tablet));
*tablet = (struct wlr_tablet){
.impl = impl,
};
wlr_input_device_init(&tablet->base, WLR_INPUT_DEVICE_TABLET_TOOL, name);
tablet->impl = impl;
wl_signal_init(&tablet->events.axis);
wl_signal_init(&tablet->events.proximity);
wl_signal_init(&tablet->events.tip);

View file

@ -15,10 +15,11 @@ struct wlr_touch *wlr_touch_from_input_device(
void wlr_touch_init(struct wlr_touch *touch,
const struct wlr_touch_impl *impl, const char *name) {
memset(touch, 0, sizeof(*touch));
*touch = (struct wlr_touch){
.impl = impl,
};
wlr_input_device_init(&touch->base, WLR_INPUT_DEVICE_TOUCH, name);
touch->impl = impl;
wl_signal_init(&touch->events.down);
wl_signal_init(&touch->events.up);
wl_signal_init(&touch->events.motion);

View file

@ -113,7 +113,7 @@ static void virtual_pointer_frame(struct wl_client *client,
/* Deliver pending axis event */
wl_signal_emit_mutable(&pointer->pointer.events.axis,
&pointer->axis_event[i]);
memset(&pointer->axis_event[i], 0, sizeof(pointer->axis_event[i]));
pointer->axis_event[i] = (struct wlr_pointer_axis_event){0};
pointer->axis_valid[i] = false;
}
}

View file

@ -6,7 +6,7 @@
#include <wlr/util/log.h>
void wlr_addon_set_init(struct wlr_addon_set *set) {
memset(set, 0, sizeof(*set));
*set = (struct wlr_addon_set){0};
wl_list_init(&set->addons);
}
@ -26,7 +26,10 @@ void wlr_addon_set_finish(struct wlr_addon_set *set) {
void wlr_addon_init(struct wlr_addon *addon, struct wlr_addon_set *set,
const void *owner, const struct wlr_addon_interface *impl) {
assert(impl);
memset(addon, 0, sizeof(*addon));
*addon = (struct wlr_addon){
.impl = impl,
.owner = owner,
};
struct wlr_addon *iter;
wl_list_for_each(iter, &set->addons, link) {
if (iter->owner == addon->owner && iter->impl == addon->impl) {
@ -34,8 +37,6 @@ void wlr_addon_init(struct wlr_addon *addon, struct wlr_addon_set *set,
}
}
wl_list_insert(&set->addons, &addon->link);
addon->owner = owner;
addon->impl = impl;
}
void wlr_addon_finish(struct wlr_addon *addon) {

View file

@ -34,9 +34,10 @@ void xwm_selection_transfer_destroy_property_reply(
void xwm_selection_transfer_init(struct wlr_xwm_selection_transfer *transfer,
struct wlr_xwm_selection *selection) {
memset(transfer, 0, sizeof(*transfer));
transfer->selection = selection;
transfer->wl_client_fd = -1;
*transfer = (struct wlr_xwm_selection_transfer){
.selection = selection,
.wl_client_fd = -1,
};
}
void xwm_selection_transfer_destroy(
@ -175,14 +176,14 @@ int xwm_handle_selection_event(struct wlr_xwm *xwm,
void xwm_selection_init(struct wlr_xwm_selection *selection,
struct wlr_xwm *xwm, xcb_atom_t atom) {
memset(selection, 0, sizeof(*selection));
*selection = (struct wlr_xwm_selection){
.xwm = xwm,
.atom = atom,
.window = xcb_generate_id(xwm->xcb_conn),
};
wl_list_init(&selection->incoming);
wl_list_init(&selection->outgoing);
selection->xwm = xwm;
selection->atom = atom;
selection->window = xcb_generate_id(xwm->xcb_conn);
if (atom == xwm->atoms[DND_SELECTION]) {
xcb_create_window(
xwm->xcb_conn,