[WK2] ConnectionUnix should use FastMalloc to allocate on-heap resources
authorzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 22 Jun 2015 12:06:46 +0000 (12:06 +0000)
committerzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 22 Jun 2015 12:06:46 +0000 (12:06 +0000)
https://bugs.webkit.org/show_bug.cgi?id=146143

Reviewed by Carlos Garcia Campos.

IPC handling in Unix-specific IPC::Connection implementation should use
FastMalloc to allocate on-heap resources, instead of allocating via the
system allocator.

The AttachmentInfo class is marked as allocatable through FastMalloc.
That way it can be allocated through FastMalloc while still handled
through std::unique_ptr<>.

The char[] arrays in readBytesFromSocket() and Connection::sendOutgoingMessage()
are now handled through a MallocPtr<> object.

In Connection::sendOutgoingMessage(), both the AttachmentInfo[] and char[]
arrays are now only allocated if there are actual attachments contained
in the message. The code that's conditioned with a non-empty attachments
Vector is now also grouped together, in a single branch.

* Platform/IPC/unix/ConnectionUnix.cpp:
(IPC::readBytesFromSocket):
(IPC::Connection::sendOutgoingMessage):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@185824 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/Platform/IPC/unix/ConnectionUnix.cpp

index 15a0465..6aebf08 100644 (file)
@@ -1,3 +1,30 @@
+2015-06-22  Zan Dobersek  <zdobersek@igalia.com>
+
+        [WK2] ConnectionUnix should use FastMalloc to allocate on-heap resources
+        https://bugs.webkit.org/show_bug.cgi?id=146143
+
+        Reviewed by Carlos Garcia Campos.
+
+        IPC handling in Unix-specific IPC::Connection implementation should use
+        FastMalloc to allocate on-heap resources, instead of allocating via the
+        system allocator.
+
+        The AttachmentInfo class is marked as allocatable through FastMalloc.
+        That way it can be allocated through FastMalloc while still handled
+        through std::unique_ptr<>.
+
+        The char[] arrays in readBytesFromSocket() and Connection::sendOutgoingMessage()
+        are now handled through a MallocPtr<> object.
+
+        In Connection::sendOutgoingMessage(), both the AttachmentInfo[] and char[]
+        arrays are now only allocated if there are actual attachments contained
+        in the message. The code that's conditioned with a non-empty attachments
+        Vector is now also grouped together, in a single branch.
+
+        * Platform/IPC/unix/ConnectionUnix.cpp:
+        (IPC::readBytesFromSocket):
+        (IPC::Connection::sendOutgoingMessage):
+
 2015-06-22  Gyuyoung Kim  <gyuyoung.kim@webkit.org>
 
         [EFL] test_ewk2_application_cache_manager has been failed since r185527
index a53afc7..56ab5a3 100644 (file)
@@ -94,6 +94,7 @@ private:
 };
 
 class AttachmentInfo {
+    WTF_MAKE_FAST_ALLOCATED;
 public:
     AttachmentInfo()
         : m_type(Attachment::Uninitialized)
@@ -274,8 +275,8 @@ static ssize_t readBytesFromSocket(int socketDescriptor, uint8_t* buffer, int co
     memset(&iov, 0, sizeof(iov));
 
     message.msg_controllen = CMSG_SPACE(sizeof(int) * attachmentMaxAmount);
-    auto attachmentDescriptorBuffer = std::make_unique<char[]>(message.msg_controllen);
-    memset(attachmentDescriptorBuffer.get(), 0, message.msg_controllen);
+    MallocPtr<char> attachmentDescriptorBuffer = MallocPtr<char>::malloc(sizeof(char) * message.msg_controllen);
+    memset(attachmentDescriptorBuffer.get(), 0, sizeof(char) * message.msg_controllen);
     message.msg_control = attachmentDescriptorBuffer.get();
 
     iov[0].iov_base = buffer;
@@ -438,21 +439,20 @@ bool Connection::sendOutgoingMessage(std::unique_ptr<MessageEncoder> encoder)
     iov[0].iov_base = reinterpret_cast<void*>(&messageInfo);
     iov[0].iov_len = sizeof(messageInfo);
 
-    auto attachmentInfo = std::make_unique<AttachmentInfo[]>(attachments.size());
-
-    size_t attachmentFDBufferLength = 0;
-    if (!attachments.isEmpty()) {
-        for (size_t i = 0; i < attachments.size(); ++i) {
-            if (attachments[i].fileDescriptor() != -1)
-                attachmentFDBufferLength++;
-        }
-    }
-    auto attachmentFDBuffer = std::make_unique<char[]>(CMSG_SPACE(sizeof(int) * attachmentFDBufferLength));
+    std::unique_ptr<AttachmentInfo[]> attachmentInfo;
+    MallocPtr<char> attachmentFDBuffer;
 
     if (!attachments.isEmpty()) {
         int* fdPtr = 0;
 
+        size_t attachmentFDBufferLength = std::count_if(attachments.begin(), attachments.end(),
+            [](const Attachment& attachment) {
+                return attachment.fileDescriptor() != -1;
+            });
+
         if (attachmentFDBufferLength) {
+            attachmentFDBuffer = MallocPtr<char>::malloc(sizeof(char) * CMSG_SPACE(sizeof(int) * attachmentFDBufferLength));
+
             message.msg_control = attachmentFDBuffer.get();
             message.msg_controllen = CMSG_SPACE(sizeof(int) * attachmentFDBufferLength);
             memset(message.msg_control, 0, message.msg_controllen);
@@ -465,6 +465,7 @@ bool Connection::sendOutgoingMessage(std::unique_ptr<MessageEncoder> encoder)
             fdPtr = reinterpret_cast<int*>(CMSG_DATA(cmsg));
         }
 
+        attachmentInfo = std::make_unique<AttachmentInfo[]>(attachments.size());
         int fdIndex = 0;
         for (size_t i = 0; i < attachments.size(); ++i) {
             attachmentInfo[i].setType(attachments[i].type());