From: Bartosz Golaszewski Date: Sat, 24 Jun 2017 10:37:50 +0000 (+0200) Subject: core: kill zalloc() and use calloc() where applicable X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=2b8b4cef8443746d67f35c82f457911c563c45ae;p=qemu-gpiodev%2Flibgpiod.git core: kill zalloc() and use calloc() where applicable 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 --- diff --git a/src/lib/core.c b/src/lib/core.c index e235394..3c9ed21 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -23,8 +23,6 @@ #include #include -#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;