particles: use calloc() instead of malloc()

In cases where it makes sense, use calloc() instead of malloc():

* When allocating large objects with many members, many for which
  NULL/0 is a good default value.
* Arrays etc where we explicitly initialize to NULL anyway.
This commit is contained in:
Daniel Eklöf 2019-02-09 11:05:12 +01:00
parent 29e9cea1dd
commit 6bba9200cf
7 changed files with 16 additions and 23 deletions

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;