forked from external/yambar
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:
parent
29e9cea1dd
commit
6bba9200cf
7 changed files with 16 additions and 23 deletions
|
@ -28,7 +28,7 @@ particle_common_new(int left_margin, int right_margin,
|
||||||
struct font *font, struct rgba foreground,
|
struct font *font, struct rgba foreground,
|
||||||
struct deco *deco)
|
struct deco *deco)
|
||||||
{
|
{
|
||||||
struct particle *p = malloc(sizeof(*p));
|
struct particle *p = calloc(1, sizeof(*p));
|
||||||
p->left_margin = left_margin;
|
p->left_margin = left_margin;
|
||||||
p->right_margin = right_margin;
|
p->right_margin = right_margin;
|
||||||
p->on_click_template =
|
p->on_click_template =
|
||||||
|
@ -120,14 +120,10 @@ exposable_default_on_mouse(struct exposable *exposable, struct bar *bar,
|
||||||
struct exposable *
|
struct exposable *
|
||||||
exposable_common_new(const struct particle *particle, const char *on_click)
|
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->particle = particle;
|
||||||
exposable->private = NULL;
|
|
||||||
exposable->width = 0;
|
|
||||||
exposable->on_click = on_click != NULL ? strdup(on_click) : NULL;
|
exposable->on_click = on_click != NULL ? strdup(on_click) : NULL;
|
||||||
exposable->destroy = &exposable_default_destroy;
|
exposable->destroy = &exposable_default_destroy;
|
||||||
exposable->on_mouse = &exposable_default_on_mouse;
|
exposable->on_mouse = &exposable_default_on_mouse;
|
||||||
exposable->begin_expose = NULL;
|
|
||||||
exposable->expose = NULL;
|
|
||||||
return exposable;
|
return exposable;
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,10 +98,10 @@ struct exposable *
|
||||||
dynlist_exposable_new(struct exposable **exposables, size_t count,
|
dynlist_exposable_new(struct exposable **exposables, size_t count,
|
||||||
int left_spacing, int right_spacing)
|
int left_spacing, int right_spacing)
|
||||||
{
|
{
|
||||||
struct private *e = malloc(sizeof(*e));
|
struct private *e = calloc(1, sizeof(*e));
|
||||||
e->count = count;
|
e->count = count;
|
||||||
e->exposables = malloc(count * sizeof(e->exposables[0]));
|
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->left_spacing = left_spacing;
|
||||||
e->right_spacing = right_spacing;
|
e->right_spacing = right_spacing;
|
||||||
|
|
||||||
|
|
|
@ -108,9 +108,9 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
{
|
{
|
||||||
const struct private *p = particle->private;
|
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->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->count = p->count;
|
||||||
e->left_spacing = p->left_spacing;
|
e->left_spacing = p->left_spacing;
|
||||||
e->right_spacing = p->right_spacing;
|
e->right_spacing = p->right_spacing;
|
||||||
|
@ -149,7 +149,7 @@ particle_list_new(struct particle *common,
|
||||||
struct particle *particles[], size_t count,
|
struct particle *particles[], size_t count,
|
||||||
int left_spacing, int right_spacing)
|
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->particles = malloc(count * sizeof(p->particles[0]));
|
||||||
p->count = count;
|
p->count = count;
|
||||||
p->left_spacing = left_spacing;
|
p->left_spacing = left_spacing;
|
||||||
|
|
|
@ -111,7 +111,7 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
pp = p->default_particle;
|
pp = p->default_particle;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct eprivate *e = malloc(sizeof(*e));
|
struct eprivate *e = calloc(1, sizeof(*e));
|
||||||
e->exposable = pp->instantiate(pp, tags);
|
e->exposable = pp->instantiate(pp, tags);
|
||||||
|
|
||||||
char *on_click = tags_expand_template(particle->on_click_template, 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,
|
const struct particle_map particle_map[], size_t count,
|
||||||
struct particle *default_particle)
|
struct particle *default_particle)
|
||||||
{
|
{
|
||||||
struct private *priv = malloc(sizeof(*priv));
|
struct private *priv = calloc(1, sizeof(*priv));
|
||||||
priv->tag = strdup(tag);
|
priv->tag = strdup(tag);
|
||||||
priv->default_particle = default_particle;
|
priv->default_particle = default_particle;
|
||||||
priv->count = count;
|
priv->count = count;
|
||||||
|
|
|
@ -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 fill_count = max == min ? 0 : p->width * value / (max - min);
|
||||||
long empty_count = p->width - fill_count;
|
long empty_count = p->width - fill_count;
|
||||||
|
|
||||||
struct eprivate *epriv = malloc(sizeof(*epriv));
|
struct eprivate *epriv = calloc(1, sizeof(*epriv));
|
||||||
epriv->count = (
|
epriv->count = (
|
||||||
1 + /* Start marker */
|
1 + /* Start marker */
|
||||||
fill_count + /* Before current position */
|
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 *fill, struct particle *empty,
|
||||||
struct particle *indicator)
|
struct particle *indicator)
|
||||||
{
|
{
|
||||||
struct private *priv = malloc(sizeof(*priv));
|
struct private *priv = calloc(1, sizeof(*priv));
|
||||||
priv->tag = strdup(tag);
|
priv->tag = strdup(tag);
|
||||||
priv->width = width;
|
priv->width = width;
|
||||||
priv->start_marker = start_marker;
|
priv->start_marker = start_marker;
|
||||||
|
|
|
@ -120,7 +120,7 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
|
|
||||||
struct particle *pp = p->particles[idx];
|
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);
|
e->exposable = pp->instantiate(pp, tags);
|
||||||
|
|
||||||
char *on_click = tags_expand_template(particle->on_click_template, 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 particle *particles[], size_t count)
|
||||||
{
|
{
|
||||||
|
|
||||||
struct private *priv = malloc(sizeof(*priv));
|
struct private *priv = calloc(1, sizeof(*priv));
|
||||||
priv->tag = strdup(tag);
|
priv->tag = strdup(tag);
|
||||||
priv->particles = calloc(count, sizeof(priv->particles[0]));
|
priv->particles = malloc(count * sizeof(priv->particles[0]));
|
||||||
priv->count = count;
|
priv->count = count;
|
||||||
|
|
||||||
for (size_t i = 0; i < count; i++)
|
for (size_t i = 0; i < count; i++)
|
||||||
|
|
|
@ -108,12 +108,9 @@ static struct exposable *
|
||||||
instantiate(const struct particle *particle, const struct tag_set *tags)
|
instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
{
|
{
|
||||||
const struct private *p = particle->private;
|
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);
|
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_glyphs = -1;
|
||||||
e->num_clusters = -1;
|
e->num_clusters = -1;
|
||||||
|
|
||||||
|
@ -164,7 +161,7 @@ particle_destroy(struct particle *particle)
|
||||||
static struct particle *
|
static struct particle *
|
||||||
string_new(struct particle *common, const char *text, size_t max_len)
|
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->text = strdup(text);
|
||||||
p->max_len = max_len;
|
p->max_len = max_len;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue