diff --git a/kitty/data-types.h b/kitty/data-types.h index 150a0b47b..51dbb16ab 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -31,6 +31,9 @@ #define xstr(s) str(s) #define str(s) #s #define arraysz(x) (sizeof(x)/sizeof(x[0])) +#define zero_at_i(array, idx) memset((array) + (idx), 0, sizeof((array)[0])) +#define zero_at_ptr(p) memset((p), 0, sizeof((p)[0])) +#define zero_at_ptr_count(p, count) memset((p), 0, (count) * sizeof((p)[0])) void log_error(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); #define fatal(...) { log_error(__VA_ARGS__); exit(EXIT_FAILURE); } diff --git a/kitty/fonts.c b/kitty/fonts.c index 904c74a7e..f68754f92 100644 --- a/kitty/fonts.c +++ b/kitty/fonts.c @@ -163,7 +163,7 @@ font_group_for(double font_sz_in_pts, double logical_dpi_x, double logical_dpi_y } add_font_group(); FontGroup *fg = font_groups + num_font_groups - 1; - memset(fg, 0, sizeof(FontGroup)); + zero_at_ptr(fg); fg->font_sz_in_pts = font_sz_in_pts; fg->logical_dpi_x = logical_dpi_x; fg->logical_dpi_y = logical_dpi_y; @@ -301,7 +301,7 @@ free_maps(Font *font) { void clear_sprite_map(Font *font) { -#define CLEAR(s) s->filled = false; s->rendered = false; s->colored = false; s->glyph = 0; memset(&s->extra_glyphs, 0, sizeof(ExtraGlyphs)); s->x = 0; s->y = 0; s->z = 0; s->ligature_index = 0; +#define CLEAR(s) s->filled = false; s->rendered = false; s->colored = false; s->glyph = 0; zero_at_ptr(&s->extra_glyphs); s->x = 0; s->y = 0; s->z = 0; s->ligature_index = 0; SpritePosition *s; for (size_t i = 0; i < sizeof(font->sprite_map)/sizeof(font->sprite_map[0]); i++) { s = font->sprite_map + i; @@ -728,7 +728,7 @@ shape(CPUCell *first_cpu_cell, GPUCell *first_gpu_cell, index_type num_cells, hb group_state.current_cell_data.num_codepoints = num_codepoints_in_cell(first_cpu_cell); group_state.current_cell_data.codepoints_consumed = 0; group_state.current_cell_data.current_codepoint = first_cpu_cell->ch; - memset(group_state.groups, 0, sizeof(Group) * group_state.groups_capacity); + zero_at_ptr_count(group_state.groups, group_state.groups_capacity); group_state.group_idx = 0; group_state.glyph_idx = 0; group_state.cell_idx = 0; diff --git a/kitty/fonts.h b/kitty/fonts.h index 0791c633b..068dcc104 100644 --- a/kitty/fonts.h +++ b/kitty/fonts.h @@ -46,6 +46,6 @@ right_shift_canvas(pixel *canvas, size_t width, size_t height, size_t amt) { size_t r; for (r = 0, src = canvas; r < height; r++, src += width) { memmove(src + amt, src, sizeof(pixel) * (width - amt)); - memset(src, 0, sizeof(pixel) * amt); + zero_at_ptr_count(src, amt); } } diff --git a/kitty/graphics.c b/kitty/graphics.c index 49cb1d743..e70fdee82 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -261,7 +261,7 @@ find_or_create_image(GraphicsManager *self, uint32_t id, bool *existing) { *existing = false; ensure_space_for(self, images, Image, self->image_count + 1, images_capacity, 64, true); Image *ans = self->images + self->image_count++; - memset(ans, 0, sizeof(ans[0])); + zero_at_ptr(ans); return ans; } @@ -494,7 +494,7 @@ handle_put_command(GraphicsManager *self, const GraphicsCommand *g, Cursor *c, b } if (ref == NULL) { ref = img->refs + img->refcnt++; - memset(ref, 0, sizeof(ref[0])); + zero_at_ptr(ref); } img->atime = monotonic(); ref->src_x = g->x_offset; ref->src_y = g->y_offset; ref->src_width = g->width ? g->width : img->width; ref->src_height = g->height ? g->height : img->height; @@ -568,7 +568,7 @@ grman_update_layers(GraphicsManager *self, unsigned int scrolled_by, float scree if (ref->z_index < 0) self->num_of_negative_refs++; else self->num_of_positive_refs++; ensure_space_for(self, render_data, ImageRenderData, self->count + 1, capacity, 64, true); ImageRenderData *rd = self->render_data + self->count; - memset(rd, 0, sizeof(rd[0])); + zero_at_ptr(rd); set_vertex_data(rd, ref, &r); self->count++; rd->z_index = ref->z_index; rd->image_id = img->internal_id; diff --git a/kitty/line-buf.c b/kitty/line-buf.c index 87dff3ebe..0add07d87 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -29,9 +29,9 @@ clear_chars_to(LineBuf* linebuf, index_type y, char_type ch) { void linebuf_clear(LineBuf *self, char_type ch) { - memset(self->cpu_cell_buf, 0, self->xnum * self->ynum * sizeof(CPUCell)); - memset(self->gpu_cell_buf, 0, self->xnum * self->ynum * sizeof(GPUCell)); - memset(self->line_attrs, 0, self->ynum * sizeof(line_attrs_type)); + zero_at_ptr_count(self->cpu_cell_buf, self->xnum * self->ynum); + zero_at_ptr_count(self->gpu_cell_buf, self->xnum * self->ynum); + zero_at_ptr_count(self->line_attrs, self->ynum); for (index_type i = 0; i < self->ynum; i++) self->line_map[i] = i; if (ch != 0) { for (index_type i = 0; i < self->ynum; i++) { @@ -243,8 +243,8 @@ copy_line_to(LineBuf *self, PyObject *args) { static inline void clear_line_(Line *l, index_type xnum) { - memset(l->cpu_cells, 0, xnum * sizeof(CPUCell)); - memset(l->gpu_cells, 0, xnum * sizeof(GPUCell)); + zero_at_ptr_count(l->cpu_cells, xnum); + zero_at_ptr_count(l->gpu_cells, xnum); if (BLANK_CHAR != 0) clear_chars_in_line(l->cpu_cells, l->gpu_cells, xnum, BLANK_CHAR); l->has_dirty_text = false; } diff --git a/kitty/parse-graphics-command.h b/kitty/parse-graphics-command.h index 3139935e7..407580a70 100644 --- a/kitty/parse-graphics-command.h +++ b/kitty/parse-graphics-command.h @@ -11,7 +11,7 @@ static inline void parse_graphics_code(Screen *screen, unsigned int i, code; uint64_t lcode; bool is_negative; - memset(&g, 0, sizeof(g)); + zero_at_ptr(&g); size_t sz; static uint8_t payload[4096]; diff --git a/kitty/state.c b/kitty/state.c index f5f233d28..7ef7c6e3e 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -14,7 +14,7 @@ GlobalState global_state = {{0}}; for (size_t i = 0; i < count; i++) { \ if (array[i].id == qid) { \ destroy(array + i); \ - memset(array + i, 0, sizeof(array[0])); \ + zero_at_i(array, i); \ remove_i_from_array(array, i, count); \ break; \ } \ @@ -76,7 +76,7 @@ add_os_window() { WITH_OS_WINDOW_REFS ensure_space_for(&global_state, os_windows, OSWindow, global_state.num_os_windows + 1, capacity, 1, true); OSWindow *ans = global_state.os_windows + global_state.num_os_windows++; - memset(ans, 0, sizeof(OSWindow)); + zero_at_ptr(ans); ans->id = ++global_state.os_window_id_counter; ans->tab_bar_render_data.vao_idx = create_cell_vao(); ans->gvao_idx = create_graphics_vao(); @@ -91,7 +91,7 @@ add_tab(id_type os_window_id) { WITH_OS_WINDOW(os_window_id) make_os_window_context_current(os_window); ensure_space_for(os_window, tabs, Tab, os_window->num_tabs + 1, capacity, 1, true); - memset(os_window->tabs + os_window->num_tabs, 0, sizeof(Tab)); + zero_at_i(os_window->tabs, os_window->num_tabs); os_window->tabs[os_window->num_tabs].id = ++global_state.tab_id_counter; os_window->tabs[os_window->num_tabs].border_rects.vao_idx = create_border_vao(); return os_window->tabs[os_window->num_tabs++].id; @@ -104,7 +104,7 @@ add_window(id_type os_window_id, id_type tab_id, PyObject *title) { WITH_TAB(os_window_id, tab_id); ensure_space_for(tab, windows, Window, tab->num_windows + 1, capacity, 1, true); make_os_window_context_current(osw); - memset(tab->windows + tab->num_windows, 0, sizeof(Window)); + zero_at_i(tab->windows, tab->num_windows); tab->windows[tab->num_windows].id = ++global_state.window_id_counter; tab->windows[tab->num_windows].visible = true; tab->windows[tab->num_windows].title = title; @@ -265,7 +265,7 @@ os_window_regions(OSWindow *os_window, Region *central, Region *tab_bar) { break; } } else { - memset(tab_bar, 0, sizeof(Region)); + zero_at_ptr(tab_bar); central->left = 0; central->top = 0; central->right = os_window->viewport_width - 1; central->bottom = os_window->viewport_height - 1; }