Merge branch 'calloc-not-malloc'

This commit is contained in:
Daniel Eklöf 2019-02-10 12:29:14 +01:00
commit 1c5d017dc3
22 changed files with 42 additions and 102 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -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();

View file

@ -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();

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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++)

View file

@ -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;