diff --git a/src/h2load_http2_session.cc b/src/h2load_http2_session.cc index 641edf8c..690b11bb 100644 --- a/src/h2load_http2_session.cc +++ b/src/h2load_http2_session.cc @@ -106,8 +106,8 @@ void Http2Session::on_connect() nghttp2_session_callbacks_new(&callbacks); - util::auto_delete callbacks_deleter - (callbacks, nghttp2_session_callbacks_del); + auto callbacks_deleter = + util::defer(callbacks, nghttp2_session_callbacks_del); nghttp2_session_callbacks_set_on_frame_recv_callback (callbacks, on_frame_recv_callback); diff --git a/src/nghttp.cc b/src/nghttp.cc index a3146d10..fe99ed33 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -1832,8 +1832,7 @@ int run(char **uris, int n) nghttp2_session_callbacks *callbacks; nghttp2_session_callbacks_new(&callbacks); - util::auto_delete cbsdel - (callbacks, nghttp2_session_callbacks_del); + auto cbsdel = util::defer(callbacks, nghttp2_session_callbacks_del); nghttp2_session_callbacks_set_on_stream_close_callback (callbacks, on_stream_close_callback); diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 2ed4346c..eb4ef3b5 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -1377,8 +1377,8 @@ int Http2Session::on_connect() return -1; } - util::auto_delete callbacks_deleter - (callbacks, nghttp2_session_callbacks_del); + auto callbacks_deleter = + util::defer(callbacks, nghttp2_session_callbacks_del); nghttp2_session_callbacks_set_on_stream_close_callback (callbacks, on_stream_close_callback); diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index bf9b5319..56d7abf3 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -637,8 +637,8 @@ Http2Upstream::Http2Upstream(ClientHandler *handler) assert(rv == 0); - util::auto_delete callbacks_deleter - (callbacks, nghttp2_session_callbacks_del); + auto callbacks_deleter = + util::defer(callbacks, nghttp2_session_callbacks_del); nghttp2_session_callbacks_set_on_stream_close_callback (callbacks, on_stream_close_callback); diff --git a/src/shrpx_ssl.cc b/src/shrpx_ssl.cc index 8ea81d9a..63c7dae2 100644 --- a/src/shrpx_ssl.cc +++ b/src/shrpx_ssl.cc @@ -602,8 +602,7 @@ void get_altnames(X509 *cert, static_cast (X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr)); if(altnames) { - util::auto_delete altnames_deleter(altnames, - GENERAL_NAMES_free); + auto altnames_deleter = util::defer(altnames, GENERAL_NAMES_free); size_t n = sk_GENERAL_NAME_num(altnames); for(size_t i = 0; i < n; ++i) { const GENERAL_NAME *altname = sk_GENERAL_NAME_value(altnames, i); @@ -665,7 +664,7 @@ int check_cert(SSL *ssl) LOG(ERROR) << "No certificate found"; return -1; } - util::auto_delete cert_deleter(cert, X509_free); + auto cert_deleter = util::defer(cert, X509_free); long verify_res = SSL_get_verify_result(ssl); if(verify_res != X509_V_OK) { LOG(ERROR) << "Certificate verification failed: " @@ -864,7 +863,7 @@ int cert_lookup_tree_add_cert_from_file(CertLookupTree *lt, SSL_CTX *ssl_ctx, LOG(ERROR) << "BIO_new failed"; return -1; } - util::auto_delete bio_deleter(bio, BIO_vfree); + auto bio_deleter = util::defer(bio, BIO_vfree); if(!BIO_read_filename(bio, certfile)) { LOG(ERROR) << "Could not read certificate file '" << certfile << "'"; return -1; @@ -875,7 +874,7 @@ int cert_lookup_tree_add_cert_from_file(CertLookupTree *lt, SSL_CTX *ssl_ctx, << certfile << "'"; return -1; } - util::auto_delete cert_deleter(cert, X509_free); + auto cert_deleter = util::defer(cert, X509_free); std::string common_name; std::vector dns_names; std::vector ip_addrs; diff --git a/src/util.h b/src/util.h index 494bb6b2..8c52ecf3 100644 --- a/src/util.h +++ b/src/util.h @@ -51,46 +51,26 @@ constexpr size_t array_size(T (&)[N]) return N; } -template -class auto_delete { -private: - T obj_; - void (*deleter_)(T); -public: - auto_delete(T obj, void (*deleter)(T)):obj_(obj), deleter_(deleter) {} +template +struct Defer { + Defer(T t, F f) + : t(t), f(std::move(f)) + {} - ~auto_delete() + ~Defer() { - deleter_(obj_); + f(t); } + + T t; + F f; }; -template -class auto_delete_d { -private: - T obj_; -public: - auto_delete_d(T obj):obj_(obj) {} - - ~auto_delete_d() - { - delete obj_; - } -}; - -template -class auto_delete_r { -private: - T obj_; - R (*deleter_)(T); -public: - auto_delete_r(T obj, R (*deleter)(T)):obj_(obj), deleter_(deleter) {} - - ~auto_delete_r() - { - (void)deleter_(obj_); - } -}; +template +Defer defer(T&& t, F f) +{ + return Defer(std::forward(t), std::forward(f)); +} extern const char DEFAULT_STRIP_CHARSET[];