Commit 7d9dbc59 authored by Claire (Yue) Zhang's avatar Claire (Yue) Zhang Committed by Facebook GitHub Bot

Change AsyncSocket::UniquePtr destructor to ReleasableDestuctor

Summary: Adding a new ReleasableDestructor and use it in `folly::AsyncSocket::UniquePtr`.

Reviewed By: yfeldblum

Differential Revision: D25352913

fbshipit-source-id: b85204688706cb82a323e37063c9a6a390ec2ee8
parent a484d190
......@@ -75,7 +75,7 @@ class AsyncSSLSocketConnector;
*/
class AsyncSSLSocket : public AsyncSocket {
public:
typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
typedef std::unique_ptr<AsyncSSLSocket, ReleasableDestructor> UniquePtr;
using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
class HandshakeCB {
......@@ -290,9 +290,8 @@ class AsyncSSLSocket : public AsyncSocket {
NetworkSocket fd,
bool server = true,
bool deferSecurityNegotiation = false) {
return std::shared_ptr<AsyncSSLSocket>(
new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
Destructor());
return std::shared_ptr<AsyncSSLSocket>(AsyncSSLSocket::UniquePtr(
new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation)));
}
/**
......@@ -302,8 +301,8 @@ class AsyncSSLSocket : public AsyncSocket {
const std::shared_ptr<folly::SSLContext>& ctx,
EventBase* evb,
bool deferSecurityNegotiation = false) {
return std::shared_ptr<AsyncSSLSocket>(
new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation), Destructor());
return std::shared_ptr<AsyncSSLSocket>(AsyncSSLSocket::UniquePtr(
new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation)));
}
#if FOLLY_OPENSSL_HAS_SNI
......@@ -344,9 +343,8 @@ class AsyncSSLSocket : public AsyncSocket {
EventBase* evb,
const std::string& serverName,
bool deferSecurityNegotiation = false) {
return std::shared_ptr<AsyncSSLSocket>(
new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
Destructor());
return std::shared_ptr<AsyncSSLSocket>(AsyncSSLSocket::UniquePtr(
new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation)));
}
#endif // FOLLY_OPENSSL_HAS_SNI
......
......@@ -76,7 +76,31 @@ namespace folly {
class AsyncSocket : public AsyncTransport {
public:
using UniquePtr = std::unique_ptr<AsyncSocket, Destructor>;
/**
* Use ReleasableDestructor with AsyncSocket to enable transferring the
* ownership of the socket owned by smart pointers.
*/
class ReleasableDestructor : public DelayedDestruction::Destructor {
public:
void operator()(DelayedDestruction* dd) const {
if (!released_) {
dd->destroy();
}
}
/**
* Release the object managed by smart pointers. This is used when the
* object ownership is transferred to another smart pointer or manually
* managed by the caller. The original object must be properly deleted at
* the end of its life cycle to avoid resource leaks.
*/
void release() { released_ = true; }
private:
bool released_{false};
};
using UniquePtr = std::unique_ptr<AsyncSocket, ReleasableDestructor>;
class ConnectCallback {
public:
......@@ -304,7 +328,7 @@ class AsyncSocket : public AsyncTransport {
* destructor is protected and cannot be invoked directly.
*/
static UniquePtr newSocket(EventBase* evb) {
return UniquePtr{new AsyncSocket(evb), Destructor()};
return UniquePtr{new AsyncSocket(evb)};
}
/**
......@@ -315,8 +339,8 @@ class AsyncSocket : public AsyncTransport {
const folly::SocketAddress& address,
uint32_t connectTimeout = 0,
bool useZeroCopy = false) {
return UniquePtr{new AsyncSocket(evb, address, connectTimeout, useZeroCopy),
Destructor()};
return UniquePtr{
new AsyncSocket(evb, address, connectTimeout, useZeroCopy)};
}
/**
......@@ -329,15 +353,14 @@ class AsyncSocket : public AsyncTransport {
uint32_t connectTimeout = 0,
bool useZeroCopy = false) {
return UniquePtr{
new AsyncSocket(evb, ip, port, connectTimeout, useZeroCopy),
Destructor()};
new AsyncSocket(evb, ip, port, connectTimeout, useZeroCopy)};
}
/**
* Helper function to create an AsyncSocket.
*/
static UniquePtr newSocket(EventBase* evb, NetworkSocket fd) {
return UniquePtr{new AsyncSocket(evb, fd), Destructor()};
return UniquePtr{new AsyncSocket(evb, fd)};
}
/**
......
......@@ -2694,7 +2694,8 @@ TEST(AsyncSSLSocketTest, TTLSDisabled) {
class MockAsyncTFOSSLSocket : public AsyncSSLSocket {
public:
using UniquePtr = std::unique_ptr<MockAsyncTFOSSLSocket, Destructor>;
using UniquePtr =
std::unique_ptr<MockAsyncTFOSSLSocket, ReleasableDestructor>;
explicit MockAsyncTFOSSLSocket(
std::shared_ptr<folly::SSLContext> sslCtx,
......
......@@ -26,7 +26,7 @@ namespace test {
class MockAsyncSocket : public AsyncSocket {
public:
typedef std::unique_ptr<MockAsyncSocket, Destructor> UniquePtr;
typedef std::unique_ptr<MockAsyncSocket, ReleasableDestructor> UniquePtr;
explicit MockAsyncSocket(EventBase* base) : AsyncSocket(base) {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment