-
Notifications
You must be signed in to change notification settings - Fork 38.7k
[Refactor] RAII of libevent stuff using unique ptrs with deleters #9387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
General concept ACK on a RAII approach (C++ wrapper) for libevent. |
b86f75a to
472eada
Compare
|
@jonasschnelli Fixed. |
|
ACK on making things RAII. Thanks. I'm not so sure about wrapping the entire interface, though. I'm not sure about others but I'd prefer something thinner. For example (also it adds maintenance overhead as every function of libevent to be used has to be wrapped) |
|
@laanwj It felt sensible to make the underlying |
|
It's not a problem but it's one of the little things that I've learned in years developing in open source. If a project makes use of some library which you already know, it's easier to get into it if it is simply using the "lexicon" of that library instead of wrapping everything in custom-named calls. You could work around this by making the wrapping functions the same as the evhttp calls, e.g. don't do the camel-case thing. But yes I'd prefer not making the inner objects private - make it behave more like a "smart pointer". Another (less-LoC) alternative to all of this is to define a std::unique_ptr with our own cleanup hook. I've done a similar thing recently in |
|
There is also a slight issue with the |
|
That (names being same) makes sense. The |
Couldn't, in that case, the calling code could just |
|
You mean, explicitly tell the wrapper it no longer owns the object whenever you do |
|
True, though hiding behaviour in the wrapper instead of explicit transfer
of ownership could also cause confusion, as there is a certain expectation
from the API.
…On Dec 20, 2016 11:46 AM, "kallewoof" ***@***.***> wrote:
You mean, explicitly tell the wrapper it no longer owns the object
whenever you do evhttp_make_request? I suppose that is doable. I would
have loved to not have to risk developers forgetting to do so though.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#9387 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAHutu5cec1TevmA-izXHyoEfiUmitigks5rJ7IDgaJpZM4LRjq0>
.
|
|
Yeah, that makes sense. Thanks for the feedback, will fix! |
472eada to
7f7f102
Compare
|
@laanwj I decided to go with the unique pointer + deleter approach. It became awfully verbose, so I did some macroing. |
|
Nice! |
dcousens
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
concept and light utACK
80b1aa0 to
f920a9d
Compare
f920a9d to
280a559
Compare
|
I tried building this and it fails: Perhaps the include directory for libevent headers is not specified for the tests in the Makefile.am? |
|
@laanwj Weird. What does Edit: Did see that |
|
Sure: My configure script looks like: |
|
On my system, I have event2 symlinked inside |
|
Yep, that solved it, thanks! utACK 05a55a6 |
… deleters 05a55a6 Added EVENT_CFLAGS to test makefile to explicitly include libevent headers. (Karl-Johan Alm) 280a559 Added some simple tests for the RAII-style events. (Karl-Johan Alm) 7f7f102 Switched bitcoin-cli.cpp to use RAII unique pointers with deleters. (Karl-Johan Alm) e5534d2 Added std::unique_ptr<> wrappers with deleters for libevent modules. (Karl-Johan Alm)
libevent-based http server Cherry-picked from the following upstream PRs: - bitcoin/bitcoin#5677 - bitcoin/bitcoin#6695 - bitcoin/bitcoin#6899 - bitcoin/bitcoin#7016 - bitcoin/bitcoin#7964 - bitcoin/bitcoin#8722 - bitcoin/bitcoin#8730 - bitcoin/bitcoin#9073 - bitcoin/bitcoin#9265 - bitcoin/bitcoin#9387 - bitcoin/bitcoin#9471 - bitcoin/bitcoin#9647 - bitcoin/bitcoin#9903 Closes #1593 and #1856.
libevent-based http server Cherry-picked from the following upstream PRs: - bitcoin/bitcoin#5677 - bitcoin/bitcoin#6695 - bitcoin/bitcoin#6899 - bitcoin/bitcoin#7016 - bitcoin/bitcoin#7964 - bitcoin/bitcoin#8722 - bitcoin/bitcoin#8730 - bitcoin/bitcoin#9073 - bitcoin/bitcoin#9265 - bitcoin/bitcoin#9387 - bitcoin/bitcoin#9471 - bitcoin/bitcoin#9647 - bitcoin/bitcoin#9903 - bitcoin/bitcoin#6640 - bitcoin/bitcoin#8139 - bitcoin/bitcoin#8839 Closes #1593 and #1856.
…rs with deleters 05a55a6 Added EVENT_CFLAGS to test makefile to explicitly include libevent headers. (Karl-Johan Alm) 280a559 Added some simple tests for the RAII-style events. (Karl-Johan Alm) 7f7f102 Switched bitcoin-cli.cpp to use RAII unique pointers with deleters. (Karl-Johan Alm) e5534d2 Added std::unique_ptr<> wrappers with deleters for libevent modules. (Karl-Johan Alm)
…rs with deleters 05a55a6 Added EVENT_CFLAGS to test makefile to explicitly include libevent headers. (Karl-Johan Alm) 280a559 Added some simple tests for the RAII-style events. (Karl-Johan Alm) 7f7f102 Switched bitcoin-cli.cpp to use RAII unique pointers with deleters. (Karl-Johan Alm) e5534d2 Added std::unique_ptr<> wrappers with deleters for libevent modules. (Karl-Johan Alm)
…rs with deleters 05a55a6 Added EVENT_CFLAGS to test makefile to explicitly include libevent headers. (Karl-Johan Alm) 280a559 Added some simple tests for the RAII-style events. (Karl-Johan Alm) 7f7f102 Switched bitcoin-cli.cpp to use RAII unique pointers with deleters. (Karl-Johan Alm) e5534d2 Added std::unique_ptr<> wrappers with deleters for libevent modules. (Karl-Johan Alm)
Summary: PR description: > The test suite must always be defined (even when EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED is not defined) so that the test harness doesn't fail due to not being able to find the raii_event_tests test. > > This improves upon 95f97f4 actually fixing bitcoin/bitcoin#9493 Context from issue: > [[bitcoin/bitcoin#9387 | core#9387]] added a new test that uses libevent's event_set_mem_functions which is sometimes not included with libevent. In particular, Gentoo only enables this when libevent is installed with the "debug" option. > Currently, this causes the build to simply fail on tests This is a backport of [[bitcoin/bitcoin#16564 | core#16564]] Test Plan: `ninja check` Reviewers: #bitcoin_abc, majcosta Reviewed By: #bitcoin_abc, majcosta Differential Revision: https://reviews.bitcoinabc.org/D9930
This PR is the initial part of cleaning up the use of
ev*_new/ev*_free.The relevant, owned types are wrapped in
std::unique_ptrs with associated deleters.Some cases require manually releasing the pointer, such as when an
evhttp_requestis handed toevhttp_make_request(as its ownership is moved at that point), but otherwise things become mostly automagic.