When zeroing memory, use type independent code
Reduces the potential for bugs
This commit is contained in:
parent
d9f90ef077
commit
49429b5f49
@ -31,6 +31,9 @@
|
|||||||
#define xstr(s) str(s)
|
#define xstr(s) str(s)
|
||||||
#define str(s) #s
|
#define str(s) #s
|
||||||
#define arraysz(x) (sizeof(x)/sizeof(x[0]))
|
#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)));
|
void log_error(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
|
||||||
#define fatal(...) { log_error(__VA_ARGS__); exit(EXIT_FAILURE); }
|
#define fatal(...) { log_error(__VA_ARGS__); exit(EXIT_FAILURE); }
|
||||||
|
|
||||||
|
|||||||
@ -163,7 +163,7 @@ font_group_for(double font_sz_in_pts, double logical_dpi_x, double logical_dpi_y
|
|||||||
}
|
}
|
||||||
add_font_group();
|
add_font_group();
|
||||||
FontGroup *fg = font_groups + num_font_groups - 1;
|
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->font_sz_in_pts = font_sz_in_pts;
|
||||||
fg->logical_dpi_x = logical_dpi_x;
|
fg->logical_dpi_x = logical_dpi_x;
|
||||||
fg->logical_dpi_y = logical_dpi_y;
|
fg->logical_dpi_y = logical_dpi_y;
|
||||||
@ -301,7 +301,7 @@ free_maps(Font *font) {
|
|||||||
|
|
||||||
void
|
void
|
||||||
clear_sprite_map(Font *font) {
|
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;
|
SpritePosition *s;
|
||||||
for (size_t i = 0; i < sizeof(font->sprite_map)/sizeof(font->sprite_map[0]); i++) {
|
for (size_t i = 0; i < sizeof(font->sprite_map)/sizeof(font->sprite_map[0]); i++) {
|
||||||
s = font->sprite_map + 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.num_codepoints = num_codepoints_in_cell(first_cpu_cell);
|
||||||
group_state.current_cell_data.codepoints_consumed = 0;
|
group_state.current_cell_data.codepoints_consumed = 0;
|
||||||
group_state.current_cell_data.current_codepoint = first_cpu_cell->ch;
|
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.group_idx = 0;
|
||||||
group_state.glyph_idx = 0;
|
group_state.glyph_idx = 0;
|
||||||
group_state.cell_idx = 0;
|
group_state.cell_idx = 0;
|
||||||
|
|||||||
@ -46,6 +46,6 @@ right_shift_canvas(pixel *canvas, size_t width, size_t height, size_t amt) {
|
|||||||
size_t r;
|
size_t r;
|
||||||
for (r = 0, src = canvas; r < height; r++, src += width) {
|
for (r = 0, src = canvas; r < height; r++, src += width) {
|
||||||
memmove(src + amt, src, sizeof(pixel) * (width - amt));
|
memmove(src + amt, src, sizeof(pixel) * (width - amt));
|
||||||
memset(src, 0, sizeof(pixel) * amt);
|
zero_at_ptr_count(src, amt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -261,7 +261,7 @@ find_or_create_image(GraphicsManager *self, uint32_t id, bool *existing) {
|
|||||||
*existing = false;
|
*existing = false;
|
||||||
ensure_space_for(self, images, Image, self->image_count + 1, images_capacity, 64, true);
|
ensure_space_for(self, images, Image, self->image_count + 1, images_capacity, 64, true);
|
||||||
Image *ans = self->images + self->image_count++;
|
Image *ans = self->images + self->image_count++;
|
||||||
memset(ans, 0, sizeof(ans[0]));
|
zero_at_ptr(ans);
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -494,7 +494,7 @@ handle_put_command(GraphicsManager *self, const GraphicsCommand *g, Cursor *c, b
|
|||||||
}
|
}
|
||||||
if (ref == NULL) {
|
if (ref == NULL) {
|
||||||
ref = img->refs + img->refcnt++;
|
ref = img->refs + img->refcnt++;
|
||||||
memset(ref, 0, sizeof(ref[0]));
|
zero_at_ptr(ref);
|
||||||
}
|
}
|
||||||
img->atime = monotonic();
|
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;
|
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++;
|
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);
|
ensure_space_for(self, render_data, ImageRenderData, self->count + 1, capacity, 64, true);
|
||||||
ImageRenderData *rd = self->render_data + self->count;
|
ImageRenderData *rd = self->render_data + self->count;
|
||||||
memset(rd, 0, sizeof(rd[0]));
|
zero_at_ptr(rd);
|
||||||
set_vertex_data(rd, ref, &r);
|
set_vertex_data(rd, ref, &r);
|
||||||
self->count++;
|
self->count++;
|
||||||
rd->z_index = ref->z_index; rd->image_id = img->internal_id;
|
rd->z_index = ref->z_index; rd->image_id = img->internal_id;
|
||||||
|
|||||||
@ -29,9 +29,9 @@ clear_chars_to(LineBuf* linebuf, index_type y, char_type ch) {
|
|||||||
|
|
||||||
void
|
void
|
||||||
linebuf_clear(LineBuf *self, char_type ch) {
|
linebuf_clear(LineBuf *self, char_type ch) {
|
||||||
memset(self->cpu_cell_buf, 0, self->xnum * self->ynum * sizeof(CPUCell));
|
zero_at_ptr_count(self->cpu_cell_buf, self->xnum * self->ynum);
|
||||||
memset(self->gpu_cell_buf, 0, self->xnum * self->ynum * sizeof(GPUCell));
|
zero_at_ptr_count(self->gpu_cell_buf, self->xnum * self->ynum);
|
||||||
memset(self->line_attrs, 0, self->ynum * sizeof(line_attrs_type));
|
zero_at_ptr_count(self->line_attrs, self->ynum);
|
||||||
for (index_type i = 0; i < self->ynum; i++) self->line_map[i] = i;
|
for (index_type i = 0; i < self->ynum; i++) self->line_map[i] = i;
|
||||||
if (ch != 0) {
|
if (ch != 0) {
|
||||||
for (index_type i = 0; i < self->ynum; i++) {
|
for (index_type i = 0; i < self->ynum; i++) {
|
||||||
@ -243,8 +243,8 @@ copy_line_to(LineBuf *self, PyObject *args) {
|
|||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
clear_line_(Line *l, index_type xnum) {
|
clear_line_(Line *l, index_type xnum) {
|
||||||
memset(l->cpu_cells, 0, xnum * sizeof(CPUCell));
|
zero_at_ptr_count(l->cpu_cells, xnum);
|
||||||
memset(l->gpu_cells, 0, xnum * sizeof(GPUCell));
|
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);
|
if (BLANK_CHAR != 0) clear_chars_in_line(l->cpu_cells, l->gpu_cells, xnum, BLANK_CHAR);
|
||||||
l->has_dirty_text = false;
|
l->has_dirty_text = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,7 @@ static inline void parse_graphics_code(Screen *screen,
|
|||||||
unsigned int i, code;
|
unsigned int i, code;
|
||||||
uint64_t lcode;
|
uint64_t lcode;
|
||||||
bool is_negative;
|
bool is_negative;
|
||||||
memset(&g, 0, sizeof(g));
|
zero_at_ptr(&g);
|
||||||
size_t sz;
|
size_t sz;
|
||||||
static uint8_t payload[4096];
|
static uint8_t payload[4096];
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@ GlobalState global_state = {{0}};
|
|||||||
for (size_t i = 0; i < count; i++) { \
|
for (size_t i = 0; i < count; i++) { \
|
||||||
if (array[i].id == qid) { \
|
if (array[i].id == qid) { \
|
||||||
destroy(array + i); \
|
destroy(array + i); \
|
||||||
memset(array + i, 0, sizeof(array[0])); \
|
zero_at_i(array, i); \
|
||||||
remove_i_from_array(array, i, count); \
|
remove_i_from_array(array, i, count); \
|
||||||
break; \
|
break; \
|
||||||
} \
|
} \
|
||||||
@ -76,7 +76,7 @@ add_os_window() {
|
|||||||
WITH_OS_WINDOW_REFS
|
WITH_OS_WINDOW_REFS
|
||||||
ensure_space_for(&global_state, os_windows, OSWindow, global_state.num_os_windows + 1, capacity, 1, true);
|
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++;
|
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->id = ++global_state.os_window_id_counter;
|
||||||
ans->tab_bar_render_data.vao_idx = create_cell_vao();
|
ans->tab_bar_render_data.vao_idx = create_cell_vao();
|
||||||
ans->gvao_idx = create_graphics_vao();
|
ans->gvao_idx = create_graphics_vao();
|
||||||
@ -91,7 +91,7 @@ add_tab(id_type os_window_id) {
|
|||||||
WITH_OS_WINDOW(os_window_id)
|
WITH_OS_WINDOW(os_window_id)
|
||||||
make_os_window_context_current(os_window);
|
make_os_window_context_current(os_window);
|
||||||
ensure_space_for(os_window, tabs, Tab, os_window->num_tabs + 1, capacity, 1, true);
|
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].id = ++global_state.tab_id_counter;
|
||||||
os_window->tabs[os_window->num_tabs].border_rects.vao_idx = create_border_vao();
|
os_window->tabs[os_window->num_tabs].border_rects.vao_idx = create_border_vao();
|
||||||
return os_window->tabs[os_window->num_tabs++].id;
|
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);
|
WITH_TAB(os_window_id, tab_id);
|
||||||
ensure_space_for(tab, windows, Window, tab->num_windows + 1, capacity, 1, true);
|
ensure_space_for(tab, windows, Window, tab->num_windows + 1, capacity, 1, true);
|
||||||
make_os_window_context_current(osw);
|
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].id = ++global_state.window_id_counter;
|
||||||
tab->windows[tab->num_windows].visible = true;
|
tab->windows[tab->num_windows].visible = true;
|
||||||
tab->windows[tab->num_windows].title = title;
|
tab->windows[tab->num_windows].title = title;
|
||||||
@ -265,7 +265,7 @@ os_window_regions(OSWindow *os_window, Region *central, Region *tab_bar) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} 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->left = 0; central->top = 0; central->right = os_window->viewport_width - 1;
|
||||||
central->bottom = os_window->viewport_height - 1;
|
central->bottom = os_window->viewport_height - 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user