decorations: 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 10:58:26 +01:00
parent 83591b9269
commit 29e9cea1dd
3 changed files with 6 additions and 6 deletions

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 * static struct deco *
background_new(struct rgba color) background_new(struct rgba color)
{ {
struct private *priv = malloc(sizeof(*priv)); struct private *priv = calloc(1, sizeof(*priv));
priv->color = color; priv->color = color;
struct deco *deco = malloc(sizeof(*deco)); struct deco *deco = calloc(1, sizeof(*deco));
deco->private = priv; deco->private = priv;
deco->expose = &expose; deco->expose = &expose;
deco->destroy = &destroy; 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 * static struct deco *
stack_new(struct deco *decos[], size_t count) 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->decos = malloc(count * sizeof(priv->decos[0]));
priv->count = count; priv->count = count;
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
priv->decos[i] = decos[i]; priv->decos[i] = decos[i];
struct deco *deco = malloc(sizeof(*deco)); struct deco *deco = calloc(1, sizeof(*deco));
deco->private = priv; deco->private = priv;
deco->expose = &expose; deco->expose = &expose;
deco->destroy = &destroy; 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 * static struct deco *
underline_new(int size, struct rgba color) underline_new(int size, struct rgba color)
{ {
struct private *priv = malloc(sizeof(*priv)); struct private *priv = calloc(1, sizeof(*priv));
priv->size = size; priv->size = size;
priv->color = color; priv->color = color;
struct deco *deco = malloc(sizeof(*deco)); struct deco *deco = calloc(1, sizeof(*deco));
deco->private = priv; deco->private = priv;
deco->expose = &expose; deco->expose = &expose;
deco->destroy = &destroy; deco->destroy = &destroy;