From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Viresh Kumar <viresh.kumar@linaro.org>
Cc: "Linus Walleij" <linus.walleij@linaro.org>,
"Vincent Guittot" <vincent.guittot@linaro.org>,
"open list:GPIO SUBSYSTEM" <linux-gpio@vger.kernel.org>,
"Kent Gibson" <warthog618@gmail.com>,
"Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@google.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
stratos-dev@op-lists.linaro.org
Subject: Re: [PATCH V2 2/4] libgpiod: Add rust wrappers
Date: Fri, 17 Dec 2021 10:12:05 +0100 [thread overview]
Message-ID: <CAMRc=MeP7xPsaiEoJ4ML8SNMo7BH9pb34eUkG6+3SLf+A=biMA@mail.gmail.com> (raw)
In-Reply-To: <20211217050135.l7p3sudbdvzewi6y@vireshk-i7>
On Fri, Dec 17, 2021 at 6:01 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
[snip]
>
> +impl Drop for GpiodEdgeEvent {
> + /// Free the edge event object and release all associated resources.
> + fn drop(&mut self) {
> + unsafe { bindings::gpiod_edge_event_free(self.event) }
> + }
> +}
>
> While testing this I found a bug few days back and I wonder why
> gpiod_edge_event_free() even exists. The memory for "events" is allocated with
> the buffer and gpiod_edge_event_free() shouldn't try to free a part of that.
> This looks buggy.
>
> Yes I realize that the edge event shouldn't exist past the buffer itself, I will
> try to fix it in a Rusty way (maybe with Arc or something else).
>
No, it's a different story altogether. In C the buffer allocates
memory for events and when you "get" an event, you only have a pointer
to the memory space in the buffer that you must not free. But you can
"copy" an event with gpiod_edge_event_copy() which returns you a deep
copy of the event that will survive the parent and that must be freed
with gpiod_edge_event_free(). This is done so that by default we try
to limit the number of allocations (as there can be a lot of events)
unless the user decides to manually copy the event.
In C++ I used that mechanism together with the buffer's const
event_get() and event's copy assignment operator. "Getting" an event
returns a const reference to the event (still in buffer's memory) but
copying it triggers a deep copy. The memory management is of course
handled by the destructor.
This is not used in Python as speed is no longer a concern and we'd be
creating new python objects anyway. But in Rust, I think it makes
sense to reuse this mechanism.
Bart
next prev parent reply other threads:[~2021-12-17 9:12 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-02 11:22 [PATCH V2 0/4] libgpiod: Add Rust bindings Viresh Kumar
2021-12-02 11:22 ` [PATCH V2 1/4] libgpiod: Generate rust FFI bindings Viresh Kumar
2021-12-02 11:22 ` [PATCH V2 2/4] libgpiod: Add rust wrappers Viresh Kumar
2021-12-16 13:59 ` Bartosz Golaszewski
2021-12-17 0:12 ` Kent Gibson
2021-12-17 5:11 ` Viresh Kumar
2022-01-06 15:47 ` Kent Gibson
2022-01-07 7:37 ` Viresh Kumar
2021-12-17 5:01 ` Viresh Kumar
2021-12-17 5:52 ` Wedson Almeida Filho
2021-12-17 6:29 ` Viresh Kumar
2021-12-17 7:48 ` Viresh Kumar
2021-12-17 9:12 ` Bartosz Golaszewski [this message]
2021-12-17 9:32 ` Viresh Kumar
2021-12-17 9:43 ` Bartosz Golaszewski
2021-12-17 9:54 ` Viresh Kumar
2021-12-17 12:02 ` Bartosz Golaszewski
2021-12-20 5:28 ` Viresh Kumar
2021-12-17 9:44 ` Miguel Ojeda
2021-12-17 10:04 ` Viresh Kumar
2021-12-17 9:21 ` Miguel Ojeda
2021-12-17 9:43 ` Viresh Kumar
2021-12-02 11:22 ` [PATCH V2 3/4] rust: Add examples Viresh Kumar
2021-12-02 11:22 ` [PATCH V2 4/4] rust: Integrate building of rust bindings with make Viresh Kumar
2022-04-11 3:33 ` [PATCH V2 0/4] libgpiod: Add Rust bindings Viresh Kumar
2022-04-11 8:57 ` Bartosz Golaszewski
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='CAMRc=MeP7xPsaiEoJ4ML8SNMo7BH9pb34eUkG6+3SLf+A=biMA@mail.gmail.com' \
--to=brgl@bgdev.pl \
--cc=alex.bennee@linaro.org \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=stratos-dev@op-lists.linaro.org \
--cc=vincent.guittot@linaro.org \
--cc=viresh.kumar@linaro.org \
--cc=warthog618@gmail.com \
--cc=wedsonaf@google.com \
/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).