[UNIX] Simplify the file descriptor handling in SharedMemory
[WebKit-https.git] / Source / WebKit2 / Platform / unix / SharedMemoryUnix.cpp
index be5f1a1..0c5a2cc 100644 (file)
@@ -47,8 +47,6 @@
 namespace WebKit {
 
 SharedMemory::Handle::Handle()
-    : m_fileDescriptor(-1)
-    , m_size(0)
 {
 }
 
@@ -59,18 +57,17 @@ SharedMemory::Handle::~Handle()
 
 void SharedMemory::Handle::clear()
 {
-    if (!isNull())
-        closeWithRetry(m_fileDescriptor);
+    m_attachment.dispose();
 }
 
 bool SharedMemory::Handle::isNull() const
 {
-    return m_fileDescriptor == -1;
+    return m_attachment.fileDescriptor() == -1;
 }
 
 void SharedMemory::Handle::encode(IPC::ArgumentEncoder& encoder) const
 {
-    encoder << releaseToAttachment();
+    encoder << releaseAttachment();
 }
 
 bool SharedMemory::Handle::decode(IPC::ArgumentDecoder& decoder, Handle& handle)
@@ -82,24 +79,21 @@ bool SharedMemory::Handle::decode(IPC::ArgumentDecoder& decoder, Handle& handle)
     if (!decoder.decode(attachment))
         return false;
 
-    handle.adoptFromAttachment(attachment.releaseFileDescriptor(), attachment.size());
+    handle.adoptAttachment(WTF::move(attachment));
     return true;
 }
 
-IPC::Attachment SharedMemory::Handle::releaseToAttachment() const
+IPC::Attachment SharedMemory::Handle::releaseAttachment() const
 {
-    int temp = m_fileDescriptor;
-    m_fileDescriptor = -1;
-    return IPC::Attachment(temp, m_size);
+    ASSERT(!isNull());
+    return WTF::move(m_attachment);
 }
 
-void SharedMemory::Handle::adoptFromAttachment(int fileDescriptor, size_t size)
+void SharedMemory::Handle::adoptAttachment(IPC::Attachment&& attachment)
 {
-    ASSERT(!m_size);
     ASSERT(isNull());
 
-    m_fileDescriptor = fileDescriptor;
-    m_size = size;
+    m_attachment = WTF::move(attachment);
 }
 
 RefPtr<SharedMemory> SharedMemory::allocate(size_t size)
@@ -161,15 +155,14 @@ RefPtr<SharedMemory> SharedMemory::map(const Handle& handle, Protection protecti
 {
     ASSERT(!handle.isNull());
 
-    void* data = mmap(0, handle.m_size, accessModeMMap(protection), MAP_SHARED, handle.m_fileDescriptor, 0);
+    void* data = mmap(0, handle.m_attachment.size(), accessModeMMap(protection), MAP_SHARED, handle.m_attachment.fileDescriptor(), 0);
     if (data == MAP_FAILED)
-        return 0;
+        return nullptr;
 
     RefPtr<SharedMemory> instance = adoptRef(new SharedMemory());
     instance->m_data = data;
-    instance->m_fileDescriptor = handle.m_fileDescriptor;
-    instance->m_size = handle.m_size;
-    handle.m_fileDescriptor = -1;
+    instance->m_fileDescriptor = handle.m_attachment.releaseFileDescriptor();
+    instance->m_size = handle.m_attachment.size();
     return instance;
 }
 
@@ -202,8 +195,7 @@ bool SharedMemory::createHandle(Handle& handle, Protection)
             return false;
         }
     }
-    handle.m_fileDescriptor = duplicatedHandle;
-    handle.m_size = m_size;
+    handle.m_attachment = IPC::Attachment(duplicatedHandle, m_size);
     return true;
 }