kallewoof
commented at 7:54 am on December 20, 2016:
member
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_request is handed to evhttp_make_request (as its ownership is moved at that point), but otherwise things become mostly automagic.
kallewoof renamed this:
RAII of libevent stuff using set of wrappers
[Refactor] RAII of libevent stuff using set of wrappers
on Dec 20, 2016
fanquake added the label
Refactoring
on Dec 20, 2016
jonasschnelli
commented at 8:18 am on December 20, 2016:
contributor
General concept ACK on a RAII approach (C++ wrapper) for libevent.
I think we should avoid boost::noncopyable and instead use C++11’s MyClass& operator=(const MyClass&) = delete; approach.
kallewoof force-pushed
on Dec 20, 2016
kallewoof
commented at 8:32 am on December 20, 2016:
member
laanwj
commented at 10:20 am on December 20, 2016:
member
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 evhttp_set_max_headers_size -> setMaxHeadersSize. This makes it fractionally harder for someone that has learned the libevent APIs to understand the code because the functions are named differently.
(also it adds maintenance overhead as every function of libevent to be used has to be wrapped)
kallewoof
commented at 10:31 am on December 20, 2016:
member
@laanwj It felt sensible to make the underlying ev* structs private but maybe my intuition was off on that. I’m not entirely convinced that evhttp_set_max_headers_size -> setMaxHeadersSize is a problem, but if you think it is, it may be better to un-private’ify the structs. That would remove the need for friends as well.
laanwj
commented at 10:38 am on December 20, 2016:
member
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 paymentserver.cpp.
kallewoof
commented at 10:39 am on December 20, 2016:
member
There is also a slight issue with the evhttp_request which needs to not be freed if a call to evhttp_make_request has been made. By exposing the internal evhttp_request, it’s not possible to determine which is the case.
kallewoof
commented at 10:42 am on December 20, 2016:
member
That (names being same) makes sense. The unique_ptr approach looks very nice, but unsure if it can address the above issue adequately.
laanwj
commented at 10:43 am on December 20, 2016:
member
There is also a slight issue with the evhttp_request which needs to not be freed if a call to evhttp_make_request has been made. By exposing the internal evhttp_request, it’s not possible to determine which is the case.
Couldn’t, in that case, the calling code could just .release() the pointer? No need to build that intelligence into the pointer object.
kallewoof
commented at 10:46 am on December 20, 2016:
member
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.
laanwj
commented at 10:50 am on December 20, 2016:
member
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.
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.
kallewoof
commented at 10:51 am on December 20, 2016:
member
Yeah, that makes sense. Thanks for the feedback, will fix!
Added std::unique_ptr<> wrappers with deleters for libevent modules.e5534d2f01
Switched bitcoin-cli.cpp to use RAII unique pointers with deleters.7f7f102b8d
kallewoof force-pushed
on Dec 20, 2016
kallewoof renamed this:
[Refactor] RAII of libevent stuff using set of wrappers
[Refactor] RAII of libevent stuff using unique ptrs with deleters
on Dec 20, 2016
kallewoof
commented at 11:53 am on December 20, 2016:
member
@laanwj I decided to go with the unique pointer + deleter approach. It became awfully verbose, so I did some macroing.
laanwj
commented at 12:22 pm on December 20, 2016:
member
Nice!
Concept ACK. I think the use of macros is okay in this case, as indeed they all follow the same pattern and it saves a lot of dumb repetition.
dcousens approved
dcousens
commented at 7:49 pm on December 20, 2016:
contributor
concept and light utACK
kallewoof force-pushed
on Dec 21, 2016
kallewoof force-pushed
on Dec 21, 2016
Added some simple tests for the RAII-style events.280a5599eb
kallewoof force-pushed
on Dec 21, 2016
laanwj
commented at 7:52 am on January 4, 2017:
member
I tried building this and it fails:
0$ make
1Making all in src
2make[1]: Entering directory '/store2/build/bitcoin/bitcoin/src'3make[2]: Entering directory '/store2/build/bitcoin/bitcoin/src'4...
5 CXX test/test_test_bitcoin-raii_event_tests.o
6/.../bitcoin/src/test/raii_event_tests.cpp:5:10: fatal error: 'event2/event.h' file not found
7#include<event2/event.h>8^~~~~~~~~~~~~~~~91 error generated.
Perhaps the include directory for libevent headers is not specified for the tests in the Makefile.am?
kallewoof
commented at 7:55 am on January 4, 2017:
member
Edit: Did see that $(EVENT_CFLAGS) was not added to test_test_bitcoin_CPPFLAGS in Makefile.test.include (line 113). I’m confused why my machines (1 Mac OS X and 1 Ubuntu inside VirtualBox) and Travis both find the headers. Does it work if you add that?
laanwj
commented at 9:04 am on January 4, 2017:
member
Added EVENT_CFLAGS to test makefile to explicitly include libevent headers.05a55a639b
kallewoof
commented at 9:17 am on January 4, 2017:
member
On my system, I have event2 symlinked inside /usr/local/include, which means I don’t have to explicitly include it in the g++ call. Adding $(EVENT_CFLAGS) to test_test_bitcoin_CPPFLAGS added explicit references so this should fix it on your end.
laanwj
commented at 11:06 am on January 4, 2017:
member
Yep, that solved it, thanks! utACK05a55a6
laanwj approved
laanwj merged this
on Jan 5, 2017
laanwj closed this
on Jan 5, 2017
laanwj referenced this in commit
cfe41d7a60
on Jan 5, 2017
kallewoof deleted the branch
on Jan 5, 2017
zkbot referenced this in commit
b0afc4ba1d
on Mar 22, 2017
zkbot referenced this in commit
f9f48667be
on Mar 25, 2017
codablock referenced this in commit
718e6223b2
on Jan 18, 2018
andvgal referenced this in commit
0e8c93acb6
on Jan 6, 2019
CryptoCentric referenced this in commit
b4154caf5c
on Feb 26, 2019
Fabcien referenced this in commit
708ab698c6
on Aug 25, 2021
This is a metadata mirror of the GitHub repository
bitcoin/bitcoin.
This site is not affiliated with GitHub.
Content is generated from a GitHub metadata backup.
generated: 2024-11-18 06:12 UTC
This site is hosted by @0xB10C More mirrored repositories can be found on mirror.b10c.me