linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: linux-gpio@vger.kernel.org, brgl@bgdev.pl
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [libgpiod v2][PATCH 4/4] tools: minimize object lifetimes
Date: Thu, 31 Mar 2022 09:11:41 +0800	[thread overview]
Message-ID: <20220331011141.53489-5-warthog618@gmail.com> (raw)
In-Reply-To: <20220331011141.53489-1-warthog618@gmail.com>

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 --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;
 }
-- 
2.35.1


  parent reply	other threads:[~2022-03-31  1:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-31  1:11 [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Kent Gibson
2022-03-31  1:11 ` [libgpiod v2][PATCH 1/4] " Kent Gibson
2022-03-31  1:11 ` [libgpiod v2][PATCH 2/4] tools: rename inexistent to nonexistent Kent Gibson
2022-03-31  1:11 ` [libgpiod v2][PATCH 3/4] tools: migrate to gpiod_request_lines Kent Gibson
2022-03-31  1:11 ` Kent Gibson [this message]
2022-04-02 12:47 ` [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Bartosz Golaszewski
2022-04-04  9:54   ` Kent Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220331011141.53489-5-warthog618@gmail.com \
    --to=warthog618@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).