From: tornilua@gmail.com
To: linux-gpio@vger.kernel.org
Cc: Ilya Kulaev <tornilua@gmail.com>
Subject: [libgpiod][PATCH] treewide: replace malloc() and memset() with calloc()
Date: Sun, 11 Oct 2026 16:00:59 +0600 [thread overview]
Message-ID: <20261011100059.29832-1-tornilua@gmail.com> (raw)
From: Ilya Kulaev <tornilua@gmail.com>
Replace malloc() followed by memset() with a single calloc()
call where zero-initialized memory is needed.
This reduces boilerplate without changing the intended behavior.
Link: https://github.com/brgl/libgpiod/issues/154
Signed-off-by: Ilya Kulaev <tornilua@gmail.com>
---
lib/chip-info.c | 4 +---
lib/chip.c | 4 +---
lib/edge-event.c | 3 +--
lib/info-event.c | 3 +--
lib/line-config.c | 4 +---
lib/line-info.c | 4 +---
lib/line-request.c | 4 +---
lib/request-config.c | 4 +---
tests/gpiosim/gpiosim.c | 13 ++++---------
tools/gpiotools.c | 4 +---
10 files changed, 13 insertions(+), 34 deletions(-)
diff --git a/lib/chip-info.c b/lib/chip-info.c
index 972b2a8..786b8b5 100644
--- a/lib/chip-info.c
+++ b/lib/chip-info.c
@@ -45,12 +45,10 @@ gpiod_chip_info_from_uapi(struct gpiochip_info *uapi_info)
{
struct gpiod_chip_info *info;
- info = malloc(sizeof(*info));
+ info = calloc(1, sizeof(*info));
if (!info)
return NULL;
- memset(info, 0, sizeof(*info));
-
info->num_lines = uapi_info->lines;
/*
diff --git a/lib/chip.c b/lib/chip.c
index 611eb32..8e210b4 100644
--- a/lib/chip.c
+++ b/lib/chip.c
@@ -33,12 +33,10 @@ GPIOD_API struct gpiod_chip *gpiod_chip_open(const char *path)
if (fd < 0)
return NULL;
- chip = malloc(sizeof(*chip));
+ chip = calloc(1, sizeof(*chip));
if (!chip)
goto err_close_fd;
- memset(chip, 0, sizeof(*chip));
-
chip->path = strdup(path);
if (!chip->path)
goto err_free_chip;
diff --git a/lib/edge-event.c b/lib/edge-event.c
index be46dce..779af1f 100644
--- a/lib/edge-event.c
+++ b/lib/edge-event.c
@@ -99,11 +99,10 @@ gpiod_edge_event_buffer_new(size_t capacity)
if (capacity > EVENT_BUFFER_MAX_CAPACITY)
capacity = EVENT_BUFFER_MAX_CAPACITY;
- buf = malloc(sizeof(*buf));
+ buf = calloc(1, sizeof(*buf));
if (!buf)
return NULL;
- memset(buf, 0, sizeof(*buf));
buf->capacity = capacity;
buf->events = calloc(capacity, sizeof(*buf->events));
diff --git a/lib/info-event.c b/lib/info-event.c
index 0e3ef9b..8b456e8 100644
--- a/lib/info-event.c
+++ b/lib/info-event.c
@@ -20,11 +20,10 @@ gpiod_info_event_from_uapi(struct gpio_v2_line_info_changed *uapi_evt)
{
struct gpiod_info_event *event;
- event = malloc(sizeof(*event));
+ event = calloc(1, sizeof(*event));
if (!event)
return NULL;
- memset(event, 0, sizeof(*event));
event->timestamp = uapi_evt->timestamp_ns;
switch (uapi_evt->event_type) {
diff --git a/lib/line-config.c b/lib/line-config.c
index f00892e..34414b9 100644
--- a/lib/line-config.c
+++ b/lib/line-config.c
@@ -36,12 +36,10 @@ GPIOD_API struct gpiod_line_config *gpiod_line_config_new(void)
{
struct gpiod_line_config *config;
- config = malloc(sizeof(*config));
+ config = calloc(1, sizeof(*config));
if (!config)
return NULL;
- memset(config, 0, sizeof(*config));
-
return config;
}
diff --git a/lib/line-info.c b/lib/line-info.c
index 1613c92..0bf1a38 100644
--- a/lib/line-info.c
+++ b/lib/line-info.c
@@ -141,12 +141,10 @@ gpiod_line_info_from_uapi(struct gpio_v2_line_info *uapi_info)
struct gpiod_line_info *info;
size_t i;
- info = malloc(sizeof(*info));
+ info = calloc(1, sizeof(*info));
if (!info)
return NULL;
- memset(info, 0, sizeof(*info));
-
info->offset = uapi_info->offset;
strncpy(info->name, uapi_info->name, GPIO_MAX_NAME_SIZE);
info->name[GPIO_MAX_NAME_SIZE] = '\0';
diff --git a/lib/line-request.c b/lib/line-request.c
index 32ab9ae..2720d69 100644
--- a/lib/line-request.c
+++ b/lib/line-request.c
@@ -29,12 +29,10 @@ gpiod_line_request_from_uapi(struct gpio_v2_line_request *uapi_req,
return NULL;
}
- request = malloc(sizeof(*request));
+ request = calloc(1, sizeof(*request));
if (!request)
return NULL;
- memset(request, 0, sizeof(*request));
-
request->chip_name = strdup(chip_name);
if (!request->chip_name) {
free(request);
diff --git a/lib/request-config.c b/lib/request-config.c
index cd22028..923c9e7 100644
--- a/lib/request-config.c
+++ b/lib/request-config.c
@@ -18,12 +18,10 @@ GPIOD_API struct gpiod_request_config *gpiod_request_config_new(void)
{
struct gpiod_request_config *config;
- config = malloc(sizeof(*config));
+ config = calloc(1, sizeof(*config));
if (!config)
return NULL;
- memset(config, 0, sizeof(*config));
-
return config;
}
diff --git a/tests/gpiosim/gpiosim.c b/tests/gpiosim/gpiosim.c
index 920edb4..80b2da0 100644
--- a/tests/gpiosim/gpiosim.c
+++ b/tests/gpiosim/gpiosim.c
@@ -559,11 +559,10 @@ GPIOSIM_API struct gpiosim_ctx *gpiosim_ctx_new(void)
if (ret)
return NULL;
- ctx = malloc(sizeof(*ctx));
+ ctx = calloc(1, sizeof(*ctx));
if (!ctx)
return NULL;
- memset(ctx, 0, sizeof(*ctx));
refcount_init(&ctx->refcnt, ctx_release);
ret = ctx_get_configfs_fd(ctx);
@@ -623,7 +622,7 @@ GPIOSIM_API struct gpiosim_dev *gpiosim_dev_new(struct gpiosim_ctx *ctx)
if (configfs_fd < 0)
goto err_unlink;
- dev = malloc(sizeof(*dev));
+ dev = calloc(1, sizeof(*dev));
if (!dev)
goto err_close_fd;
@@ -632,7 +631,6 @@ GPIOSIM_API struct gpiosim_dev *gpiosim_dev_new(struct gpiosim_ctx *ctx)
if (ret)
goto err_free_dev;
- memset(dev, 0, sizeof(*dev));
refcount_init(&dev->refcnt, dev_release);
list_init(&dev->banks);
list_init(&dev->deferred_banks);
@@ -920,12 +918,10 @@ GPIOSIM_API struct gpiosim_bank *gpiosim_bank_new(struct gpiosim_dev *dev)
if (configfs_fd < 0)
goto err_unlink;
- bank = malloc(sizeof(*bank));
+ bank = calloc(1, sizeof(*bank));
if (!bank)
goto err_close_cfs;
- memset(bank, 0, sizeof(*bank));
-
refcount_init(&bank->refcnt, bank_release);
list_add(&bank->siblings, &dev->banks);
bank->cfs_dir_fd = configfs_fd;
@@ -1017,7 +1013,7 @@ static int bank_make_line_dir(struct gpiosim_bank *bank, unsigned int offset)
if (ret && errno != ENOENT)
return -1;
- line = malloc(sizeof(*line));
+ line = calloc(1, sizeof(*line));
if (!line)
return -1;
@@ -1027,7 +1023,6 @@ static int bank_make_line_dir(struct gpiosim_bank *bank, unsigned int offset)
return -1;
}
- memset(line, 0, sizeof(*line));
line->offset = offset;
list_add(&line->siblings, &bank->lines);
diff --git a/tools/gpiotools.c b/tools/gpiotools.c
index dda36da..48ba5ea 100644
--- a/tools/gpiotools.c
+++ b/tools/gpiotools.c
@@ -153,12 +153,10 @@ gpiotools_resolver_init(int num_lines, char **lines, int num_chips,
int i;
resolver_size = sizeof(*resolver) + num_lines * sizeof(*line);
- resolver = malloc(resolver_size);
+ resolver = calloc(1, resolver_size);
if (!resolver)
return NULL;
- memset(resolver, 0, resolver_size);
-
resolver->chips = calloc(num_chips,
sizeof(struct gpiotools_resolved_chip));
if (!resolver->chips) {
--
2.56.0
reply other threads:[~2026-10-11 10:01 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261011100059.29832-1-tornilua@gmail.com \
--to=tornilua@gmail.com \
--cc=linux-gpio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox