diff --git a/bar/bar.c b/bar/bar.c index 065460b..f252ca5 100644 --- a/bar/bar.c +++ b/bar/bar.c @@ -367,7 +367,7 @@ destroy(struct bar *bar) struct bar * bar_new(const struct bar_config *config) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->monitor = config->monitor != NULL ? strdup(config->monitor) : NULL; priv->location = config->location; priv->height = config->height; @@ -379,15 +379,14 @@ bar_new(const struct bar_config *config) priv->border.width = config->border.width; priv->border.color = config->border.color; priv->left.mods = malloc(config->left.count * sizeof(priv->left.mods[0])); - priv->left.exps = malloc(config->left.count * sizeof(priv->left.exps[0])); + priv->left.exps = calloc(config->left.count, sizeof(priv->left.exps[0])); priv->center.mods = malloc(config->center.count * sizeof(priv->center.mods[0])); - priv->center.exps = malloc(config->center.count * sizeof(priv->center.exps[0])); + priv->center.exps = calloc(config->center.count, sizeof(priv->center.exps[0])); priv->right.mods = malloc(config->right.count * sizeof(priv->right.mods[0])); - priv->right.exps = malloc(config->right.count * sizeof(priv->right.exps[0])); + priv->right.exps = calloc(config->right.count, sizeof(priv->right.exps[0])); priv->left.count = config->left.count; priv->center.count = config->center.count; priv->right.count = config->right.count; - priv->cursor_name = NULL; #if defined(ENABLE_X11) && !defined(ENABLE_WAYLAND) priv->backend.data = bar_backend_xcb_new(); @@ -407,20 +406,14 @@ bar_new(const struct bar_config *config) #endif #endif - for (size_t i = 0; i < priv->left.count; i++) { + for (size_t i = 0; i < priv->left.count; i++) priv->left.mods[i] = config->left.mods[i]; - priv->left.exps[i] = NULL; - } - for (size_t i = 0; i < priv->center.count; i++) { + for (size_t i = 0; i < priv->center.count; i++) priv->center.mods[i] = config->center.mods[i]; - priv->center.exps[i] = NULL; - } - for (size_t i = 0; i < priv->right.count; i++) { + for (size_t i = 0; i < priv->right.count; i++) priv->right.mods[i] = config->right.mods[i]; - priv->right.exps[i] = NULL; - } - struct bar *bar = malloc(sizeof(*bar)); + struct bar *bar = calloc(1, sizeof(*bar)); bar->private = priv; bar->run = &run; bar->destroy = &destroy; diff --git a/decorations/background.c b/decorations/background.c index 992bdf2..ef7b12b 100644 --- a/decorations/background.c +++ b/decorations/background.c @@ -30,10 +30,10 @@ expose(const struct deco *deco, cairo_t *cr, int x, int y, int width, int height static struct deco * background_new(struct rgba color) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->color = color; - struct deco *deco = malloc(sizeof(*deco)); + struct deco *deco = calloc(1, sizeof(*deco)); deco->private = priv; deco->expose = &expose; deco->destroy = &destroy; diff --git a/decorations/stack.c b/decorations/stack.c index a5662b0..3ffaab5 100644 --- a/decorations/stack.c +++ b/decorations/stack.c @@ -34,14 +34,14 @@ expose(const struct deco *deco, cairo_t *cr, int x, int y, int width, int height static struct deco * stack_new(struct deco *decos[], size_t count) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->decos = malloc(count * sizeof(priv->decos[0])); priv->count = count; for (size_t i = 0; i < count; i++) priv->decos[i] = decos[i]; - struct deco *deco = malloc(sizeof(*deco)); + struct deco *deco = calloc(1, sizeof(*deco)); deco->private = priv; deco->expose = &expose; deco->destroy = &destroy; diff --git a/decorations/underline.c b/decorations/underline.c index 9c71efe..996d54a 100644 --- a/decorations/underline.c +++ b/decorations/underline.c @@ -31,11 +31,11 @@ expose(const struct deco *deco, cairo_t *cr, int x, int y, int width, int height static struct deco * underline_new(int size, struct rgba color) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->size = size; priv->color = color; - struct deco *deco = malloc(sizeof(*deco)); + struct deco *deco = calloc(1, sizeof(*deco)); deco->private = priv; deco->expose = &expose; deco->destroy = &destroy; diff --git a/module.c b/module.c index bf4ec3e..1e80c32 100644 --- a/module.c +++ b/module.c @@ -6,17 +6,9 @@ struct module * module_common_new(void) { - struct module *mod = malloc(sizeof(*mod)); - mod->bar = NULL; + struct module *mod = calloc(1, sizeof(*mod)); mtx_init(&mod->lock, mtx_plain); - mod->private = NULL; mod->destroy = &module_default_destroy; - - /* No defaults for these; must be provided by implementation */ - mod->run = NULL; - mod->content = NULL; - mod->refresh_in = NULL; - return mod; } diff --git a/modules/alsa.c b/modules/alsa.c index 0cb5e41..bc17a88 100644 --- a/modules/alsa.c +++ b/modules/alsa.c @@ -269,13 +269,10 @@ err: static struct module * alsa_new(const char *card, const char *mixer, struct particle *label) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->label = label; priv->card = strdup(card); priv->mixer = strdup(mixer); - memset(&priv->channels, 0, sizeof(priv->channels)); - priv->vol_cur = priv->vol_min = priv->vol_max = 0; - priv->muted = true; struct module *mod = module_common_new(); mod->private = priv; diff --git a/modules/backlight.c b/modules/backlight.c index 69cb8f5..98142f6 100644 --- a/modules/backlight.c +++ b/modules/backlight.c @@ -206,11 +206,9 @@ run(struct module *mod) static struct module * backlight_new(const char *device, struct particle *label) { - struct private *m = malloc(sizeof(*m)); + struct private *m = calloc(1, sizeof(*m)); m->label = label; m->device = strdup(device); - m->max_brightness = 0; - m->current_brightness = 0; struct module *mod = module_common_new(); mod->private = m; diff --git a/modules/battery.c b/modules/battery.c index 2192bb0..246cc61 100644 --- a/modules/battery.c +++ b/modules/battery.c @@ -327,20 +327,11 @@ out: static struct module * battery_new(const char *battery, struct particle *label, int poll_interval_secs) { - struct private *m = malloc(sizeof(*m)); + struct private *m = calloc(1, sizeof(*m)); m->label = label; m->poll_interval = poll_interval_secs; m->battery = strdup(battery); - - m->manufacturer = NULL; - m->model = NULL; - - m->energy_full_design = 0; - m->energy_full = 0; m->state = STATE_DISCHARGING; - m->capacity = 0; - m->energy = 0; - m->power = 0; struct module *mod = module_common_new(); mod->private = m; diff --git a/modules/clock.c b/modules/clock.c index 27e19fd..fb28633 100644 --- a/modules/clock.c +++ b/modules/clock.c @@ -82,7 +82,7 @@ run(struct module *mod) static struct module * clock_new(struct particle *label, const char *date_format, const char *time_format) { - struct private *m = malloc(sizeof(*m)); + struct private *m = calloc(1, sizeof(*m)); m->label = label; m->date_format = strdup(date_format); m->time_format = strdup(time_format); diff --git a/modules/i3.c b/modules/i3.c index b4951a2..7869693 100644 --- a/modules/i3.c +++ b/modules/i3.c @@ -248,7 +248,7 @@ handle_get_workspaces_reply(struct private *m, const struct json_object *json) size_t count = json_object_array_length(json); m->workspaces.count = count; - m->workspaces.v = malloc(count * sizeof(m->workspaces.v[0])); + m->workspaces.v = calloc(count, sizeof(m->workspaces.v[0])); for (size_t i = 0; i < count; i++) { if (!workspace_from_json( @@ -709,7 +709,7 @@ static struct module * i3_new(struct i3_workspaces workspaces[], size_t workspace_count, int left_spacing, int right_spacing) { - struct private *m = malloc(sizeof(*m)); + struct private *m = calloc(1, sizeof(*m)); m->left_spacing = left_spacing; m->right_spacing = right_spacing; @@ -722,9 +722,6 @@ i3_new(struct i3_workspaces workspaces[], size_t workspace_count, m->ws_content.v[i].content = workspaces[i].content; } - m->workspaces.v = NULL; - m->workspaces.count = 0; - struct module *mod = module_common_new(); mod->private = m; mod->run = &run; diff --git a/modules/label.c b/modules/label.c index 8427050..099d22f 100644 --- a/modules/label.c +++ b/modules/label.c @@ -37,7 +37,7 @@ run(struct module *mod) static struct module * label_new(struct particle *label) { - struct private *m = malloc(sizeof(*m)); + struct private *m = calloc(1, sizeof(*m)); m->label = label; struct module *mod = module_common_new(); diff --git a/modules/mpd.c b/modules/mpd.c index b91c057..43420e6 100644 --- a/modules/mpd.c +++ b/modules/mpd.c @@ -565,20 +565,11 @@ refresh_in(struct module *mod, long milli_seconds) static struct module * mpd_new(const char *host, uint16_t port, struct particle *label) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->host = strdup(host); priv->port = port; priv->label = label; - priv->conn = NULL; priv->state = STATE_OFFLINE; - priv->repeat = priv->random = priv->consume = false; - priv->album = NULL; - priv->artist = NULL; - priv->title = NULL; - priv->elapsed.value = 0; - priv->elapsed.when.tv_sec = priv->elapsed.when.tv_nsec = 0; - priv->duration = 0; - priv->refresh_thread_id = 0; priv->refresh_abort_fd = -1; struct module *mod = module_common_new(); diff --git a/modules/network.c b/modules/network.c index 2c6522f..2bc624d 100644 --- a/modules/network.c +++ b/modules/network.c @@ -511,17 +511,14 @@ run(struct module *mod) static struct module * network_new(const char *iface, struct particle *label) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->iface = strdup(iface); priv->label = label; priv->nl_sock = -1; priv->get_addresses = true; priv->ifindex = -1; - memset(priv->mac, 0, sizeof(priv->mac)); - priv->carrier = false; priv->state = IF_OPER_DOWN; - memset(&priv->addrs, 0, sizeof(priv->addrs)); struct module *mod = module_common_new(); mod->private = priv; diff --git a/modules/removables.c b/modules/removables.c index e05db7d..d31c69c 100644 --- a/modules/removables.c +++ b/modules/removables.c @@ -545,11 +545,10 @@ run(struct module *mod) static struct module * removables_new(struct particle *label, int left_spacing, int right_spacing) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->label = label; priv->left_spacing = left_spacing; priv->right_spacing = right_spacing; - memset(&priv->devices, 0, sizeof(priv->devices)); struct module *mod = module_common_new(); mod->private = priv; diff --git a/modules/xkb.c b/modules/xkb.c index ae86c38..627f422 100644 --- a/modules/xkb.c +++ b/modules/xkb.c @@ -642,16 +642,8 @@ run(struct module *mod) static struct module * xkb_new(struct particle *label) { - struct private *m = malloc(sizeof(*m)); + struct private *m = calloc(1, sizeof(*m)); m->label = label; - m->current = 0; - m->layouts.count = 0; - m->layouts.layouts = NULL; - m->indicators.count = 0; - m->indicators.names = NULL; - m->caps_lock = false; - m->num_lock = false; - m->scroll_lock = false; struct module *mod = module_common_new(); mod->private = m; diff --git a/particle.c b/particle.c index dfca044..5f605ea 100644 --- a/particle.c +++ b/particle.c @@ -28,7 +28,7 @@ particle_common_new(int left_margin, int right_margin, struct font *font, struct rgba foreground, struct deco *deco) { - struct particle *p = malloc(sizeof(*p)); + struct particle *p = calloc(1, sizeof(*p)); p->left_margin = left_margin; p->right_margin = right_margin; p->on_click_template = @@ -120,14 +120,10 @@ exposable_default_on_mouse(struct exposable *exposable, struct bar *bar, struct exposable * exposable_common_new(const struct particle *particle, const char *on_click) { - struct exposable *exposable = malloc(sizeof(*exposable)); + struct exposable *exposable = calloc(1, sizeof(*exposable)); exposable->particle = particle; - exposable->private = NULL; - exposable->width = 0; exposable->on_click = on_click != NULL ? strdup(on_click) : NULL; exposable->destroy = &exposable_default_destroy; exposable->on_mouse = &exposable_default_on_mouse; - exposable->begin_expose = NULL; - exposable->expose = NULL; return exposable; } diff --git a/particles/dynlist.c b/particles/dynlist.c index ed656fc..e6a3636 100644 --- a/particles/dynlist.c +++ b/particles/dynlist.c @@ -98,10 +98,10 @@ struct exposable * dynlist_exposable_new(struct exposable **exposables, size_t count, int left_spacing, int right_spacing) { - struct private *e = malloc(sizeof(*e)); + struct private *e = calloc(1, sizeof(*e)); e->count = count; e->exposables = malloc(count * sizeof(e->exposables[0])); - e->widths = malloc(count * sizeof(e->widths[0])); + e->widths = calloc(count, sizeof(e->widths[0])); e->left_spacing = left_spacing; e->right_spacing = right_spacing; diff --git a/particles/list.c b/particles/list.c index 68b7186..a47c468 100644 --- a/particles/list.c +++ b/particles/list.c @@ -108,9 +108,9 @@ instantiate(const struct particle *particle, const struct tag_set *tags) { const struct private *p = particle->private; - struct eprivate *e = malloc(sizeof(*e)); + struct eprivate *e = calloc(1, sizeof(*e)); e->exposables = malloc(p->count * sizeof(*e->exposables)); - e->widths = malloc(p->count * sizeof(*e->widths)); + e->widths = calloc(p->count, sizeof(*e->widths)); e->count = p->count; e->left_spacing = p->left_spacing; e->right_spacing = p->right_spacing; @@ -149,7 +149,7 @@ particle_list_new(struct particle *common, struct particle *particles[], size_t count, int left_spacing, int right_spacing) { - struct private *p = malloc(sizeof(*p)); + struct private *p = calloc(1, sizeof(*p)); p->particles = malloc(count * sizeof(p->particles[0])); p->count = count; p->left_spacing = left_spacing; diff --git a/particles/map.c b/particles/map.c index 5acc7f9..a8895cb 100644 --- a/particles/map.c +++ b/particles/map.c @@ -111,7 +111,7 @@ instantiate(const struct particle *particle, const struct tag_set *tags) pp = p->default_particle; } - struct eprivate *e = malloc(sizeof(*e)); + struct eprivate *e = calloc(1, sizeof(*e)); e->exposable = pp->instantiate(pp, tags); char *on_click = tags_expand_template(particle->on_click_template, tags); @@ -151,7 +151,7 @@ map_new(struct particle *common, const char *tag, const struct particle_map particle_map[], size_t count, struct particle *default_particle) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->tag = strdup(tag); priv->default_particle = default_particle; priv->count = count; diff --git a/particles/progress-bar.c b/particles/progress-bar.c index a10f9e2..762178e 100644 --- a/particles/progress-bar.c +++ b/particles/progress-bar.c @@ -144,7 +144,7 @@ instantiate(const struct particle *particle, const struct tag_set *tags) long fill_count = max == min ? 0 : p->width * value / (max - min); long empty_count = p->width - fill_count; - struct eprivate *epriv = malloc(sizeof(*epriv)); + struct eprivate *epriv = calloc(1, sizeof(*epriv)); epriv->count = ( 1 + /* Start marker */ fill_count + /* Before current position */ @@ -214,7 +214,7 @@ progress_bar_new(struct particle *common, const char *tag, int width, struct particle *fill, struct particle *empty, struct particle *indicator) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->tag = strdup(tag); priv->width = width; priv->start_marker = start_marker; diff --git a/particles/ramp.c b/particles/ramp.c index a9cf627..1f8ef11 100644 --- a/particles/ramp.c +++ b/particles/ramp.c @@ -120,7 +120,7 @@ instantiate(const struct particle *particle, const struct tag_set *tags) struct particle *pp = p->particles[idx]; - struct eprivate *e = malloc(sizeof(*e)); + struct eprivate *e = calloc(1, sizeof(*e)); e->exposable = pp->instantiate(pp, tags); char *on_click = tags_expand_template(particle->on_click_template, tags); @@ -140,9 +140,9 @@ ramp_new(struct particle *common, const char *tag, struct particle *particles[], size_t count) { - struct private *priv = malloc(sizeof(*priv)); + struct private *priv = calloc(1, sizeof(*priv)); priv->tag = strdup(tag); - priv->particles = calloc(count, sizeof(priv->particles[0])); + priv->particles = malloc(count * sizeof(priv->particles[0])); priv->count = count; for (size_t i = 0; i < count; i++) diff --git a/particles/string.c b/particles/string.c index 5c06378..58e1984 100644 --- a/particles/string.c +++ b/particles/string.c @@ -108,12 +108,9 @@ static struct exposable * instantiate(const struct particle *particle, const struct tag_set *tags) { const struct private *p = particle->private; - struct eprivate *e = malloc(sizeof(*e)); + struct eprivate *e = calloc(1, sizeof(*e)); e->text = tags_expand_template(p->text, tags); - memset(&e->extents, 0, sizeof(e->extents)); - e->glyphs = NULL; - e->clusters = NULL; e->num_glyphs = -1; e->num_clusters = -1; @@ -164,7 +161,7 @@ particle_destroy(struct particle *particle) static struct particle * string_new(struct particle *common, const char *text, size_t max_len) { - struct private *p = malloc(sizeof(*p)); + struct private *p = calloc(1, sizeof(*p)); p->text = strdup(text); p->max_len = max_len;