core: dynamically allocate memory for GPIO line request data
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 2 Jan 2017 13:51:01 +0000 (14:51 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 2 Jan 2017 13:51:01 +0000 (14:51 +0100)
For bulk requests, the lines will share the struct gpiohandle_request
object. Instead of it being a part of struct gpiod_line, allocate its
memory dynamically and only store the pointer in the line context
structure.

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

diff --git a/core.c b/core.c
index b8b6b916fc1577a524e52341693772cfb1e33a12..077c116d3af9bb44be78c2f72ae10dcddbfa2399 100644 (file)
--- a/core.c
+++ b/core.c
@@ -146,7 +146,7 @@ struct gpiod_line {
        bool requested;
        struct gpiod_chip *chip;
        struct gpioline_info info;
-       struct gpiohandle_request req;
+       struct gpiohandle_request *req;
 };
 
 unsigned int gpiod_line_offset(struct gpiod_line *line)
@@ -197,7 +197,11 @@ int gpiod_line_request(struct gpiod_line *line, const char *consumer,
        struct gpiod_chip *chip;
        int status, fd;
 
-       req = &line->req;
+       req = zalloc(sizeof(*req));
+       if (!req)
+               return -1;
+
+       line->req = req;
        memset(req, 0, sizeof(*req));
 
        if (flags & GPIOD_REQUEST_ACTIVE_LOW)
@@ -234,7 +238,8 @@ int gpiod_line_request(struct gpiod_line *line, const char *consumer,
 
 void gpiod_line_release(struct gpiod_line *line)
 {
-       close(line->req.fd);
+       close(line->req->fd);
+       free(line->req);
        line->requested = false;
 }
 
@@ -255,7 +260,7 @@ int gpiod_line_get_value(struct gpiod_line *line)
 
        memset(&data, 0, sizeof(data));
 
-       status = gpio_ioctl(line->req.fd,
+       status = gpio_ioctl(line->req->fd,
                            GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
        if (status < 0)
                return -1;
@@ -276,7 +281,7 @@ int gpiod_line_set_value(struct gpiod_line *line, int value)
        memset(&data, 0, sizeof(data));
        data.values[0] = value ? 1 : 0;
 
-       status = gpio_ioctl(line->req.fd,
+       status = gpio_ioctl(line->req->fd,
                            GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
        if (status < 0)
                return -1;