Skip to content
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

Inlined defer #311

Merged
merged 9 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ ptr_array_t<T> ptr_array(T* pbegin, size_t n)
return {pbegin, pbegin + n};
}

#define __INLINE__ __attribute__((always_inline))
#define __FORCE_INLINE__ __INLINE__ inline

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering why force incline doesn't equal to always inline ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plain functions need the inline keyword, otherwise the compiler will generates warnings. Whereas lambdas can not have inline. So we have to have 2 macros. Maybe the wording can be improved.

template<typename T>
class Defer
{
public:
Defer(T fn) : m_func(fn) {}
~Defer() { m_func(); }
Defer(T fn) __INLINE__ : m_func(fn) {}
~Defer() __INLINE__ { m_func(); }
void operator=(const Defer<T>&) = delete;
operator bool () { return true; } // if (DEFER(...)) { ... }

private:
T m_func;
};

template<typename T>
template<typename T> __FORCE_INLINE__
Defer<T> make_defer(T func) { return Defer<T>(func); }

#define __INLINE__ __attribute__((always_inline))
#define __FORCE_INLINE__ __INLINE__ inline

#define _CONCAT_(a, b) a##b
#define _CONCAT(a, b) _CONCAT_(a, b)

Expand Down
4 changes: 1 addition & 3 deletions net/http/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ class ClientImpl : public Client {
CommonHeaders<> m_common_headers;
ICookieJar *m_cookie_jar;
ClientImpl(ICookieJar *cookie_jar, TLSContext *tls_ctx) :
m_cookie_jar(cookie_jar),
m_dialer(tls_ctx) {
}
m_dialer(tls_ctx), m_cookie_jar(cookie_jar) { }

using SocketStream_ptr = std::unique_ptr<ISocketStream>;
int redirect(Operation* op) {
Expand Down