From 912a3362d53f8ff10110fe2abf91a9fe525b9378 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Tue, 10 Jan 2017 16:09:23 +0100 Subject: [PATCH] core: implement gpiod_chip_iter_free_noclose() 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 --- core.c | 9 ++++++--- gpiod.h | 14 +++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/core.c b/core.c index 8056e9f..4fe2fa2 100644 --- 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 d5245ca..ecba21a 100644 --- 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. -- 2.30.2