core: implement gpiod_chip_iter_free_noclose()
authorBartosz Golaszewski <bartekgola@gmail.com>
Tue, 10 Jan 2017 15:09:23 +0000 (16:09 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Tue, 10 Jan 2017 15:12:08 +0000 (16:12 +0100)
Implement a new version of gpiod_chip_iter_free() that doesn't close
the most recently opened gpiochip.

Use the new routine in gpiod_line_find_by_name().

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

diff --git a/core.c b/core.c
index 8056e9fdaa771292316a12f6d1d1b67cdd885544..4fe2fa2d23b4b2e7171c73fed7cdb7152d5b58db 100644 (file)
--- a/core.c
+++ b/core.c
@@ -612,9 +612,7 @@ struct gpiod_line * gpiod_line_find_by_name(const char *name)
                                continue;
 
                        if (strcmp(gpiod_line_name(line), name) == 0) {
-                               /* TODO A separate function for that maybe? */
-                               closedir(chip_iter->dir);
-                               free(chip_iter);
+                               gpiod_chip_iter_free_noclose(chip_iter);
                                return line;
                        }
                }
@@ -943,6 +941,11 @@ void gpiod_chip_iter_free(struct gpiod_chip_iter *iter)
        if (iter->current)
                gpiod_chip_close(iter->current);
 
+       gpiod_chip_iter_free_noclose(iter);
+}
+
+void gpiod_chip_iter_free_noclose(struct gpiod_chip_iter *iter)
+{
        closedir(iter->dir);
        free(iter);
 }
diff --git a/gpiod.h b/gpiod.h
index d5245caf251c23aa78134fda68d8397036d8a85c..ecba21a2db1a27cf9e0b2bac12ba0fb67b431a26 100644 (file)
--- a/gpiod.h
+++ b/gpiod.h
@@ -661,11 +661,23 @@ gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset) GPIOD_API;
 struct gpiod_chip_iter * gpiod_chip_iter_new(void) GPIOD_API;
 
 /**
- * @brief Release all data allocated for a gpiochip iterator.
+ * @brief Release all resources allocated for the gpiochip iterator and close
+ *        the most recently opened gpiochip (if any).
  * @param iter The gpiochip iterator object.
  */
 void gpiod_chip_iter_free(struct gpiod_chip_iter *iter) GPIOD_API;
 
+/**
+ * @brief Release all resources allocated for the gpiochip iterator but
+ *        don't close the most recently opened gpiochip (if any).
+ * @param iter The gpiochip iterator object.
+ *
+ * Users may want to break the loop when iterating over gpiochips and keep
+ * the most recently opened chip active while freeing the iterator data.
+ * This routine enables that.
+ */
+void gpiod_chip_iter_free_noclose(struct gpiod_chip_iter *iter) GPIOD_API;
+
 /**
  * @brief Get the next gpiochip handle.
  * @param iter The gpiochip iterator object.