diff mbox series

[libgpiod,v2,4/4] tools: minimize object lifetimes

Message ID 20220331011141.53489-5-warthog618@gmail.com
State New
Headers show
Series core: add gpiod_request_lines | expand

Commit Message

Kent Gibson March 31, 2022, 1:11 a.m. UTC
The tools double as examples of API usage, so keep object lifetimes to a
minimum to highlight where transient objects are no longer required and
may be discarded.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 tools/gpioget.c | 16 ++++++++++------
 tools/gpiomon.c |  7 ++++---
 tools/gpioset.c |  9 +++++----
 3 files changed, 19 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/tools/gpioget.c b/tools/gpioget.c
index f4c5d46..f789198 100644
--- a/tools/gpioget.c
+++ b/tools/gpioget.c
@@ -92,8 +92,7 @@  int main(int argc, char **argv)
 	num_lines = argc - 1;
 
 	offsets = calloc(num_lines, sizeof(*offsets));
-	values = calloc(num_lines, sizeof(*values));
-	if (!offsets || ! values)
+	if (!offsets)
 		die("out of memory");
 
 	for (i = 0; i < num_lines; i++) {
@@ -124,11 +123,20 @@  int main(int argc, char **argv)
 
 	gpiod_request_config_set_consumer(req_cfg, "gpioget");
 	gpiod_request_config_set_offsets(req_cfg, num_lines, offsets);
+	free(offsets);
 
 	request = gpiod_request_lines(path, req_cfg, line_cfg);
 	if (!request)
 		die_perror("unable to request lines");
 
+	free(path);
+	gpiod_request_config_free(req_cfg);
+	gpiod_line_config_free(line_cfg);
+
+	values = calloc(num_lines, sizeof(*values));
+	if (!values)
+		die("out of memory");
+
 	ret = gpiod_line_request_get_values(request, values);
 	if (ret)
 		die_perror("unable to read GPIO line values");
@@ -141,10 +149,6 @@  int main(int argc, char **argv)
 	printf("\n");
 
 	gpiod_line_request_release(request);
-	gpiod_request_config_free(req_cfg);
-	gpiod_line_config_free(line_cfg);
-	free(path);
-	free(offsets);
 	free(values);
 
 	return EXIT_SUCCESS;
diff --git a/tools/gpiomon.c b/tools/gpiomon.c
index e461458..34de2b2 100644
--- a/tools/gpiomon.c
+++ b/tools/gpiomon.c
@@ -274,6 +274,10 @@  int main(int argc, char **argv)
 	if (!request)
 		die_perror("unable to request lines");
 
+	free(path);
+	gpiod_request_config_free(req_cfg);
+	gpiod_line_config_free(line_cfg);
+
 	event_buffer = gpiod_edge_event_buffer_new(EVENT_BUF_SIZE);
 	if (!event_buffer)
 		die_perror("unable to allocate the line event buffer");
@@ -311,9 +315,6 @@  int main(int argc, char **argv)
 done:
 	gpiod_edge_event_buffer_free(event_buffer);
 	gpiod_line_request_release(request);
-	gpiod_request_config_free(req_cfg);
-	gpiod_line_config_free(line_cfg);
-	free(path);
 
 	return EXIT_SUCCESS;
 }
diff --git a/tools/gpioset.c b/tools/gpioset.c
index 7497eab..f28f8b6 100644
--- a/tools/gpioset.c
+++ b/tools/gpioset.c
@@ -312,19 +312,20 @@  int main(int argc, char **argv)
 
 	gpiod_request_config_set_consumer(req_cfg, "gpioset");
 	gpiod_request_config_set_offsets(req_cfg, num_lines, offsets);
+	free(offsets);
 
 	request = gpiod_request_lines(path, req_cfg, line_cfg);
 	if (!request)
 		die_perror("unable to request lines");
 
+	free(path);
+	gpiod_request_config_free(req_cfg);
+	gpiod_line_config_free(line_cfg);
+
 	if (mode->callback)
 		mode->callback(&cbdata);
 
 	gpiod_line_request_release(request);
-	gpiod_request_config_free(req_cfg);
-	gpiod_line_config_free(line_cfg);
-	free(path);
-	free(offsets);
 
 	return EXIT_SUCCESS;
 }