Bug 1289457 - Take advantage of infallible new in XPCNativeSet::NewInstance{,Mutate} to skip checks. r=mrbkap draft
authorAndrew McCreight <continuation@gmail.com>
Wed, 27 Jul 2016 16:38:24 -0700
changeset 393538 a1bfec8ec4ef758f548ac09717a4a7fa59d42827
parent 393537 830a8dd9d9c33db2df75a2780dc5c9e45158c611
child 526598 3957c00f5a892ca6d00d34f2208348830fa63289
push id24330
push userbmo:continuation@gmail.com
push dateWed, 27 Jul 2016 23:53:19 +0000
reviewersmrbkap
bugs1289457
milestone50.0a1
Bug 1289457 - Take advantage of infallible new in XPCNativeSet::NewInstance{,Mutate} to skip checks. r=mrbkap
js/xpconnect/src/XPCWrappedNativeInfo.cpp
--- a/js/xpconnect/src/XPCWrappedNativeInfo.cpp
+++ b/js/xpconnect/src/XPCWrappedNativeInfo.cpp
@@ -668,18 +668,16 @@ XPCNativeSet::GetNewOrUsed(XPCNativeSet*
     MOZ_ASSERT(currentSet->mInterfaceCount == uniqueCount);
     return currentSet;
 }
 
 // static
 XPCNativeSet*
 XPCNativeSet::NewInstance(nsTArray<RefPtr<XPCNativeInterface>>& array)
 {
-    XPCNativeSet* obj = nullptr;
-
     if (array.Length() == 0)
         return nullptr;
 
     // We impose the invariant:
     // "All sets have exactly one nsISupports interface and it comes first."
     // This is the place where we impose that rule - even if given inputs
     // that don't exactly follow the rule.
 
@@ -692,78 +690,70 @@ XPCNativeSet::NewInstance(nsTArray<RefPt
     }
 
     // Use placement new to create an object with the right amount of space
     // to hold the members array
     int size = sizeof(XPCNativeSet);
     if (slots > 1)
         size += (slots - 1) * sizeof(XPCNativeInterface*);
     void* place = new char[size];
-    if (place)
-        obj = new(place) XPCNativeSet();
+    XPCNativeSet* obj = new(place) XPCNativeSet();
 
-    if (obj) {
-        // Stick the nsISupports in front and skip additional nsISupport(s)
-        XPCNativeInterface** outp = (XPCNativeInterface**) &obj->mInterfaces;
-        uint16_t memberCount = 1;   // for the one member in nsISupports
+    // Stick the nsISupports in front and skip additional nsISupport(s)
+    XPCNativeInterface** outp = (XPCNativeInterface**) &obj->mInterfaces;
+    uint16_t memberCount = 1;   // for the one member in nsISupports
 
-        NS_ADDREF(*(outp++) = isup);
+    NS_ADDREF(*(outp++) = isup);
 
-        for (auto key = array.begin(); key != array.end(); key++) {
-            RefPtr<XPCNativeInterface> cur = key->forget();
-            if (isup == cur)
-                continue;
-            memberCount += cur->GetMemberCount();
-            *(outp++) = cur.forget().take();
-        }
-        obj->mMemberCount = memberCount;
-        obj->mInterfaceCount = slots;
+    for (auto key = array.begin(); key != array.end(); key++) {
+        RefPtr<XPCNativeInterface> cur = key->forget();
+        if (isup == cur)
+            continue;
+        memberCount += cur->GetMemberCount();
+        *(outp++) = cur.forget().take();
     }
+    obj->mMemberCount = memberCount;
+    obj->mInterfaceCount = slots;
 
     return obj;
 }
 
 // static
 XPCNativeSet*
 XPCNativeSet::NewInstanceMutate(XPCNativeSet*       otherSet,
                                 XPCNativeInterface* newInterface,
                                 uint16_t            position)
 {
-    XPCNativeSet* obj = nullptr;
-
     if (!newInterface)
         return nullptr;
     if (otherSet && position > otherSet->mInterfaceCount)
         return nullptr;
 
     // Use placement new to create an object with the right amount of space
     // to hold the members array
     int size = sizeof(XPCNativeSet);
     if (otherSet)
         size += otherSet->mInterfaceCount * sizeof(XPCNativeInterface*);
     void* place = new char[size];
-    if (place)
-        obj = new(place) XPCNativeSet();
+    XPCNativeSet* obj = new(place) XPCNativeSet();
 
-    if (obj) {
-        if (otherSet) {
-            obj->mMemberCount = otherSet->GetMemberCount() +
-                                newInterface->GetMemberCount();
-            obj->mInterfaceCount = otherSet->mInterfaceCount + 1;
+    if (otherSet) {
+        obj->mMemberCount = otherSet->GetMemberCount() +
+            newInterface->GetMemberCount();
+        obj->mInterfaceCount = otherSet->mInterfaceCount + 1;
 
-            XPCNativeInterface** src = otherSet->mInterfaces;
-            XPCNativeInterface** dest = obj->mInterfaces;
-            for (uint16_t i = 0; i < obj->mInterfaceCount; i++) {
-                NS_ADDREF(*dest++ = (i == position) ? newInterface : *src++);
-            }
-        } else {
-            obj->mMemberCount = newInterface->GetMemberCount();
-            obj->mInterfaceCount = 1;
-            obj->mInterfaces[0] = newInterface;
+        XPCNativeInterface** src = otherSet->mInterfaces;
+        XPCNativeInterface** dest = obj->mInterfaces;
+        for (uint16_t i = 0; i < obj->mInterfaceCount; i++) {
+            NS_ADDREF(*dest++ = (i == position) ? newInterface : *src++);
         }
+    } else {
+        obj->mMemberCount = newInterface->GetMemberCount();
+        obj->mInterfaceCount = 1;
+        obj->mInterfaces[0] = newInterface;
     }
 
     return obj;
 }
 
 // static
 void
 XPCNativeSet::DestroyInstance(XPCNativeSet* inst)