treewide: replace malloc() and memset() with calloc()
Subsystems (2)
PATCH 1/1 treewide: replace malloc() and memset() with calloc()
From: Ilya Kulaev <[email protected]>
Replace malloc() followed by memset() with a single calloc() call where zero-initialized memory is needed.
This reduces boilerplate without changing the intended behavior.
--- 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(-)
lib/chip-info.c +1 -3
@@ -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;/*
lib/chip.c +1 -3
@@ -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;
lib/edge-event.c +1 -2
@@ -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));
lib/info-event.c +1 -2
@@ -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) {
lib/line-config.c +1 -3
@@ -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; }
lib/line-info.c +1 -3
@@ -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' ;
lib/line-request.c +1 -3
@@ -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);
lib/request-config.c +1 -3
@@ -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; }
tests/gpiosim/gpiosim.c +4 -9
@@ -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);
tools/gpiotools.c +1 -3
@@ -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) {
Signature · 1 line
2.56.0
Bartosz Golaszewski
maintainerQuoted 10 lines · On Sun, 11 Oct 2026 12:00:59 +0200, [email protected] said:
From: Ilya Kulaev <[email protected]>
Replace
malloc()followed bymemset()with a singlecalloc()call where zero-initialized memory is needed.This reduces boilerplate without changing the intended behavior.
Signed-off-by Ilya Kulaev <[email protected]>---
Thank you for the patch. Like I said in the github thread, I think it's just needless churn. It fixes no bug, it saves 21 lines of source code but doesn't result in the generated code being smaller as - verified with GCC 14.2 on x86 - the compiler merges malloc() + memset() into calloc() anyway in all but 5 out of 13 call sites. I really don't see any technical reason to apply it.
My personal preference is malloc() + memset() as it's very clear about the intention of having the code zeroed.
Bartosz