forked from external/yambar
modules: 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
6bba9200cf
commit
b6e61f9c7e
11 changed files with 12 additions and 58 deletions
10
module.c
10
module.c
|
@ -6,17 +6,9 @@
|
||||||
struct module *
|
struct module *
|
||||||
module_common_new(void)
|
module_common_new(void)
|
||||||
{
|
{
|
||||||
struct module *mod = malloc(sizeof(*mod));
|
struct module *mod = calloc(1, sizeof(*mod));
|
||||||
mod->bar = NULL;
|
|
||||||
mtx_init(&mod->lock, mtx_plain);
|
mtx_init(&mod->lock, mtx_plain);
|
||||||
mod->private = NULL;
|
|
||||||
mod->destroy = &module_default_destroy;
|
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;
|
return mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -269,13 +269,10 @@ err:
|
||||||
static struct module *
|
static struct module *
|
||||||
alsa_new(const char *card, const char *mixer, struct particle *label)
|
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->label = label;
|
||||||
priv->card = strdup(card);
|
priv->card = strdup(card);
|
||||||
priv->mixer = strdup(mixer);
|
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();
|
struct module *mod = module_common_new();
|
||||||
mod->private = priv;
|
mod->private = priv;
|
||||||
|
|
|
@ -206,11 +206,9 @@ run(struct module *mod)
|
||||||
static struct module *
|
static struct module *
|
||||||
backlight_new(const char *device, struct particle *label)
|
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->label = label;
|
||||||
m->device = strdup(device);
|
m->device = strdup(device);
|
||||||
m->max_brightness = 0;
|
|
||||||
m->current_brightness = 0;
|
|
||||||
|
|
||||||
struct module *mod = module_common_new();
|
struct module *mod = module_common_new();
|
||||||
mod->private = m;
|
mod->private = m;
|
||||||
|
|
|
@ -327,20 +327,11 @@ out:
|
||||||
static struct module *
|
static struct module *
|
||||||
battery_new(const char *battery, struct particle *label, int poll_interval_secs)
|
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->label = label;
|
||||||
m->poll_interval = poll_interval_secs;
|
m->poll_interval = poll_interval_secs;
|
||||||
m->battery = strdup(battery);
|
m->battery = strdup(battery);
|
||||||
|
|
||||||
m->manufacturer = NULL;
|
|
||||||
m->model = NULL;
|
|
||||||
|
|
||||||
m->energy_full_design = 0;
|
|
||||||
m->energy_full = 0;
|
|
||||||
m->state = STATE_DISCHARGING;
|
m->state = STATE_DISCHARGING;
|
||||||
m->capacity = 0;
|
|
||||||
m->energy = 0;
|
|
||||||
m->power = 0;
|
|
||||||
|
|
||||||
struct module *mod = module_common_new();
|
struct module *mod = module_common_new();
|
||||||
mod->private = m;
|
mod->private = m;
|
||||||
|
|
|
@ -82,7 +82,7 @@ run(struct module *mod)
|
||||||
static struct module *
|
static struct module *
|
||||||
clock_new(struct particle *label, const char *date_format, const char *time_format)
|
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->label = label;
|
||||||
m->date_format = strdup(date_format);
|
m->date_format = strdup(date_format);
|
||||||
m->time_format = strdup(time_format);
|
m->time_format = strdup(time_format);
|
||||||
|
|
|
@ -248,7 +248,7 @@ handle_get_workspaces_reply(struct private *m, const struct json_object *json)
|
||||||
|
|
||||||
size_t count = json_object_array_length(json);
|
size_t count = json_object_array_length(json);
|
||||||
m->workspaces.count = count;
|
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++) {
|
for (size_t i = 0; i < count; i++) {
|
||||||
if (!workspace_from_json(
|
if (!workspace_from_json(
|
||||||
|
@ -709,7 +709,7 @@ static struct module *
|
||||||
i3_new(struct i3_workspaces workspaces[], size_t workspace_count,
|
i3_new(struct i3_workspaces workspaces[], size_t workspace_count,
|
||||||
int left_spacing, int right_spacing)
|
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->left_spacing = left_spacing;
|
||||||
m->right_spacing = right_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->ws_content.v[i].content = workspaces[i].content;
|
||||||
}
|
}
|
||||||
|
|
||||||
m->workspaces.v = NULL;
|
|
||||||
m->workspaces.count = 0;
|
|
||||||
|
|
||||||
struct module *mod = module_common_new();
|
struct module *mod = module_common_new();
|
||||||
mod->private = m;
|
mod->private = m;
|
||||||
mod->run = &run;
|
mod->run = &run;
|
||||||
|
|
|
@ -37,7 +37,7 @@ run(struct module *mod)
|
||||||
static struct module *
|
static struct module *
|
||||||
label_new(struct particle *label)
|
label_new(struct particle *label)
|
||||||
{
|
{
|
||||||
struct private *m = malloc(sizeof(*m));
|
struct private *m = calloc(1, sizeof(*m));
|
||||||
m->label = label;
|
m->label = label;
|
||||||
|
|
||||||
struct module *mod = module_common_new();
|
struct module *mod = module_common_new();
|
||||||
|
|
|
@ -565,20 +565,11 @@ refresh_in(struct module *mod, long milli_seconds)
|
||||||
static struct module *
|
static struct module *
|
||||||
mpd_new(const char *host, uint16_t port, struct particle *label)
|
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->host = strdup(host);
|
||||||
priv->port = port;
|
priv->port = port;
|
||||||
priv->label = label;
|
priv->label = label;
|
||||||
priv->conn = NULL;
|
|
||||||
priv->state = STATE_OFFLINE;
|
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;
|
priv->refresh_abort_fd = -1;
|
||||||
|
|
||||||
struct module *mod = module_common_new();
|
struct module *mod = module_common_new();
|
||||||
|
|
|
@ -511,17 +511,14 @@ run(struct module *mod)
|
||||||
static struct module *
|
static struct module *
|
||||||
network_new(const char *iface, struct particle *label)
|
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->iface = strdup(iface);
|
||||||
priv->label = label;
|
priv->label = label;
|
||||||
|
|
||||||
priv->nl_sock = -1;
|
priv->nl_sock = -1;
|
||||||
priv->get_addresses = true;
|
priv->get_addresses = true;
|
||||||
priv->ifindex = -1;
|
priv->ifindex = -1;
|
||||||
memset(priv->mac, 0, sizeof(priv->mac));
|
|
||||||
priv->carrier = false;
|
|
||||||
priv->state = IF_OPER_DOWN;
|
priv->state = IF_OPER_DOWN;
|
||||||
memset(&priv->addrs, 0, sizeof(priv->addrs));
|
|
||||||
|
|
||||||
struct module *mod = module_common_new();
|
struct module *mod = module_common_new();
|
||||||
mod->private = priv;
|
mod->private = priv;
|
||||||
|
|
|
@ -545,11 +545,10 @@ run(struct module *mod)
|
||||||
static struct module *
|
static struct module *
|
||||||
removables_new(struct particle *label, int left_spacing, int right_spacing)
|
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->label = label;
|
||||||
priv->left_spacing = left_spacing;
|
priv->left_spacing = left_spacing;
|
||||||
priv->right_spacing = right_spacing;
|
priv->right_spacing = right_spacing;
|
||||||
memset(&priv->devices, 0, sizeof(priv->devices));
|
|
||||||
|
|
||||||
struct module *mod = module_common_new();
|
struct module *mod = module_common_new();
|
||||||
mod->private = priv;
|
mod->private = priv;
|
||||||
|
|
|
@ -642,16 +642,8 @@ run(struct module *mod)
|
||||||
static struct module *
|
static struct module *
|
||||||
xkb_new(struct particle *label)
|
xkb_new(struct particle *label)
|
||||||
{
|
{
|
||||||
struct private *m = malloc(sizeof(*m));
|
struct private *m = calloc(1, sizeof(*m));
|
||||||
m->label = label;
|
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();
|
struct module *mod = module_common_new();
|
||||||
mod->private = m;
|
mod->private = m;
|
||||||
|
|
Loading…
Add table
Reference in a new issue