core: kill zalloc() and use calloc() where applicable
authorBartosz Golaszewski <bartekgola@gmail.com>
Sat, 24 Jun 2017 10:37:50 +0000 (12:37 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Thu, 6 Jul 2017 09:11:36 +0000 (11:11 +0200)
Remove zalloc() and use malloc() + memset() in the code. When
allocating an array of elements, use calloc(). This is done in
preparation for splitting up the core library code into separate
files.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
src/lib/core.c

index e23539470655aae60e5bd7508bf386fec3be4876..3c9ed217039061c857074d734718ac26bf52211a 100644 (file)
@@ -23,8 +23,6 @@
 #include <poll.h>
 #include <linux/gpio.h>
 
-#define MALLOC __attribute__((malloc))
-
 struct gpiod_chip {
        int fd;
        struct gpiochip_info cinfo;
@@ -69,17 +67,6 @@ struct gpiod_chip_iter {
 static const char dev_dir[] = "/dev/";
 static const char cdev_prefix[] = "gpiochip";
 
-static MALLOC void * zalloc(size_t size)
-{
-       void *ptr;
-
-       ptr = malloc(size);
-       if (ptr)
-               memset(ptr, 0, size);
-
-       return ptr;
-}
-
 static bool is_unsigned_int(const char *str)
 {
        for (; *str && isdigit(*str); str++);
@@ -486,10 +473,12 @@ int gpiod_line_request_bulk(struct gpiod_line_bulk *bulk,
        if (!verify_line_bulk(bulk))
                return -1;
 
-       handle = zalloc(sizeof(*handle));
+       handle = malloc(sizeof(*handle));
        if (!handle)
                return -1;
 
+       memset(handle, 0, sizeof(*handle));
+
        req = &handle->request;
 
        if (config->flags & GPIOD_REQUEST_OPEN_DRAIN)
@@ -906,12 +895,13 @@ struct gpiod_chip * gpiod_chip_open(const char *path)
        if (fd < 0)
                return NULL;
 
-       chip = zalloc(sizeof(*chip));
+       chip = malloc(sizeof(*chip));
        if (!chip) {
                close(fd);
                return NULL;
        }
 
+       memset(chip, 0, sizeof(*chip));
        chip->fd = fd;
 
        status = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &chip->cinfo);
@@ -921,7 +911,7 @@ struct gpiod_chip * gpiod_chip_open(const char *path)
                return NULL;
        }
 
-       chip->lines = zalloc(chip->cinfo.lines * sizeof(*chip->lines));
+       chip->lines = calloc(chip->cinfo.lines, sizeof(*chip->lines));
        if (!chip->lines) {
                close(chip->fd);
                free(chip);
@@ -1071,10 +1061,12 @@ struct gpiod_chip_iter * gpiod_chip_iter_new(void)
 {
        struct gpiod_chip_iter *new;
 
-       new = zalloc(sizeof(*new));
+       new = malloc(sizeof(*new));
        if (!new)
                return NULL;
 
+       memset(new, 0, sizeof(*new));
+
        new->dir = opendir(dev_dir);
        if (!new->dir)
                return NULL;